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