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