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