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