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