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