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