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