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