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