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