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