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