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