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