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