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