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