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