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