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