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 * QueuedEntityFactory.java
021 */
022
023package com.echothree.model.data.queue.server.factory;
024
025import com.echothree.model.data.queue.common.pk.QueueTypePK;
026import com.echothree.model.data.core.common.pk.EntityInstancePK;
027
028import com.echothree.model.data.queue.server.entity.QueueType;
029import com.echothree.model.data.core.server.entity.EntityInstance;
030
031import com.echothree.model.data.queue.common.QueuedEntityConstants;
032import com.echothree.model.data.queue.common.pk.QueuedEntityPK;
033import com.echothree.model.data.queue.server.value.QueuedEntityValue;
034import com.echothree.model.data.queue.server.entity.QueuedEntity;
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 QueuedEntityFactory
063        implements BaseFactory<QueuedEntityPK, QueuedEntity> {
064    
065    //final private static Log log = LogFactory.getLog(QueuedEntityFactory.class);
066    
067    final private static String SQL_SELECT_READ_ONLY = "SELECT qeni_queuedentityid, qeni_qtyp_queuetypeid, qeni_eni_entityinstanceid, qeni_time FROM queuedentities WHERE qeni_queuedentityid = ?";
068    final private static String SQL_SELECT_READ_WRITE = "SELECT qeni_queuedentityid, qeni_qtyp_queuetypeid, qeni_eni_entityinstanceid, qeni_time FROM queuedentities WHERE qeni_queuedentityid = ? FOR UPDATE";
069    final private static String SQL_INSERT = "INSERT INTO queuedentities (qeni_queuedentityid, qeni_qtyp_queuetypeid, qeni_eni_entityinstanceid, qeni_time) VALUES (?, ?, ?, ?)";
070    final private static String SQL_UPDATE = "UPDATE queuedentities SET qeni_qtyp_queuetypeid = ?, qeni_eni_entityinstanceid = ?, qeni_time = ? WHERE qeni_queuedentityid = ?";
071    final private static String SQL_DELETE = "DELETE FROM queuedentities WHERE qeni_queuedentityid = ?";
072    final private static String SQL_VALID = "SELECT COUNT(*) FROM queuedentities WHERE qeni_queuedentityid = ?";
073    
074    final private static String PK_COLUMN = "qeni_queuedentityid";
075    final private static String ALL_COLUMNS = "qeni_queuedentityid, qeni_qtyp_queuetypeid, qeni_eni_entityinstanceid, qeni_time";
076    final public static String TABLE_NAME = "queuedentities";
077    
078    final public static String QENI_QUEUEDENTITYID = "qeni_queuedentityid";
079    final public static String QENI_QTYP_QUEUETYPEID = "qeni_qtyp_queuetypeid";
080    final public static String QENI_ENI_ENTITYINSTANCEID = "qeni_eni_entityinstanceid";
081    final public static String QENI_TIME = "qeni_time";
082    
083    final private static EntityIdGenerator entityIdGenerator = new EntityIdGenerator(QueuedEntityConstants.COMPONENT_VENDOR_NAME, QueuedEntityConstants.ENTITY_TYPE_NAME);
084    
085    /** Creates a new instance of QueuedEntityFactory */
086    protected QueuedEntityFactory() {
087        super();
088    }
089    
090    public static QueuedEntityFactory getInstance() {
091        return CDI.current().select(QueuedEntityFactory.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 QueuedEntityConstants.COMPONENT_VENDOR_NAME;
112    }
113    
114    @Override
115    public String getEntityTypeName() {
116        return QueuedEntityConstants.ENTITY_TYPE_NAME;
117    }
118    
119    public PreparedStatement prepareStatement(String query) {
120        return ThreadSession.currentSession().prepareStatement(QueuedEntityFactory.class, query);
121    }
122    
123    public QueuedEntityPK getNextPK() {
124        return new QueuedEntityPK(entityIdGenerator.getNextEntityId());
125    }
126    
127    public Set<QueuedEntityPK> getPKsFromResultSetAsSet(ResultSet rs)
128            throws PersistenceDatabaseException {
129        Set<QueuedEntityPK> _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<QueuedEntityPK> getPKsFromResultSetAsList(ResultSet rs)
143            throws PersistenceDatabaseException {
144        java.util.List<QueuedEntityPK> _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 QueuedEntityPK getPKFromResultSet(ResultSet rs)
158            throws PersistenceDatabaseException {
159        QueuedEntityPK _result;
160        
161        try {
162            long qeni_queuedentityid = rs.getLong(QENI_QUEUEDENTITYID);
163            Long _entityId = rs.wasNull() ? null : qeni_queuedentityid;
164            
165            _result = new QueuedEntityPK(_entityId);
166        } catch (SQLException se) {
167            throw new PersistenceDatabaseException(se);
168        }
169        
170        return _result;
171    }
172    
173    public java.util.List<QueuedEntityValue> getValuesFromPKs(Session session, Collection<QueuedEntityPK> pks)
174            throws PersistenceDatabaseException {
175        java.util.List<QueuedEntityValue> _values = new ArrayList<>(pks.size());
176        
177        for(QueuedEntityPK _pk: pks) {
178            _values.add(getValueFromPK(session, _pk));
179        }
180        
181        return _values;
182    }
183    
184    public QueuedEntityValue getValueFromPK(Session session, QueuedEntityPK pk)
185            throws PersistenceDatabaseException {
186        QueuedEntityValue _value;
187        
188        // See if we already have the entity in the session cache
189        QueuedEntity _entity = (QueuedEntity)session.getEntity(pk);
190        if(_entity == null)
191            _value = getEntityFromPK(session, EntityPermission.READ_ONLY, pk).getQueuedEntityValue();
192        else
193            _value = _entity.getQueuedEntityValue();
194        
195        return _value;
196    }
197    
198    public java.util.List<QueuedEntityValue> getValuesFromResultSet(Session session, ResultSet rs)
199            throws PersistenceDatabaseException {
200        java.util.List<QueuedEntityValue> _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 QueuedEntityValue getValueFromResultSet(Session session, ResultSet rs)
214            throws PersistenceDatabaseException {
215        QueuedEntityValue _value;
216        
217        try {
218            Long qeni_queuedentityid = rs.getLong(QENI_QUEUEDENTITYID);
219            QueuedEntityPK _pk = new QueuedEntityPK(qeni_queuedentityid);
220            
221            // See if we already have the entity in the session cache
222            QueuedEntity _entity = (QueuedEntity)session.getEntity(_pk);
223            
224            if(_entity == null) {
225                Long qeni_qtyp_queuetypeid = rs.getLong(QENI_QTYP_QUEUETYPEID);
226                if(rs.wasNull())
227                    qeni_qtyp_queuetypeid = null;
228                
229                Long qeni_eni_entityinstanceid = rs.getLong(QENI_ENI_ENTITYINSTANCEID);
230                if(rs.wasNull())
231                    qeni_eni_entityinstanceid = null;
232                
233                Long qeni_time = rs.getLong(QENI_TIME);
234                if(rs.wasNull())
235                    qeni_time = null;
236                
237                _value = new QueuedEntityValue(_pk, new QueueTypePK(qeni_qtyp_queuetypeid), new EntityInstancePK(qeni_eni_entityinstanceid), qeni_time);
238            } else
239                _value = _entity.getQueuedEntityValue();
240        } catch (SQLException se) {
241            throw new PersistenceDatabaseException(se);
242        }
243        
244        return _value;
245    }
246    
247    public java.util.List<QueuedEntity> getEntitiesFromPKs(EntityPermission entityPermission, Collection<QueuedEntityPK> pks)
248            throws PersistenceDatabaseException {
249        return getEntitiesFromPKs(ThreadSession.currentSession(), entityPermission, pks);
250    }
251    
252    public java.util.List<QueuedEntity> getEntitiesFromPKs(Session session, EntityPermission entityPermission, Collection<QueuedEntityPK> pks)
253            throws PersistenceDatabaseException {
254        java.util.List<QueuedEntity> _entities = new ArrayList<>(pks.size());
255        
256        for(QueuedEntityPK _pk: pks) {
257            _entities.add(getEntityFromPK(session, entityPermission, _pk));
258        }
259        
260        return _entities;
261    }
262    
263    public QueuedEntity getEntityFromValue(EntityPermission entityPermission, QueuedEntityValue value) {
264        return getEntityFromPK(ThreadSession.currentSession(), entityPermission, value.getPrimaryKey());
265    }
266    
267    public QueuedEntity getEntityFromValue(Session session, EntityPermission entityPermission, QueuedEntityValue value) {
268        return getEntityFromPK(session, entityPermission, value.getPrimaryKey());
269    }
270    
271    public QueuedEntity getEntityFromPK(EntityPermission entityPermission, QueuedEntityPK pk)
272            throws PersistenceDatabaseException {
273        return getEntityFromPK(ThreadSession.currentSession(), entityPermission, pk);
274    }
275    
276    public QueuedEntity getEntityFromCache(Session session, QueuedEntityPK pk) {
277        QueuedEntityValue _value = (QueuedEntityValue)session.getValueCache().get(pk);
278    
279        return _value == null ? null : new QueuedEntity(_value, EntityPermission.READ_ONLY);
280    }
281    
282    public QueuedEntity getEntityFromPK(Session session, EntityPermission entityPermission, QueuedEntityPK pk)
283            throws PersistenceDatabaseException {
284        QueuedEntity _entity;
285        
286        // See if we already have the entity in the session cache
287        _entity = (QueuedEntity)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<QueuedEntityPK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params)
330            throws PersistenceDatabaseException {
331        Set<QueuedEntityPK> _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<QueuedEntityPK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params)
358            throws PersistenceDatabaseException {
359        java.util.List<QueuedEntityPK> _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 QueuedEntityPK getPKFromQuery(PreparedStatement ps, final Object... params)
386            throws PersistenceDatabaseException {
387        QueuedEntityPK _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<QueuedEntity> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
416            throws PersistenceDatabaseException {
417        PreparedStatement ps = session.prepareStatement(QueuedEntityFactory.class, queryMap.get(entityPermission));
418        
419        return getEntitiesFromQuery(session, entityPermission, ps, params);
420    }
421    
422    public java.util.List<QueuedEntity> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
423            throws PersistenceDatabaseException {
424        Session session = ThreadSession.currentSession();
425        PreparedStatement ps = session.prepareStatement(QueuedEntityFactory.class, queryMap.get(entityPermission));
426        
427        return getEntitiesFromQuery(session, entityPermission, ps, params);
428    }
429    
430    public java.util.List<QueuedEntity> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
431            throws PersistenceDatabaseException {
432        PreparedStatement ps = session.prepareStatement(QueuedEntityFactory.class, queryMap.get(entityPermission));
433        
434        return getEntitiesFromQuery(session, entityPermission, ps);
435    }
436    
437    public java.util.List<QueuedEntity> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
438            throws PersistenceDatabaseException {
439        Session session = ThreadSession.currentSession();
440        PreparedStatement ps = session.prepareStatement(QueuedEntityFactory.class, queryMap.get(entityPermission));
441        
442        return getEntitiesFromQuery(session, entityPermission, ps);
443    }
444    
445    public java.util.List<QueuedEntity> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps)
446            throws PersistenceDatabaseException {
447        return getEntitiesFromQuery(ThreadSession.currentSession(), entityPermission, ps);
448    }
449    
450    public java.util.List<QueuedEntity> 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<QueuedEntity> getEntitiesFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params)
456            throws PersistenceDatabaseException {
457        java.util.List<QueuedEntity> _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 QueuedEntity getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
484            throws PersistenceDatabaseException {
485        PreparedStatement ps = session.prepareStatement(QueuedEntityFactory.class, queryMap.get(entityPermission));
486        
487        return getEntityFromQuery(session, entityPermission, ps, params);
488    }
489    
490    public QueuedEntity getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
491            throws PersistenceDatabaseException {
492        Session session = ThreadSession.currentSession();
493        PreparedStatement ps = session.prepareStatement(QueuedEntityFactory.class, queryMap.get(entityPermission));
494        
495        return getEntityFromQuery(session, entityPermission, ps, params);
496    }
497    
498    public QueuedEntity getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
499            throws PersistenceDatabaseException {
500        PreparedStatement ps = session.prepareStatement(QueuedEntityFactory.class, queryMap.get(entityPermission));
501        
502        return getEntityFromQuery(session, entityPermission, ps);
503    }
504    
505    public QueuedEntity getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
506            throws PersistenceDatabaseException {
507        Session session = ThreadSession.currentSession();
508        PreparedStatement ps = session.prepareStatement(QueuedEntityFactory.class, queryMap.get(entityPermission));
509        
510        return getEntityFromQuery(session, entityPermission, ps);
511    }
512    
513    public QueuedEntity getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps)
514            throws PersistenceDatabaseException {
515        return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps);
516    }
517    
518    public QueuedEntity getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
519            throws PersistenceDatabaseException {
520        return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps, params);
521    }
522    
523    public QueuedEntity getEntityFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params)
524            throws PersistenceDatabaseException {
525        QueuedEntity _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<QueuedEntity> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs)
554            throws PersistenceDatabaseException {
555        return getEntitiesFromResultSet(ThreadSession.currentSession(), entityPermission, rs);
556    }
557    
558    public java.util.List<QueuedEntity> getEntitiesFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs)
559            throws PersistenceDatabaseException {
560        java.util.List<QueuedEntity> _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 QueuedEntity getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs)
574            throws PersistenceDatabaseException {
575        return getEntityFromResultSet(ThreadSession.currentSession(), entityPermission, rs);
576    }
577    
578    public QueuedEntity getEntityFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs)
579            throws PersistenceDatabaseException {
580        QueuedEntity _entity;
581        
582        try {
583            Long qeni_queuedentityid = rs.getLong(QENI_QUEUEDENTITYID);
584            QueuedEntityPK _pk = new QueuedEntityPK(qeni_queuedentityid);
585            
586            // See if we already have the entity in the session cache
587            _entity = (QueuedEntity)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 qeni_qtyp_queuetypeid = rs.getLong(QENI_QTYP_QUEUETYPEID);
605                if(rs.wasNull())
606                    qeni_qtyp_queuetypeid = null;
607                
608                Long qeni_eni_entityinstanceid = rs.getLong(QENI_ENI_ENTITYINSTANCEID);
609                if(rs.wasNull())
610                    qeni_eni_entityinstanceid = null;
611                
612                Long qeni_time = rs.getLong(QENI_TIME);
613                if(rs.wasNull())
614                    qeni_time = null;
615                
616                QueuedEntityValue _value = new QueuedEntityValue(_pk, qeni_qtyp_queuetypeid == null? null: new QueueTypePK(qeni_qtyp_queuetypeid), qeni_eni_entityinstanceid == null? null: new EntityInstancePK(qeni_eni_entityinstanceid), qeni_time);
617                _entity = new QueuedEntity(_value, entityPermission);
618            }
619            
620            if(!foundInSessionCache) {
621                if(entityPermission.equals(EntityPermission.READ_ONLY)) {
622                    session.putReadOnlyEntity(_pk, _entity);
623                    session.getValueCache().put(_entity.getQueuedEntityValue());
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 QueuedEntity create(Session session, QueueType queueType, EntityInstance entityInstance, Long time)
636            throws PersistenceDatabaseException, PersistenceNotNullException {
637        return create(session, queueType == null ? null : queueType.getPrimaryKey(), entityInstance == null ? null : entityInstance.getPrimaryKey(), time);
638    }
639    
640    public QueuedEntity create(QueueType queueType, EntityInstance entityInstance, Long time)
641            throws PersistenceDatabaseException, PersistenceNotNullException {
642        return create(ThreadSession.currentSession(), queueType == null ? null : queueType.getPrimaryKey(), entityInstance == null ? null : entityInstance.getPrimaryKey(), time);
643    }
644    
645    private void bindForCreate(PreparedStatement _ps, QueuedEntityValue _value)
646            throws SQLException {
647        _ps.setLong(1, _value.getEntityId());
648        
649        QueueTypePK qeni_qtyp_queuetypeid = _value.getQueueTypePK();
650        if(qeni_qtyp_queuetypeid == null)
651            _ps.setNull(2, Types.BIGINT);
652        else
653            _ps.setLong(2, qeni_qtyp_queuetypeid.getEntityId());
654            
655        EntityInstancePK qeni_eni_entityinstanceid = _value.getEntityInstancePK();
656        if(qeni_eni_entityinstanceid == null)
657            _ps.setNull(3, Types.BIGINT);
658        else
659            _ps.setLong(3, qeni_eni_entityinstanceid.getEntityId());
660            
661        Long qeni_time = _value.getTime();
662        if(qeni_time == null)
663            _ps.setNull(4, Types.BIGINT);
664        else
665            _ps.setLong(4, qeni_time);
666            
667    }
668    
669    public QueuedEntity create(Session session, QueueTypePK queueTypePK, EntityInstancePK entityInstancePK, Long time)
670            throws PersistenceDatabaseException, PersistenceNotNullException {
671        QueuedEntityPK _pk = getNextPK();
672        QueuedEntityValue _value = new QueuedEntityValue(_pk, queueTypePK, entityInstancePK, time);
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        QueuedEntity _entity = new QueuedEntity(_value, EntityPermission.READ_ONLY);
695        session.putReadOnlyEntity(_pk, _entity);
696        
697        return _entity;
698    }
699    
700    public QueuedEntity create(QueueTypePK queueTypePK, EntityInstancePK entityInstancePK, Long time)
701            throws PersistenceDatabaseException, PersistenceNotNullException {
702        return create(ThreadSession.currentSession(), queueTypePK, entityInstancePK, time);
703    }
704    
705    public void create(Session session, Collection<QueuedEntityValue> _values)
706            throws PersistenceDatabaseException, PersistenceNotNullException {
707        int _size = _values.size();
708        
709        if(_size > 0) {
710            PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
711            List<QueuedEntityValue> _cacheValues = new ArrayList<>(_size);
712            
713            try {
714                for(QueuedEntityValue _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                QueuedEntity _cacheEntity = new QueuedEntity(_cacheValue, EntityPermission.READ_ONLY);
742                
743                session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity);
744            });
745        }
746    }
747    
748    public void create(Collection<QueuedEntityValue> _values)
749            throws PersistenceDatabaseException, PersistenceNotNullException {
750        create(ThreadSession.currentSession(), _values);
751    }
752    
753    private boolean bindForStore(PreparedStatement _ps, QueuedEntityValue _value)
754            throws SQLException {
755        boolean _hasBeenModified = _value.hasBeenModified();
756        
757        if(_hasBeenModified) {
758            QueueTypePK qeni_qtyp_queuetypeid = _value.getQueueTypePK();
759            if(qeni_qtyp_queuetypeid == null)
760                _ps.setNull(1, Types.BIGINT);
761            else
762                _ps.setLong(1, qeni_qtyp_queuetypeid.getEntityId());
763            
764            EntityInstancePK qeni_eni_entityinstanceid = _value.getEntityInstancePK();
765            if(qeni_eni_entityinstanceid == null)
766                _ps.setNull(2, Types.BIGINT);
767            else
768                _ps.setLong(2, qeni_eni_entityinstanceid.getEntityId());
769            
770            Long qeni_time = _value.getTime();
771            if(qeni_time == null)
772                _ps.setNull(3, Types.BIGINT);
773            else
774                _ps.setLong(3, qeni_time);
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, QueuedEntity entity)
786            throws PersistenceDatabaseException {
787        PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
788        
789        try {
790            QueuedEntityValue _value = entity.getQueuedEntityValue();
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<QueuedEntity> 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(QueuedEntity entity : entities) {
819                    if(bindForStore(_ps, entity.getQueuedEntityValue())) {
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.getQueuedEntityValue());
842                    });
843                }
844            } catch (SQLException se) {
845                throw new PersistenceDatabaseException(se);
846            }
847        }
848    }
849    
850    @Override
851    public void store(Collection<QueuedEntity> entities)
852            throws PersistenceDatabaseException {
853        store(ThreadSession.currentSession(), entities);
854    }
855    
856    @Override
857    public void remove(Session session, QueuedEntity entity)
858            throws PersistenceDatabaseException {
859        remove(session, entity.getPrimaryKey());
860    }
861    
862    @Override
863    public void remove(Session session, QueuedEntityPK 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<QueuedEntityPK> 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(QueuedEntityPK 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<QueuedEntityPK> pks)
937            throws PersistenceDatabaseException {
938        remove(ThreadSession.currentSession(), pks);
939    }
940    
941    @Override
942    public boolean validPK(Session session, QueuedEntityPK 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}