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