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