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