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