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