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