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