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