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 * PersonFactory.java 021 */ 022 023package com.echothree.model.data.party.server.factory; 024 025import com.echothree.model.data.party.common.pk.PartyPK; 026import com.echothree.model.data.party.common.pk.PersonalTitlePK; 027import com.echothree.model.data.party.common.pk.NameSuffixPK; 028 029import com.echothree.model.data.party.server.entity.Party; 030import com.echothree.model.data.party.server.entity.PersonalTitle; 031import com.echothree.model.data.party.server.entity.NameSuffix; 032 033import com.echothree.model.data.party.common.PersonConstants; 034import com.echothree.model.data.party.common.pk.PersonPK; 035import com.echothree.model.data.party.server.value.PersonValue; 036import com.echothree.model.data.party.server.entity.Person; 037import com.echothree.util.common.exception.PersistenceDatabaseException; 038import com.echothree.util.common.exception.PersistenceDatabaseUpdateException; 039import com.echothree.util.common.exception.PersistenceNotNullException; 040import com.echothree.util.server.persistence.BaseFactory; 041import com.echothree.util.server.persistence.EntityIdGenerator; 042import com.echothree.util.server.persistence.EntityPermission; 043import com.echothree.util.server.persistence.PersistenceDebugFlags; 044import com.echothree.util.server.persistence.Session; 045import java.sql.PreparedStatement; 046import java.sql.ResultSet; 047import java.sql.SQLException; 048import java.sql.Types; 049import java.io.ByteArrayInputStream; 050import java.io.StringReader; 051import java.util.ArrayList; 052import java.util.Collection; 053import java.util.HashSet; 054import java.util.List; 055import java.util.Map; 056import java.util.Set; 057import javax.enterprise.context.ApplicationScoped; 058import javax.enterprise.inject.spi.CDI; 059import javax.inject.Inject; 060import org.slf4j.Logger; 061import org.slf4j.LoggerFactory; 062 063@ApplicationScoped 064public class PersonFactory 065 implements BaseFactory<PersonPK, Person> { 066 067 @Inject 068 Session session; 069 070 //private static final Logger log = LoggerFactory.getLogger(PersonFactory.class); 071 072 final private static String SQL_SELECT_READ_ONLY = "SELECT peop_personid, peop_par_partyid, peop_pert_personaltitleid, peop_firstname, peop_firstnamesdx, peop_middlename, peop_middlenamesdx, peop_lastname, peop_lastnamesdx, peop_nsfx_namesuffixid, peop_fromtime, peop_thrutime FROM people WHERE peop_personid = ?"; 073 final private static String SQL_SELECT_READ_WRITE = "SELECT peop_personid, peop_par_partyid, peop_pert_personaltitleid, peop_firstname, peop_firstnamesdx, peop_middlename, peop_middlenamesdx, peop_lastname, peop_lastnamesdx, peop_nsfx_namesuffixid, peop_fromtime, peop_thrutime FROM people WHERE peop_personid = ? FOR UPDATE"; 074 final private static String SQL_INSERT = "INSERT INTO people (peop_personid, peop_par_partyid, peop_pert_personaltitleid, peop_firstname, peop_firstnamesdx, peop_middlename, peop_middlenamesdx, peop_lastname, peop_lastnamesdx, peop_nsfx_namesuffixid, peop_fromtime, peop_thrutime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 075 final private static String SQL_UPDATE = "UPDATE people SET peop_par_partyid = ?, peop_pert_personaltitleid = ?, peop_firstname = ?, peop_firstnamesdx = ?, peop_middlename = ?, peop_middlenamesdx = ?, peop_lastname = ?, peop_lastnamesdx = ?, peop_nsfx_namesuffixid = ?, peop_fromtime = ?, peop_thrutime = ? WHERE peop_personid = ?"; 076 final private static String SQL_DELETE = "DELETE FROM people WHERE peop_personid = ?"; 077 final private static String SQL_VALID = "SELECT COUNT(*) FROM people WHERE peop_personid = ?"; 078 079 final private static String PK_COLUMN = "peop_personid"; 080 final private static String ALL_COLUMNS = "peop_personid, peop_par_partyid, peop_pert_personaltitleid, peop_firstname, peop_firstnamesdx, peop_middlename, peop_middlenamesdx, peop_lastname, peop_lastnamesdx, peop_nsfx_namesuffixid, peop_fromtime, peop_thrutime"; 081 final public static String TABLE_NAME = "people"; 082 083 final public static String PEOP_PERSONID = "peop_personid"; 084 final public static String PEOP_PAR_PARTYID = "peop_par_partyid"; 085 final public static String PEOP_PERT_PERSONALTITLEID = "peop_pert_personaltitleid"; 086 final public static String PEOP_FIRSTNAME = "peop_firstname"; 087 final public static String PEOP_FIRSTNAMESDX = "peop_firstnamesdx"; 088 final public static String PEOP_MIDDLENAME = "peop_middlename"; 089 final public static String PEOP_MIDDLENAMESDX = "peop_middlenamesdx"; 090 final public static String PEOP_LASTNAME = "peop_lastname"; 091 final public static String PEOP_LASTNAMESDX = "peop_lastnamesdx"; 092 final public static String PEOP_NSFX_NAMESUFFIXID = "peop_nsfx_namesuffixid"; 093 final public static String PEOP_FROMTIME = "peop_fromtime"; 094 final public static String PEOP_THRUTIME = "peop_thrutime"; 095 096 final private static EntityIdGenerator entityIdGenerator = new EntityIdGenerator(PersonConstants.COMPONENT_VENDOR_NAME, PersonConstants.ENTITY_TYPE_NAME); 097 098 /** Creates a new instance of PersonFactory */ 099 protected PersonFactory() { 100 super(); 101 } 102 103 public static PersonFactory getInstance() { 104 return CDI.current().select(PersonFactory.class).get(); 105 } 106 107 @Override 108 public String getPKColumn() { 109 return PK_COLUMN; 110 } 111 112 @Override 113 public String getAllColumns() { 114 return ALL_COLUMNS; 115 } 116 117 @Override 118 public String getTableName() { 119 return TABLE_NAME; 120 } 121 122 @Override 123 public String getComponentVendorName() { 124 return PersonConstants.COMPONENT_VENDOR_NAME; 125 } 126 127 @Override 128 public String getEntityTypeName() { 129 return PersonConstants.ENTITY_TYPE_NAME; 130 } 131 132 public PreparedStatement prepareStatement(String query) { 133 return session.prepareStatement(PersonFactory.class, query); 134 } 135 136 public PersonPK getNextPK() { 137 return new PersonPK(entityIdGenerator.getNextEntityId()); 138 } 139 140 public Set<PersonPK> getPKsFromResultSetAsSet(ResultSet rs) 141 throws PersistenceDatabaseException { 142 Set<PersonPK> _result = new HashSet<>(); 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 java.util.List<PersonPK> getPKsFromResultSetAsList(ResultSet rs) 156 throws PersistenceDatabaseException { 157 java.util.List<PersonPK> _result = new ArrayList<>(); 158 159 try { 160 while(rs.next()) { 161 _result.add(getPKFromResultSet(rs)); 162 } 163 } catch (SQLException se) { 164 throw new PersistenceDatabaseException(se); 165 } 166 167 return _result; 168 } 169 170 public PersonPK getPKFromResultSet(ResultSet rs) 171 throws PersistenceDatabaseException { 172 PersonPK _result; 173 174 try { 175 long peop_personid = rs.getLong(PEOP_PERSONID); 176 Long _entityId = rs.wasNull() ? null : peop_personid; 177 178 _result = new PersonPK(_entityId); 179 } catch (SQLException se) { 180 throw new PersistenceDatabaseException(se); 181 } 182 183 return _result; 184 } 185 186 public java.util.List<PersonValue> getValuesFromPKs(Collection<PersonPK> pks) 187 throws PersistenceDatabaseException { 188 java.util.List<PersonValue> _values = new ArrayList<>(pks.size()); 189 190 for(PersonPK _pk: pks) { 191 _values.add(getValueFromPK(_pk)); 192 } 193 194 return _values; 195 } 196 197 public PersonValue getValueFromPK(PersonPK pk) 198 throws PersistenceDatabaseException { 199 PersonValue _value; 200 201 // See if we already have the entity in the session cache 202 Person _entity = (Person)session.getEntity(pk); 203 if(_entity == null) 204 _value = getEntityFromPK(EntityPermission.READ_ONLY, pk).getPersonValue(); 205 else 206 _value = _entity.getPersonValue(); 207 208 return _value; 209 } 210 211 public java.util.List<PersonValue> getValuesFromResultSet(ResultSet rs) 212 throws PersistenceDatabaseException { 213 java.util.List<PersonValue> _result = new ArrayList<>(); 214 215 try { 216 while(rs.next()) { 217 _result.add(getValueFromResultSet(rs)); 218 } 219 } catch (SQLException se) { 220 throw new PersistenceDatabaseException(se); 221 } 222 223 return _result; 224 } 225 226 public PersonValue getValueFromResultSet(ResultSet rs) 227 throws PersistenceDatabaseException { 228 PersonValue _value; 229 230 try { 231 Long peop_personid = rs.getLong(PEOP_PERSONID); 232 PersonPK _pk = new PersonPK(peop_personid); 233 234 // See if we already have the entity in the session cache 235 Person _entity = (Person)session.getEntity(_pk); 236 237 if(_entity == null) { 238 Long peop_par_partyid = rs.getLong(PEOP_PAR_PARTYID); 239 if(rs.wasNull()) 240 peop_par_partyid = null; 241 242 Long peop_pert_personaltitleid = rs.getLong(PEOP_PERT_PERSONALTITLEID); 243 if(rs.wasNull()) 244 peop_pert_personaltitleid = null; 245 246 String peop_firstname = rs.getString(PEOP_FIRSTNAME); 247 if(rs.wasNull()) 248 peop_firstname = null; 249 250 String peop_firstnamesdx = rs.getString(PEOP_FIRSTNAMESDX); 251 if(rs.wasNull()) 252 peop_firstnamesdx = null; 253 254 String peop_middlename = rs.getString(PEOP_MIDDLENAME); 255 if(rs.wasNull()) 256 peop_middlename = null; 257 258 String peop_middlenamesdx = rs.getString(PEOP_MIDDLENAMESDX); 259 if(rs.wasNull()) 260 peop_middlenamesdx = null; 261 262 String peop_lastname = rs.getString(PEOP_LASTNAME); 263 if(rs.wasNull()) 264 peop_lastname = null; 265 266 String peop_lastnamesdx = rs.getString(PEOP_LASTNAMESDX); 267 if(rs.wasNull()) 268 peop_lastnamesdx = null; 269 270 Long peop_nsfx_namesuffixid = rs.getLong(PEOP_NSFX_NAMESUFFIXID); 271 if(rs.wasNull()) 272 peop_nsfx_namesuffixid = null; 273 274 Long peop_fromtime = rs.getLong(PEOP_FROMTIME); 275 if(rs.wasNull()) 276 peop_fromtime = null; 277 278 Long peop_thrutime = rs.getLong(PEOP_THRUTIME); 279 if(rs.wasNull()) 280 peop_thrutime = null; 281 282 _value = new PersonValue(_pk, new PartyPK(peop_par_partyid), new PersonalTitlePK(peop_pert_personaltitleid), peop_firstname, peop_firstnamesdx, peop_middlename, peop_middlenamesdx, peop_lastname, peop_lastnamesdx, new NameSuffixPK(peop_nsfx_namesuffixid), peop_fromtime, peop_thrutime); 283 } else 284 _value = _entity.getPersonValue(); 285 } catch (SQLException se) { 286 throw new PersistenceDatabaseException(se); 287 } 288 289 return _value; 290 } 291 292 public java.util.List<Person> getEntitiesFromPKs(EntityPermission entityPermission, Collection<PersonPK> pks) 293 throws PersistenceDatabaseException { 294 java.util.List<Person> _entities = new ArrayList<>(pks.size()); 295 296 for(PersonPK _pk: pks) { 297 _entities.add(getEntityFromPK(entityPermission, _pk)); 298 } 299 300 return _entities; 301 } 302 303 public Person getEntityFromValue(EntityPermission entityPermission, PersonValue value) { 304 return getEntityFromPK(entityPermission, value.getPrimaryKey()); 305 } 306 307 public Person getEntityFromCache(PersonPK pk) { 308 PersonValue _value = (PersonValue)session.getValueCache().get(pk); 309 310 return _value == null ? null : new Person(_value, EntityPermission.READ_ONLY); 311 } 312 313 public Person getEntityFromPK(EntityPermission entityPermission, PersonPK pk) 314 throws PersistenceDatabaseException { 315 Person _entity; 316 317 // See if we already have the entity in the session cache 318 _entity = (Person)session.getEntity(pk); 319 if(_entity != null) { 320 // If the requested permission is READ_WRITE, and the cached permission is 321 // READ_ONLY, then pretend that the cached object wasn't found, and create 322 // a new entity that is READ_WRITE. 323 if(entityPermission.equals(EntityPermission.READ_WRITE)) { 324 if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY)) 325 _entity = null; 326 } 327 } 328 329 if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) { 330 _entity = getEntityFromCache(pk); 331 } 332 333 if(_entity == null) { 334 PreparedStatement _ps = session.prepareStatement(entityPermission.equals(EntityPermission.READ_ONLY)? SQL_SELECT_READ_ONLY: SQL_SELECT_READ_WRITE); 335 long _entityId = pk.getEntityId(); 336 ResultSet _rs = null; 337 338 try { 339 _ps.setLong(1, _entityId); 340 _rs = _ps.executeQuery(); 341 if(_rs.next()) { 342 _entity = getEntityFromResultSet(entityPermission, _rs); 343 } 344 } catch (SQLException se) { 345 throw new PersistenceDatabaseException(se); 346 } finally { 347 if(_rs != null) { 348 try { 349 _rs.close(); 350 } catch (SQLException se) { 351 // do nothing 352 } 353 } 354 } 355 } 356 357 return _entity; 358 } 359 360 public Set<PersonPK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params) 361 throws PersistenceDatabaseException { 362 Set<PersonPK> _pks; 363 ResultSet _rs = null; 364 365 try { 366 if(params.length != 0) { 367 Session.setQueryParams(ps, params); 368 } 369 370 _rs = ps.executeQuery(); 371 _pks = getPKsFromResultSetAsSet(_rs); 372 _rs.close(); 373 } catch (SQLException se) { 374 throw new PersistenceDatabaseException(se); 375 } finally { 376 if(_rs != null) { 377 try { 378 _rs.close(); 379 } catch (SQLException se) { 380 // do nothing 381 } 382 } 383 } 384 385 return _pks; 386 } 387 388 public java.util.List<PersonPK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params) 389 throws PersistenceDatabaseException { 390 java.util.List<PersonPK> _pks; 391 ResultSet _rs = null; 392 393 try { 394 if(params.length != 0) { 395 Session.setQueryParams(ps, params); 396 } 397 398 _rs = ps.executeQuery(); 399 _pks = getPKsFromResultSetAsList(_rs); 400 _rs.close(); 401 } catch (SQLException se) { 402 throw new PersistenceDatabaseException(se); 403 } finally { 404 if(_rs != null) { 405 try { 406 _rs.close(); 407 } catch (SQLException se) { 408 // do nothing 409 } 410 } 411 } 412 413 return _pks; 414 } 415 416 public PersonPK getPKFromQuery(PreparedStatement ps, final Object... params) 417 throws PersistenceDatabaseException { 418 PersonPK _pk = null; 419 ResultSet _rs = null; 420 421 try { 422 if(params.length != 0) { 423 Session.setQueryParams(ps, params); 424 } 425 426 _rs = ps.executeQuery(); 427 if(_rs.next()) { 428 _pk = getPKFromResultSet(_rs); 429 } 430 _rs.close(); 431 } catch (SQLException se) { 432 throw new PersistenceDatabaseException(se); 433 } finally { 434 if(_rs != null) { 435 try { 436 _rs.close(); 437 } catch (SQLException se) { 438 // do nothing 439 } 440 } 441 } 442 443 return _pk; 444 } 445 446 public java.util.List<Person> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params) 447 throws PersistenceDatabaseException { 448 PreparedStatement ps = session.prepareStatement(PersonFactory.class, queryMap.get(entityPermission)); 449 450 return getEntitiesFromQuery(entityPermission, ps, params); 451 } 452 453 public java.util.List<Person> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap) 454 throws PersistenceDatabaseException { 455 PreparedStatement ps = session.prepareStatement(PersonFactory.class, queryMap.get(entityPermission)); 456 457 return getEntitiesFromQuery(entityPermission, ps); 458 } 459 460 public java.util.List<Person> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params) 461 throws PersistenceDatabaseException { 462 java.util.List<Person> _entities; 463 ResultSet _rs = null; 464 465 try { 466 if(params.length != 0) { 467 Session.setQueryParams(ps, params); 468 } 469 470 _rs = ps.executeQuery(); 471 _entities = getEntitiesFromResultSet(entityPermission, _rs); 472 _rs.close(); 473 } catch (SQLException se) { 474 throw new PersistenceDatabaseException(se); 475 } finally { 476 if(_rs != null) { 477 try { 478 _rs.close(); 479 } catch (SQLException se) { 480 // do nothing 481 } 482 } 483 } 484 485 return _entities; 486 } 487 488 public Person getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params) 489 throws PersistenceDatabaseException { 490 PreparedStatement ps = session.prepareStatement(PersonFactory.class, queryMap.get(entityPermission)); 491 492 return getEntityFromQuery(entityPermission, ps, params); 493 } 494 495 public Person getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap) 496 throws PersistenceDatabaseException { 497 PreparedStatement ps = session.prepareStatement(PersonFactory.class, queryMap.get(entityPermission)); 498 499 return getEntityFromQuery(entityPermission, ps); 500 } 501 502 public Person getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params) 503 throws PersistenceDatabaseException { 504 Person _entity = null; 505 ResultSet _rs = null; 506 507 try { 508 if(params.length != 0) { 509 Session.setQueryParams(ps, params); 510 } 511 512 _rs = ps.executeQuery(); 513 if(_rs.next()) { 514 _entity = getEntityFromResultSet(entityPermission, _rs); 515 } 516 _rs.close(); 517 } catch (SQLException se) { 518 throw new PersistenceDatabaseException(se); 519 } finally { 520 if(_rs != null) { 521 try { 522 _rs.close(); 523 } catch (SQLException se) { 524 // do nothing 525 } 526 } 527 } 528 529 return _entity; 530 } 531 532 public java.util.List<Person> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs) 533 throws PersistenceDatabaseException { 534 java.util.List<Person> _result = new ArrayList<>(); 535 536 try { 537 while(rs.next()) { 538 _result.add(getEntityFromResultSet(entityPermission, rs)); 539 } 540 } catch (SQLException se) { 541 throw new PersistenceDatabaseException(se); 542 } 543 544 return _result; 545 } 546 547 public Person getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs) 548 throws PersistenceDatabaseException { 549 Person _entity; 550 551 try { 552 Long peop_personid = rs.getLong(PEOP_PERSONID); 553 PersonPK _pk = new PersonPK(peop_personid); 554 555 // See if we already have the entity in the session cache 556 _entity = (Person)session.getEntity(_pk); 557 if(_entity != null) { 558 // If the requested permission is READ_WRITE, and the cached permission is 559 // READ_ONLY, then pretend that the cached object wasn't found, and create 560 // a new entity that is READ_WRITE. 561 if(entityPermission.equals(EntityPermission.READ_WRITE)) { 562 if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY)) 563 _entity = null; 564 } 565 } 566 boolean foundInSessionCache = _entity != null; 567 568 if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) { 569 _entity = getEntityFromCache(_pk); 570 } 571 572 if(_entity == null) { 573 Long peop_par_partyid = rs.getLong(PEOP_PAR_PARTYID); 574 if(rs.wasNull()) 575 peop_par_partyid = null; 576 577 Long peop_pert_personaltitleid = rs.getLong(PEOP_PERT_PERSONALTITLEID); 578 if(rs.wasNull()) 579 peop_pert_personaltitleid = null; 580 581 String peop_firstname = rs.getString(PEOP_FIRSTNAME); 582 if(rs.wasNull()) 583 peop_firstname = null; 584 585 String peop_firstnamesdx = rs.getString(PEOP_FIRSTNAMESDX); 586 if(rs.wasNull()) 587 peop_firstnamesdx = null; 588 589 String peop_middlename = rs.getString(PEOP_MIDDLENAME); 590 if(rs.wasNull()) 591 peop_middlename = null; 592 593 String peop_middlenamesdx = rs.getString(PEOP_MIDDLENAMESDX); 594 if(rs.wasNull()) 595 peop_middlenamesdx = null; 596 597 String peop_lastname = rs.getString(PEOP_LASTNAME); 598 if(rs.wasNull()) 599 peop_lastname = null; 600 601 String peop_lastnamesdx = rs.getString(PEOP_LASTNAMESDX); 602 if(rs.wasNull()) 603 peop_lastnamesdx = null; 604 605 Long peop_nsfx_namesuffixid = rs.getLong(PEOP_NSFX_NAMESUFFIXID); 606 if(rs.wasNull()) 607 peop_nsfx_namesuffixid = null; 608 609 Long peop_fromtime = rs.getLong(PEOP_FROMTIME); 610 if(rs.wasNull()) 611 peop_fromtime = null; 612 613 Long peop_thrutime = rs.getLong(PEOP_THRUTIME); 614 if(rs.wasNull()) 615 peop_thrutime = null; 616 617 PersonValue _value = new PersonValue(_pk, peop_par_partyid == null? null: new PartyPK(peop_par_partyid), peop_pert_personaltitleid == null? null: new PersonalTitlePK(peop_pert_personaltitleid), peop_firstname, peop_firstnamesdx, peop_middlename, peop_middlenamesdx, peop_lastname, peop_lastnamesdx, peop_nsfx_namesuffixid == null? null: new NameSuffixPK(peop_nsfx_namesuffixid), peop_fromtime, peop_thrutime); 618 _entity = new Person(_value, entityPermission); 619 } 620 621 if(!foundInSessionCache) { 622 if(entityPermission.equals(EntityPermission.READ_ONLY)) { 623 session.putReadOnlyEntity(_pk, _entity); 624 session.getValueCache().put(_entity.getPersonValue()); 625 } else { 626 session.putReadWriteEntity(_pk, _entity); 627 } 628 } 629 } catch (SQLException se) { 630 throw new PersistenceDatabaseException(se); 631 } 632 633 return _entity; 634 } 635 636 public Person create(Party party, PersonalTitle personalTitle, String firstName, String firstNameSdx, String middleName, String middleNameSdx, String lastName, String lastNameSdx, NameSuffix nameSuffix, Long fromTime, Long thruTime) 637 throws PersistenceDatabaseException, PersistenceNotNullException { 638 return create(party == null ? null : party.getPrimaryKey(), personalTitle == null ? null : personalTitle.getPrimaryKey(), firstName, firstNameSdx, middleName, middleNameSdx, lastName, lastNameSdx, nameSuffix == null ? null : nameSuffix.getPrimaryKey(), fromTime, thruTime); 639 } 640 641 private void bindForCreate(PreparedStatement _ps, PersonValue _value) 642 throws SQLException { 643 _ps.setLong(1, _value.getEntityId()); 644 645 PartyPK peop_par_partyid = _value.getPartyPK(); 646 if(peop_par_partyid == null) 647 _ps.setNull(2, Types.BIGINT); 648 else 649 _ps.setLong(2, peop_par_partyid.getEntityId()); 650 651 PersonalTitlePK peop_pert_personaltitleid = _value.getPersonalTitlePK(); 652 if(peop_pert_personaltitleid == null) 653 _ps.setNull(3, Types.BIGINT); 654 else 655 _ps.setLong(3, peop_pert_personaltitleid.getEntityId()); 656 657 String peop_firstname = _value.getFirstName(); 658 if(peop_firstname == null) 659 _ps.setNull(4, Types.VARCHAR); 660 else 661 _ps.setString(4, peop_firstname); 662 663 String peop_firstnamesdx = _value.getFirstNameSdx(); 664 if(peop_firstnamesdx == null) 665 _ps.setNull(5, Types.VARCHAR); 666 else 667 _ps.setString(5, peop_firstnamesdx); 668 669 String peop_middlename = _value.getMiddleName(); 670 if(peop_middlename == null) 671 _ps.setNull(6, Types.VARCHAR); 672 else 673 _ps.setString(6, peop_middlename); 674 675 String peop_middlenamesdx = _value.getMiddleNameSdx(); 676 if(peop_middlenamesdx == null) 677 _ps.setNull(7, Types.VARCHAR); 678 else 679 _ps.setString(7, peop_middlenamesdx); 680 681 String peop_lastname = _value.getLastName(); 682 if(peop_lastname == null) 683 _ps.setNull(8, Types.VARCHAR); 684 else 685 _ps.setString(8, peop_lastname); 686 687 String peop_lastnamesdx = _value.getLastNameSdx(); 688 if(peop_lastnamesdx == null) 689 _ps.setNull(9, Types.VARCHAR); 690 else 691 _ps.setString(9, peop_lastnamesdx); 692 693 NameSuffixPK peop_nsfx_namesuffixid = _value.getNameSuffixPK(); 694 if(peop_nsfx_namesuffixid == null) 695 _ps.setNull(10, Types.BIGINT); 696 else 697 _ps.setLong(10, peop_nsfx_namesuffixid.getEntityId()); 698 699 Long peop_fromtime = _value.getFromTime(); 700 if(peop_fromtime == null) 701 _ps.setNull(11, Types.BIGINT); 702 else 703 _ps.setLong(11, peop_fromtime); 704 705 Long peop_thrutime = _value.getThruTime(); 706 if(peop_thrutime == null) 707 _ps.setNull(12, Types.BIGINT); 708 else 709 _ps.setLong(12, peop_thrutime); 710 711 } 712 713 public Person create(PartyPK partyPK, PersonalTitlePK personalTitlePK, String firstName, String firstNameSdx, String middleName, String middleNameSdx, String lastName, String lastNameSdx, NameSuffixPK nameSuffixPK, Long fromTime, Long thruTime) 714 throws PersistenceDatabaseException, PersistenceNotNullException { 715 PersonPK _pk = getNextPK(); 716 PersonValue _value = new PersonValue(_pk, partyPK, personalTitlePK, firstName, firstNameSdx, middleName, middleNameSdx, lastName, lastNameSdx, nameSuffixPK, fromTime, thruTime); 717 718 PreparedStatement _ps = session.prepareStatement(SQL_INSERT); 719 720 try { 721 bindForCreate(_ps, _value); 722 723 if(PersistenceDebugFlags.CheckEntityInsertRowCount) { 724 int _count = _ps.executeUpdate(); 725 726 if(_count != 1) { 727 throw new PersistenceDatabaseUpdateException("insert failed, _count = " + _count); 728 } 729 } else { 730 _ps.executeUpdate(); 731 } 732 733 session.getValueCache().put(_value); 734 } catch (SQLException se) { 735 throw new PersistenceDatabaseException(se); 736 } 737 738 Person _entity = new Person(_value, EntityPermission.READ_ONLY); 739 session.putReadOnlyEntity(_pk, _entity); 740 741 return _entity; 742 } 743 744 public void create(Collection<PersonValue> _values) 745 throws PersistenceDatabaseException, PersistenceNotNullException { 746 int _size = _values.size(); 747 748 if(_size > 0) { 749 PreparedStatement _ps = session.prepareStatement(SQL_INSERT); 750 List<PersonValue> _cacheValues = new ArrayList<>(_size); 751 752 try { 753 for(PersonValue _value : _values) { 754 _value.setEntityId(entityIdGenerator.getNextEntityId()); 755 bindForCreate(_ps, _value); 756 757 _ps.addBatch(); 758 759 _cacheValues.add(_value); 760 } 761 762 if(PersistenceDebugFlags.CheckEntityInsertRowCount) { 763 int[] _counts = _ps.executeBatch(); 764 765 for(int _countOffset = 0 ; _countOffset < _size ; _countOffset++) { 766 if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) { 767 throw new PersistenceDatabaseUpdateException("batch insert failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]); 768 } 769 } 770 } else { 771 _ps.executeBatch(); 772 } 773 774 _ps.clearBatch(); 775 } catch (SQLException se) { 776 throw new PersistenceDatabaseException(se); 777 } 778 779 _cacheValues.forEach((_cacheValue) -> { 780 Person _cacheEntity = new Person(_cacheValue, EntityPermission.READ_ONLY); 781 782 session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity); 783 }); 784 } 785 } 786 787 private boolean bindForStore(PreparedStatement _ps, PersonValue _value) 788 throws SQLException { 789 boolean _hasBeenModified = _value.hasBeenModified(); 790 791 if(_hasBeenModified) { 792 PartyPK peop_par_partyid = _value.getPartyPK(); 793 if(peop_par_partyid == null) 794 _ps.setNull(1, Types.BIGINT); 795 else 796 _ps.setLong(1, peop_par_partyid.getEntityId()); 797 798 PersonalTitlePK peop_pert_personaltitleid = _value.getPersonalTitlePK(); 799 if(peop_pert_personaltitleid == null) 800 _ps.setNull(2, Types.BIGINT); 801 else 802 _ps.setLong(2, peop_pert_personaltitleid.getEntityId()); 803 804 String peop_firstname = _value.getFirstName(); 805 if(peop_firstname == null) 806 _ps.setNull(3, Types.VARCHAR); 807 else 808 _ps.setString(3, peop_firstname); 809 810 String peop_firstnamesdx = _value.getFirstNameSdx(); 811 if(peop_firstnamesdx == null) 812 _ps.setNull(4, Types.VARCHAR); 813 else 814 _ps.setString(4, peop_firstnamesdx); 815 816 String peop_middlename = _value.getMiddleName(); 817 if(peop_middlename == null) 818 _ps.setNull(5, Types.VARCHAR); 819 else 820 _ps.setString(5, peop_middlename); 821 822 String peop_middlenamesdx = _value.getMiddleNameSdx(); 823 if(peop_middlenamesdx == null) 824 _ps.setNull(6, Types.VARCHAR); 825 else 826 _ps.setString(6, peop_middlenamesdx); 827 828 String peop_lastname = _value.getLastName(); 829 if(peop_lastname == null) 830 _ps.setNull(7, Types.VARCHAR); 831 else 832 _ps.setString(7, peop_lastname); 833 834 String peop_lastnamesdx = _value.getLastNameSdx(); 835 if(peop_lastnamesdx == null) 836 _ps.setNull(8, Types.VARCHAR); 837 else 838 _ps.setString(8, peop_lastnamesdx); 839 840 NameSuffixPK peop_nsfx_namesuffixid = _value.getNameSuffixPK(); 841 if(peop_nsfx_namesuffixid == null) 842 _ps.setNull(9, Types.BIGINT); 843 else 844 _ps.setLong(9, peop_nsfx_namesuffixid.getEntityId()); 845 846 Long peop_fromtime = _value.getFromTime(); 847 if(peop_fromtime == null) 848 _ps.setNull(10, Types.BIGINT); 849 else 850 _ps.setLong(10, peop_fromtime); 851 852 Long peop_thrutime = _value.getThruTime(); 853 if(peop_thrutime == null) 854 _ps.setNull(11, Types.BIGINT); 855 else 856 _ps.setLong(11, peop_thrutime); 857 858 _ps.setLong(12, _value.getPrimaryKey().getEntityId()); 859 860 _value.clearHasBeenModified(); 861 } 862 863 return _hasBeenModified; 864 } 865 866 @Override 867 public void store(Person entity) 868 throws PersistenceDatabaseException { 869 PreparedStatement _ps = session.prepareStatement(SQL_UPDATE); 870 871 try { 872 PersonValue _value = entity.getPersonValue(); 873 874 if(bindForStore(_ps, _value)) { 875 if(PersistenceDebugFlags.CheckEntityUpdateRowCount) { 876 int _count = _ps.executeUpdate(); 877 878 if(_count != 1) { 879 throw new PersistenceDatabaseUpdateException("update failed, _count = " + _count); 880 } 881 } else { 882 _ps.executeUpdate(); 883 } 884 885 session.getValueCache().put(_value); 886 } 887 } catch (SQLException se) { 888 throw new PersistenceDatabaseException(se); 889 } 890 } 891 892 @Override 893 public void store(Collection<Person> entities) 894 throws PersistenceDatabaseException { 895 if(entities.size() > 0) { 896 PreparedStatement _ps = session.prepareStatement(SQL_UPDATE); 897 int _modifiedEntities = 0; 898 899 try { 900 for(Person entity : entities) { 901 if(bindForStore(_ps, entity.getPersonValue())) { 902 _ps.addBatch(); 903 _modifiedEntities++; 904 } 905 } 906 907 if(_modifiedEntities != 0) { 908 if(PersistenceDebugFlags.CheckEntityUpdateRowCount) { 909 int[] _counts = _ps.executeBatch(); 910 911 for(int _countOffset = 0 ; _countOffset < _modifiedEntities ; _countOffset++) { 912 if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) { 913 throw new PersistenceDatabaseUpdateException("batch update failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]); 914 } 915 } 916 } else { 917 _ps.executeBatch(); 918 } 919 920 _ps.clearBatch(); 921 922 entities.forEach((entity) -> { 923 session.getValueCache().put(entity.getPersonValue()); 924 }); 925 } 926 } catch (SQLException se) { 927 throw new PersistenceDatabaseException(se); 928 } 929 } 930 } 931 932 @Override 933 public void remove(Person entity) 934 throws PersistenceDatabaseException { 935 remove(entity.getPrimaryKey()); 936 } 937 938 @Override 939 public void remove(PersonPK pk) 940 throws PersistenceDatabaseException { 941 PreparedStatement _ps = session.prepareStatement(SQL_DELETE); 942 long _entityId = pk.getEntityId(); 943 944 try { 945 _ps.setLong(1, _entityId); 946 947 if(PersistenceDebugFlags.CheckEntityDeleteRowCount) { 948 int _count = _ps.executeUpdate(); 949 950 if(_count != 1) { 951 throw new PersistenceDatabaseUpdateException("remove failed, _count = " + _count); 952 } 953 } else { 954 _ps.executeUpdate(); 955 } 956 957 session.getValueCache().remove(pk); 958 } catch (SQLException se) { 959 throw new PersistenceDatabaseException(se); 960 } 961 962 session.removed(pk, false); 963 } 964 965 @Override 966 public void remove(Collection<PersonPK> pks) 967 throws PersistenceDatabaseException { 968 if(pks.size() > 0) { 969 PreparedStatement _ps = session.prepareStatement(SQL_DELETE); 970 int _modifiedEntities = 0; 971 972 try { 973 for(PersonPK pk : pks) { 974 long _entityId = pk.getEntityId(); 975 976 _ps.setLong(1, _entityId); 977 978 _ps.addBatch(); 979 _modifiedEntities++; 980 } 981 982 if(_modifiedEntities != 0) { 983 if(PersistenceDebugFlags.CheckEntityDeleteRowCount) { 984 int[] _counts = _ps.executeBatch(); 985 986 for(int _countOffset = 0 ; _countOffset < _modifiedEntities ; _countOffset++) { 987 if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) { 988 throw new PersistenceDatabaseUpdateException("batch remove failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]); 989 } 990 } 991 } else { 992 _ps.executeBatch(); 993 } 994 995 _ps.clearBatch(); 996 997 pks.forEach((pk) -> { 998 session.getValueCache().remove(pk); 999 }); 1000 } 1001 } catch (SQLException se) { 1002 throw new PersistenceDatabaseException(se); 1003 } 1004 1005 pks.forEach((pk) -> { 1006 session.removed(pk, true); 1007 }); 1008 } 1009 } 1010 1011 @Override 1012 public boolean validPK(PersonPK pk) 1013 throws PersistenceDatabaseException { 1014 boolean valid = false; 1015 PreparedStatement _ps = session.prepareStatement(SQL_VALID); 1016 ResultSet _rs = null; 1017 1018 try { 1019 _ps.setLong(1, pk.getEntityId()); 1020 1021 _rs = _ps.executeQuery(); 1022 if(_rs.next()) { 1023 long _count = _rs.getLong(1); 1024 if(_rs.wasNull()) 1025 _count = 0; 1026 1027 if(_count == 1) 1028 valid = true; 1029 } 1030 } catch (SQLException se) { 1031 throw new PersistenceDatabaseException(se); 1032 } finally { 1033 if(_rs != null) { 1034 try { 1035 _rs.close(); 1036 } catch (SQLException se) { 1037 // do nothing 1038 } 1039 } 1040 } 1041 1042 return valid; 1043 } 1044 1045}