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