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