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 * CampaignSourceDetailFactory.java
021 */
022
023package com.echothree.model.data.campaign.server.factory;
024
025import com.echothree.model.data.campaign.common.pk.CampaignSourcePK;
026
027import com.echothree.model.data.campaign.server.entity.CampaignSource;
028
029import com.echothree.model.data.campaign.common.CampaignSourceDetailConstants;
030import com.echothree.model.data.campaign.common.pk.CampaignSourceDetailPK;
031import com.echothree.model.data.campaign.server.value.CampaignSourceDetailValue;
032import com.echothree.model.data.campaign.server.entity.CampaignSourceDetail;
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 CampaignSourceDetailFactory
062        implements BaseFactory<CampaignSourceDetailPK, CampaignSourceDetail> {
063    
064    //final private static Log log = LogFactory.getLog(CampaignSourceDetailFactory.class);
065    
066    final private static String SQL_SELECT_READ_ONLY = "SELECT cmpgnsrcdt_campaignsourcedetailid, cmpgnsrcdt_cmpgnsrc_campaignsourceid, cmpgnsrcdt_campaignsourcename, cmpgnsrcdt_valuesha1hash, cmpgnsrcdt_value, cmpgnsrcdt_isdefault, cmpgnsrcdt_sortorder, cmpgnsrcdt_fromtime, cmpgnsrcdt_thrutime FROM campaignsourcedetails WHERE cmpgnsrcdt_campaignsourcedetailid = ?";
067    final private static String SQL_SELECT_READ_WRITE = "SELECT cmpgnsrcdt_campaignsourcedetailid, cmpgnsrcdt_cmpgnsrc_campaignsourceid, cmpgnsrcdt_campaignsourcename, cmpgnsrcdt_valuesha1hash, cmpgnsrcdt_value, cmpgnsrcdt_isdefault, cmpgnsrcdt_sortorder, cmpgnsrcdt_fromtime, cmpgnsrcdt_thrutime FROM campaignsourcedetails WHERE cmpgnsrcdt_campaignsourcedetailid = ? FOR UPDATE";
068    final private static String SQL_INSERT = "INSERT INTO campaignsourcedetails (cmpgnsrcdt_campaignsourcedetailid, cmpgnsrcdt_cmpgnsrc_campaignsourceid, cmpgnsrcdt_campaignsourcename, cmpgnsrcdt_valuesha1hash, cmpgnsrcdt_value, cmpgnsrcdt_isdefault, cmpgnsrcdt_sortorder, cmpgnsrcdt_fromtime, cmpgnsrcdt_thrutime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
069    final private static String SQL_UPDATE = "UPDATE campaignsourcedetails SET cmpgnsrcdt_cmpgnsrc_campaignsourceid = ?, cmpgnsrcdt_campaignsourcename = ?, cmpgnsrcdt_valuesha1hash = ?, cmpgnsrcdt_value = ?, cmpgnsrcdt_isdefault = ?, cmpgnsrcdt_sortorder = ?, cmpgnsrcdt_fromtime = ?, cmpgnsrcdt_thrutime = ? WHERE cmpgnsrcdt_campaignsourcedetailid = ?";
070    final private static String SQL_DELETE = "DELETE FROM campaignsourcedetails WHERE cmpgnsrcdt_campaignsourcedetailid = ?";
071    final private static String SQL_VALID = "SELECT COUNT(*) FROM campaignsourcedetails WHERE cmpgnsrcdt_campaignsourcedetailid = ?";
072    
073    final private static String PK_COLUMN = "cmpgnsrcdt_campaignsourcedetailid";
074    final private static String ALL_COLUMNS = "cmpgnsrcdt_campaignsourcedetailid, cmpgnsrcdt_cmpgnsrc_campaignsourceid, cmpgnsrcdt_campaignsourcename, cmpgnsrcdt_valuesha1hash, cmpgnsrcdt_value, cmpgnsrcdt_isdefault, cmpgnsrcdt_sortorder, cmpgnsrcdt_fromtime, cmpgnsrcdt_thrutime";
075    final public static String TABLE_NAME = "campaignsourcedetails";
076    
077    final public static String CMPGNSRCDT_CAMPAIGNSOURCEDETAILID = "cmpgnsrcdt_campaignsourcedetailid";
078    final public static String CMPGNSRCDT_CMPGNSRC_CAMPAIGNSOURCEID = "cmpgnsrcdt_cmpgnsrc_campaignsourceid";
079    final public static String CMPGNSRCDT_CAMPAIGNSOURCENAME = "cmpgnsrcdt_campaignsourcename";
080    final public static String CMPGNSRCDT_VALUESHA1HASH = "cmpgnsrcdt_valuesha1hash";
081    final public static String CMPGNSRCDT_VALUE = "cmpgnsrcdt_value";
082    final public static String CMPGNSRCDT_ISDEFAULT = "cmpgnsrcdt_isdefault";
083    final public static String CMPGNSRCDT_SORTORDER = "cmpgnsrcdt_sortorder";
084    final public static String CMPGNSRCDT_FROMTIME = "cmpgnsrcdt_fromtime";
085    final public static String CMPGNSRCDT_THRUTIME = "cmpgnsrcdt_thrutime";
086    
087    final private static EntityIdGenerator entityIdGenerator = new EntityIdGenerator(CampaignSourceDetailConstants.COMPONENT_VENDOR_NAME, CampaignSourceDetailConstants.ENTITY_TYPE_NAME);
088    
089    /** Creates a new instance of CampaignSourceDetailFactory */
090    protected CampaignSourceDetailFactory() {
091        super();
092    }
093    
094    public static CampaignSourceDetailFactory getInstance() {
095        return CDI.current().select(CampaignSourceDetailFactory.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 CampaignSourceDetailConstants.COMPONENT_VENDOR_NAME;
116    }
117    
118    @Override
119    public String getEntityTypeName() {
120        return CampaignSourceDetailConstants.ENTITY_TYPE_NAME;
121    }
122    
123    public PreparedStatement prepareStatement(String query) {
124        return ThreadSession.currentSession().prepareStatement(CampaignSourceDetailFactory.class, query);
125    }
126    
127    public CampaignSourceDetailPK getNextPK() {
128        return new CampaignSourceDetailPK(entityIdGenerator.getNextEntityId());
129    }
130    
131    public Set<CampaignSourceDetailPK> getPKsFromResultSetAsSet(ResultSet rs)
132            throws PersistenceDatabaseException {
133        Set<CampaignSourceDetailPK> _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<CampaignSourceDetailPK> getPKsFromResultSetAsList(ResultSet rs)
147            throws PersistenceDatabaseException {
148        java.util.List<CampaignSourceDetailPK> _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 CampaignSourceDetailPK getPKFromResultSet(ResultSet rs)
162            throws PersistenceDatabaseException {
163        CampaignSourceDetailPK _result;
164        
165        try {
166            long cmpgnsrcdt_campaignsourcedetailid = rs.getLong(CMPGNSRCDT_CAMPAIGNSOURCEDETAILID);
167            Long _entityId = rs.wasNull() ? null : cmpgnsrcdt_campaignsourcedetailid;
168            
169            _result = new CampaignSourceDetailPK(_entityId);
170        } catch (SQLException se) {
171            throw new PersistenceDatabaseException(se);
172        }
173        
174        return _result;
175    }
176    
177    public java.util.List<CampaignSourceDetailValue> getValuesFromPKs(Session session, Collection<CampaignSourceDetailPK> pks)
178            throws PersistenceDatabaseException {
179        java.util.List<CampaignSourceDetailValue> _values = new ArrayList<>(pks.size());
180        
181        for(CampaignSourceDetailPK _pk: pks) {
182            _values.add(getValueFromPK(session, _pk));
183        }
184        
185        return _values;
186    }
187    
188    public CampaignSourceDetailValue getValueFromPK(Session session, CampaignSourceDetailPK pk)
189            throws PersistenceDatabaseException {
190        CampaignSourceDetailValue _value;
191        
192        // See if we already have the entity in the session cache
193        CampaignSourceDetail _entity = (CampaignSourceDetail)session.getEntity(pk);
194        if(_entity == null)
195            _value = getEntityFromPK(session, EntityPermission.READ_ONLY, pk).getCampaignSourceDetailValue();
196        else
197            _value = _entity.getCampaignSourceDetailValue();
198        
199        return _value;
200    }
201    
202    public java.util.List<CampaignSourceDetailValue> getValuesFromResultSet(Session session, ResultSet rs)
203            throws PersistenceDatabaseException {
204        java.util.List<CampaignSourceDetailValue> _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 CampaignSourceDetailValue getValueFromResultSet(Session session, ResultSet rs)
218            throws PersistenceDatabaseException {
219        CampaignSourceDetailValue _value;
220        
221        try {
222            Long cmpgnsrcdt_campaignsourcedetailid = rs.getLong(CMPGNSRCDT_CAMPAIGNSOURCEDETAILID);
223            CampaignSourceDetailPK _pk = new CampaignSourceDetailPK(cmpgnsrcdt_campaignsourcedetailid);
224            
225            // See if we already have the entity in the session cache
226            CampaignSourceDetail _entity = (CampaignSourceDetail)session.getEntity(_pk);
227            
228            if(_entity == null) {
229                Long cmpgnsrcdt_cmpgnsrc_campaignsourceid = rs.getLong(CMPGNSRCDT_CMPGNSRC_CAMPAIGNSOURCEID);
230                if(rs.wasNull())
231                    cmpgnsrcdt_cmpgnsrc_campaignsourceid = null;
232                
233                String cmpgnsrcdt_campaignsourcename = rs.getString(CMPGNSRCDT_CAMPAIGNSOURCENAME);
234                if(rs.wasNull())
235                    cmpgnsrcdt_campaignsourcename = null;
236                
237                String cmpgnsrcdt_valuesha1hash = rs.getString(CMPGNSRCDT_VALUESHA1HASH);
238                if(rs.wasNull())
239                    cmpgnsrcdt_valuesha1hash = null;
240                
241                Clob cmpgnsrcdt_value = rs.getClob(CMPGNSRCDT_VALUE);
242                if(rs.wasNull())
243                    cmpgnsrcdt_value = null;
244                
245                Boolean cmpgnsrcdt_isdefault = rs.getInt(CMPGNSRCDT_ISDEFAULT) == 1;
246                if(rs.wasNull())
247                    cmpgnsrcdt_isdefault = null;
248                
249                Integer cmpgnsrcdt_sortorder = rs.getInt(CMPGNSRCDT_SORTORDER);
250                if(rs.wasNull())
251                    cmpgnsrcdt_sortorder = null;
252                
253                Long cmpgnsrcdt_fromtime = rs.getLong(CMPGNSRCDT_FROMTIME);
254                if(rs.wasNull())
255                    cmpgnsrcdt_fromtime = null;
256                
257                Long cmpgnsrcdt_thrutime = rs.getLong(CMPGNSRCDT_THRUTIME);
258                if(rs.wasNull())
259                    cmpgnsrcdt_thrutime = null;
260                
261                _value = new CampaignSourceDetailValue(_pk, new CampaignSourcePK(cmpgnsrcdt_cmpgnsrc_campaignsourceid), cmpgnsrcdt_campaignsourcename, cmpgnsrcdt_valuesha1hash, cmpgnsrcdt_value == null? null: cmpgnsrcdt_value.getSubString(1L, (int)cmpgnsrcdt_value.length()), cmpgnsrcdt_isdefault, cmpgnsrcdt_sortorder, cmpgnsrcdt_fromtime, cmpgnsrcdt_thrutime);
262            } else
263                _value = _entity.getCampaignSourceDetailValue();
264        } catch (SQLException se) {
265            throw new PersistenceDatabaseException(se);
266        }
267        
268        return _value;
269    }
270    
271    public java.util.List<CampaignSourceDetail> getEntitiesFromPKs(EntityPermission entityPermission, Collection<CampaignSourceDetailPK> pks)
272            throws PersistenceDatabaseException {
273        return getEntitiesFromPKs(ThreadSession.currentSession(), entityPermission, pks);
274    }
275    
276    public java.util.List<CampaignSourceDetail> getEntitiesFromPKs(Session session, EntityPermission entityPermission, Collection<CampaignSourceDetailPK> pks)
277            throws PersistenceDatabaseException {
278        java.util.List<CampaignSourceDetail> _entities = new ArrayList<>(pks.size());
279        
280        for(CampaignSourceDetailPK _pk: pks) {
281            _entities.add(getEntityFromPK(session, entityPermission, _pk));
282        }
283        
284        return _entities;
285    }
286    
287    public CampaignSourceDetail getEntityFromValue(EntityPermission entityPermission, CampaignSourceDetailValue value) {
288        return getEntityFromPK(ThreadSession.currentSession(), entityPermission, value.getPrimaryKey());
289    }
290    
291    public CampaignSourceDetail getEntityFromValue(Session session, EntityPermission entityPermission, CampaignSourceDetailValue value) {
292        return getEntityFromPK(session, entityPermission, value.getPrimaryKey());
293    }
294    
295    public CampaignSourceDetail getEntityFromPK(EntityPermission entityPermission, CampaignSourceDetailPK pk)
296            throws PersistenceDatabaseException {
297        return getEntityFromPK(ThreadSession.currentSession(), entityPermission, pk);
298    }
299    
300    public CampaignSourceDetail getEntityFromCache(Session session, CampaignSourceDetailPK pk) {
301        CampaignSourceDetailValue _value = (CampaignSourceDetailValue)session.getValueCache().get(pk);
302    
303        return _value == null ? null : new CampaignSourceDetail(_value, EntityPermission.READ_ONLY);
304    }
305    
306    public CampaignSourceDetail getEntityFromPK(Session session, EntityPermission entityPermission, CampaignSourceDetailPK pk)
307            throws PersistenceDatabaseException {
308        CampaignSourceDetail _entity;
309        
310        // See if we already have the entity in the session cache
311        _entity = (CampaignSourceDetail)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<CampaignSourceDetailPK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params)
354            throws PersistenceDatabaseException {
355        Set<CampaignSourceDetailPK> _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<CampaignSourceDetailPK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params)
382            throws PersistenceDatabaseException {
383        java.util.List<CampaignSourceDetailPK> _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 CampaignSourceDetailPK getPKFromQuery(PreparedStatement ps, final Object... params)
410            throws PersistenceDatabaseException {
411        CampaignSourceDetailPK _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<CampaignSourceDetail> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
440            throws PersistenceDatabaseException {
441        PreparedStatement ps = session.prepareStatement(CampaignSourceDetailFactory.class, queryMap.get(entityPermission));
442        
443        return getEntitiesFromQuery(session, entityPermission, ps, params);
444    }
445    
446    public java.util.List<CampaignSourceDetail> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
447            throws PersistenceDatabaseException {
448        Session session = ThreadSession.currentSession();
449        PreparedStatement ps = session.prepareStatement(CampaignSourceDetailFactory.class, queryMap.get(entityPermission));
450        
451        return getEntitiesFromQuery(session, entityPermission, ps, params);
452    }
453    
454    public java.util.List<CampaignSourceDetail> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
455            throws PersistenceDatabaseException {
456        PreparedStatement ps = session.prepareStatement(CampaignSourceDetailFactory.class, queryMap.get(entityPermission));
457        
458        return getEntitiesFromQuery(session, entityPermission, ps);
459    }
460    
461    public java.util.List<CampaignSourceDetail> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
462            throws PersistenceDatabaseException {
463        Session session = ThreadSession.currentSession();
464        PreparedStatement ps = session.prepareStatement(CampaignSourceDetailFactory.class, queryMap.get(entityPermission));
465        
466        return getEntitiesFromQuery(session, entityPermission, ps);
467    }
468    
469    public java.util.List<CampaignSourceDetail> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps)
470            throws PersistenceDatabaseException {
471        return getEntitiesFromQuery(ThreadSession.currentSession(), entityPermission, ps);
472    }
473    
474    public java.util.List<CampaignSourceDetail> 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<CampaignSourceDetail> getEntitiesFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params)
480            throws PersistenceDatabaseException {
481        java.util.List<CampaignSourceDetail> _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 CampaignSourceDetail getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
508            throws PersistenceDatabaseException {
509        PreparedStatement ps = session.prepareStatement(CampaignSourceDetailFactory.class, queryMap.get(entityPermission));
510        
511        return getEntityFromQuery(session, entityPermission, ps, params);
512    }
513    
514    public CampaignSourceDetail getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
515            throws PersistenceDatabaseException {
516        Session session = ThreadSession.currentSession();
517        PreparedStatement ps = session.prepareStatement(CampaignSourceDetailFactory.class, queryMap.get(entityPermission));
518        
519        return getEntityFromQuery(session, entityPermission, ps, params);
520    }
521    
522    public CampaignSourceDetail getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
523            throws PersistenceDatabaseException {
524        PreparedStatement ps = session.prepareStatement(CampaignSourceDetailFactory.class, queryMap.get(entityPermission));
525        
526        return getEntityFromQuery(session, entityPermission, ps);
527    }
528    
529    public CampaignSourceDetail getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
530            throws PersistenceDatabaseException {
531        Session session = ThreadSession.currentSession();
532        PreparedStatement ps = session.prepareStatement(CampaignSourceDetailFactory.class, queryMap.get(entityPermission));
533        
534        return getEntityFromQuery(session, entityPermission, ps);
535    }
536    
537    public CampaignSourceDetail getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps)
538            throws PersistenceDatabaseException {
539        return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps);
540    }
541    
542    public CampaignSourceDetail getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
543            throws PersistenceDatabaseException {
544        return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps, params);
545    }
546    
547    public CampaignSourceDetail getEntityFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params)
548            throws PersistenceDatabaseException {
549        CampaignSourceDetail _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<CampaignSourceDetail> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs)
578            throws PersistenceDatabaseException {
579        return getEntitiesFromResultSet(ThreadSession.currentSession(), entityPermission, rs);
580    }
581    
582    public java.util.List<CampaignSourceDetail> getEntitiesFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs)
583            throws PersistenceDatabaseException {
584        java.util.List<CampaignSourceDetail> _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 CampaignSourceDetail getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs)
598            throws PersistenceDatabaseException {
599        return getEntityFromResultSet(ThreadSession.currentSession(), entityPermission, rs);
600    }
601    
602    public CampaignSourceDetail getEntityFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs)
603            throws PersistenceDatabaseException {
604        CampaignSourceDetail _entity;
605        
606        try {
607            Long cmpgnsrcdt_campaignsourcedetailid = rs.getLong(CMPGNSRCDT_CAMPAIGNSOURCEDETAILID);
608            CampaignSourceDetailPK _pk = new CampaignSourceDetailPK(cmpgnsrcdt_campaignsourcedetailid);
609            
610            // See if we already have the entity in the session cache
611            _entity = (CampaignSourceDetail)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 cmpgnsrcdt_cmpgnsrc_campaignsourceid = rs.getLong(CMPGNSRCDT_CMPGNSRC_CAMPAIGNSOURCEID);
629                if(rs.wasNull())
630                    cmpgnsrcdt_cmpgnsrc_campaignsourceid = null;
631                
632                String cmpgnsrcdt_campaignsourcename = rs.getString(CMPGNSRCDT_CAMPAIGNSOURCENAME);
633                if(rs.wasNull())
634                    cmpgnsrcdt_campaignsourcename = null;
635                
636                String cmpgnsrcdt_valuesha1hash = rs.getString(CMPGNSRCDT_VALUESHA1HASH);
637                if(rs.wasNull())
638                    cmpgnsrcdt_valuesha1hash = null;
639                
640                Clob cmpgnsrcdt_value = rs.getClob(CMPGNSRCDT_VALUE);
641                if(rs.wasNull())
642                    cmpgnsrcdt_value = null;
643                
644                Boolean cmpgnsrcdt_isdefault = rs.getInt(CMPGNSRCDT_ISDEFAULT) == 1;
645                if(rs.wasNull())
646                    cmpgnsrcdt_isdefault = null;
647                
648                Integer cmpgnsrcdt_sortorder = rs.getInt(CMPGNSRCDT_SORTORDER);
649                if(rs.wasNull())
650                    cmpgnsrcdt_sortorder = null;
651                
652                Long cmpgnsrcdt_fromtime = rs.getLong(CMPGNSRCDT_FROMTIME);
653                if(rs.wasNull())
654                    cmpgnsrcdt_fromtime = null;
655                
656                Long cmpgnsrcdt_thrutime = rs.getLong(CMPGNSRCDT_THRUTIME);
657                if(rs.wasNull())
658                    cmpgnsrcdt_thrutime = null;
659                
660                CampaignSourceDetailValue _value = new CampaignSourceDetailValue(_pk, cmpgnsrcdt_cmpgnsrc_campaignsourceid == null? null: new CampaignSourcePK(cmpgnsrcdt_cmpgnsrc_campaignsourceid), cmpgnsrcdt_campaignsourcename, cmpgnsrcdt_valuesha1hash, cmpgnsrcdt_value == null? null: cmpgnsrcdt_value.getSubString(1L, (int)cmpgnsrcdt_value.length()), cmpgnsrcdt_isdefault, cmpgnsrcdt_sortorder, cmpgnsrcdt_fromtime, cmpgnsrcdt_thrutime);
661                _entity = new CampaignSourceDetail(_value, entityPermission);
662            }
663            
664            if(!foundInSessionCache) {
665                if(entityPermission.equals(EntityPermission.READ_ONLY)) {
666                    session.putReadOnlyEntity(_pk, _entity);
667                    session.getValueCache().put(_entity.getCampaignSourceDetailValue());
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 CampaignSourceDetail create(Session session, CampaignSource campaignSource, String campaignSourceName, String valueSha1Hash, String value, Boolean isDefault, Integer sortOrder, Long fromTime, Long thruTime)
680            throws PersistenceDatabaseException, PersistenceNotNullException {
681        return create(session, campaignSource == null ? null : campaignSource.getPrimaryKey(), campaignSourceName, valueSha1Hash, value, isDefault, sortOrder, fromTime, thruTime);
682    }
683    
684    public CampaignSourceDetail create(CampaignSource campaignSource, String campaignSourceName, String valueSha1Hash, String value, Boolean isDefault, Integer sortOrder, Long fromTime, Long thruTime)
685            throws PersistenceDatabaseException, PersistenceNotNullException {
686        return create(ThreadSession.currentSession(), campaignSource == null ? null : campaignSource.getPrimaryKey(), campaignSourceName, valueSha1Hash, value, isDefault, sortOrder, fromTime, thruTime);
687    }
688    
689    private void bindForCreate(PreparedStatement _ps, CampaignSourceDetailValue _value)
690            throws SQLException {
691        _ps.setLong(1, _value.getEntityId());
692        
693        CampaignSourcePK cmpgnsrcdt_cmpgnsrc_campaignsourceid = _value.getCampaignSourcePK();
694        if(cmpgnsrcdt_cmpgnsrc_campaignsourceid == null)
695            _ps.setNull(2, Types.BIGINT);
696        else
697            _ps.setLong(2, cmpgnsrcdt_cmpgnsrc_campaignsourceid.getEntityId());
698            
699        String cmpgnsrcdt_campaignsourcename = _value.getCampaignSourceName();
700        if(cmpgnsrcdt_campaignsourcename == null)
701            _ps.setNull(3, Types.VARCHAR);
702        else
703            _ps.setString(3, cmpgnsrcdt_campaignsourcename);
704            
705        String cmpgnsrcdt_valuesha1hash = _value.getValueSha1Hash();
706        if(cmpgnsrcdt_valuesha1hash == null)
707            _ps.setNull(4, Types.VARCHAR);
708        else
709            _ps.setString(4, cmpgnsrcdt_valuesha1hash);
710            
711        String cmpgnsrcdt_value = _value.getValue();
712        if(cmpgnsrcdt_value == null)
713            _ps.setNull(5, Types.CLOB);
714        else
715            _ps.setCharacterStream(5, new StringReader(cmpgnsrcdt_value), cmpgnsrcdt_value.length());
716            
717        Boolean cmpgnsrcdt_isdefault = _value.getIsDefault();
718        if(cmpgnsrcdt_isdefault == null)
719            _ps.setNull(6, Types.BIT);
720        else
721            _ps.setInt(6, cmpgnsrcdt_isdefault? 1: 0);
722            
723        Integer cmpgnsrcdt_sortorder = _value.getSortOrder();
724        if(cmpgnsrcdt_sortorder == null)
725            _ps.setNull(7, Types.INTEGER);
726        else
727            _ps.setInt(7, cmpgnsrcdt_sortorder);
728            
729        Long cmpgnsrcdt_fromtime = _value.getFromTime();
730        if(cmpgnsrcdt_fromtime == null)
731            _ps.setNull(8, Types.BIGINT);
732        else
733            _ps.setLong(8, cmpgnsrcdt_fromtime);
734            
735        Long cmpgnsrcdt_thrutime = _value.getThruTime();
736        if(cmpgnsrcdt_thrutime == null)
737            _ps.setNull(9, Types.BIGINT);
738        else
739            _ps.setLong(9, cmpgnsrcdt_thrutime);
740            
741    }
742    
743    public CampaignSourceDetail create(Session session, CampaignSourcePK campaignSourcePK, String campaignSourceName, String valueSha1Hash, String value, Boolean isDefault, Integer sortOrder, Long fromTime, Long thruTime)
744            throws PersistenceDatabaseException, PersistenceNotNullException {
745        CampaignSourceDetailPK _pk = getNextPK();
746        CampaignSourceDetailValue _value = new CampaignSourceDetailValue(_pk, campaignSourcePK, campaignSourceName, 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        CampaignSourceDetail _entity = new CampaignSourceDetail(_value, EntityPermission.READ_ONLY);
769        session.putReadOnlyEntity(_pk, _entity);
770        
771        return _entity;
772    }
773    
774    public CampaignSourceDetail create(CampaignSourcePK campaignSourcePK, String campaignSourceName, String valueSha1Hash, String value, Boolean isDefault, Integer sortOrder, Long fromTime, Long thruTime)
775            throws PersistenceDatabaseException, PersistenceNotNullException {
776        return create(ThreadSession.currentSession(), campaignSourcePK, campaignSourceName, valueSha1Hash, value, isDefault, sortOrder, fromTime, thruTime);
777    }
778    
779    public void create(Session session, Collection<CampaignSourceDetailValue> _values)
780            throws PersistenceDatabaseException, PersistenceNotNullException {
781        int _size = _values.size();
782        
783        if(_size > 0) {
784            PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
785            List<CampaignSourceDetailValue> _cacheValues = new ArrayList<>(_size);
786            
787            try {
788                for(CampaignSourceDetailValue _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                CampaignSourceDetail _cacheEntity = new CampaignSourceDetail(_cacheValue, EntityPermission.READ_ONLY);
816                
817                session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity);
818            });
819        }
820    }
821    
822    public void create(Collection<CampaignSourceDetailValue> _values)
823            throws PersistenceDatabaseException, PersistenceNotNullException {
824        create(ThreadSession.currentSession(), _values);
825    }
826    
827    private boolean bindForStore(PreparedStatement _ps, CampaignSourceDetailValue _value)
828            throws SQLException {
829        boolean _hasBeenModified = _value.hasBeenModified();
830        
831        if(_hasBeenModified) {
832            CampaignSourcePK cmpgnsrcdt_cmpgnsrc_campaignsourceid = _value.getCampaignSourcePK();
833            if(cmpgnsrcdt_cmpgnsrc_campaignsourceid == null)
834                _ps.setNull(1, Types.BIGINT);
835            else
836                _ps.setLong(1, cmpgnsrcdt_cmpgnsrc_campaignsourceid.getEntityId());
837            
838            String cmpgnsrcdt_campaignsourcename = _value.getCampaignSourceName();
839            if(cmpgnsrcdt_campaignsourcename == null)
840                _ps.setNull(2, Types.VARCHAR);
841            else
842                _ps.setString(2, cmpgnsrcdt_campaignsourcename);
843            
844            String cmpgnsrcdt_valuesha1hash = _value.getValueSha1Hash();
845            if(cmpgnsrcdt_valuesha1hash == null)
846                _ps.setNull(3, Types.VARCHAR);
847            else
848                _ps.setString(3, cmpgnsrcdt_valuesha1hash);
849            
850            String cmpgnsrcdt_value = _value.getValue();
851            if(cmpgnsrcdt_value == null)
852                _ps.setNull(4, Types.CLOB);
853            else
854                _ps.setCharacterStream(4, new StringReader(cmpgnsrcdt_value), cmpgnsrcdt_value.length());
855            
856            Boolean cmpgnsrcdt_isdefault = _value.getIsDefault();
857            if(cmpgnsrcdt_isdefault == null)
858                _ps.setNull(5, Types.BIT);
859            else
860                _ps.setInt(5, cmpgnsrcdt_isdefault? 1: 0);
861            
862            Integer cmpgnsrcdt_sortorder = _value.getSortOrder();
863            if(cmpgnsrcdt_sortorder == null)
864                _ps.setNull(6, Types.INTEGER);
865            else
866                _ps.setInt(6, cmpgnsrcdt_sortorder);
867            
868            Long cmpgnsrcdt_fromtime = _value.getFromTime();
869            if(cmpgnsrcdt_fromtime == null)
870                _ps.setNull(7, Types.BIGINT);
871            else
872                _ps.setLong(7, cmpgnsrcdt_fromtime);
873            
874            Long cmpgnsrcdt_thrutime = _value.getThruTime();
875            if(cmpgnsrcdt_thrutime == null)
876                _ps.setNull(8, Types.BIGINT);
877            else
878                _ps.setLong(8, cmpgnsrcdt_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, CampaignSourceDetail entity)
890            throws PersistenceDatabaseException {
891        PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
892        
893        try {
894            CampaignSourceDetailValue _value = entity.getCampaignSourceDetailValue();
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<CampaignSourceDetail> 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(CampaignSourceDetail entity : entities) {
923                    if(bindForStore(_ps, entity.getCampaignSourceDetailValue())) {
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.getCampaignSourceDetailValue());
946                    });
947                }
948            } catch (SQLException se) {
949                throw new PersistenceDatabaseException(se);
950            }
951        }
952    }
953    
954    @Override
955    public void store(Collection<CampaignSourceDetail> entities)
956            throws PersistenceDatabaseException {
957        store(ThreadSession.currentSession(), entities);
958    }
959    
960    @Override
961    public void remove(Session session, CampaignSourceDetail entity)
962            throws PersistenceDatabaseException {
963        remove(session, entity.getPrimaryKey());
964    }
965    
966    @Override
967    public void remove(Session session, CampaignSourceDetailPK 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<CampaignSourceDetailPK> 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(CampaignSourceDetailPK 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<CampaignSourceDetailPK> pks)
1041            throws PersistenceDatabaseException {
1042        remove(ThreadSession.currentSession(), pks);
1043    }
1044    
1045    @Override
1046    public boolean validPK(Session session, CampaignSourceDetailPK 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}