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