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