001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016// Generated File -- DO NOT EDIT BY HAND
017// --------------------------------------------------------------------------------
018
019/**
020 * 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 java.sql.PreparedStatement;
044import java.sql.ResultSet;
045import java.sql.SQLException;
046import java.sql.Types;
047import java.io.ByteArrayInputStream;
048import java.io.StringReader;
049import java.util.ArrayList;
050import java.util.Collection;
051import java.util.HashSet;
052import java.util.List;
053import java.util.Map;
054import java.util.Set;
055import javax.enterprise.context.ApplicationScoped;
056import javax.enterprise.inject.spi.CDI;
057import javax.inject.Inject;
058import org.jooq.ResultQuery;
059import org.slf4j.Logger;
060import org.slf4j.LoggerFactory;
061
062@ApplicationScoped
063public class SequenceDetailFactory
064        implements BaseFactory<SequenceDetailPK, SequenceDetail> {
065    
066    @Inject
067    Session session;
068    
069    //private static final Logger log = LoggerFactory.getLogger(SequenceDetailFactory.class);
070    
071    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 = ?";
072    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";
073    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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
074    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 = ?";
075    final private static String SQL_DELETE = "DELETE FROM sequencedetails WHERE sqdt_sequencedetailid = ?";
076    final private static String SQL_VALID = "SELECT COUNT(*) FROM sequencedetails WHERE sqdt_sequencedetailid = ?";
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 getComponentVendorName() {
102        return SequenceDetailConstants.COMPONENT_VENDOR_NAME;
103    }
104    
105    @Override
106    public String getEntityTypeName() {
107        return SequenceDetailConstants.ENTITY_TYPE_NAME;
108    }
109    
110    public PreparedStatement prepareStatement(String query) {
111        return session.prepareStatement(query);
112    }
113    
114    public SequenceDetailPK getNextPK() {
115        return new SequenceDetailPK(entityIdGenerator.getNextEntityId());
116    }
117    
118    public Set<SequenceDetailPK> getPKsFromResultSetAsSet(ResultSet rs)
119            throws PersistenceDatabaseException {
120        Set<SequenceDetailPK> _result = new HashSet<>();
121        
122        try {
123            while(rs.next()) {
124                _result.add(getPKFromResultSet(rs));
125            }
126        } catch (SQLException se) {
127            throw new PersistenceDatabaseException(se);
128        }
129        
130        return _result;
131    }
132    
133    public java.util.List<SequenceDetailPK> getPKsFromResultSetAsList(ResultSet rs)
134            throws PersistenceDatabaseException {
135        java.util.List<SequenceDetailPK> _result = new ArrayList<>();
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 SequenceDetailPK getPKFromResultSet(ResultSet rs)
149            throws PersistenceDatabaseException {
150        SequenceDetailPK _result;
151        
152        try {
153            long sqdt_sequencedetailid = rs.getLong(SQDT_SEQUENCEDETAILID);
154            Long _entityId = rs.wasNull() ? null : sqdt_sequencedetailid;
155            
156            _result = new SequenceDetailPK(_entityId);
157        } catch (SQLException se) {
158            throw new PersistenceDatabaseException(se);
159        }
160        
161        return _result;
162    }
163    
164    public java.util.List<SequenceDetailValue> getValuesFromPKs(Collection<SequenceDetailPK> pks)
165            throws PersistenceDatabaseException {
166        java.util.List<SequenceDetailValue> _values = new ArrayList<>(pks.size());
167        
168        for(SequenceDetailPK _pk: pks) {
169            _values.add(getValueFromPK(_pk));
170        }
171        
172        return _values;
173    }
174    
175    public SequenceDetailValue getValueFromPK(SequenceDetailPK pk)
176            throws PersistenceDatabaseException {
177        SequenceDetailValue _value;
178        
179        // See if we already have the entity in the session cache
180        SequenceDetail _entity = (SequenceDetail)session.getEntity(pk);
181        if(_entity == null)
182            _value = getEntityFromPK(EntityPermission.READ_ONLY, pk).getSequenceDetailValue();
183        else
184            _value = _entity.getSequenceDetailValue();
185        
186        return _value;
187    }
188    
189    public java.util.List<SequenceDetailValue> getValuesFromResultSet(ResultSet rs)
190            throws PersistenceDatabaseException {
191        java.util.List<SequenceDetailValue> _result = new ArrayList<>();
192        
193        try {
194            while(rs.next()) {
195                _result.add(getValueFromResultSet(rs));
196            }
197        } catch (SQLException se) {
198            throw new PersistenceDatabaseException(se);
199        }
200        
201        return _result;
202    }
203    
204    public SequenceDetailValue getValueFromResultSet(ResultSet rs)
205            throws PersistenceDatabaseException {
206        SequenceDetailValue _value;
207        
208        try {
209            Long sqdt_sequencedetailid = rs.getLong(SQDT_SEQUENCEDETAILID);
210            SequenceDetailPK _pk = new SequenceDetailPK(sqdt_sequencedetailid);
211            
212            // See if we already have the entity in the session cache
213            SequenceDetail _entity = (SequenceDetail)session.getEntity(_pk);
214            
215            if(_entity == null) {
216                Long sqdt_sq_sequenceid = rs.getLong(SQDT_SQ_SEQUENCEID);
217                if(rs.wasNull())
218                    sqdt_sq_sequenceid = null;
219                
220                Long sqdt_sqtyp_sequencetypeid = rs.getLong(SQDT_SQTYP_SEQUENCETYPEID);
221                if(rs.wasNull())
222                    sqdt_sqtyp_sequencetypeid = null;
223                
224                String sqdt_sequencename = rs.getString(SQDT_SEQUENCENAME);
225                if(rs.wasNull())
226                    sqdt_sequencename = null;
227                
228                String sqdt_mask = rs.getString(SQDT_MASK);
229                if(rs.wasNull())
230                    sqdt_mask = null;
231                
232                Integer sqdt_chunksize = rs.getInt(SQDT_CHUNKSIZE);
233                if(rs.wasNull())
234                    sqdt_chunksize = null;
235                
236                Boolean sqdt_isdefault = rs.getInt(SQDT_ISDEFAULT) == 1;
237                if(rs.wasNull())
238                    sqdt_isdefault = null;
239                
240                Integer sqdt_sortorder = rs.getInt(SQDT_SORTORDER);
241                if(rs.wasNull())
242                    sqdt_sortorder = null;
243                
244                Long sqdt_fromtime = rs.getLong(SQDT_FROMTIME);
245                if(rs.wasNull())
246                    sqdt_fromtime = null;
247                
248                Long sqdt_thrutime = rs.getLong(SQDT_THRUTIME);
249                if(rs.wasNull())
250                    sqdt_thrutime = null;
251                
252                _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);
253            } else
254                _value = _entity.getSequenceDetailValue();
255        } catch (SQLException se) {
256            throw new PersistenceDatabaseException(se);
257        }
258        
259        return _value;
260    }
261    
262    public java.util.List<SequenceDetail> getEntitiesFromPKs(EntityPermission entityPermission, Collection<SequenceDetailPK> pks)
263            throws PersistenceDatabaseException {
264        java.util.List<SequenceDetail> _entities = new ArrayList<>(pks.size());
265        
266        for(SequenceDetailPK _pk: pks) {
267            _entities.add(getEntityFromPK(entityPermission, _pk));
268        }
269        
270        return _entities;
271    }
272    
273    public SequenceDetail getEntityFromValue(EntityPermission entityPermission, SequenceDetailValue value) {
274        return getEntityFromPK(entityPermission, value.getPrimaryKey());
275    }
276    
277    public SequenceDetail getEntityFromCache(SequenceDetailPK pk) {
278        SequenceDetailValue _value = (SequenceDetailValue)session.getValueCache().get(pk);
279    
280        return _value == null ? null : new SequenceDetail(_value, EntityPermission.READ_ONLY);
281    }
282    
283    public SequenceDetail getEntityFromPK(EntityPermission entityPermission, SequenceDetailPK pk)
284            throws PersistenceDatabaseException {
285        SequenceDetail _entity;
286        
287        // See if we already have the entity in the session cache
288        _entity = (SequenceDetail)session.getEntity(pk);
289        if(_entity != null) {
290            // If the requested permission is READ_WRITE, and the cached permission is
291            // READ_ONLY, then pretend that the cached object wasn't found, and create
292            // a new entity that is READ_WRITE.
293            if(entityPermission.equals(EntityPermission.READ_WRITE)) {
294                if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
295                    _entity = null;
296            }
297        }
298        
299        if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
300            _entity = getEntityFromCache(pk);
301        }
302        
303        if(_entity == null) {
304            PreparedStatement _ps = session.prepareStatement(entityPermission.equals(EntityPermission.READ_ONLY)? SQL_SELECT_READ_ONLY: SQL_SELECT_READ_WRITE);
305            long _entityId = pk.getEntityId();
306            ResultSet _rs = null;
307            
308            try {
309                _ps.setLong(1, _entityId);
310                _rs = _ps.executeQuery();
311                if(_rs.next()) {
312                    _entity = getEntityFromResultSet(entityPermission, _rs);
313                }
314            } catch (SQLException se) {
315                throw new PersistenceDatabaseException(se);
316            } finally {
317                if(_rs != null) {
318                    try {
319                        _rs.close();
320                    } catch (SQLException se) {
321                        // do nothing
322                    }
323                }
324            }
325        }
326        
327        return _entity;
328    }
329    
330    public Set<SequenceDetailPK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params)
331            throws PersistenceDatabaseException {
332        Set<SequenceDetailPK> _pks;
333        ResultSet _rs = null;
334        
335        try {
336            if(params.length != 0) {
337                Session.setQueryParams(ps, params);
338            }
339            
340            _rs = ps.executeQuery();
341            _pks = getPKsFromResultSetAsSet(_rs);
342            _rs.close();
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        return _pks;
356    }
357    
358    public java.util.List<SequenceDetailPK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params)
359            throws PersistenceDatabaseException {
360        java.util.List<SequenceDetailPK> _pks;
361        ResultSet _rs = null;
362        
363        try {
364            if(params.length != 0) {
365                Session.setQueryParams(ps, params);
366            }
367            
368            _rs = ps.executeQuery();
369            _pks = getPKsFromResultSetAsList(_rs);
370            _rs.close();
371        } catch (SQLException se) {
372            throw new PersistenceDatabaseException(se);
373        } finally {
374            if(_rs != null) {
375                try {
376                    _rs.close();
377                } catch (SQLException se) {
378                    // do nothing
379                }
380            }
381        }
382        
383        return _pks;
384    }
385    
386    public SequenceDetailPK getPKFromQuery(PreparedStatement ps, final Object... params)
387            throws PersistenceDatabaseException {
388        SequenceDetailPK _pk = null;
389        ResultSet _rs = null;
390        
391        try {
392            if(params.length != 0) {
393                Session.setQueryParams(ps, params);
394            }
395            
396            _rs = ps.executeQuery();
397            if(_rs.next()) {
398                _pk = getPKFromResultSet(_rs);
399            }
400            _rs.close();
401        } catch (SQLException se) {
402            throw new PersistenceDatabaseException(se);
403        } finally {
404            if(_rs != null) {
405                try {
406                    _rs.close();
407                } catch (SQLException se) {
408                    // do nothing
409                }
410            }
411        }
412        
413        return _pk;
414    }
415    
416    public java.util.List<SequenceDetail> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
417            throws PersistenceDatabaseException {
418        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
419        
420        return getEntitiesFromQuery(entityPermission, ps, params);
421    }
422    
423    public java.util.List<SequenceDetail> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
424            throws PersistenceDatabaseException {
425        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
426        
427        return getEntitiesFromQuery(entityPermission, ps);
428    }
429    
430    public java.util.List<SequenceDetail> getEntitiesFromQuery(EntityPermission entityPermission, ResultQuery<?> query)
431            throws PersistenceDatabaseException {
432        PreparedStatement ps = session.prepareStatement(query.getSQL());
433        
434        return getEntitiesFromQuery(entityPermission, ps, query.getBindValues().toArray());
435    }
436    
437    public java.util.List<SequenceDetail> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
438            throws PersistenceDatabaseException {
439        java.util.List<SequenceDetail> _entities;
440        ResultSet _rs = null;
441        
442        try {
443            if(params.length != 0) {
444                Session.setQueryParams(ps, params);
445            }
446            
447            _rs = ps.executeQuery();
448            _entities = getEntitiesFromResultSet(entityPermission, _rs);
449            _rs.close();
450        } catch (SQLException se) {
451            throw new PersistenceDatabaseException(se);
452        } finally {
453            if(_rs != null) {
454                try {
455                    _rs.close();
456                } catch (SQLException se) {
457                    // do nothing
458                }
459            }
460        }
461        
462        return _entities;
463    }
464    
465    public SequenceDetail getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
466            throws PersistenceDatabaseException {
467        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
468        
469        return getEntityFromQuery(entityPermission, ps, params);
470    }
471    
472    public SequenceDetail getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
473            throws PersistenceDatabaseException {
474        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
475        
476        return getEntityFromQuery(entityPermission, ps);
477    }
478    
479    public SequenceDetail getEntityFromQuery(EntityPermission entityPermission, ResultQuery<?> query)
480            throws PersistenceDatabaseException {
481        PreparedStatement ps = session.prepareStatement(query.getSQL());
482        
483        return getEntityFromQuery(entityPermission, ps, query.getBindValues().toArray());
484    }
485    
486    public SequenceDetail getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
487            throws PersistenceDatabaseException {
488        SequenceDetail _entity = null;
489        ResultSet _rs = null;
490        
491        try {
492            if(params.length != 0) {
493                Session.setQueryParams(ps, params);
494            }
495            
496            _rs = ps.executeQuery();
497            if(_rs.next()) {
498                _entity = getEntityFromResultSet(entityPermission, _rs);
499            }
500            _rs.close();
501        } catch (SQLException se) {
502            throw new PersistenceDatabaseException(se);
503        } finally {
504            if(_rs != null) {
505                try {
506                    _rs.close();
507                } catch (SQLException se) {
508                    // do nothing
509                }
510            }
511        }
512        
513        return _entity;
514    }
515    
516    public java.util.List<SequenceDetail> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs)
517            throws PersistenceDatabaseException {
518        java.util.List<SequenceDetail> _result = new ArrayList<>();
519        
520        try {
521            while(rs.next()) {
522                _result.add(getEntityFromResultSet(entityPermission, rs));
523            }
524        } catch (SQLException se) {
525            throw new PersistenceDatabaseException(se);
526        }
527        
528        return _result;
529    }
530    
531    public SequenceDetail getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs)
532            throws PersistenceDatabaseException {
533        SequenceDetail _entity;
534        
535        try {
536            Long sqdt_sequencedetailid = rs.getLong(SQDT_SEQUENCEDETAILID);
537            SequenceDetailPK _pk = new SequenceDetailPK(sqdt_sequencedetailid);
538            
539            // See if we already have the entity in the session cache
540            _entity = (SequenceDetail)session.getEntity(_pk);
541            if(_entity != null) {
542                // If the requested permission is READ_WRITE, and the cached permission is
543                // READ_ONLY, then pretend that the cached object wasn't found, and create
544                // a new entity that is READ_WRITE.
545                if(entityPermission.equals(EntityPermission.READ_WRITE)) {
546                    if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
547                        _entity = null;
548                }
549            }
550            boolean foundInSessionCache = _entity != null;
551            
552            if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
553                _entity = getEntityFromCache(_pk);
554            }
555            
556            if(_entity == null) {
557                Long sqdt_sq_sequenceid = rs.getLong(SQDT_SQ_SEQUENCEID);
558                if(rs.wasNull())
559                    sqdt_sq_sequenceid = null;
560                
561                Long sqdt_sqtyp_sequencetypeid = rs.getLong(SQDT_SQTYP_SEQUENCETYPEID);
562                if(rs.wasNull())
563                    sqdt_sqtyp_sequencetypeid = null;
564                
565                String sqdt_sequencename = rs.getString(SQDT_SEQUENCENAME);
566                if(rs.wasNull())
567                    sqdt_sequencename = null;
568                
569                String sqdt_mask = rs.getString(SQDT_MASK);
570                if(rs.wasNull())
571                    sqdt_mask = null;
572                
573                Integer sqdt_chunksize = rs.getInt(SQDT_CHUNKSIZE);
574                if(rs.wasNull())
575                    sqdt_chunksize = null;
576                
577                Boolean sqdt_isdefault = rs.getInt(SQDT_ISDEFAULT) == 1;
578                if(rs.wasNull())
579                    sqdt_isdefault = null;
580                
581                Integer sqdt_sortorder = rs.getInt(SQDT_SORTORDER);
582                if(rs.wasNull())
583                    sqdt_sortorder = null;
584                
585                Long sqdt_fromtime = rs.getLong(SQDT_FROMTIME);
586                if(rs.wasNull())
587                    sqdt_fromtime = null;
588                
589                Long sqdt_thrutime = rs.getLong(SQDT_THRUTIME);
590                if(rs.wasNull())
591                    sqdt_thrutime = null;
592                
593                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);
594                _entity = new SequenceDetail(_value, entityPermission);
595            }
596            
597            if(!foundInSessionCache) {
598                if(entityPermission.equals(EntityPermission.READ_ONLY)) {
599                    session.putReadOnlyEntity(_pk, _entity);
600                    session.getValueCache().put(_entity.getSequenceDetailValue());
601                } else {
602                    session.putReadWriteEntity(_pk, _entity);
603                }
604            }
605        } catch (SQLException se) {
606            throw new PersistenceDatabaseException(se);
607        }
608        
609        return _entity;
610    }
611    
612    public SequenceDetail create(Sequence sequence, SequenceType sequenceType, String sequenceName, String mask, Integer chunkSize, Boolean isDefault, Integer sortOrder, Long fromTime, Long thruTime)
613            throws PersistenceDatabaseException, PersistenceNotNullException {
614        return create(sequence == null ? null : sequence.getPrimaryKey(), sequenceType == null ? null : sequenceType.getPrimaryKey(), sequenceName, mask, chunkSize, isDefault, sortOrder, fromTime, thruTime);
615    }
616    
617    private void bindForCreate(PreparedStatement _ps, SequenceDetailValue _value)
618            throws SQLException {
619        _ps.setLong(1, _value.getEntityId());
620        
621        SequencePK sqdt_sq_sequenceid = _value.getSequencePK();
622        if(sqdt_sq_sequenceid == null)
623            _ps.setNull(2, Types.BIGINT);
624        else
625            _ps.setLong(2, sqdt_sq_sequenceid.getEntityId());
626            
627        SequenceTypePK sqdt_sqtyp_sequencetypeid = _value.getSequenceTypePK();
628        if(sqdt_sqtyp_sequencetypeid == null)
629            _ps.setNull(3, Types.BIGINT);
630        else
631            _ps.setLong(3, sqdt_sqtyp_sequencetypeid.getEntityId());
632            
633        String sqdt_sequencename = _value.getSequenceName();
634        if(sqdt_sequencename == null)
635            _ps.setNull(4, Types.VARCHAR);
636        else
637            _ps.setString(4, sqdt_sequencename);
638            
639        String sqdt_mask = _value.getMask();
640        if(sqdt_mask == null)
641            _ps.setNull(5, Types.VARCHAR);
642        else
643            _ps.setString(5, sqdt_mask);
644            
645        Integer sqdt_chunksize = _value.getChunkSize();
646        if(sqdt_chunksize == null)
647            _ps.setNull(6, Types.INTEGER);
648        else
649            _ps.setInt(6, sqdt_chunksize);
650            
651        Boolean sqdt_isdefault = _value.getIsDefault();
652        if(sqdt_isdefault == null)
653            _ps.setNull(7, Types.BIT);
654        else
655            _ps.setInt(7, sqdt_isdefault? 1: 0);
656            
657        Integer sqdt_sortorder = _value.getSortOrder();
658        if(sqdt_sortorder == null)
659            _ps.setNull(8, Types.INTEGER);
660        else
661            _ps.setInt(8, sqdt_sortorder);
662            
663        Long sqdt_fromtime = _value.getFromTime();
664        if(sqdt_fromtime == null)
665            _ps.setNull(9, Types.BIGINT);
666        else
667            _ps.setLong(9, sqdt_fromtime);
668            
669        Long sqdt_thrutime = _value.getThruTime();
670        if(sqdt_thrutime == null)
671            _ps.setNull(10, Types.BIGINT);
672        else
673            _ps.setLong(10, sqdt_thrutime);
674            
675    }
676    
677    public SequenceDetail create(SequencePK sequencePK, SequenceTypePK sequenceTypePK, String sequenceName, String mask, Integer chunkSize, Boolean isDefault, Integer sortOrder, Long fromTime, Long thruTime)
678            throws PersistenceDatabaseException, PersistenceNotNullException {
679        SequenceDetailPK _pk = getNextPK();
680        SequenceDetailValue _value = new SequenceDetailValue(_pk, sequencePK, sequenceTypePK, sequenceName, mask, chunkSize, isDefault, sortOrder, fromTime, thruTime);
681        
682        PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
683        
684        try {
685            bindForCreate(_ps, _value);
686            
687            if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
688                int _count = _ps.executeUpdate();
689                
690                if(_count != 1) {
691                    throw new PersistenceDatabaseUpdateException("insert failed, _count = " + _count);
692                }
693            } else {
694                 _ps.executeUpdate();
695            }
696            
697            session.getValueCache().put(_value);
698        } catch (SQLException se) {
699            throw new PersistenceDatabaseException(se);
700        }
701        
702        SequenceDetail _entity = new SequenceDetail(_value, EntityPermission.READ_ONLY);
703        session.putReadOnlyEntity(_pk, _entity);
704        
705        return _entity;
706    }
707    
708    public void create(Collection<SequenceDetailValue> _values)
709            throws PersistenceDatabaseException, PersistenceNotNullException {
710        int _size = _values.size();
711        
712        if(_size > 0) {
713            PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
714            List<SequenceDetailValue> _cacheValues = new ArrayList<>(_size);
715            
716            try {
717                for(SequenceDetailValue _value : _values) {
718                    _value.setEntityId(entityIdGenerator.getNextEntityId());
719                    bindForCreate(_ps, _value);
720                    
721                    _ps.addBatch();
722                    
723                    _cacheValues.add(_value);
724                }
725                
726                if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
727                    int[] _counts = _ps.executeBatch();
728                    
729                    for(int _countOffset = 0 ; _countOffset < _size ; _countOffset++) {
730                        if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
731                            throw new PersistenceDatabaseUpdateException("batch insert failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
732                        }
733                    }
734                } else {
735                     _ps.executeBatch();
736                }
737                
738                _ps.clearBatch();
739            } catch (SQLException se) {
740                throw new PersistenceDatabaseException(se);
741            }
742            
743            _cacheValues.forEach((_cacheValue) -> {
744                SequenceDetail _cacheEntity = new SequenceDetail(_cacheValue, EntityPermission.READ_ONLY);
745                
746                session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity);
747            });
748        }
749    }
750    
751    private boolean bindForStore(PreparedStatement _ps, SequenceDetailValue _value)
752            throws SQLException {
753        boolean _hasBeenModified = _value.hasBeenModified();
754        
755        if(_hasBeenModified) {
756            SequencePK sqdt_sq_sequenceid = _value.getSequencePK();
757            if(sqdt_sq_sequenceid == null)
758                _ps.setNull(1, Types.BIGINT);
759            else
760                _ps.setLong(1, sqdt_sq_sequenceid.getEntityId());
761            
762            SequenceTypePK sqdt_sqtyp_sequencetypeid = _value.getSequenceTypePK();
763            if(sqdt_sqtyp_sequencetypeid == null)
764                _ps.setNull(2, Types.BIGINT);
765            else
766                _ps.setLong(2, sqdt_sqtyp_sequencetypeid.getEntityId());
767            
768            String sqdt_sequencename = _value.getSequenceName();
769            if(sqdt_sequencename == null)
770                _ps.setNull(3, Types.VARCHAR);
771            else
772                _ps.setString(3, sqdt_sequencename);
773            
774            String sqdt_mask = _value.getMask();
775            if(sqdt_mask == null)
776                _ps.setNull(4, Types.VARCHAR);
777            else
778                _ps.setString(4, sqdt_mask);
779            
780            Integer sqdt_chunksize = _value.getChunkSize();
781            if(sqdt_chunksize == null)
782                _ps.setNull(5, Types.INTEGER);
783            else
784                _ps.setInt(5, sqdt_chunksize);
785            
786            Boolean sqdt_isdefault = _value.getIsDefault();
787            if(sqdt_isdefault == null)
788                _ps.setNull(6, Types.BIT);
789            else
790                _ps.setInt(6, sqdt_isdefault? 1: 0);
791            
792            Integer sqdt_sortorder = _value.getSortOrder();
793            if(sqdt_sortorder == null)
794                _ps.setNull(7, Types.INTEGER);
795            else
796                _ps.setInt(7, sqdt_sortorder);
797            
798            Long sqdt_fromtime = _value.getFromTime();
799            if(sqdt_fromtime == null)
800                _ps.setNull(8, Types.BIGINT);
801            else
802                _ps.setLong(8, sqdt_fromtime);
803            
804            Long sqdt_thrutime = _value.getThruTime();
805            if(sqdt_thrutime == null)
806                _ps.setNull(9, Types.BIGINT);
807            else
808                _ps.setLong(9, sqdt_thrutime);
809            
810            _ps.setLong(10, _value.getPrimaryKey().getEntityId());
811            
812            _value.clearHasBeenModified();
813        }
814        
815        return _hasBeenModified;
816    }
817    
818    @Override
819    public void store(SequenceDetail entity)
820            throws PersistenceDatabaseException {
821        PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
822        
823        try {
824            SequenceDetailValue _value = entity.getSequenceDetailValue();
825            
826            if(bindForStore(_ps, _value)) {
827                if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
828                    int _count = _ps.executeUpdate();
829                    
830                    if(_count != 1) {
831                        throw new PersistenceDatabaseUpdateException("update failed, _count = " + _count);
832                    }
833                } else {
834                     _ps.executeUpdate();
835                }
836                
837                session.getValueCache().put(_value);
838            }
839        } catch (SQLException se) {
840            throw new PersistenceDatabaseException(se);
841        }
842    }
843    
844    @Override
845    public void store(Collection<SequenceDetail> entities)
846            throws PersistenceDatabaseException {
847        if(entities.size() > 0) {
848            PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
849            int _modifiedEntities = 0;
850            
851            try {
852                for(SequenceDetail entity : entities) {
853                    if(bindForStore(_ps, entity.getSequenceDetailValue())) {
854                        _ps.addBatch();
855                        _modifiedEntities++;
856                    }
857                }
858                
859                if(_modifiedEntities != 0) {
860                    if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
861                        int[] _counts = _ps.executeBatch();
862                        
863                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
864                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
865                                throw new PersistenceDatabaseUpdateException("batch update failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
866                            }
867                        }
868                    } else {
869                         _ps.executeBatch();
870                    }
871                    
872                    _ps.clearBatch();
873                    
874                    entities.forEach((entity) -> {
875                        session.getValueCache().put(entity.getSequenceDetailValue());
876                    });
877                }
878            } catch (SQLException se) {
879                throw new PersistenceDatabaseException(se);
880            }
881        }
882    }
883    
884    @Override
885    public void remove(SequenceDetail entity)
886            throws PersistenceDatabaseException {
887        remove(entity.getPrimaryKey());
888    }
889    
890    @Override
891    public void remove(SequenceDetailPK pk)
892            throws PersistenceDatabaseException {
893        PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
894        long _entityId = pk.getEntityId();
895        
896        try {
897            _ps.setLong(1, _entityId);
898            
899            if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
900                int _count = _ps.executeUpdate();
901                
902                if(_count != 1) {
903                    throw new PersistenceDatabaseUpdateException("remove failed, _count = " + _count);
904                }
905            } else {
906                 _ps.executeUpdate();
907            }
908            
909            session.getValueCache().remove(pk);
910        } catch (SQLException se) {
911            throw new PersistenceDatabaseException(se);
912        }
913        
914        session.removed(pk, false);
915    }
916    
917    @Override
918    public void remove(Collection<SequenceDetailPK> pks)
919            throws PersistenceDatabaseException {
920        if(pks.size() > 0) {
921            PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
922            int _modifiedEntities = 0;
923            
924            try {
925                for(SequenceDetailPK pk : pks) {
926                    long _entityId = pk.getEntityId();
927                    
928                    _ps.setLong(1, _entityId);
929                    
930                    _ps.addBatch();
931                    _modifiedEntities++;
932                }
933                
934                if(_modifiedEntities != 0) {
935                    if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
936                        int[] _counts = _ps.executeBatch();
937                        
938                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
939                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
940                                throw new PersistenceDatabaseUpdateException("batch remove failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
941                            }
942                        }
943                    } else {
944                        _ps.executeBatch();
945                    }
946                    
947                    _ps.clearBatch();
948                    
949                    pks.forEach((pk) -> {
950                        session.getValueCache().remove(pk);
951                    });
952                }
953            } catch (SQLException se) {
954                throw new PersistenceDatabaseException(se);
955            }
956            
957            pks.forEach((pk) -> {
958                session.removed(pk, true);
959            });
960        }
961    }
962    
963    @Override
964    public boolean validPK(SequenceDetailPK pk)
965            throws PersistenceDatabaseException {
966        boolean valid = false;
967        PreparedStatement _ps = session.prepareStatement(SQL_VALID);
968        ResultSet _rs = null;
969        
970        try {
971            _ps.setLong(1, pk.getEntityId());
972            
973            _rs = _ps.executeQuery();
974            if(_rs.next()) {
975                long _count = _rs.getLong(1);
976                if(_rs.wasNull())
977                    _count = 0;
978                
979                if(_count == 1)
980                    valid = true;
981            }
982        } catch (SQLException se) {
983            throw new PersistenceDatabaseException(se);
984        } finally {
985            if(_rs != null) {
986                try {
987                    _rs.close();
988                } catch (SQLException se) {
989                    // do nothing
990                }
991            }
992        }
993        
994        return valid;
995    }
996    
997}