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