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