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