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