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