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