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