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