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