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