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