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