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