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