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