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