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