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