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