001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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 * ContactPostalAddressFactory.java
021 */
022
023package com.echothree.model.data.contact.server.factory;
024
025import com.echothree.model.data.contact.common.pk.ContactMechanismPK;
026import com.echothree.model.data.party.common.pk.PersonalTitlePK;
027import com.echothree.model.data.party.common.pk.NameSuffixPK;
028import com.echothree.model.data.geo.common.pk.GeoCodePK;
029
030import com.echothree.model.data.contact.server.entity.ContactMechanism;
031import com.echothree.model.data.party.server.entity.PersonalTitle;
032import com.echothree.model.data.party.server.entity.NameSuffix;
033import com.echothree.model.data.geo.server.entity.GeoCode;
034
035import com.echothree.model.data.contact.common.ContactPostalAddressConstants;
036import com.echothree.model.data.contact.common.pk.ContactPostalAddressPK;
037import com.echothree.model.data.contact.server.value.ContactPostalAddressValue;
038import com.echothree.model.data.contact.server.entity.ContactPostalAddress;
039import com.echothree.util.common.exception.PersistenceDatabaseException;
040import com.echothree.util.common.exception.PersistenceDatabaseUpdateException;
041import com.echothree.util.common.exception.PersistenceNotNullException;
042import com.echothree.util.server.persistence.BaseFactory;
043import com.echothree.util.server.persistence.EntityIdGenerator;
044import com.echothree.util.server.persistence.EntityPermission;
045import com.echothree.util.server.persistence.PersistenceDebugFlags;
046import com.echothree.util.server.persistence.Session;
047import com.echothree.util.server.persistence.ThreadSession;
048import java.sql.PreparedStatement;
049import java.sql.ResultSet;
050import java.sql.SQLException;
051import java.sql.Types;
052import java.io.ByteArrayInputStream;
053import java.io.StringReader;
054import java.util.ArrayList;
055import java.util.Collection;
056import java.util.HashSet;
057import java.util.List;
058import java.util.Map;
059import java.util.Set;
060import javax.enterprise.context.ApplicationScoped;
061import javax.enterprise.inject.spi.CDI;
062import org.apache.commons.logging.Log;
063import org.apache.commons.logging.LogFactory;
064
065@ApplicationScoped
066public class ContactPostalAddressFactory
067        implements BaseFactory<ContactPostalAddressPK, ContactPostalAddress> {
068    
069    //final private static Log log = LogFactory.getLog(ContactPostalAddressFactory.class);
070    
071    final private static String SQL_SELECT_READ_ONLY = "SELECT ctpa_contactpostaladdressid, ctpa_cmch_contactmechanismid, ctpa_pert_personaltitleid, ctpa_firstname, ctpa_firstnamesdx, ctpa_middlename, ctpa_middlenamesdx, ctpa_lastname, ctpa_lastnamesdx, ctpa_nsfx_namesuffixid, ctpa_companyname, ctpa_attention, ctpa_address1, ctpa_address2, ctpa_address3, ctpa_city, ctpa_citygeocodeid, ctpa_countygeocodeid, ctpa_state, ctpa_stategeocodeid, ctpa_postalcode, ctpa_postalcodegeocodeid, ctpa_countrygeocodeid, ctpa_iscommercial, ctpa_fromtime, ctpa_thrutime FROM contactpostaladdresses WHERE ctpa_contactpostaladdressid = ?";
072    final private static String SQL_SELECT_READ_WRITE = "SELECT ctpa_contactpostaladdressid, ctpa_cmch_contactmechanismid, ctpa_pert_personaltitleid, ctpa_firstname, ctpa_firstnamesdx, ctpa_middlename, ctpa_middlenamesdx, ctpa_lastname, ctpa_lastnamesdx, ctpa_nsfx_namesuffixid, ctpa_companyname, ctpa_attention, ctpa_address1, ctpa_address2, ctpa_address3, ctpa_city, ctpa_citygeocodeid, ctpa_countygeocodeid, ctpa_state, ctpa_stategeocodeid, ctpa_postalcode, ctpa_postalcodegeocodeid, ctpa_countrygeocodeid, ctpa_iscommercial, ctpa_fromtime, ctpa_thrutime FROM contactpostaladdresses WHERE ctpa_contactpostaladdressid = ? FOR UPDATE";
073    final private static String SQL_INSERT = "INSERT INTO contactpostaladdresses (ctpa_contactpostaladdressid, ctpa_cmch_contactmechanismid, ctpa_pert_personaltitleid, ctpa_firstname, ctpa_firstnamesdx, ctpa_middlename, ctpa_middlenamesdx, ctpa_lastname, ctpa_lastnamesdx, ctpa_nsfx_namesuffixid, ctpa_companyname, ctpa_attention, ctpa_address1, ctpa_address2, ctpa_address3, ctpa_city, ctpa_citygeocodeid, ctpa_countygeocodeid, ctpa_state, ctpa_stategeocodeid, ctpa_postalcode, ctpa_postalcodegeocodeid, ctpa_countrygeocodeid, ctpa_iscommercial, ctpa_fromtime, ctpa_thrutime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
074    final private static String SQL_UPDATE = "UPDATE contactpostaladdresses SET ctpa_cmch_contactmechanismid = ?, ctpa_pert_personaltitleid = ?, ctpa_firstname = ?, ctpa_firstnamesdx = ?, ctpa_middlename = ?, ctpa_middlenamesdx = ?, ctpa_lastname = ?, ctpa_lastnamesdx = ?, ctpa_nsfx_namesuffixid = ?, ctpa_companyname = ?, ctpa_attention = ?, ctpa_address1 = ?, ctpa_address2 = ?, ctpa_address3 = ?, ctpa_city = ?, ctpa_citygeocodeid = ?, ctpa_countygeocodeid = ?, ctpa_state = ?, ctpa_stategeocodeid = ?, ctpa_postalcode = ?, ctpa_postalcodegeocodeid = ?, ctpa_countrygeocodeid = ?, ctpa_iscommercial = ?, ctpa_fromtime = ?, ctpa_thrutime = ? WHERE ctpa_contactpostaladdressid = ?";
075    final private static String SQL_DELETE = "DELETE FROM contactpostaladdresses WHERE ctpa_contactpostaladdressid = ?";
076    final private static String SQL_VALID = "SELECT COUNT(*) FROM contactpostaladdresses WHERE ctpa_contactpostaladdressid = ?";
077    
078    final private static String PK_COLUMN = "ctpa_contactpostaladdressid";
079    final private static String ALL_COLUMNS = "ctpa_contactpostaladdressid, ctpa_cmch_contactmechanismid, ctpa_pert_personaltitleid, ctpa_firstname, ctpa_firstnamesdx, ctpa_middlename, ctpa_middlenamesdx, ctpa_lastname, ctpa_lastnamesdx, ctpa_nsfx_namesuffixid, ctpa_companyname, ctpa_attention, ctpa_address1, ctpa_address2, ctpa_address3, ctpa_city, ctpa_citygeocodeid, ctpa_countygeocodeid, ctpa_state, ctpa_stategeocodeid, ctpa_postalcode, ctpa_postalcodegeocodeid, ctpa_countrygeocodeid, ctpa_iscommercial, ctpa_fromtime, ctpa_thrutime";
080    final public static String TABLE_NAME = "contactpostaladdresses";
081    
082    final public static String CTPA_CONTACTPOSTALADDRESSID = "ctpa_contactpostaladdressid";
083    final public static String CTPA_CMCH_CONTACTMECHANISMID = "ctpa_cmch_contactmechanismid";
084    final public static String CTPA_PERT_PERSONALTITLEID = "ctpa_pert_personaltitleid";
085    final public static String CTPA_FIRSTNAME = "ctpa_firstname";
086    final public static String CTPA_FIRSTNAMESDX = "ctpa_firstnamesdx";
087    final public static String CTPA_MIDDLENAME = "ctpa_middlename";
088    final public static String CTPA_MIDDLENAMESDX = "ctpa_middlenamesdx";
089    final public static String CTPA_LASTNAME = "ctpa_lastname";
090    final public static String CTPA_LASTNAMESDX = "ctpa_lastnamesdx";
091    final public static String CTPA_NSFX_NAMESUFFIXID = "ctpa_nsfx_namesuffixid";
092    final public static String CTPA_COMPANYNAME = "ctpa_companyname";
093    final public static String CTPA_ATTENTION = "ctpa_attention";
094    final public static String CTPA_ADDRESS1 = "ctpa_address1";
095    final public static String CTPA_ADDRESS2 = "ctpa_address2";
096    final public static String CTPA_ADDRESS3 = "ctpa_address3";
097    final public static String CTPA_CITY = "ctpa_city";
098    final public static String CTPA_CITYGEOCODEID = "ctpa_citygeocodeid";
099    final public static String CTPA_COUNTYGEOCODEID = "ctpa_countygeocodeid";
100    final public static String CTPA_STATE = "ctpa_state";
101    final public static String CTPA_STATEGEOCODEID = "ctpa_stategeocodeid";
102    final public static String CTPA_POSTALCODE = "ctpa_postalcode";
103    final public static String CTPA_POSTALCODEGEOCODEID = "ctpa_postalcodegeocodeid";
104    final public static String CTPA_COUNTRYGEOCODEID = "ctpa_countrygeocodeid";
105    final public static String CTPA_ISCOMMERCIAL = "ctpa_iscommercial";
106    final public static String CTPA_FROMTIME = "ctpa_fromtime";
107    final public static String CTPA_THRUTIME = "ctpa_thrutime";
108    
109    final private static EntityIdGenerator entityIdGenerator = new EntityIdGenerator(ContactPostalAddressConstants.COMPONENT_VENDOR_NAME, ContactPostalAddressConstants.ENTITY_TYPE_NAME);
110    
111    /** Creates a new instance of ContactPostalAddressFactory */
112    protected ContactPostalAddressFactory() {
113        super();
114    }
115    
116    public static ContactPostalAddressFactory getInstance() {
117        return CDI.current().select(ContactPostalAddressFactory.class).get();
118    }
119    
120    @Override
121    public String getPKColumn() {
122        return PK_COLUMN;
123    }
124    
125    @Override
126    public String getAllColumns() {
127        return ALL_COLUMNS;
128    }
129    
130    @Override
131    public String getTableName() {
132        return TABLE_NAME;
133    }
134    
135    @Override
136    public String getComponentVendorName() {
137        return ContactPostalAddressConstants.COMPONENT_VENDOR_NAME;
138    }
139    
140    @Override
141    public String getEntityTypeName() {
142        return ContactPostalAddressConstants.ENTITY_TYPE_NAME;
143    }
144    
145    public PreparedStatement prepareStatement(String query) {
146        return ThreadSession.currentSession().prepareStatement(ContactPostalAddressFactory.class, query);
147    }
148    
149    public ContactPostalAddressPK getNextPK() {
150        return new ContactPostalAddressPK(entityIdGenerator.getNextEntityId());
151    }
152    
153    public Set<ContactPostalAddressPK> getPKsFromResultSetAsSet(ResultSet rs)
154            throws PersistenceDatabaseException {
155        Set<ContactPostalAddressPK> _result = new HashSet<>();
156        
157        try {
158            while(rs.next()) {
159                _result.add(getPKFromResultSet(rs));
160            }
161        } catch (SQLException se) {
162            throw new PersistenceDatabaseException(se);
163        }
164        
165        return _result;
166    }
167    
168    public java.util.List<ContactPostalAddressPK> getPKsFromResultSetAsList(ResultSet rs)
169            throws PersistenceDatabaseException {
170        java.util.List<ContactPostalAddressPK> _result = new ArrayList<>();
171        
172        try {
173            while(rs.next()) {
174                _result.add(getPKFromResultSet(rs));
175            }
176        } catch (SQLException se) {
177            throw new PersistenceDatabaseException(se);
178        }
179        
180        return _result;
181    }
182    
183    public ContactPostalAddressPK getPKFromResultSet(ResultSet rs)
184            throws PersistenceDatabaseException {
185        ContactPostalAddressPK _result;
186        
187        try {
188            long ctpa_contactpostaladdressid = rs.getLong(CTPA_CONTACTPOSTALADDRESSID);
189            Long _entityId = rs.wasNull() ? null : ctpa_contactpostaladdressid;
190            
191            _result = new ContactPostalAddressPK(_entityId);
192        } catch (SQLException se) {
193            throw new PersistenceDatabaseException(se);
194        }
195        
196        return _result;
197    }
198    
199    public java.util.List<ContactPostalAddressValue> getValuesFromPKs(Session session, Collection<ContactPostalAddressPK> pks)
200            throws PersistenceDatabaseException {
201        java.util.List<ContactPostalAddressValue> _values = new ArrayList<>(pks.size());
202        
203        for(ContactPostalAddressPK _pk: pks) {
204            _values.add(getValueFromPK(session, _pk));
205        }
206        
207        return _values;
208    }
209    
210    public ContactPostalAddressValue getValueFromPK(Session session, ContactPostalAddressPK pk)
211            throws PersistenceDatabaseException {
212        ContactPostalAddressValue _value;
213        
214        // See if we already have the entity in the session cache
215        ContactPostalAddress _entity = (ContactPostalAddress)session.getEntity(pk);
216        if(_entity == null)
217            _value = getEntityFromPK(session, EntityPermission.READ_ONLY, pk).getContactPostalAddressValue();
218        else
219            _value = _entity.getContactPostalAddressValue();
220        
221        return _value;
222    }
223    
224    public java.util.List<ContactPostalAddressValue> getValuesFromResultSet(Session session, ResultSet rs)
225            throws PersistenceDatabaseException {
226        java.util.List<ContactPostalAddressValue> _result = new ArrayList<>();
227        
228        try {
229            while(rs.next()) {
230                _result.add(getValueFromResultSet(session, rs));
231            }
232        } catch (SQLException se) {
233            throw new PersistenceDatabaseException(se);
234        }
235        
236        return _result;
237    }
238    
239    public ContactPostalAddressValue getValueFromResultSet(Session session, ResultSet rs)
240            throws PersistenceDatabaseException {
241        ContactPostalAddressValue _value;
242        
243        try {
244            Long ctpa_contactpostaladdressid = rs.getLong(CTPA_CONTACTPOSTALADDRESSID);
245            ContactPostalAddressPK _pk = new ContactPostalAddressPK(ctpa_contactpostaladdressid);
246            
247            // See if we already have the entity in the session cache
248            ContactPostalAddress _entity = (ContactPostalAddress)session.getEntity(_pk);
249            
250            if(_entity == null) {
251                Long ctpa_cmch_contactmechanismid = rs.getLong(CTPA_CMCH_CONTACTMECHANISMID);
252                if(rs.wasNull())
253                    ctpa_cmch_contactmechanismid = null;
254                
255                Long ctpa_pert_personaltitleid = rs.getLong(CTPA_PERT_PERSONALTITLEID);
256                if(rs.wasNull())
257                    ctpa_pert_personaltitleid = null;
258                
259                String ctpa_firstname = rs.getString(CTPA_FIRSTNAME);
260                if(rs.wasNull())
261                    ctpa_firstname = null;
262                
263                String ctpa_firstnamesdx = rs.getString(CTPA_FIRSTNAMESDX);
264                if(rs.wasNull())
265                    ctpa_firstnamesdx = null;
266                
267                String ctpa_middlename = rs.getString(CTPA_MIDDLENAME);
268                if(rs.wasNull())
269                    ctpa_middlename = null;
270                
271                String ctpa_middlenamesdx = rs.getString(CTPA_MIDDLENAMESDX);
272                if(rs.wasNull())
273                    ctpa_middlenamesdx = null;
274                
275                String ctpa_lastname = rs.getString(CTPA_LASTNAME);
276                if(rs.wasNull())
277                    ctpa_lastname = null;
278                
279                String ctpa_lastnamesdx = rs.getString(CTPA_LASTNAMESDX);
280                if(rs.wasNull())
281                    ctpa_lastnamesdx = null;
282                
283                Long ctpa_nsfx_namesuffixid = rs.getLong(CTPA_NSFX_NAMESUFFIXID);
284                if(rs.wasNull())
285                    ctpa_nsfx_namesuffixid = null;
286                
287                String ctpa_companyname = rs.getString(CTPA_COMPANYNAME);
288                if(rs.wasNull())
289                    ctpa_companyname = null;
290                
291                String ctpa_attention = rs.getString(CTPA_ATTENTION);
292                if(rs.wasNull())
293                    ctpa_attention = null;
294                
295                String ctpa_address1 = rs.getString(CTPA_ADDRESS1);
296                if(rs.wasNull())
297                    ctpa_address1 = null;
298                
299                String ctpa_address2 = rs.getString(CTPA_ADDRESS2);
300                if(rs.wasNull())
301                    ctpa_address2 = null;
302                
303                String ctpa_address3 = rs.getString(CTPA_ADDRESS3);
304                if(rs.wasNull())
305                    ctpa_address3 = null;
306                
307                String ctpa_city = rs.getString(CTPA_CITY);
308                if(rs.wasNull())
309                    ctpa_city = null;
310                
311                Long ctpa_citygeocodeid = rs.getLong(CTPA_CITYGEOCODEID);
312                if(rs.wasNull())
313                    ctpa_citygeocodeid = null;
314                
315                Long ctpa_countygeocodeid = rs.getLong(CTPA_COUNTYGEOCODEID);
316                if(rs.wasNull())
317                    ctpa_countygeocodeid = null;
318                
319                String ctpa_state = rs.getString(CTPA_STATE);
320                if(rs.wasNull())
321                    ctpa_state = null;
322                
323                Long ctpa_stategeocodeid = rs.getLong(CTPA_STATEGEOCODEID);
324                if(rs.wasNull())
325                    ctpa_stategeocodeid = null;
326                
327                String ctpa_postalcode = rs.getString(CTPA_POSTALCODE);
328                if(rs.wasNull())
329                    ctpa_postalcode = null;
330                
331                Long ctpa_postalcodegeocodeid = rs.getLong(CTPA_POSTALCODEGEOCODEID);
332                if(rs.wasNull())
333                    ctpa_postalcodegeocodeid = null;
334                
335                Long ctpa_countrygeocodeid = rs.getLong(CTPA_COUNTRYGEOCODEID);
336                if(rs.wasNull())
337                    ctpa_countrygeocodeid = null;
338                
339                Boolean ctpa_iscommercial = rs.getInt(CTPA_ISCOMMERCIAL) == 1;
340                if(rs.wasNull())
341                    ctpa_iscommercial = null;
342                
343                Long ctpa_fromtime = rs.getLong(CTPA_FROMTIME);
344                if(rs.wasNull())
345                    ctpa_fromtime = null;
346                
347                Long ctpa_thrutime = rs.getLong(CTPA_THRUTIME);
348                if(rs.wasNull())
349                    ctpa_thrutime = null;
350                
351                _value = new ContactPostalAddressValue(_pk, new ContactMechanismPK(ctpa_cmch_contactmechanismid), new PersonalTitlePK(ctpa_pert_personaltitleid), ctpa_firstname, ctpa_firstnamesdx, ctpa_middlename, ctpa_middlenamesdx, ctpa_lastname, ctpa_lastnamesdx, new NameSuffixPK(ctpa_nsfx_namesuffixid), ctpa_companyname, ctpa_attention, ctpa_address1, ctpa_address2, ctpa_address3, ctpa_city, new GeoCodePK(ctpa_citygeocodeid), new GeoCodePK(ctpa_countygeocodeid), ctpa_state, new GeoCodePK(ctpa_stategeocodeid), ctpa_postalcode, new GeoCodePK(ctpa_postalcodegeocodeid), new GeoCodePK(ctpa_countrygeocodeid), ctpa_iscommercial, ctpa_fromtime, ctpa_thrutime);
352            } else
353                _value = _entity.getContactPostalAddressValue();
354        } catch (SQLException se) {
355            throw new PersistenceDatabaseException(se);
356        }
357        
358        return _value;
359    }
360    
361    public java.util.List<ContactPostalAddress> getEntitiesFromPKs(EntityPermission entityPermission, Collection<ContactPostalAddressPK> pks)
362            throws PersistenceDatabaseException {
363        return getEntitiesFromPKs(ThreadSession.currentSession(), entityPermission, pks);
364    }
365    
366    public java.util.List<ContactPostalAddress> getEntitiesFromPKs(Session session, EntityPermission entityPermission, Collection<ContactPostalAddressPK> pks)
367            throws PersistenceDatabaseException {
368        java.util.List<ContactPostalAddress> _entities = new ArrayList<>(pks.size());
369        
370        for(ContactPostalAddressPK _pk: pks) {
371            _entities.add(getEntityFromPK(session, entityPermission, _pk));
372        }
373        
374        return _entities;
375    }
376    
377    public ContactPostalAddress getEntityFromValue(EntityPermission entityPermission, ContactPostalAddressValue value) {
378        return getEntityFromPK(ThreadSession.currentSession(), entityPermission, value.getPrimaryKey());
379    }
380    
381    public ContactPostalAddress getEntityFromValue(Session session, EntityPermission entityPermission, ContactPostalAddressValue value) {
382        return getEntityFromPK(session, entityPermission, value.getPrimaryKey());
383    }
384    
385    public ContactPostalAddress getEntityFromPK(EntityPermission entityPermission, ContactPostalAddressPK pk)
386            throws PersistenceDatabaseException {
387        return getEntityFromPK(ThreadSession.currentSession(), entityPermission, pk);
388    }
389    
390    public ContactPostalAddress getEntityFromCache(Session session, ContactPostalAddressPK pk) {
391        ContactPostalAddressValue _value = (ContactPostalAddressValue)session.getValueCache().get(pk);
392    
393        return _value == null ? null : new ContactPostalAddress(_value, EntityPermission.READ_ONLY);
394    }
395    
396    public ContactPostalAddress getEntityFromPK(Session session, EntityPermission entityPermission, ContactPostalAddressPK pk)
397            throws PersistenceDatabaseException {
398        ContactPostalAddress _entity;
399        
400        // See if we already have the entity in the session cache
401        _entity = (ContactPostalAddress)session.getEntity(pk);
402        if(_entity != null) {
403            // If the requested permission is READ_WRITE, and the cached permission is
404            // READ_ONLY, then pretend that the cached object wasn't found, and create
405            // a new entity that is READ_WRITE.
406            if(entityPermission.equals(EntityPermission.READ_WRITE)) {
407                if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
408                    _entity = null;
409            }
410        }
411        
412        if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
413            _entity = getEntityFromCache(session, pk);
414        }
415        
416        if(_entity == null) {
417            PreparedStatement _ps = session.prepareStatement(entityPermission.equals(EntityPermission.READ_ONLY)? SQL_SELECT_READ_ONLY: SQL_SELECT_READ_WRITE);
418            long _entityId = pk.getEntityId();
419            ResultSet _rs = null;
420            
421            try {
422                _ps.setLong(1, _entityId);
423                _rs = _ps.executeQuery();
424                if(_rs.next()) {
425                    _entity = getEntityFromResultSet(session, entityPermission, _rs);
426                }
427            } catch (SQLException se) {
428                throw new PersistenceDatabaseException(se);
429            } finally {
430                if(_rs != null) {
431                    try {
432                        _rs.close();
433                    } catch (SQLException se) {
434                        // do nothing
435                    }
436                }
437            }
438        }
439        
440        return _entity;
441    }
442    
443    public Set<ContactPostalAddressPK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params)
444            throws PersistenceDatabaseException {
445        Set<ContactPostalAddressPK> _pks;
446        ResultSet _rs = null;
447        
448        try {
449            if(params.length != 0) {
450                Session.setQueryParams(ps, params);
451            }
452            
453            _rs = ps.executeQuery();
454            _pks = getPKsFromResultSetAsSet(_rs);
455            _rs.close();
456        } catch (SQLException se) {
457            throw new PersistenceDatabaseException(se);
458        } finally {
459            if(_rs != null) {
460                try {
461                    _rs.close();
462                } catch (SQLException se) {
463                    // do nothing
464                }
465            }
466        }
467        
468        return _pks;
469    }
470    
471    public java.util.List<ContactPostalAddressPK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params)
472            throws PersistenceDatabaseException {
473        java.util.List<ContactPostalAddressPK> _pks;
474        ResultSet _rs = null;
475        
476        try {
477            if(params.length != 0) {
478                Session.setQueryParams(ps, params);
479            }
480            
481            _rs = ps.executeQuery();
482            _pks = getPKsFromResultSetAsList(_rs);
483            _rs.close();
484        } catch (SQLException se) {
485            throw new PersistenceDatabaseException(se);
486        } finally {
487            if(_rs != null) {
488                try {
489                    _rs.close();
490                } catch (SQLException se) {
491                    // do nothing
492                }
493            }
494        }
495        
496        return _pks;
497    }
498    
499    public ContactPostalAddressPK getPKFromQuery(PreparedStatement ps, final Object... params)
500            throws PersistenceDatabaseException {
501        ContactPostalAddressPK _pk = null;
502        ResultSet _rs = null;
503        
504        try {
505            if(params.length != 0) {
506                Session.setQueryParams(ps, params);
507            }
508            
509            _rs = ps.executeQuery();
510            if(_rs.next()) {
511                _pk = getPKFromResultSet(_rs);
512            }
513            _rs.close();
514        } catch (SQLException se) {
515            throw new PersistenceDatabaseException(se);
516        } finally {
517            if(_rs != null) {
518                try {
519                    _rs.close();
520                } catch (SQLException se) {
521                    // do nothing
522                }
523            }
524        }
525        
526        return _pk;
527    }
528    
529    public java.util.List<ContactPostalAddress> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
530            throws PersistenceDatabaseException {
531        PreparedStatement ps = session.prepareStatement(ContactPostalAddressFactory.class, queryMap.get(entityPermission));
532        
533        return getEntitiesFromQuery(session, entityPermission, ps, params);
534    }
535    
536    public java.util.List<ContactPostalAddress> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
537            throws PersistenceDatabaseException {
538        Session session = ThreadSession.currentSession();
539        PreparedStatement ps = session.prepareStatement(ContactPostalAddressFactory.class, queryMap.get(entityPermission));
540        
541        return getEntitiesFromQuery(session, entityPermission, ps, params);
542    }
543    
544    public java.util.List<ContactPostalAddress> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
545            throws PersistenceDatabaseException {
546        PreparedStatement ps = session.prepareStatement(ContactPostalAddressFactory.class, queryMap.get(entityPermission));
547        
548        return getEntitiesFromQuery(session, entityPermission, ps);
549    }
550    
551    public java.util.List<ContactPostalAddress> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
552            throws PersistenceDatabaseException {
553        Session session = ThreadSession.currentSession();
554        PreparedStatement ps = session.prepareStatement(ContactPostalAddressFactory.class, queryMap.get(entityPermission));
555        
556        return getEntitiesFromQuery(session, entityPermission, ps);
557    }
558    
559    public java.util.List<ContactPostalAddress> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps)
560            throws PersistenceDatabaseException {
561        return getEntitiesFromQuery(ThreadSession.currentSession(), entityPermission, ps);
562    }
563    
564    public java.util.List<ContactPostalAddress> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
565            throws PersistenceDatabaseException {
566        return getEntitiesFromQuery(ThreadSession.currentSession(), entityPermission, ps, params);
567    }
568    
569    public java.util.List<ContactPostalAddress> getEntitiesFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params)
570            throws PersistenceDatabaseException {
571        java.util.List<ContactPostalAddress> _entities;
572        ResultSet _rs = null;
573        
574        try {
575            if(params.length != 0) {
576                Session.setQueryParams(ps, params);
577            }
578            
579            _rs = ps.executeQuery();
580            _entities = getEntitiesFromResultSet(session, entityPermission, _rs);
581            _rs.close();
582        } catch (SQLException se) {
583            throw new PersistenceDatabaseException(se);
584        } finally {
585            if(_rs != null) {
586                try {
587                    _rs.close();
588                } catch (SQLException se) {
589                    // do nothing
590                }
591            }
592        }
593        
594        return _entities;
595    }
596    
597    public ContactPostalAddress getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
598            throws PersistenceDatabaseException {
599        PreparedStatement ps = session.prepareStatement(ContactPostalAddressFactory.class, queryMap.get(entityPermission));
600        
601        return getEntityFromQuery(session, entityPermission, ps, params);
602    }
603    
604    public ContactPostalAddress getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
605            throws PersistenceDatabaseException {
606        Session session = ThreadSession.currentSession();
607        PreparedStatement ps = session.prepareStatement(ContactPostalAddressFactory.class, queryMap.get(entityPermission));
608        
609        return getEntityFromQuery(session, entityPermission, ps, params);
610    }
611    
612    public ContactPostalAddress getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
613            throws PersistenceDatabaseException {
614        PreparedStatement ps = session.prepareStatement(ContactPostalAddressFactory.class, queryMap.get(entityPermission));
615        
616        return getEntityFromQuery(session, entityPermission, ps);
617    }
618    
619    public ContactPostalAddress getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
620            throws PersistenceDatabaseException {
621        Session session = ThreadSession.currentSession();
622        PreparedStatement ps = session.prepareStatement(ContactPostalAddressFactory.class, queryMap.get(entityPermission));
623        
624        return getEntityFromQuery(session, entityPermission, ps);
625    }
626    
627    public ContactPostalAddress getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps)
628            throws PersistenceDatabaseException {
629        return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps);
630    }
631    
632    public ContactPostalAddress getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
633            throws PersistenceDatabaseException {
634        return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps, params);
635    }
636    
637    public ContactPostalAddress getEntityFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params)
638            throws PersistenceDatabaseException {
639        ContactPostalAddress _entity = null;
640        ResultSet _rs = null;
641        
642        try {
643            if(params.length != 0) {
644                Session.setQueryParams(ps, params);
645            }
646            
647            _rs = ps.executeQuery();
648            if(_rs.next()) {
649                _entity = getEntityFromResultSet(session, entityPermission, _rs);
650            }
651            _rs.close();
652        } catch (SQLException se) {
653            throw new PersistenceDatabaseException(se);
654        } finally {
655            if(_rs != null) {
656                try {
657                    _rs.close();
658                } catch (SQLException se) {
659                    // do nothing
660                }
661            }
662        }
663        
664        return _entity;
665    }
666    
667    public java.util.List<ContactPostalAddress> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs)
668            throws PersistenceDatabaseException {
669        return getEntitiesFromResultSet(ThreadSession.currentSession(), entityPermission, rs);
670    }
671    
672    public java.util.List<ContactPostalAddress> getEntitiesFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs)
673            throws PersistenceDatabaseException {
674        java.util.List<ContactPostalAddress> _result = new ArrayList<>();
675        
676        try {
677            while(rs.next()) {
678                _result.add(getEntityFromResultSet(session, entityPermission, rs));
679            }
680        } catch (SQLException se) {
681            throw new PersistenceDatabaseException(se);
682        }
683        
684        return _result;
685    }
686    
687    public ContactPostalAddress getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs)
688            throws PersistenceDatabaseException {
689        return getEntityFromResultSet(ThreadSession.currentSession(), entityPermission, rs);
690    }
691    
692    public ContactPostalAddress getEntityFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs)
693            throws PersistenceDatabaseException {
694        ContactPostalAddress _entity;
695        
696        try {
697            Long ctpa_contactpostaladdressid = rs.getLong(CTPA_CONTACTPOSTALADDRESSID);
698            ContactPostalAddressPK _pk = new ContactPostalAddressPK(ctpa_contactpostaladdressid);
699            
700            // See if we already have the entity in the session cache
701            _entity = (ContactPostalAddress)session.getEntity(_pk);
702            if(_entity != null) {
703                // If the requested permission is READ_WRITE, and the cached permission is
704                // READ_ONLY, then pretend that the cached object wasn't found, and create
705                // a new entity that is READ_WRITE.
706                if(entityPermission.equals(EntityPermission.READ_WRITE)) {
707                    if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
708                        _entity = null;
709                }
710            }
711            boolean foundInSessionCache = _entity != null;
712            
713            if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
714                _entity = getEntityFromCache(session, _pk);
715            }
716            
717            if(_entity == null) {
718                Long ctpa_cmch_contactmechanismid = rs.getLong(CTPA_CMCH_CONTACTMECHANISMID);
719                if(rs.wasNull())
720                    ctpa_cmch_contactmechanismid = null;
721                
722                Long ctpa_pert_personaltitleid = rs.getLong(CTPA_PERT_PERSONALTITLEID);
723                if(rs.wasNull())
724                    ctpa_pert_personaltitleid = null;
725                
726                String ctpa_firstname = rs.getString(CTPA_FIRSTNAME);
727                if(rs.wasNull())
728                    ctpa_firstname = null;
729                
730                String ctpa_firstnamesdx = rs.getString(CTPA_FIRSTNAMESDX);
731                if(rs.wasNull())
732                    ctpa_firstnamesdx = null;
733                
734                String ctpa_middlename = rs.getString(CTPA_MIDDLENAME);
735                if(rs.wasNull())
736                    ctpa_middlename = null;
737                
738                String ctpa_middlenamesdx = rs.getString(CTPA_MIDDLENAMESDX);
739                if(rs.wasNull())
740                    ctpa_middlenamesdx = null;
741                
742                String ctpa_lastname = rs.getString(CTPA_LASTNAME);
743                if(rs.wasNull())
744                    ctpa_lastname = null;
745                
746                String ctpa_lastnamesdx = rs.getString(CTPA_LASTNAMESDX);
747                if(rs.wasNull())
748                    ctpa_lastnamesdx = null;
749                
750                Long ctpa_nsfx_namesuffixid = rs.getLong(CTPA_NSFX_NAMESUFFIXID);
751                if(rs.wasNull())
752                    ctpa_nsfx_namesuffixid = null;
753                
754                String ctpa_companyname = rs.getString(CTPA_COMPANYNAME);
755                if(rs.wasNull())
756                    ctpa_companyname = null;
757                
758                String ctpa_attention = rs.getString(CTPA_ATTENTION);
759                if(rs.wasNull())
760                    ctpa_attention = null;
761                
762                String ctpa_address1 = rs.getString(CTPA_ADDRESS1);
763                if(rs.wasNull())
764                    ctpa_address1 = null;
765                
766                String ctpa_address2 = rs.getString(CTPA_ADDRESS2);
767                if(rs.wasNull())
768                    ctpa_address2 = null;
769                
770                String ctpa_address3 = rs.getString(CTPA_ADDRESS3);
771                if(rs.wasNull())
772                    ctpa_address3 = null;
773                
774                String ctpa_city = rs.getString(CTPA_CITY);
775                if(rs.wasNull())
776                    ctpa_city = null;
777                
778                Long ctpa_citygeocodeid = rs.getLong(CTPA_CITYGEOCODEID);
779                if(rs.wasNull())
780                    ctpa_citygeocodeid = null;
781                
782                Long ctpa_countygeocodeid = rs.getLong(CTPA_COUNTYGEOCODEID);
783                if(rs.wasNull())
784                    ctpa_countygeocodeid = null;
785                
786                String ctpa_state = rs.getString(CTPA_STATE);
787                if(rs.wasNull())
788                    ctpa_state = null;
789                
790                Long ctpa_stategeocodeid = rs.getLong(CTPA_STATEGEOCODEID);
791                if(rs.wasNull())
792                    ctpa_stategeocodeid = null;
793                
794                String ctpa_postalcode = rs.getString(CTPA_POSTALCODE);
795                if(rs.wasNull())
796                    ctpa_postalcode = null;
797                
798                Long ctpa_postalcodegeocodeid = rs.getLong(CTPA_POSTALCODEGEOCODEID);
799                if(rs.wasNull())
800                    ctpa_postalcodegeocodeid = null;
801                
802                Long ctpa_countrygeocodeid = rs.getLong(CTPA_COUNTRYGEOCODEID);
803                if(rs.wasNull())
804                    ctpa_countrygeocodeid = null;
805                
806                Boolean ctpa_iscommercial = rs.getInt(CTPA_ISCOMMERCIAL) == 1;
807                if(rs.wasNull())
808                    ctpa_iscommercial = null;
809                
810                Long ctpa_fromtime = rs.getLong(CTPA_FROMTIME);
811                if(rs.wasNull())
812                    ctpa_fromtime = null;
813                
814                Long ctpa_thrutime = rs.getLong(CTPA_THRUTIME);
815                if(rs.wasNull())
816                    ctpa_thrutime = null;
817                
818                ContactPostalAddressValue _value = new ContactPostalAddressValue(_pk, ctpa_cmch_contactmechanismid == null? null: new ContactMechanismPK(ctpa_cmch_contactmechanismid), ctpa_pert_personaltitleid == null? null: new PersonalTitlePK(ctpa_pert_personaltitleid), ctpa_firstname, ctpa_firstnamesdx, ctpa_middlename, ctpa_middlenamesdx, ctpa_lastname, ctpa_lastnamesdx, ctpa_nsfx_namesuffixid == null? null: new NameSuffixPK(ctpa_nsfx_namesuffixid), ctpa_companyname, ctpa_attention, ctpa_address1, ctpa_address2, ctpa_address3, ctpa_city, ctpa_citygeocodeid == null? null: new GeoCodePK(ctpa_citygeocodeid), ctpa_countygeocodeid == null? null: new GeoCodePK(ctpa_countygeocodeid), ctpa_state, ctpa_stategeocodeid == null? null: new GeoCodePK(ctpa_stategeocodeid), ctpa_postalcode, ctpa_postalcodegeocodeid == null? null: new GeoCodePK(ctpa_postalcodegeocodeid), ctpa_countrygeocodeid == null? null: new GeoCodePK(ctpa_countrygeocodeid), ctpa_iscommercial, ctpa_fromtime, ctpa_thrutime);
819                _entity = new ContactPostalAddress(_value, entityPermission);
820            }
821            
822            if(!foundInSessionCache) {
823                if(entityPermission.equals(EntityPermission.READ_ONLY)) {
824                    session.putReadOnlyEntity(_pk, _entity);
825                    session.getValueCache().put(_entity.getContactPostalAddressValue());
826                } else {
827                    session.putReadWriteEntity(_pk, _entity);
828                }
829            }
830        } catch (SQLException se) {
831            throw new PersistenceDatabaseException(se);
832        }
833        
834        return _entity;
835    }
836    
837    public ContactPostalAddress create(Session session, ContactMechanism contactMechanism, PersonalTitle personalTitle, String firstName, String firstNameSdx, String middleName, String middleNameSdx, String lastName, String lastNameSdx, NameSuffix nameSuffix, String companyName, String attention, String address1, String address2, String address3, String city, GeoCode cityGeoCode, GeoCode countyGeoCode, String state, GeoCode stateGeoCode, String postalCode, GeoCode postalCodeGeoCode, GeoCode countryGeoCode, Boolean isCommercial, Long fromTime, Long thruTime)
838            throws PersistenceDatabaseException, PersistenceNotNullException {
839        return create(session, contactMechanism == null ? null : contactMechanism.getPrimaryKey(), personalTitle == null ? null : personalTitle.getPrimaryKey(), firstName, firstNameSdx, middleName, middleNameSdx, lastName, lastNameSdx, nameSuffix == null ? null : nameSuffix.getPrimaryKey(), companyName, attention, address1, address2, address3, city, cityGeoCode == null ? null : cityGeoCode.getPrimaryKey(), countyGeoCode == null ? null : countyGeoCode.getPrimaryKey(), state, stateGeoCode == null ? null : stateGeoCode.getPrimaryKey(), postalCode, postalCodeGeoCode == null ? null : postalCodeGeoCode.getPrimaryKey(), countryGeoCode == null ? null : countryGeoCode.getPrimaryKey(), isCommercial, fromTime, thruTime);
840    }
841    
842    public ContactPostalAddress create(ContactMechanism contactMechanism, PersonalTitle personalTitle, String firstName, String firstNameSdx, String middleName, String middleNameSdx, String lastName, String lastNameSdx, NameSuffix nameSuffix, String companyName, String attention, String address1, String address2, String address3, String city, GeoCode cityGeoCode, GeoCode countyGeoCode, String state, GeoCode stateGeoCode, String postalCode, GeoCode postalCodeGeoCode, GeoCode countryGeoCode, Boolean isCommercial, Long fromTime, Long thruTime)
843            throws PersistenceDatabaseException, PersistenceNotNullException {
844        return create(ThreadSession.currentSession(), contactMechanism == null ? null : contactMechanism.getPrimaryKey(), personalTitle == null ? null : personalTitle.getPrimaryKey(), firstName, firstNameSdx, middleName, middleNameSdx, lastName, lastNameSdx, nameSuffix == null ? null : nameSuffix.getPrimaryKey(), companyName, attention, address1, address2, address3, city, cityGeoCode == null ? null : cityGeoCode.getPrimaryKey(), countyGeoCode == null ? null : countyGeoCode.getPrimaryKey(), state, stateGeoCode == null ? null : stateGeoCode.getPrimaryKey(), postalCode, postalCodeGeoCode == null ? null : postalCodeGeoCode.getPrimaryKey(), countryGeoCode == null ? null : countryGeoCode.getPrimaryKey(), isCommercial, fromTime, thruTime);
845    }
846    
847    private void bindForCreate(PreparedStatement _ps, ContactPostalAddressValue _value)
848            throws SQLException {
849        _ps.setLong(1, _value.getEntityId());
850        
851        ContactMechanismPK ctpa_cmch_contactmechanismid = _value.getContactMechanismPK();
852        if(ctpa_cmch_contactmechanismid == null)
853            _ps.setNull(2, Types.BIGINT);
854        else
855            _ps.setLong(2, ctpa_cmch_contactmechanismid.getEntityId());
856            
857        PersonalTitlePK ctpa_pert_personaltitleid = _value.getPersonalTitlePK();
858        if(ctpa_pert_personaltitleid == null)
859            _ps.setNull(3, Types.BIGINT);
860        else
861            _ps.setLong(3, ctpa_pert_personaltitleid.getEntityId());
862            
863        String ctpa_firstname = _value.getFirstName();
864        if(ctpa_firstname == null)
865            _ps.setNull(4, Types.VARCHAR);
866        else
867            _ps.setString(4, ctpa_firstname);
868            
869        String ctpa_firstnamesdx = _value.getFirstNameSdx();
870        if(ctpa_firstnamesdx == null)
871            _ps.setNull(5, Types.VARCHAR);
872        else
873            _ps.setString(5, ctpa_firstnamesdx);
874            
875        String ctpa_middlename = _value.getMiddleName();
876        if(ctpa_middlename == null)
877            _ps.setNull(6, Types.VARCHAR);
878        else
879            _ps.setString(6, ctpa_middlename);
880            
881        String ctpa_middlenamesdx = _value.getMiddleNameSdx();
882        if(ctpa_middlenamesdx == null)
883            _ps.setNull(7, Types.VARCHAR);
884        else
885            _ps.setString(7, ctpa_middlenamesdx);
886            
887        String ctpa_lastname = _value.getLastName();
888        if(ctpa_lastname == null)
889            _ps.setNull(8, Types.VARCHAR);
890        else
891            _ps.setString(8, ctpa_lastname);
892            
893        String ctpa_lastnamesdx = _value.getLastNameSdx();
894        if(ctpa_lastnamesdx == null)
895            _ps.setNull(9, Types.VARCHAR);
896        else
897            _ps.setString(9, ctpa_lastnamesdx);
898            
899        NameSuffixPK ctpa_nsfx_namesuffixid = _value.getNameSuffixPK();
900        if(ctpa_nsfx_namesuffixid == null)
901            _ps.setNull(10, Types.BIGINT);
902        else
903            _ps.setLong(10, ctpa_nsfx_namesuffixid.getEntityId());
904            
905        String ctpa_companyname = _value.getCompanyName();
906        if(ctpa_companyname == null)
907            _ps.setNull(11, Types.VARCHAR);
908        else
909            _ps.setString(11, ctpa_companyname);
910            
911        String ctpa_attention = _value.getAttention();
912        if(ctpa_attention == null)
913            _ps.setNull(12, Types.VARCHAR);
914        else
915            _ps.setString(12, ctpa_attention);
916            
917        String ctpa_address1 = _value.getAddress1();
918        if(ctpa_address1 == null)
919            _ps.setNull(13, Types.VARCHAR);
920        else
921            _ps.setString(13, ctpa_address1);
922            
923        String ctpa_address2 = _value.getAddress2();
924        if(ctpa_address2 == null)
925            _ps.setNull(14, Types.VARCHAR);
926        else
927            _ps.setString(14, ctpa_address2);
928            
929        String ctpa_address3 = _value.getAddress3();
930        if(ctpa_address3 == null)
931            _ps.setNull(15, Types.VARCHAR);
932        else
933            _ps.setString(15, ctpa_address3);
934            
935        String ctpa_city = _value.getCity();
936        if(ctpa_city == null)
937            _ps.setNull(16, Types.VARCHAR);
938        else
939            _ps.setString(16, ctpa_city);
940            
941        GeoCodePK ctpa_citygeocodeid = _value.getCityGeoCodePK();
942        if(ctpa_citygeocodeid == null)
943            _ps.setNull(17, Types.BIGINT);
944        else
945            _ps.setLong(17, ctpa_citygeocodeid.getEntityId());
946            
947        GeoCodePK ctpa_countygeocodeid = _value.getCountyGeoCodePK();
948        if(ctpa_countygeocodeid == null)
949            _ps.setNull(18, Types.BIGINT);
950        else
951            _ps.setLong(18, ctpa_countygeocodeid.getEntityId());
952            
953        String ctpa_state = _value.getState();
954        if(ctpa_state == null)
955            _ps.setNull(19, Types.VARCHAR);
956        else
957            _ps.setString(19, ctpa_state);
958            
959        GeoCodePK ctpa_stategeocodeid = _value.getStateGeoCodePK();
960        if(ctpa_stategeocodeid == null)
961            _ps.setNull(20, Types.BIGINT);
962        else
963            _ps.setLong(20, ctpa_stategeocodeid.getEntityId());
964            
965        String ctpa_postalcode = _value.getPostalCode();
966        if(ctpa_postalcode == null)
967            _ps.setNull(21, Types.VARCHAR);
968        else
969            _ps.setString(21, ctpa_postalcode);
970            
971        GeoCodePK ctpa_postalcodegeocodeid = _value.getPostalCodeGeoCodePK();
972        if(ctpa_postalcodegeocodeid == null)
973            _ps.setNull(22, Types.BIGINT);
974        else
975            _ps.setLong(22, ctpa_postalcodegeocodeid.getEntityId());
976            
977        GeoCodePK ctpa_countrygeocodeid = _value.getCountryGeoCodePK();
978        if(ctpa_countrygeocodeid == null)
979            _ps.setNull(23, Types.BIGINT);
980        else
981            _ps.setLong(23, ctpa_countrygeocodeid.getEntityId());
982            
983        Boolean ctpa_iscommercial = _value.getIsCommercial();
984        if(ctpa_iscommercial == null)
985            _ps.setNull(24, Types.BIT);
986        else
987            _ps.setInt(24, ctpa_iscommercial? 1: 0);
988            
989        Long ctpa_fromtime = _value.getFromTime();
990        if(ctpa_fromtime == null)
991            _ps.setNull(25, Types.BIGINT);
992        else
993            _ps.setLong(25, ctpa_fromtime);
994            
995        Long ctpa_thrutime = _value.getThruTime();
996        if(ctpa_thrutime == null)
997            _ps.setNull(26, Types.BIGINT);
998        else
999            _ps.setLong(26, ctpa_thrutime);
1000            
1001    }
1002    
1003    public ContactPostalAddress create(Session session, ContactMechanismPK contactMechanismPK, PersonalTitlePK personalTitlePK, String firstName, String firstNameSdx, String middleName, String middleNameSdx, String lastName, String lastNameSdx, NameSuffixPK nameSuffixPK, String companyName, String attention, String address1, String address2, String address3, String city, GeoCodePK cityGeoCodePK, GeoCodePK countyGeoCodePK, String state, GeoCodePK stateGeoCodePK, String postalCode, GeoCodePK postalCodeGeoCodePK, GeoCodePK countryGeoCodePK, Boolean isCommercial, Long fromTime, Long thruTime)
1004            throws PersistenceDatabaseException, PersistenceNotNullException {
1005        ContactPostalAddressPK _pk = getNextPK();
1006        ContactPostalAddressValue _value = new ContactPostalAddressValue(_pk, contactMechanismPK, personalTitlePK, firstName, firstNameSdx, middleName, middleNameSdx, lastName, lastNameSdx, nameSuffixPK, companyName, attention, address1, address2, address3, city, cityGeoCodePK, countyGeoCodePK, state, stateGeoCodePK, postalCode, postalCodeGeoCodePK, countryGeoCodePK, isCommercial, fromTime, thruTime);
1007        
1008        PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
1009        
1010        try {
1011            bindForCreate(_ps, _value);
1012            
1013            if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
1014                int _count = _ps.executeUpdate();
1015                
1016                if(_count != 1) {
1017                    throw new PersistenceDatabaseUpdateException("insert failed, _count = " + _count);
1018                }
1019            } else {
1020                 _ps.executeUpdate();
1021            }
1022            
1023            session.getValueCache().put(_value);
1024        } catch (SQLException se) {
1025            throw new PersistenceDatabaseException(se);
1026        }
1027        
1028        ContactPostalAddress _entity = new ContactPostalAddress(_value, EntityPermission.READ_ONLY);
1029        session.putReadOnlyEntity(_pk, _entity);
1030        
1031        return _entity;
1032    }
1033    
1034    public ContactPostalAddress create(ContactMechanismPK contactMechanismPK, PersonalTitlePK personalTitlePK, String firstName, String firstNameSdx, String middleName, String middleNameSdx, String lastName, String lastNameSdx, NameSuffixPK nameSuffixPK, String companyName, String attention, String address1, String address2, String address3, String city, GeoCodePK cityGeoCodePK, GeoCodePK countyGeoCodePK, String state, GeoCodePK stateGeoCodePK, String postalCode, GeoCodePK postalCodeGeoCodePK, GeoCodePK countryGeoCodePK, Boolean isCommercial, Long fromTime, Long thruTime)
1035            throws PersistenceDatabaseException, PersistenceNotNullException {
1036        return create(ThreadSession.currentSession(), contactMechanismPK, personalTitlePK, firstName, firstNameSdx, middleName, middleNameSdx, lastName, lastNameSdx, nameSuffixPK, companyName, attention, address1, address2, address3, city, cityGeoCodePK, countyGeoCodePK, state, stateGeoCodePK, postalCode, postalCodeGeoCodePK, countryGeoCodePK, isCommercial, fromTime, thruTime);
1037    }
1038    
1039    public void create(Session session, Collection<ContactPostalAddressValue> _values)
1040            throws PersistenceDatabaseException, PersistenceNotNullException {
1041        int _size = _values.size();
1042        
1043        if(_size > 0) {
1044            PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
1045            List<ContactPostalAddressValue> _cacheValues = new ArrayList<>(_size);
1046            
1047            try {
1048                for(ContactPostalAddressValue _value : _values) {
1049                    _value.setEntityId(entityIdGenerator.getNextEntityId());
1050                    bindForCreate(_ps, _value);
1051                    
1052                    _ps.addBatch();
1053                    
1054                    _cacheValues.add(_value);
1055                }
1056                
1057                if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
1058                    int[] _counts = _ps.executeBatch();
1059                    
1060                    for(int _countOffset = 0 ; _countOffset < _size ; _countOffset++) {
1061                        if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
1062                            throw new PersistenceDatabaseUpdateException("batch insert failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
1063                        }
1064                    }
1065                } else {
1066                     _ps.executeBatch();
1067                }
1068                
1069                _ps.clearBatch();
1070            } catch (SQLException se) {
1071                throw new PersistenceDatabaseException(se);
1072            }
1073            
1074            _cacheValues.forEach((_cacheValue) -> {
1075                ContactPostalAddress _cacheEntity = new ContactPostalAddress(_cacheValue, EntityPermission.READ_ONLY);
1076                
1077                session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity);
1078            });
1079        }
1080    }
1081    
1082    public void create(Collection<ContactPostalAddressValue> _values)
1083            throws PersistenceDatabaseException, PersistenceNotNullException {
1084        create(ThreadSession.currentSession(), _values);
1085    }
1086    
1087    private boolean bindForStore(PreparedStatement _ps, ContactPostalAddressValue _value)
1088            throws SQLException {
1089        boolean _hasBeenModified = _value.hasBeenModified();
1090        
1091        if(_hasBeenModified) {
1092            ContactMechanismPK ctpa_cmch_contactmechanismid = _value.getContactMechanismPK();
1093            if(ctpa_cmch_contactmechanismid == null)
1094                _ps.setNull(1, Types.BIGINT);
1095            else
1096                _ps.setLong(1, ctpa_cmch_contactmechanismid.getEntityId());
1097            
1098            PersonalTitlePK ctpa_pert_personaltitleid = _value.getPersonalTitlePK();
1099            if(ctpa_pert_personaltitleid == null)
1100                _ps.setNull(2, Types.BIGINT);
1101            else
1102                _ps.setLong(2, ctpa_pert_personaltitleid.getEntityId());
1103            
1104            String ctpa_firstname = _value.getFirstName();
1105            if(ctpa_firstname == null)
1106                _ps.setNull(3, Types.VARCHAR);
1107            else
1108                _ps.setString(3, ctpa_firstname);
1109            
1110            String ctpa_firstnamesdx = _value.getFirstNameSdx();
1111            if(ctpa_firstnamesdx == null)
1112                _ps.setNull(4, Types.VARCHAR);
1113            else
1114                _ps.setString(4, ctpa_firstnamesdx);
1115            
1116            String ctpa_middlename = _value.getMiddleName();
1117            if(ctpa_middlename == null)
1118                _ps.setNull(5, Types.VARCHAR);
1119            else
1120                _ps.setString(5, ctpa_middlename);
1121            
1122            String ctpa_middlenamesdx = _value.getMiddleNameSdx();
1123            if(ctpa_middlenamesdx == null)
1124                _ps.setNull(6, Types.VARCHAR);
1125            else
1126                _ps.setString(6, ctpa_middlenamesdx);
1127            
1128            String ctpa_lastname = _value.getLastName();
1129            if(ctpa_lastname == null)
1130                _ps.setNull(7, Types.VARCHAR);
1131            else
1132                _ps.setString(7, ctpa_lastname);
1133            
1134            String ctpa_lastnamesdx = _value.getLastNameSdx();
1135            if(ctpa_lastnamesdx == null)
1136                _ps.setNull(8, Types.VARCHAR);
1137            else
1138                _ps.setString(8, ctpa_lastnamesdx);
1139            
1140            NameSuffixPK ctpa_nsfx_namesuffixid = _value.getNameSuffixPK();
1141            if(ctpa_nsfx_namesuffixid == null)
1142                _ps.setNull(9, Types.BIGINT);
1143            else
1144                _ps.setLong(9, ctpa_nsfx_namesuffixid.getEntityId());
1145            
1146            String ctpa_companyname = _value.getCompanyName();
1147            if(ctpa_companyname == null)
1148                _ps.setNull(10, Types.VARCHAR);
1149            else
1150                _ps.setString(10, ctpa_companyname);
1151            
1152            String ctpa_attention = _value.getAttention();
1153            if(ctpa_attention == null)
1154                _ps.setNull(11, Types.VARCHAR);
1155            else
1156                _ps.setString(11, ctpa_attention);
1157            
1158            String ctpa_address1 = _value.getAddress1();
1159            if(ctpa_address1 == null)
1160                _ps.setNull(12, Types.VARCHAR);
1161            else
1162                _ps.setString(12, ctpa_address1);
1163            
1164            String ctpa_address2 = _value.getAddress2();
1165            if(ctpa_address2 == null)
1166                _ps.setNull(13, Types.VARCHAR);
1167            else
1168                _ps.setString(13, ctpa_address2);
1169            
1170            String ctpa_address3 = _value.getAddress3();
1171            if(ctpa_address3 == null)
1172                _ps.setNull(14, Types.VARCHAR);
1173            else
1174                _ps.setString(14, ctpa_address3);
1175            
1176            String ctpa_city = _value.getCity();
1177            if(ctpa_city == null)
1178                _ps.setNull(15, Types.VARCHAR);
1179            else
1180                _ps.setString(15, ctpa_city);
1181            
1182            GeoCodePK ctpa_citygeocodeid = _value.getCityGeoCodePK();
1183            if(ctpa_citygeocodeid == null)
1184                _ps.setNull(16, Types.BIGINT);
1185            else
1186                _ps.setLong(16, ctpa_citygeocodeid.getEntityId());
1187            
1188            GeoCodePK ctpa_countygeocodeid = _value.getCountyGeoCodePK();
1189            if(ctpa_countygeocodeid == null)
1190                _ps.setNull(17, Types.BIGINT);
1191            else
1192                _ps.setLong(17, ctpa_countygeocodeid.getEntityId());
1193            
1194            String ctpa_state = _value.getState();
1195            if(ctpa_state == null)
1196                _ps.setNull(18, Types.VARCHAR);
1197            else
1198                _ps.setString(18, ctpa_state);
1199            
1200            GeoCodePK ctpa_stategeocodeid = _value.getStateGeoCodePK();
1201            if(ctpa_stategeocodeid == null)
1202                _ps.setNull(19, Types.BIGINT);
1203            else
1204                _ps.setLong(19, ctpa_stategeocodeid.getEntityId());
1205            
1206            String ctpa_postalcode = _value.getPostalCode();
1207            if(ctpa_postalcode == null)
1208                _ps.setNull(20, Types.VARCHAR);
1209            else
1210                _ps.setString(20, ctpa_postalcode);
1211            
1212            GeoCodePK ctpa_postalcodegeocodeid = _value.getPostalCodeGeoCodePK();
1213            if(ctpa_postalcodegeocodeid == null)
1214                _ps.setNull(21, Types.BIGINT);
1215            else
1216                _ps.setLong(21, ctpa_postalcodegeocodeid.getEntityId());
1217            
1218            GeoCodePK ctpa_countrygeocodeid = _value.getCountryGeoCodePK();
1219            if(ctpa_countrygeocodeid == null)
1220                _ps.setNull(22, Types.BIGINT);
1221            else
1222                _ps.setLong(22, ctpa_countrygeocodeid.getEntityId());
1223            
1224            Boolean ctpa_iscommercial = _value.getIsCommercial();
1225            if(ctpa_iscommercial == null)
1226                _ps.setNull(23, Types.BIT);
1227            else
1228                _ps.setInt(23, ctpa_iscommercial? 1: 0);
1229            
1230            Long ctpa_fromtime = _value.getFromTime();
1231            if(ctpa_fromtime == null)
1232                _ps.setNull(24, Types.BIGINT);
1233            else
1234                _ps.setLong(24, ctpa_fromtime);
1235            
1236            Long ctpa_thrutime = _value.getThruTime();
1237            if(ctpa_thrutime == null)
1238                _ps.setNull(25, Types.BIGINT);
1239            else
1240                _ps.setLong(25, ctpa_thrutime);
1241            
1242            _ps.setLong(26, _value.getPrimaryKey().getEntityId());
1243            
1244            _value.clearHasBeenModified();
1245        }
1246        
1247        return _hasBeenModified;
1248    }
1249    
1250    @Override
1251    public void store(Session session, ContactPostalAddress entity)
1252            throws PersistenceDatabaseException {
1253        PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
1254        
1255        try {
1256            ContactPostalAddressValue _value = entity.getContactPostalAddressValue();
1257            
1258            if(bindForStore(_ps, _value)) {
1259                if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
1260                    int _count = _ps.executeUpdate();
1261                    
1262                    if(_count != 1) {
1263                        throw new PersistenceDatabaseUpdateException("update failed, _count = " + _count);
1264                    }
1265                } else {
1266                     _ps.executeUpdate();
1267                }
1268                
1269                session.getValueCache().put(_value);
1270            }
1271        } catch (SQLException se) {
1272            throw new PersistenceDatabaseException(se);
1273        }
1274    }
1275    
1276    @Override
1277    public void store(Session session, Collection<ContactPostalAddress> entities)
1278            throws PersistenceDatabaseException {
1279        if(entities.size() > 0) {
1280            PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
1281            int _modifiedEntities = 0;
1282            
1283            try {
1284                for(ContactPostalAddress entity : entities) {
1285                    if(bindForStore(_ps, entity.getContactPostalAddressValue())) {
1286                        _ps.addBatch();
1287                        _modifiedEntities++;
1288                    }
1289                }
1290                
1291                if(_modifiedEntities != 0) {
1292                    if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
1293                        int[] _counts = _ps.executeBatch();
1294                        
1295                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
1296                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
1297                                throw new PersistenceDatabaseUpdateException("batch update failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
1298                            }
1299                        }
1300                    } else {
1301                         _ps.executeBatch();
1302                    }
1303                    
1304                    _ps.clearBatch();
1305                    
1306                    entities.forEach((entity) -> {
1307                        session.getValueCache().put(entity.getContactPostalAddressValue());
1308                    });
1309                }
1310            } catch (SQLException se) {
1311                throw new PersistenceDatabaseException(se);
1312            }
1313        }
1314    }
1315    
1316    @Override
1317    public void store(Collection<ContactPostalAddress> entities)
1318            throws PersistenceDatabaseException {
1319        store(ThreadSession.currentSession(), entities);
1320    }
1321    
1322    @Override
1323    public void remove(Session session, ContactPostalAddress entity)
1324            throws PersistenceDatabaseException {
1325        remove(session, entity.getPrimaryKey());
1326    }
1327    
1328    @Override
1329    public void remove(Session session, ContactPostalAddressPK pk)
1330            throws PersistenceDatabaseException {
1331        PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
1332        long _entityId = pk.getEntityId();
1333        
1334        try {
1335            _ps.setLong(1, _entityId);
1336            
1337            if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
1338                int _count = _ps.executeUpdate();
1339                
1340                if(_count != 1) {
1341                    throw new PersistenceDatabaseUpdateException("remove failed, _count = " + _count);
1342                }
1343            } else {
1344                 _ps.executeUpdate();
1345            }
1346            
1347            session.getValueCache().remove(pk);
1348        } catch (SQLException se) {
1349            throw new PersistenceDatabaseException(se);
1350        }
1351        
1352        session.removed(pk, false);
1353    }
1354    
1355    @Override
1356    public void remove(Session session, Collection<ContactPostalAddressPK> pks)
1357            throws PersistenceDatabaseException {
1358        if(pks.size() > 0) {
1359            PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
1360            int _modifiedEntities = 0;
1361            
1362            try {
1363                for(ContactPostalAddressPK pk : pks) {
1364                    long _entityId = pk.getEntityId();
1365                    
1366                    _ps.setLong(1, _entityId);
1367                    
1368                    _ps.addBatch();
1369                    _modifiedEntities++;
1370                }
1371                
1372                if(_modifiedEntities != 0) {
1373                    if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
1374                        int[] _counts = _ps.executeBatch();
1375                        
1376                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
1377                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
1378                                throw new PersistenceDatabaseUpdateException("batch remove failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
1379                            }
1380                        }
1381                    } else {
1382                        _ps.executeBatch();
1383                    }
1384                    
1385                    _ps.clearBatch();
1386                    
1387                    pks.forEach((pk) -> {
1388                        session.getValueCache().remove(pk);
1389                    });
1390                }
1391            } catch (SQLException se) {
1392                throw new PersistenceDatabaseException(se);
1393            }
1394            
1395            pks.forEach((pk) -> {
1396                session.removed(pk, true);
1397            });
1398        }
1399    }
1400    
1401    @Override
1402    public void remove(Collection<ContactPostalAddressPK> pks)
1403            throws PersistenceDatabaseException {
1404        remove(ThreadSession.currentSession(), pks);
1405    }
1406    
1407    @Override
1408    public boolean validPK(Session session, ContactPostalAddressPK pk)
1409            throws PersistenceDatabaseException {
1410        boolean valid = false;
1411        PreparedStatement _ps = session.prepareStatement(SQL_VALID);
1412        ResultSet _rs = null;
1413        
1414        try {
1415            _ps.setLong(1, pk.getEntityId());
1416            
1417            _rs = _ps.executeQuery();
1418            if(_rs.next()) {
1419                long _count = _rs.getLong(1);
1420                if(_rs.wasNull())
1421                    _count = 0;
1422                
1423                if(_count == 1)
1424                    valid = true;
1425            }
1426        } catch (SQLException se) {
1427            throw new PersistenceDatabaseException(se);
1428        } finally {
1429            if(_rs != null) {
1430                try {
1431                    _rs.close();
1432                } catch (SQLException se) {
1433                    // do nothing
1434                }
1435            }
1436        }
1437        
1438        return valid;
1439    }
1440    
1441}