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