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