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