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