001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016// Generated File -- DO NOT EDIT BY HAND
017// --------------------------------------------------------------------------------
018
019/**
020 * LetterContactMechanismPurposeDetailFactory.java
021 */
022
023package com.echothree.model.data.letter.server.factory;
024
025import com.echothree.model.data.letter.common.pk.LetterContactMechanismPurposePK;
026import com.echothree.model.data.letter.common.pk.LetterPK;
027import com.echothree.model.data.contact.common.pk.ContactMechanismPurposePK;
028
029import com.echothree.model.data.letter.server.entity.LetterContactMechanismPurpose;
030import com.echothree.model.data.letter.server.entity.Letter;
031import com.echothree.model.data.contact.server.entity.ContactMechanismPurpose;
032
033import com.echothree.model.data.letter.common.LetterContactMechanismPurposeDetailConstants;
034import com.echothree.model.data.letter.common.pk.LetterContactMechanismPurposeDetailPK;
035import com.echothree.model.data.letter.server.value.LetterContactMechanismPurposeDetailValue;
036import com.echothree.model.data.letter.server.entity.LetterContactMechanismPurposeDetail;
037import com.echothree.util.common.exception.PersistenceDatabaseException;
038import com.echothree.util.common.exception.PersistenceDatabaseUpdateException;
039import com.echothree.util.common.exception.PersistenceNotNullException;
040import com.echothree.util.server.persistence.BaseFactory;
041import com.echothree.util.server.persistence.EntityIdGenerator;
042import com.echothree.util.server.persistence.EntityPermission;
043import com.echothree.util.server.persistence.PersistenceDebugFlags;
044import com.echothree.util.server.persistence.Session;
045import java.sql.PreparedStatement;
046import java.sql.ResultSet;
047import java.sql.SQLException;
048import java.sql.Types;
049import java.io.ByteArrayInputStream;
050import java.io.StringReader;
051import java.util.ArrayList;
052import java.util.Collection;
053import java.util.HashSet;
054import java.util.List;
055import java.util.Map;
056import java.util.Set;
057import javax.enterprise.context.ApplicationScoped;
058import javax.enterprise.inject.spi.CDI;
059import javax.inject.Inject;
060import org.jooq.ResultQuery;
061import org.slf4j.Logger;
062import org.slf4j.LoggerFactory;
063
064@ApplicationScoped
065public class LetterContactMechanismPurposeDetailFactory
066        implements BaseFactory<LetterContactMechanismPurposeDetailPK, LetterContactMechanismPurposeDetail> {
067    
068    @Inject
069    Session session;
070    
071    //private static final Logger log = LoggerFactory.getLogger(LetterContactMechanismPurposeDetailFactory.class);
072    
073    final private static String SQL_SELECT_READ_ONLY = "SELECT lttrcmprdt_lettercontactmechanismpurposedetailid, lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid, lttrcmprdt_lttr_letterid, lttrcmprdt_priority, lttrcmprdt_cmpr_contactmechanismpurposeid, lttrcmprdt_fromtime, lttrcmprdt_thrutime FROM lettercontactmechanismpurposedetails WHERE lttrcmprdt_lettercontactmechanismpurposedetailid = ?";
074    final private static String SQL_SELECT_READ_WRITE = "SELECT lttrcmprdt_lettercontactmechanismpurposedetailid, lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid, lttrcmprdt_lttr_letterid, lttrcmprdt_priority, lttrcmprdt_cmpr_contactmechanismpurposeid, lttrcmprdt_fromtime, lttrcmprdt_thrutime FROM lettercontactmechanismpurposedetails WHERE lttrcmprdt_lettercontactmechanismpurposedetailid = ? FOR UPDATE";
075    final private static String SQL_INSERT = "INSERT INTO lettercontactmechanismpurposedetails (lttrcmprdt_lettercontactmechanismpurposedetailid, lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid, lttrcmprdt_lttr_letterid, lttrcmprdt_priority, lttrcmprdt_cmpr_contactmechanismpurposeid, lttrcmprdt_fromtime, lttrcmprdt_thrutime) VALUES (?, ?, ?, ?, ?, ?, ?)";
076    final private static String SQL_UPDATE = "UPDATE lettercontactmechanismpurposedetails SET lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid = ?, lttrcmprdt_lttr_letterid = ?, lttrcmprdt_priority = ?, lttrcmprdt_cmpr_contactmechanismpurposeid = ?, lttrcmprdt_fromtime = ?, lttrcmprdt_thrutime = ? WHERE lttrcmprdt_lettercontactmechanismpurposedetailid = ?";
077    final private static String SQL_DELETE = "DELETE FROM lettercontactmechanismpurposedetails WHERE lttrcmprdt_lettercontactmechanismpurposedetailid = ?";
078    final private static String SQL_VALID = "SELECT COUNT(*) FROM lettercontactmechanismpurposedetails WHERE lttrcmprdt_lettercontactmechanismpurposedetailid = ?";
079    
080    final private static String PK_COLUMN = "lttrcmprdt_lettercontactmechanismpurposedetailid";
081    final private static String ALL_COLUMNS = "lttrcmprdt_lettercontactmechanismpurposedetailid, lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid, lttrcmprdt_lttr_letterid, lttrcmprdt_priority, lttrcmprdt_cmpr_contactmechanismpurposeid, lttrcmprdt_fromtime, lttrcmprdt_thrutime";
082    final public static String TABLE_NAME = "lettercontactmechanismpurposedetails";
083    
084    final public static String LTTRCMPRDT_LETTERCONTACTMECHANISMPURPOSEDETAILID = "lttrcmprdt_lettercontactmechanismpurposedetailid";
085    final public static String LTTRCMPRDT_LTTRCMPR_LETTERCONTACTMECHANISMPURPOSEID = "lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid";
086    final public static String LTTRCMPRDT_LTTR_LETTERID = "lttrcmprdt_lttr_letterid";
087    final public static String LTTRCMPRDT_PRIORITY = "lttrcmprdt_priority";
088    final public static String LTTRCMPRDT_CMPR_CONTACTMECHANISMPURPOSEID = "lttrcmprdt_cmpr_contactmechanismpurposeid";
089    final public static String LTTRCMPRDT_FROMTIME = "lttrcmprdt_fromtime";
090    final public static String LTTRCMPRDT_THRUTIME = "lttrcmprdt_thrutime";
091    
092    final private static EntityIdGenerator entityIdGenerator = new EntityIdGenerator(LetterContactMechanismPurposeDetailConstants.COMPONENT_VENDOR_NAME, LetterContactMechanismPurposeDetailConstants.ENTITY_TYPE_NAME);
093    
094    /** Creates a new instance of LetterContactMechanismPurposeDetailFactory */
095    protected LetterContactMechanismPurposeDetailFactory() {
096        super();
097    }
098    
099    public static LetterContactMechanismPurposeDetailFactory getInstance() {
100        return CDI.current().select(LetterContactMechanismPurposeDetailFactory.class).get();
101    }
102    
103    @Override
104    public String getPKColumn() {
105        return PK_COLUMN;
106    }
107    
108    @Override
109    public String getAllColumns() {
110        return ALL_COLUMNS;
111    }
112    
113    @Override
114    public String getTableName() {
115        return TABLE_NAME;
116    }
117    
118    @Override
119    public String getComponentVendorName() {
120        return LetterContactMechanismPurposeDetailConstants.COMPONENT_VENDOR_NAME;
121    }
122    
123    @Override
124    public String getEntityTypeName() {
125        return LetterContactMechanismPurposeDetailConstants.ENTITY_TYPE_NAME;
126    }
127    
128    public PreparedStatement prepareStatement(String query) {
129        return session.prepareStatement(LetterContactMechanismPurposeDetailFactory.class, query);
130    }
131    
132    public LetterContactMechanismPurposeDetailPK getNextPK() {
133        return new LetterContactMechanismPurposeDetailPK(entityIdGenerator.getNextEntityId());
134    }
135    
136    public Set<LetterContactMechanismPurposeDetailPK> getPKsFromResultSetAsSet(ResultSet rs)
137            throws PersistenceDatabaseException {
138        Set<LetterContactMechanismPurposeDetailPK> _result = new HashSet<>();
139        
140        try {
141            while(rs.next()) {
142                _result.add(getPKFromResultSet(rs));
143            }
144        } catch (SQLException se) {
145            throw new PersistenceDatabaseException(se);
146        }
147        
148        return _result;
149    }
150    
151    public java.util.List<LetterContactMechanismPurposeDetailPK> getPKsFromResultSetAsList(ResultSet rs)
152            throws PersistenceDatabaseException {
153        java.util.List<LetterContactMechanismPurposeDetailPK> _result = new ArrayList<>();
154        
155        try {
156            while(rs.next()) {
157                _result.add(getPKFromResultSet(rs));
158            }
159        } catch (SQLException se) {
160            throw new PersistenceDatabaseException(se);
161        }
162        
163        return _result;
164    }
165    
166    public LetterContactMechanismPurposeDetailPK getPKFromResultSet(ResultSet rs)
167            throws PersistenceDatabaseException {
168        LetterContactMechanismPurposeDetailPK _result;
169        
170        try {
171            long lttrcmprdt_lettercontactmechanismpurposedetailid = rs.getLong(LTTRCMPRDT_LETTERCONTACTMECHANISMPURPOSEDETAILID);
172            Long _entityId = rs.wasNull() ? null : lttrcmprdt_lettercontactmechanismpurposedetailid;
173            
174            _result = new LetterContactMechanismPurposeDetailPK(_entityId);
175        } catch (SQLException se) {
176            throw new PersistenceDatabaseException(se);
177        }
178        
179        return _result;
180    }
181    
182    public java.util.List<LetterContactMechanismPurposeDetailValue> getValuesFromPKs(Collection<LetterContactMechanismPurposeDetailPK> pks)
183            throws PersistenceDatabaseException {
184        java.util.List<LetterContactMechanismPurposeDetailValue> _values = new ArrayList<>(pks.size());
185        
186        for(LetterContactMechanismPurposeDetailPK _pk: pks) {
187            _values.add(getValueFromPK(_pk));
188        }
189        
190        return _values;
191    }
192    
193    public LetterContactMechanismPurposeDetailValue getValueFromPK(LetterContactMechanismPurposeDetailPK pk)
194            throws PersistenceDatabaseException {
195        LetterContactMechanismPurposeDetailValue _value;
196        
197        // See if we already have the entity in the session cache
198        LetterContactMechanismPurposeDetail _entity = (LetterContactMechanismPurposeDetail)session.getEntity(pk);
199        if(_entity == null)
200            _value = getEntityFromPK(EntityPermission.READ_ONLY, pk).getLetterContactMechanismPurposeDetailValue();
201        else
202            _value = _entity.getLetterContactMechanismPurposeDetailValue();
203        
204        return _value;
205    }
206    
207    public java.util.List<LetterContactMechanismPurposeDetailValue> getValuesFromResultSet(ResultSet rs)
208            throws PersistenceDatabaseException {
209        java.util.List<LetterContactMechanismPurposeDetailValue> _result = new ArrayList<>();
210        
211        try {
212            while(rs.next()) {
213                _result.add(getValueFromResultSet(rs));
214            }
215        } catch (SQLException se) {
216            throw new PersistenceDatabaseException(se);
217        }
218        
219        return _result;
220    }
221    
222    public LetterContactMechanismPurposeDetailValue getValueFromResultSet(ResultSet rs)
223            throws PersistenceDatabaseException {
224        LetterContactMechanismPurposeDetailValue _value;
225        
226        try {
227            Long lttrcmprdt_lettercontactmechanismpurposedetailid = rs.getLong(LTTRCMPRDT_LETTERCONTACTMECHANISMPURPOSEDETAILID);
228            LetterContactMechanismPurposeDetailPK _pk = new LetterContactMechanismPurposeDetailPK(lttrcmprdt_lettercontactmechanismpurposedetailid);
229            
230            // See if we already have the entity in the session cache
231            LetterContactMechanismPurposeDetail _entity = (LetterContactMechanismPurposeDetail)session.getEntity(_pk);
232            
233            if(_entity == null) {
234                Long lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid = rs.getLong(LTTRCMPRDT_LTTRCMPR_LETTERCONTACTMECHANISMPURPOSEID);
235                if(rs.wasNull())
236                    lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid = null;
237                
238                Long lttrcmprdt_lttr_letterid = rs.getLong(LTTRCMPRDT_LTTR_LETTERID);
239                if(rs.wasNull())
240                    lttrcmprdt_lttr_letterid = null;
241                
242                Integer lttrcmprdt_priority = rs.getInt(LTTRCMPRDT_PRIORITY);
243                if(rs.wasNull())
244                    lttrcmprdt_priority = null;
245                
246                Long lttrcmprdt_cmpr_contactmechanismpurposeid = rs.getLong(LTTRCMPRDT_CMPR_CONTACTMECHANISMPURPOSEID);
247                if(rs.wasNull())
248                    lttrcmprdt_cmpr_contactmechanismpurposeid = null;
249                
250                Long lttrcmprdt_fromtime = rs.getLong(LTTRCMPRDT_FROMTIME);
251                if(rs.wasNull())
252                    lttrcmprdt_fromtime = null;
253                
254                Long lttrcmprdt_thrutime = rs.getLong(LTTRCMPRDT_THRUTIME);
255                if(rs.wasNull())
256                    lttrcmprdt_thrutime = null;
257                
258                _value = new LetterContactMechanismPurposeDetailValue(_pk, new LetterContactMechanismPurposePK(lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid), new LetterPK(lttrcmprdt_lttr_letterid), lttrcmprdt_priority, new ContactMechanismPurposePK(lttrcmprdt_cmpr_contactmechanismpurposeid), lttrcmprdt_fromtime, lttrcmprdt_thrutime);
259            } else
260                _value = _entity.getLetterContactMechanismPurposeDetailValue();
261        } catch (SQLException se) {
262            throw new PersistenceDatabaseException(se);
263        }
264        
265        return _value;
266    }
267    
268    public java.util.List<LetterContactMechanismPurposeDetail> getEntitiesFromPKs(EntityPermission entityPermission, Collection<LetterContactMechanismPurposeDetailPK> pks)
269            throws PersistenceDatabaseException {
270        java.util.List<LetterContactMechanismPurposeDetail> _entities = new ArrayList<>(pks.size());
271        
272        for(LetterContactMechanismPurposeDetailPK _pk: pks) {
273            _entities.add(getEntityFromPK(entityPermission, _pk));
274        }
275        
276        return _entities;
277    }
278    
279    public LetterContactMechanismPurposeDetail getEntityFromValue(EntityPermission entityPermission, LetterContactMechanismPurposeDetailValue value) {
280        return getEntityFromPK(entityPermission, value.getPrimaryKey());
281    }
282    
283    public LetterContactMechanismPurposeDetail getEntityFromCache(LetterContactMechanismPurposeDetailPK pk) {
284        LetterContactMechanismPurposeDetailValue _value = (LetterContactMechanismPurposeDetailValue)session.getValueCache().get(pk);
285    
286        return _value == null ? null : new LetterContactMechanismPurposeDetail(_value, EntityPermission.READ_ONLY);
287    }
288    
289    public LetterContactMechanismPurposeDetail getEntityFromPK(EntityPermission entityPermission, LetterContactMechanismPurposeDetailPK pk)
290            throws PersistenceDatabaseException {
291        LetterContactMechanismPurposeDetail _entity;
292        
293        // See if we already have the entity in the session cache
294        _entity = (LetterContactMechanismPurposeDetail)session.getEntity(pk);
295        if(_entity != null) {
296            // If the requested permission is READ_WRITE, and the cached permission is
297            // READ_ONLY, then pretend that the cached object wasn't found, and create
298            // a new entity that is READ_WRITE.
299            if(entityPermission.equals(EntityPermission.READ_WRITE)) {
300                if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
301                    _entity = null;
302            }
303        }
304        
305        if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
306            _entity = getEntityFromCache(pk);
307        }
308        
309        if(_entity == null) {
310            PreparedStatement _ps = session.prepareStatement(entityPermission.equals(EntityPermission.READ_ONLY)? SQL_SELECT_READ_ONLY: SQL_SELECT_READ_WRITE);
311            long _entityId = pk.getEntityId();
312            ResultSet _rs = null;
313            
314            try {
315                _ps.setLong(1, _entityId);
316                _rs = _ps.executeQuery();
317                if(_rs.next()) {
318                    _entity = getEntityFromResultSet(entityPermission, _rs);
319                }
320            } catch (SQLException se) {
321                throw new PersistenceDatabaseException(se);
322            } finally {
323                if(_rs != null) {
324                    try {
325                        _rs.close();
326                    } catch (SQLException se) {
327                        // do nothing
328                    }
329                }
330            }
331        }
332        
333        return _entity;
334    }
335    
336    public Set<LetterContactMechanismPurposeDetailPK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params)
337            throws PersistenceDatabaseException {
338        Set<LetterContactMechanismPurposeDetailPK> _pks;
339        ResultSet _rs = null;
340        
341        try {
342            if(params.length != 0) {
343                Session.setQueryParams(ps, params);
344            }
345            
346            _rs = ps.executeQuery();
347            _pks = getPKsFromResultSetAsSet(_rs);
348            _rs.close();
349        } catch (SQLException se) {
350            throw new PersistenceDatabaseException(se);
351        } finally {
352            if(_rs != null) {
353                try {
354                    _rs.close();
355                } catch (SQLException se) {
356                    // do nothing
357                }
358            }
359        }
360        
361        return _pks;
362    }
363    
364    public java.util.List<LetterContactMechanismPurposeDetailPK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params)
365            throws PersistenceDatabaseException {
366        java.util.List<LetterContactMechanismPurposeDetailPK> _pks;
367        ResultSet _rs = null;
368        
369        try {
370            if(params.length != 0) {
371                Session.setQueryParams(ps, params);
372            }
373            
374            _rs = ps.executeQuery();
375            _pks = getPKsFromResultSetAsList(_rs);
376            _rs.close();
377        } catch (SQLException se) {
378            throw new PersistenceDatabaseException(se);
379        } finally {
380            if(_rs != null) {
381                try {
382                    _rs.close();
383                } catch (SQLException se) {
384                    // do nothing
385                }
386            }
387        }
388        
389        return _pks;
390    }
391    
392    public LetterContactMechanismPurposeDetailPK getPKFromQuery(PreparedStatement ps, final Object... params)
393            throws PersistenceDatabaseException {
394        LetterContactMechanismPurposeDetailPK _pk = null;
395        ResultSet _rs = null;
396        
397        try {
398            if(params.length != 0) {
399                Session.setQueryParams(ps, params);
400            }
401            
402            _rs = ps.executeQuery();
403            if(_rs.next()) {
404                _pk = getPKFromResultSet(_rs);
405            }
406            _rs.close();
407        } catch (SQLException se) {
408            throw new PersistenceDatabaseException(se);
409        } finally {
410            if(_rs != null) {
411                try {
412                    _rs.close();
413                } catch (SQLException se) {
414                    // do nothing
415                }
416            }
417        }
418        
419        return _pk;
420    }
421    
422    public java.util.List<LetterContactMechanismPurposeDetail> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
423            throws PersistenceDatabaseException {
424        PreparedStatement ps = session.prepareStatement(LetterContactMechanismPurposeDetailFactory.class, queryMap.get(entityPermission));
425        
426        return getEntitiesFromQuery(entityPermission, ps, params);
427    }
428    
429    public java.util.List<LetterContactMechanismPurposeDetail> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
430            throws PersistenceDatabaseException {
431        PreparedStatement ps = session.prepareStatement(LetterContactMechanismPurposeDetailFactory.class, queryMap.get(entityPermission));
432        
433        return getEntitiesFromQuery(entityPermission, ps);
434    }
435    
436    public java.util.List<LetterContactMechanismPurposeDetail> getEntitiesFromQuery(EntityPermission entityPermission, ResultQuery<?> query)
437            throws PersistenceDatabaseException {
438        PreparedStatement ps = session.prepareStatement(LetterContactMechanismPurposeDetailFactory.class, query.getSQL());
439        
440        return getEntitiesFromQuery(entityPermission, ps, query.getBindValues().toArray());
441    }
442    
443    public java.util.List<LetterContactMechanismPurposeDetail> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
444            throws PersistenceDatabaseException {
445        java.util.List<LetterContactMechanismPurposeDetail> _entities;
446        ResultSet _rs = null;
447        
448        try {
449            if(params.length != 0) {
450                Session.setQueryParams(ps, params);
451            }
452            
453            _rs = ps.executeQuery();
454            _entities = getEntitiesFromResultSet(entityPermission, _rs);
455            _rs.close();
456        } catch (SQLException se) {
457            throw new PersistenceDatabaseException(se);
458        } finally {
459            if(_rs != null) {
460                try {
461                    _rs.close();
462                } catch (SQLException se) {
463                    // do nothing
464                }
465            }
466        }
467        
468        return _entities;
469    }
470    
471    public LetterContactMechanismPurposeDetail getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
472            throws PersistenceDatabaseException {
473        PreparedStatement ps = session.prepareStatement(LetterContactMechanismPurposeDetailFactory.class, queryMap.get(entityPermission));
474        
475        return getEntityFromQuery(entityPermission, ps, params);
476    }
477    
478    public LetterContactMechanismPurposeDetail getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
479            throws PersistenceDatabaseException {
480        PreparedStatement ps = session.prepareStatement(LetterContactMechanismPurposeDetailFactory.class, queryMap.get(entityPermission));
481        
482        return getEntityFromQuery(entityPermission, ps);
483    }
484    
485    public LetterContactMechanismPurposeDetail getEntityFromQuery(EntityPermission entityPermission, ResultQuery<?> query)
486            throws PersistenceDatabaseException {
487        PreparedStatement ps = session.prepareStatement(LetterContactMechanismPurposeDetailFactory.class, query.getSQL());
488        
489        return getEntityFromQuery(entityPermission, ps, query.getBindValues().toArray());
490    }
491    
492    public LetterContactMechanismPurposeDetail getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
493            throws PersistenceDatabaseException {
494        LetterContactMechanismPurposeDetail _entity = null;
495        ResultSet _rs = null;
496        
497        try {
498            if(params.length != 0) {
499                Session.setQueryParams(ps, params);
500            }
501            
502            _rs = ps.executeQuery();
503            if(_rs.next()) {
504                _entity = getEntityFromResultSet(entityPermission, _rs);
505            }
506            _rs.close();
507        } catch (SQLException se) {
508            throw new PersistenceDatabaseException(se);
509        } finally {
510            if(_rs != null) {
511                try {
512                    _rs.close();
513                } catch (SQLException se) {
514                    // do nothing
515                }
516            }
517        }
518        
519        return _entity;
520    }
521    
522    public java.util.List<LetterContactMechanismPurposeDetail> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs)
523            throws PersistenceDatabaseException {
524        java.util.List<LetterContactMechanismPurposeDetail> _result = new ArrayList<>();
525        
526        try {
527            while(rs.next()) {
528                _result.add(getEntityFromResultSet(entityPermission, rs));
529            }
530        } catch (SQLException se) {
531            throw new PersistenceDatabaseException(se);
532        }
533        
534        return _result;
535    }
536    
537    public LetterContactMechanismPurposeDetail getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs)
538            throws PersistenceDatabaseException {
539        LetterContactMechanismPurposeDetail _entity;
540        
541        try {
542            Long lttrcmprdt_lettercontactmechanismpurposedetailid = rs.getLong(LTTRCMPRDT_LETTERCONTACTMECHANISMPURPOSEDETAILID);
543            LetterContactMechanismPurposeDetailPK _pk = new LetterContactMechanismPurposeDetailPK(lttrcmprdt_lettercontactmechanismpurposedetailid);
544            
545            // See if we already have the entity in the session cache
546            _entity = (LetterContactMechanismPurposeDetail)session.getEntity(_pk);
547            if(_entity != null) {
548                // If the requested permission is READ_WRITE, and the cached permission is
549                // READ_ONLY, then pretend that the cached object wasn't found, and create
550                // a new entity that is READ_WRITE.
551                if(entityPermission.equals(EntityPermission.READ_WRITE)) {
552                    if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
553                        _entity = null;
554                }
555            }
556            boolean foundInSessionCache = _entity != null;
557            
558            if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
559                _entity = getEntityFromCache(_pk);
560            }
561            
562            if(_entity == null) {
563                Long lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid = rs.getLong(LTTRCMPRDT_LTTRCMPR_LETTERCONTACTMECHANISMPURPOSEID);
564                if(rs.wasNull())
565                    lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid = null;
566                
567                Long lttrcmprdt_lttr_letterid = rs.getLong(LTTRCMPRDT_LTTR_LETTERID);
568                if(rs.wasNull())
569                    lttrcmprdt_lttr_letterid = null;
570                
571                Integer lttrcmprdt_priority = rs.getInt(LTTRCMPRDT_PRIORITY);
572                if(rs.wasNull())
573                    lttrcmprdt_priority = null;
574                
575                Long lttrcmprdt_cmpr_contactmechanismpurposeid = rs.getLong(LTTRCMPRDT_CMPR_CONTACTMECHANISMPURPOSEID);
576                if(rs.wasNull())
577                    lttrcmprdt_cmpr_contactmechanismpurposeid = null;
578                
579                Long lttrcmprdt_fromtime = rs.getLong(LTTRCMPRDT_FROMTIME);
580                if(rs.wasNull())
581                    lttrcmprdt_fromtime = null;
582                
583                Long lttrcmprdt_thrutime = rs.getLong(LTTRCMPRDT_THRUTIME);
584                if(rs.wasNull())
585                    lttrcmprdt_thrutime = null;
586                
587                LetterContactMechanismPurposeDetailValue _value = new LetterContactMechanismPurposeDetailValue(_pk, lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid == null? null: new LetterContactMechanismPurposePK(lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid), lttrcmprdt_lttr_letterid == null? null: new LetterPK(lttrcmprdt_lttr_letterid), lttrcmprdt_priority, lttrcmprdt_cmpr_contactmechanismpurposeid == null? null: new ContactMechanismPurposePK(lttrcmprdt_cmpr_contactmechanismpurposeid), lttrcmprdt_fromtime, lttrcmprdt_thrutime);
588                _entity = new LetterContactMechanismPurposeDetail(_value, entityPermission);
589            }
590            
591            if(!foundInSessionCache) {
592                if(entityPermission.equals(EntityPermission.READ_ONLY)) {
593                    session.putReadOnlyEntity(_pk, _entity);
594                    session.getValueCache().put(_entity.getLetterContactMechanismPurposeDetailValue());
595                } else {
596                    session.putReadWriteEntity(_pk, _entity);
597                }
598            }
599        } catch (SQLException se) {
600            throw new PersistenceDatabaseException(se);
601        }
602        
603        return _entity;
604    }
605    
606    public LetterContactMechanismPurposeDetail create(LetterContactMechanismPurpose letterContactMechanismPurpose, Letter letter, Integer priority, ContactMechanismPurpose contactMechanismPurpose, Long fromTime, Long thruTime)
607            throws PersistenceDatabaseException, PersistenceNotNullException {
608        return create(letterContactMechanismPurpose == null ? null : letterContactMechanismPurpose.getPrimaryKey(), letter == null ? null : letter.getPrimaryKey(), priority, contactMechanismPurpose == null ? null : contactMechanismPurpose.getPrimaryKey(), fromTime, thruTime);
609    }
610    
611    private void bindForCreate(PreparedStatement _ps, LetterContactMechanismPurposeDetailValue _value)
612            throws SQLException {
613        _ps.setLong(1, _value.getEntityId());
614        
615        LetterContactMechanismPurposePK lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid = _value.getLetterContactMechanismPurposePK();
616        if(lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid == null)
617            _ps.setNull(2, Types.BIGINT);
618        else
619            _ps.setLong(2, lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid.getEntityId());
620            
621        LetterPK lttrcmprdt_lttr_letterid = _value.getLetterPK();
622        if(lttrcmprdt_lttr_letterid == null)
623            _ps.setNull(3, Types.BIGINT);
624        else
625            _ps.setLong(3, lttrcmprdt_lttr_letterid.getEntityId());
626            
627        Integer lttrcmprdt_priority = _value.getPriority();
628        if(lttrcmprdt_priority == null)
629            _ps.setNull(4, Types.INTEGER);
630        else
631            _ps.setInt(4, lttrcmprdt_priority);
632            
633        ContactMechanismPurposePK lttrcmprdt_cmpr_contactmechanismpurposeid = _value.getContactMechanismPurposePK();
634        if(lttrcmprdt_cmpr_contactmechanismpurposeid == null)
635            _ps.setNull(5, Types.BIGINT);
636        else
637            _ps.setLong(5, lttrcmprdt_cmpr_contactmechanismpurposeid.getEntityId());
638            
639        Long lttrcmprdt_fromtime = _value.getFromTime();
640        if(lttrcmprdt_fromtime == null)
641            _ps.setNull(6, Types.BIGINT);
642        else
643            _ps.setLong(6, lttrcmprdt_fromtime);
644            
645        Long lttrcmprdt_thrutime = _value.getThruTime();
646        if(lttrcmprdt_thrutime == null)
647            _ps.setNull(7, Types.BIGINT);
648        else
649            _ps.setLong(7, lttrcmprdt_thrutime);
650            
651    }
652    
653    public LetterContactMechanismPurposeDetail create(LetterContactMechanismPurposePK letterContactMechanismPurposePK, LetterPK letterPK, Integer priority, ContactMechanismPurposePK contactMechanismPurposePK, Long fromTime, Long thruTime)
654            throws PersistenceDatabaseException, PersistenceNotNullException {
655        LetterContactMechanismPurposeDetailPK _pk = getNextPK();
656        LetterContactMechanismPurposeDetailValue _value = new LetterContactMechanismPurposeDetailValue(_pk, letterContactMechanismPurposePK, letterPK, priority, contactMechanismPurposePK, fromTime, thruTime);
657        
658        PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
659        
660        try {
661            bindForCreate(_ps, _value);
662            
663            if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
664                int _count = _ps.executeUpdate();
665                
666                if(_count != 1) {
667                    throw new PersistenceDatabaseUpdateException("insert failed, _count = " + _count);
668                }
669            } else {
670                 _ps.executeUpdate();
671            }
672            
673            session.getValueCache().put(_value);
674        } catch (SQLException se) {
675            throw new PersistenceDatabaseException(se);
676        }
677        
678        LetterContactMechanismPurposeDetail _entity = new LetterContactMechanismPurposeDetail(_value, EntityPermission.READ_ONLY);
679        session.putReadOnlyEntity(_pk, _entity);
680        
681        return _entity;
682    }
683    
684    public void create(Collection<LetterContactMechanismPurposeDetailValue> _values)
685            throws PersistenceDatabaseException, PersistenceNotNullException {
686        int _size = _values.size();
687        
688        if(_size > 0) {
689            PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
690            List<LetterContactMechanismPurposeDetailValue> _cacheValues = new ArrayList<>(_size);
691            
692            try {
693                for(LetterContactMechanismPurposeDetailValue _value : _values) {
694                    _value.setEntityId(entityIdGenerator.getNextEntityId());
695                    bindForCreate(_ps, _value);
696                    
697                    _ps.addBatch();
698                    
699                    _cacheValues.add(_value);
700                }
701                
702                if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
703                    int[] _counts = _ps.executeBatch();
704                    
705                    for(int _countOffset = 0 ; _countOffset < _size ; _countOffset++) {
706                        if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
707                            throw new PersistenceDatabaseUpdateException("batch insert failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
708                        }
709                    }
710                } else {
711                     _ps.executeBatch();
712                }
713                
714                _ps.clearBatch();
715            } catch (SQLException se) {
716                throw new PersistenceDatabaseException(se);
717            }
718            
719            _cacheValues.forEach((_cacheValue) -> {
720                LetterContactMechanismPurposeDetail _cacheEntity = new LetterContactMechanismPurposeDetail(_cacheValue, EntityPermission.READ_ONLY);
721                
722                session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity);
723            });
724        }
725    }
726    
727    private boolean bindForStore(PreparedStatement _ps, LetterContactMechanismPurposeDetailValue _value)
728            throws SQLException {
729        boolean _hasBeenModified = _value.hasBeenModified();
730        
731        if(_hasBeenModified) {
732            LetterContactMechanismPurposePK lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid = _value.getLetterContactMechanismPurposePK();
733            if(lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid == null)
734                _ps.setNull(1, Types.BIGINT);
735            else
736                _ps.setLong(1, lttrcmprdt_lttrcmpr_lettercontactmechanismpurposeid.getEntityId());
737            
738            LetterPK lttrcmprdt_lttr_letterid = _value.getLetterPK();
739            if(lttrcmprdt_lttr_letterid == null)
740                _ps.setNull(2, Types.BIGINT);
741            else
742                _ps.setLong(2, lttrcmprdt_lttr_letterid.getEntityId());
743            
744            Integer lttrcmprdt_priority = _value.getPriority();
745            if(lttrcmprdt_priority == null)
746                _ps.setNull(3, Types.INTEGER);
747            else
748                _ps.setInt(3, lttrcmprdt_priority);
749            
750            ContactMechanismPurposePK lttrcmprdt_cmpr_contactmechanismpurposeid = _value.getContactMechanismPurposePK();
751            if(lttrcmprdt_cmpr_contactmechanismpurposeid == null)
752                _ps.setNull(4, Types.BIGINT);
753            else
754                _ps.setLong(4, lttrcmprdt_cmpr_contactmechanismpurposeid.getEntityId());
755            
756            Long lttrcmprdt_fromtime = _value.getFromTime();
757            if(lttrcmprdt_fromtime == null)
758                _ps.setNull(5, Types.BIGINT);
759            else
760                _ps.setLong(5, lttrcmprdt_fromtime);
761            
762            Long lttrcmprdt_thrutime = _value.getThruTime();
763            if(lttrcmprdt_thrutime == null)
764                _ps.setNull(6, Types.BIGINT);
765            else
766                _ps.setLong(6, lttrcmprdt_thrutime);
767            
768            _ps.setLong(7, _value.getPrimaryKey().getEntityId());
769            
770            _value.clearHasBeenModified();
771        }
772        
773        return _hasBeenModified;
774    }
775    
776    @Override
777    public void store(LetterContactMechanismPurposeDetail entity)
778            throws PersistenceDatabaseException {
779        PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
780        
781        try {
782            LetterContactMechanismPurposeDetailValue _value = entity.getLetterContactMechanismPurposeDetailValue();
783            
784            if(bindForStore(_ps, _value)) {
785                if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
786                    int _count = _ps.executeUpdate();
787                    
788                    if(_count != 1) {
789                        throw new PersistenceDatabaseUpdateException("update failed, _count = " + _count);
790                    }
791                } else {
792                     _ps.executeUpdate();
793                }
794                
795                session.getValueCache().put(_value);
796            }
797        } catch (SQLException se) {
798            throw new PersistenceDatabaseException(se);
799        }
800    }
801    
802    @Override
803    public void store(Collection<LetterContactMechanismPurposeDetail> entities)
804            throws PersistenceDatabaseException {
805        if(entities.size() > 0) {
806            PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
807            int _modifiedEntities = 0;
808            
809            try {
810                for(LetterContactMechanismPurposeDetail entity : entities) {
811                    if(bindForStore(_ps, entity.getLetterContactMechanismPurposeDetailValue())) {
812                        _ps.addBatch();
813                        _modifiedEntities++;
814                    }
815                }
816                
817                if(_modifiedEntities != 0) {
818                    if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
819                        int[] _counts = _ps.executeBatch();
820                        
821                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
822                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
823                                throw new PersistenceDatabaseUpdateException("batch update failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
824                            }
825                        }
826                    } else {
827                         _ps.executeBatch();
828                    }
829                    
830                    _ps.clearBatch();
831                    
832                    entities.forEach((entity) -> {
833                        session.getValueCache().put(entity.getLetterContactMechanismPurposeDetailValue());
834                    });
835                }
836            } catch (SQLException se) {
837                throw new PersistenceDatabaseException(se);
838            }
839        }
840    }
841    
842    @Override
843    public void remove(LetterContactMechanismPurposeDetail entity)
844            throws PersistenceDatabaseException {
845        remove(entity.getPrimaryKey());
846    }
847    
848    @Override
849    public void remove(LetterContactMechanismPurposeDetailPK pk)
850            throws PersistenceDatabaseException {
851        PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
852        long _entityId = pk.getEntityId();
853        
854        try {
855            _ps.setLong(1, _entityId);
856            
857            if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
858                int _count = _ps.executeUpdate();
859                
860                if(_count != 1) {
861                    throw new PersistenceDatabaseUpdateException("remove failed, _count = " + _count);
862                }
863            } else {
864                 _ps.executeUpdate();
865            }
866            
867            session.getValueCache().remove(pk);
868        } catch (SQLException se) {
869            throw new PersistenceDatabaseException(se);
870        }
871        
872        session.removed(pk, false);
873    }
874    
875    @Override
876    public void remove(Collection<LetterContactMechanismPurposeDetailPK> pks)
877            throws PersistenceDatabaseException {
878        if(pks.size() > 0) {
879            PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
880            int _modifiedEntities = 0;
881            
882            try {
883                for(LetterContactMechanismPurposeDetailPK pk : pks) {
884                    long _entityId = pk.getEntityId();
885                    
886                    _ps.setLong(1, _entityId);
887                    
888                    _ps.addBatch();
889                    _modifiedEntities++;
890                }
891                
892                if(_modifiedEntities != 0) {
893                    if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
894                        int[] _counts = _ps.executeBatch();
895                        
896                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
897                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
898                                throw new PersistenceDatabaseUpdateException("batch remove failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
899                            }
900                        }
901                    } else {
902                        _ps.executeBatch();
903                    }
904                    
905                    _ps.clearBatch();
906                    
907                    pks.forEach((pk) -> {
908                        session.getValueCache().remove(pk);
909                    });
910                }
911            } catch (SQLException se) {
912                throw new PersistenceDatabaseException(se);
913            }
914            
915            pks.forEach((pk) -> {
916                session.removed(pk, true);
917            });
918        }
919    }
920    
921    @Override
922    public boolean validPK(LetterContactMechanismPurposeDetailPK pk)
923            throws PersistenceDatabaseException {
924        boolean valid = false;
925        PreparedStatement _ps = session.prepareStatement(SQL_VALID);
926        ResultSet _rs = null;
927        
928        try {
929            _ps.setLong(1, pk.getEntityId());
930            
931            _rs = _ps.executeQuery();
932            if(_rs.next()) {
933                long _count = _rs.getLong(1);
934                if(_rs.wasNull())
935                    _count = 0;
936                
937                if(_count == 1)
938                    valid = true;
939            }
940        } catch (SQLException se) {
941            throw new PersistenceDatabaseException(se);
942        } finally {
943            if(_rs != null) {
944                try {
945                    _rs.close();
946                } catch (SQLException se) {
947                    // do nothing
948                }
949            }
950        }
951        
952        return valid;
953    }
954    
955}