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