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