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