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