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