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