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