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