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