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