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