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