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