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