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