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