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