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