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