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