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