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