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