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