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