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