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