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