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