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