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 * PostalAddressLineElementFactory.java 021 */ 022 023package com.echothree.model.data.contact.server.factory; 024 025import com.echothree.model.data.contact.common.pk.PostalAddressLinePK; 026import com.echothree.model.data.contact.common.pk.PostalAddressElementTypePK; 027 028import com.echothree.model.data.contact.server.entity.PostalAddressLine; 029import com.echothree.model.data.contact.server.entity.PostalAddressElementType; 030 031import com.echothree.model.data.contact.common.PostalAddressLineElementConstants; 032import com.echothree.model.data.contact.common.pk.PostalAddressLineElementPK; 033import com.echothree.model.data.contact.server.value.PostalAddressLineElementValue; 034import com.echothree.model.data.contact.server.entity.PostalAddressLineElement; 035import com.echothree.util.common.exception.PersistenceDatabaseException; 036import com.echothree.util.common.exception.PersistenceDatabaseUpdateException; 037import com.echothree.util.common.exception.PersistenceNotNullException; 038import com.echothree.util.server.persistence.BaseFactory; 039import com.echothree.util.server.persistence.EntityIdGenerator; 040import com.echothree.util.server.persistence.EntityPermission; 041import com.echothree.util.server.persistence.PersistenceDebugFlags; 042import com.echothree.util.server.persistence.Session; 043import com.echothree.util.server.persistence.ThreadSession; 044import java.sql.PreparedStatement; 045import java.sql.ResultSet; 046import java.sql.SQLException; 047import java.sql.Types; 048import java.io.ByteArrayInputStream; 049import java.io.StringReader; 050import java.util.ArrayList; 051import java.util.Collection; 052import java.util.HashSet; 053import java.util.List; 054import java.util.Map; 055import java.util.Set; 056import org.apache.commons.logging.Log; 057import org.apache.commons.logging.LogFactory; 058 059public class PostalAddressLineElementFactory 060 implements BaseFactory<PostalAddressLineElementPK, PostalAddressLineElement> { 061 062 //final private static Log log = LogFactory.getLog(PostalAddressLineElementFactory.class); 063 064 final private static String SQL_SELECT_READ_ONLY = "SELECT pstale_postaladdresslineelementid, pstale_pstal_postaladdresslineid, pstale_postaladdresslineelementsortorder, pstale_pstaetyp_postaladdresselementtypeid, pstale_prefix, pstale_alwaysincludeprefix, pstale_suffix, pstale_alwaysincludesuffix, pstale_fromtime, pstale_thrutime FROM postaladdresslineelements WHERE pstale_postaladdresslineelementid = ?"; 065 final private static String SQL_SELECT_READ_WRITE = "SELECT pstale_postaladdresslineelementid, pstale_pstal_postaladdresslineid, pstale_postaladdresslineelementsortorder, pstale_pstaetyp_postaladdresselementtypeid, pstale_prefix, pstale_alwaysincludeprefix, pstale_suffix, pstale_alwaysincludesuffix, pstale_fromtime, pstale_thrutime FROM postaladdresslineelements WHERE pstale_postaladdresslineelementid = ? FOR UPDATE"; 066 final private static String SQL_INSERT = "INSERT INTO postaladdresslineelements (pstale_postaladdresslineelementid, pstale_pstal_postaladdresslineid, pstale_postaladdresslineelementsortorder, pstale_pstaetyp_postaladdresselementtypeid, pstale_prefix, pstale_alwaysincludeprefix, pstale_suffix, pstale_alwaysincludesuffix, pstale_fromtime, pstale_thrutime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 067 final private static String SQL_UPDATE = "UPDATE postaladdresslineelements SET pstale_pstal_postaladdresslineid = ?, pstale_postaladdresslineelementsortorder = ?, pstale_pstaetyp_postaladdresselementtypeid = ?, pstale_prefix = ?, pstale_alwaysincludeprefix = ?, pstale_suffix = ?, pstale_alwaysincludesuffix = ?, pstale_fromtime = ?, pstale_thrutime = ? WHERE pstale_postaladdresslineelementid = ?"; 068 final private static String SQL_DELETE = "DELETE FROM postaladdresslineelements WHERE pstale_postaladdresslineelementid = ?"; 069 final private static String SQL_VALID = "SELECT COUNT(*) FROM postaladdresslineelements WHERE pstale_postaladdresslineelementid = ?"; 070 071 final private static String PK_COLUMN = "pstale_postaladdresslineelementid"; 072 final private static String ALL_COLUMNS = "pstale_postaladdresslineelementid, pstale_pstal_postaladdresslineid, pstale_postaladdresslineelementsortorder, pstale_pstaetyp_postaladdresselementtypeid, pstale_prefix, pstale_alwaysincludeprefix, pstale_suffix, pstale_alwaysincludesuffix, pstale_fromtime, pstale_thrutime"; 073 final public static String TABLE_NAME = "postaladdresslineelements"; 074 075 final public static String PSTALE_POSTALADDRESSLINEELEMENTID = "pstale_postaladdresslineelementid"; 076 final public static String PSTALE_PSTAL_POSTALADDRESSLINEID = "pstale_pstal_postaladdresslineid"; 077 final public static String PSTALE_POSTALADDRESSLINEELEMENTSORTORDER = "pstale_postaladdresslineelementsortorder"; 078 final public static String PSTALE_PSTAETYP_POSTALADDRESSELEMENTTYPEID = "pstale_pstaetyp_postaladdresselementtypeid"; 079 final public static String PSTALE_PREFIX = "pstale_prefix"; 080 final public static String PSTALE_ALWAYSINCLUDEPREFIX = "pstale_alwaysincludeprefix"; 081 final public static String PSTALE_SUFFIX = "pstale_suffix"; 082 final public static String PSTALE_ALWAYSINCLUDESUFFIX = "pstale_alwaysincludesuffix"; 083 final public static String PSTALE_FROMTIME = "pstale_fromtime"; 084 final public static String PSTALE_THRUTIME = "pstale_thrutime"; 085 086 final private static EntityIdGenerator entityIdGenerator = new EntityIdGenerator(PostalAddressLineElementConstants.COMPONENT_VENDOR_NAME, PostalAddressLineElementConstants.ENTITY_TYPE_NAME); 087 088 /** Creates a new instance of PostalAddressLineElementFactory */ 089 private PostalAddressLineElementFactory() { 090 super(); 091 } 092 093 private static class PostalAddressLineElementFactoryHolder { 094 static PostalAddressLineElementFactory instance = new PostalAddressLineElementFactory(); 095 } 096 097 public static PostalAddressLineElementFactory getInstance() { 098 return PostalAddressLineElementFactoryHolder.instance; 099 } 100 101 @Override 102 public String getPKColumn() { 103 return PK_COLUMN; 104 } 105 106 @Override 107 public String getAllColumns() { 108 return ALL_COLUMNS; 109 } 110 111 @Override 112 public String getTableName() { 113 return TABLE_NAME; 114 } 115 116 @Override 117 public String getComponentVendorName() { 118 return PostalAddressLineElementConstants.COMPONENT_VENDOR_NAME; 119 } 120 121 @Override 122 public String getEntityTypeName() { 123 return PostalAddressLineElementConstants.ENTITY_TYPE_NAME; 124 } 125 126 public PreparedStatement prepareStatement(String query) { 127 return ThreadSession.currentSession().prepareStatement(PostalAddressLineElementFactory.class, query); 128 } 129 130 public PostalAddressLineElementPK getNextPK() { 131 return new PostalAddressLineElementPK(entityIdGenerator.getNextEntityId()); 132 } 133 134 public Set<PostalAddressLineElementPK> getPKsFromResultSetAsSet(ResultSet rs) 135 throws PersistenceDatabaseException { 136 Set<PostalAddressLineElementPK> _result = new HashSet<>(); 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 java.util.List<PostalAddressLineElementPK> getPKsFromResultSetAsList(ResultSet rs) 150 throws PersistenceDatabaseException { 151 java.util.List<PostalAddressLineElementPK> _result = new ArrayList<>(); 152 153 try { 154 while(rs.next()) { 155 _result.add(getPKFromResultSet(rs)); 156 } 157 } catch (SQLException se) { 158 throw new PersistenceDatabaseException(se); 159 } 160 161 return _result; 162 } 163 164 public PostalAddressLineElementPK getPKFromResultSet(ResultSet rs) 165 throws PersistenceDatabaseException { 166 PostalAddressLineElementPK _result; 167 168 try { 169 long pstale_postaladdresslineelementid = rs.getLong(PSTALE_POSTALADDRESSLINEELEMENTID); 170 Long _entityId = rs.wasNull() ? null : pstale_postaladdresslineelementid; 171 172 _result = new PostalAddressLineElementPK(_entityId); 173 } catch (SQLException se) { 174 throw new PersistenceDatabaseException(se); 175 } 176 177 return _result; 178 } 179 180 public java.util.List<PostalAddressLineElementValue> getValuesFromPKs(Session session, Collection<PostalAddressLineElementPK> pks) 181 throws PersistenceDatabaseException { 182 java.util.List<PostalAddressLineElementValue> _values = new ArrayList<>(pks.size()); 183 184 for(PostalAddressLineElementPK _pk: pks) { 185 _values.add(getValueFromPK(session, _pk)); 186 } 187 188 return _values; 189 } 190 191 public PostalAddressLineElementValue getValueFromPK(Session session, PostalAddressLineElementPK pk) 192 throws PersistenceDatabaseException { 193 PostalAddressLineElementValue _value; 194 195 // See if we already have the entity in the session cache 196 PostalAddressLineElement _entity = (PostalAddressLineElement)session.getEntity(pk); 197 if(_entity == null) 198 _value = getEntityFromPK(session, EntityPermission.READ_ONLY, pk).getPostalAddressLineElementValue(); 199 else 200 _value = _entity.getPostalAddressLineElementValue(); 201 202 return _value; 203 } 204 205 public java.util.List<PostalAddressLineElementValue> getValuesFromResultSet(Session session, ResultSet rs) 206 throws PersistenceDatabaseException { 207 java.util.List<PostalAddressLineElementValue> _result = new ArrayList<>(); 208 209 try { 210 while(rs.next()) { 211 _result.add(getValueFromResultSet(session, rs)); 212 } 213 } catch (SQLException se) { 214 throw new PersistenceDatabaseException(se); 215 } 216 217 return _result; 218 } 219 220 public PostalAddressLineElementValue getValueFromResultSet(Session session, ResultSet rs) 221 throws PersistenceDatabaseException { 222 PostalAddressLineElementValue _value; 223 224 try { 225 Long pstale_postaladdresslineelementid = rs.getLong(PSTALE_POSTALADDRESSLINEELEMENTID); 226 PostalAddressLineElementPK _pk = new PostalAddressLineElementPK(pstale_postaladdresslineelementid); 227 228 // See if we already have the entity in the session cache 229 PostalAddressLineElement _entity = (PostalAddressLineElement)session.getEntity(_pk); 230 231 if(_entity == null) { 232 Long pstale_pstal_postaladdresslineid = rs.getLong(PSTALE_PSTAL_POSTALADDRESSLINEID); 233 if(rs.wasNull()) 234 pstale_pstal_postaladdresslineid = null; 235 236 Integer pstale_postaladdresslineelementsortorder = rs.getInt(PSTALE_POSTALADDRESSLINEELEMENTSORTORDER); 237 if(rs.wasNull()) 238 pstale_postaladdresslineelementsortorder = null; 239 240 Long pstale_pstaetyp_postaladdresselementtypeid = rs.getLong(PSTALE_PSTAETYP_POSTALADDRESSELEMENTTYPEID); 241 if(rs.wasNull()) 242 pstale_pstaetyp_postaladdresselementtypeid = null; 243 244 String pstale_prefix = rs.getString(PSTALE_PREFIX); 245 if(rs.wasNull()) 246 pstale_prefix = null; 247 248 Boolean pstale_alwaysincludeprefix = rs.getInt(PSTALE_ALWAYSINCLUDEPREFIX) == 1; 249 if(rs.wasNull()) 250 pstale_alwaysincludeprefix = null; 251 252 String pstale_suffix = rs.getString(PSTALE_SUFFIX); 253 if(rs.wasNull()) 254 pstale_suffix = null; 255 256 Boolean pstale_alwaysincludesuffix = rs.getInt(PSTALE_ALWAYSINCLUDESUFFIX) == 1; 257 if(rs.wasNull()) 258 pstale_alwaysincludesuffix = null; 259 260 Long pstale_fromtime = rs.getLong(PSTALE_FROMTIME); 261 if(rs.wasNull()) 262 pstale_fromtime = null; 263 264 Long pstale_thrutime = rs.getLong(PSTALE_THRUTIME); 265 if(rs.wasNull()) 266 pstale_thrutime = null; 267 268 _value = new PostalAddressLineElementValue(_pk, new PostalAddressLinePK(pstale_pstal_postaladdresslineid), pstale_postaladdresslineelementsortorder, new PostalAddressElementTypePK(pstale_pstaetyp_postaladdresselementtypeid), pstale_prefix, pstale_alwaysincludeprefix, pstale_suffix, pstale_alwaysincludesuffix, pstale_fromtime, pstale_thrutime); 269 } else 270 _value = _entity.getPostalAddressLineElementValue(); 271 } catch (SQLException se) { 272 throw new PersistenceDatabaseException(se); 273 } 274 275 return _value; 276 } 277 278 public java.util.List<PostalAddressLineElement> getEntitiesFromPKs(EntityPermission entityPermission, Collection<PostalAddressLineElementPK> pks) 279 throws PersistenceDatabaseException { 280 return getEntitiesFromPKs(ThreadSession.currentSession(), entityPermission, pks); 281 } 282 283 public java.util.List<PostalAddressLineElement> getEntitiesFromPKs(Session session, EntityPermission entityPermission, Collection<PostalAddressLineElementPK> pks) 284 throws PersistenceDatabaseException { 285 java.util.List<PostalAddressLineElement> _entities = new ArrayList<>(pks.size()); 286 287 for(PostalAddressLineElementPK _pk: pks) { 288 _entities.add(getEntityFromPK(session, entityPermission, _pk)); 289 } 290 291 return _entities; 292 } 293 294 public PostalAddressLineElement getEntityFromValue(EntityPermission entityPermission, PostalAddressLineElementValue value) { 295 return getEntityFromPK(ThreadSession.currentSession(), entityPermission, value.getPrimaryKey()); 296 } 297 298 public PostalAddressLineElement getEntityFromValue(Session session, EntityPermission entityPermission, PostalAddressLineElementValue value) { 299 return getEntityFromPK(session, entityPermission, value.getPrimaryKey()); 300 } 301 302 public PostalAddressLineElement getEntityFromPK(EntityPermission entityPermission, PostalAddressLineElementPK pk) 303 throws PersistenceDatabaseException { 304 return getEntityFromPK(ThreadSession.currentSession(), entityPermission, pk); 305 } 306 307 public PostalAddressLineElement getEntityFromCache(Session session, PostalAddressLineElementPK pk) { 308 PostalAddressLineElementValue _value = (PostalAddressLineElementValue)session.getValueCache().get(pk); 309 310 return _value == null ? null : new PostalAddressLineElement(_value, EntityPermission.READ_ONLY); 311 } 312 313 public PostalAddressLineElement getEntityFromPK(Session session, EntityPermission entityPermission, PostalAddressLineElementPK pk) 314 throws PersistenceDatabaseException { 315 PostalAddressLineElement _entity; 316 317 // See if we already have the entity in the session cache 318 _entity = (PostalAddressLineElement)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(session, 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(session, 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<PostalAddressLineElementPK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params) 361 throws PersistenceDatabaseException { 362 Set<PostalAddressLineElementPK> _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<PostalAddressLineElementPK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params) 389 throws PersistenceDatabaseException { 390 java.util.List<PostalAddressLineElementPK> _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 PostalAddressLineElementPK getPKFromQuery(PreparedStatement ps, final Object... params) 417 throws PersistenceDatabaseException { 418 PostalAddressLineElementPK _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<PostalAddressLineElement> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params) 447 throws PersistenceDatabaseException { 448 PreparedStatement ps = session.prepareStatement(PostalAddressLineElementFactory.class, queryMap.get(entityPermission)); 449 450 return getEntitiesFromQuery(session, entityPermission, ps, params); 451 } 452 453 public java.util.List<PostalAddressLineElement> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params) 454 throws PersistenceDatabaseException { 455 Session session = ThreadSession.currentSession(); 456 PreparedStatement ps = session.prepareStatement(PostalAddressLineElementFactory.class, queryMap.get(entityPermission)); 457 458 return getEntitiesFromQuery(session, entityPermission, ps, params); 459 } 460 461 public java.util.List<PostalAddressLineElement> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap) 462 throws PersistenceDatabaseException { 463 PreparedStatement ps = session.prepareStatement(PostalAddressLineElementFactory.class, queryMap.get(entityPermission)); 464 465 return getEntitiesFromQuery(session, entityPermission, ps); 466 } 467 468 public java.util.List<PostalAddressLineElement> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap) 469 throws PersistenceDatabaseException { 470 Session session = ThreadSession.currentSession(); 471 PreparedStatement ps = session.prepareStatement(PostalAddressLineElementFactory.class, queryMap.get(entityPermission)); 472 473 return getEntitiesFromQuery(session, entityPermission, ps); 474 } 475 476 public java.util.List<PostalAddressLineElement> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps) 477 throws PersistenceDatabaseException { 478 return getEntitiesFromQuery(ThreadSession.currentSession(), entityPermission, ps); 479 } 480 481 public java.util.List<PostalAddressLineElement> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params) 482 throws PersistenceDatabaseException { 483 return getEntitiesFromQuery(ThreadSession.currentSession(), entityPermission, ps, params); 484 } 485 486 public java.util.List<PostalAddressLineElement> getEntitiesFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params) 487 throws PersistenceDatabaseException { 488 java.util.List<PostalAddressLineElement> _entities; 489 ResultSet _rs = null; 490 491 try { 492 if(params.length != 0) { 493 Session.setQueryParams(ps, params); 494 } 495 496 _rs = ps.executeQuery(); 497 _entities = getEntitiesFromResultSet(session, entityPermission, _rs); 498 _rs.close(); 499 } catch (SQLException se) { 500 throw new PersistenceDatabaseException(se); 501 } finally { 502 if(_rs != null) { 503 try { 504 _rs.close(); 505 } catch (SQLException se) { 506 // do nothing 507 } 508 } 509 } 510 511 return _entities; 512 } 513 514 public PostalAddressLineElement getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params) 515 throws PersistenceDatabaseException { 516 PreparedStatement ps = session.prepareStatement(PostalAddressLineElementFactory.class, queryMap.get(entityPermission)); 517 518 return getEntityFromQuery(session, entityPermission, ps, params); 519 } 520 521 public PostalAddressLineElement getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params) 522 throws PersistenceDatabaseException { 523 Session session = ThreadSession.currentSession(); 524 PreparedStatement ps = session.prepareStatement(PostalAddressLineElementFactory.class, queryMap.get(entityPermission)); 525 526 return getEntityFromQuery(session, entityPermission, ps, params); 527 } 528 529 public PostalAddressLineElement getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap) 530 throws PersistenceDatabaseException { 531 PreparedStatement ps = session.prepareStatement(PostalAddressLineElementFactory.class, queryMap.get(entityPermission)); 532 533 return getEntityFromQuery(session, entityPermission, ps); 534 } 535 536 public PostalAddressLineElement getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap) 537 throws PersistenceDatabaseException { 538 Session session = ThreadSession.currentSession(); 539 PreparedStatement ps = session.prepareStatement(PostalAddressLineElementFactory.class, queryMap.get(entityPermission)); 540 541 return getEntityFromQuery(session, entityPermission, ps); 542 } 543 544 public PostalAddressLineElement getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps) 545 throws PersistenceDatabaseException { 546 return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps); 547 } 548 549 public PostalAddressLineElement getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params) 550 throws PersistenceDatabaseException { 551 return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps, params); 552 } 553 554 public PostalAddressLineElement getEntityFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params) 555 throws PersistenceDatabaseException { 556 PostalAddressLineElement _entity = null; 557 ResultSet _rs = null; 558 559 try { 560 if(params.length != 0) { 561 Session.setQueryParams(ps, params); 562 } 563 564 _rs = ps.executeQuery(); 565 if(_rs.next()) { 566 _entity = getEntityFromResultSet(session, entityPermission, _rs); 567 } 568 _rs.close(); 569 } catch (SQLException se) { 570 throw new PersistenceDatabaseException(se); 571 } finally { 572 if(_rs != null) { 573 try { 574 _rs.close(); 575 } catch (SQLException se) { 576 // do nothing 577 } 578 } 579 } 580 581 return _entity; 582 } 583 584 public java.util.List<PostalAddressLineElement> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs) 585 throws PersistenceDatabaseException { 586 return getEntitiesFromResultSet(ThreadSession.currentSession(), entityPermission, rs); 587 } 588 589 public java.util.List<PostalAddressLineElement> getEntitiesFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs) 590 throws PersistenceDatabaseException { 591 java.util.List<PostalAddressLineElement> _result = new ArrayList<>(); 592 593 try { 594 while(rs.next()) { 595 _result.add(getEntityFromResultSet(session, entityPermission, rs)); 596 } 597 } catch (SQLException se) { 598 throw new PersistenceDatabaseException(se); 599 } 600 601 return _result; 602 } 603 604 public PostalAddressLineElement getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs) 605 throws PersistenceDatabaseException { 606 return getEntityFromResultSet(ThreadSession.currentSession(), entityPermission, rs); 607 } 608 609 public PostalAddressLineElement getEntityFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs) 610 throws PersistenceDatabaseException { 611 PostalAddressLineElement _entity; 612 613 try { 614 Long pstale_postaladdresslineelementid = rs.getLong(PSTALE_POSTALADDRESSLINEELEMENTID); 615 PostalAddressLineElementPK _pk = new PostalAddressLineElementPK(pstale_postaladdresslineelementid); 616 617 // See if we already have the entity in the session cache 618 _entity = (PostalAddressLineElement)session.getEntity(_pk); 619 if(_entity != null) { 620 // If the requested permission is READ_WRITE, and the cached permission is 621 // READ_ONLY, then pretend that the cached object wasn't found, and create 622 // a new entity that is READ_WRITE. 623 if(entityPermission.equals(EntityPermission.READ_WRITE)) { 624 if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY)) 625 _entity = null; 626 } 627 } 628 boolean foundInSessionCache = _entity != null; 629 630 if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) { 631 _entity = getEntityFromCache(session, _pk); 632 } 633 634 if(_entity == null) { 635 Long pstale_pstal_postaladdresslineid = rs.getLong(PSTALE_PSTAL_POSTALADDRESSLINEID); 636 if(rs.wasNull()) 637 pstale_pstal_postaladdresslineid = null; 638 639 Integer pstale_postaladdresslineelementsortorder = rs.getInt(PSTALE_POSTALADDRESSLINEELEMENTSORTORDER); 640 if(rs.wasNull()) 641 pstale_postaladdresslineelementsortorder = null; 642 643 Long pstale_pstaetyp_postaladdresselementtypeid = rs.getLong(PSTALE_PSTAETYP_POSTALADDRESSELEMENTTYPEID); 644 if(rs.wasNull()) 645 pstale_pstaetyp_postaladdresselementtypeid = null; 646 647 String pstale_prefix = rs.getString(PSTALE_PREFIX); 648 if(rs.wasNull()) 649 pstale_prefix = null; 650 651 Boolean pstale_alwaysincludeprefix = rs.getInt(PSTALE_ALWAYSINCLUDEPREFIX) == 1; 652 if(rs.wasNull()) 653 pstale_alwaysincludeprefix = null; 654 655 String pstale_suffix = rs.getString(PSTALE_SUFFIX); 656 if(rs.wasNull()) 657 pstale_suffix = null; 658 659 Boolean pstale_alwaysincludesuffix = rs.getInt(PSTALE_ALWAYSINCLUDESUFFIX) == 1; 660 if(rs.wasNull()) 661 pstale_alwaysincludesuffix = null; 662 663 Long pstale_fromtime = rs.getLong(PSTALE_FROMTIME); 664 if(rs.wasNull()) 665 pstale_fromtime = null; 666 667 Long pstale_thrutime = rs.getLong(PSTALE_THRUTIME); 668 if(rs.wasNull()) 669 pstale_thrutime = null; 670 671 PostalAddressLineElementValue _value = new PostalAddressLineElementValue(_pk, pstale_pstal_postaladdresslineid == null? null: new PostalAddressLinePK(pstale_pstal_postaladdresslineid), pstale_postaladdresslineelementsortorder, pstale_pstaetyp_postaladdresselementtypeid == null? null: new PostalAddressElementTypePK(pstale_pstaetyp_postaladdresselementtypeid), pstale_prefix, pstale_alwaysincludeprefix, pstale_suffix, pstale_alwaysincludesuffix, pstale_fromtime, pstale_thrutime); 672 _entity = new PostalAddressLineElement(_value, entityPermission); 673 } 674 675 if(!foundInSessionCache) { 676 if(entityPermission.equals(EntityPermission.READ_ONLY)) { 677 session.putReadOnlyEntity(_pk, _entity); 678 session.getValueCache().put(_entity.getPostalAddressLineElementValue()); 679 } else { 680 session.putReadWriteEntity(_pk, _entity); 681 } 682 } 683 } catch (SQLException se) { 684 throw new PersistenceDatabaseException(se); 685 } 686 687 return _entity; 688 } 689 690 public PostalAddressLineElement create(Session session, PostalAddressLine postalAddressLine, Integer postalAddressLineElementSortOrder, PostalAddressElementType postalAddressElementType, String prefix, Boolean alwaysIncludePrefix, String suffix, Boolean alwaysIncludeSuffix, Long fromTime, Long thruTime) 691 throws PersistenceDatabaseException, PersistenceNotNullException { 692 return create(session, postalAddressLine == null ? null : postalAddressLine.getPrimaryKey(), postalAddressLineElementSortOrder, postalAddressElementType == null ? null : postalAddressElementType.getPrimaryKey(), prefix, alwaysIncludePrefix, suffix, alwaysIncludeSuffix, fromTime, thruTime); 693 } 694 695 public PostalAddressLineElement create(PostalAddressLine postalAddressLine, Integer postalAddressLineElementSortOrder, PostalAddressElementType postalAddressElementType, String prefix, Boolean alwaysIncludePrefix, String suffix, Boolean alwaysIncludeSuffix, Long fromTime, Long thruTime) 696 throws PersistenceDatabaseException, PersistenceNotNullException { 697 return create(ThreadSession.currentSession(), postalAddressLine == null ? null : postalAddressLine.getPrimaryKey(), postalAddressLineElementSortOrder, postalAddressElementType == null ? null : postalAddressElementType.getPrimaryKey(), prefix, alwaysIncludePrefix, suffix, alwaysIncludeSuffix, fromTime, thruTime); 698 } 699 700 private void bindForCreate(PreparedStatement _ps, PostalAddressLineElementValue _value) 701 throws SQLException { 702 _ps.setLong(1, _value.getEntityId()); 703 704 PostalAddressLinePK pstale_pstal_postaladdresslineid = _value.getPostalAddressLinePK(); 705 if(pstale_pstal_postaladdresslineid == null) 706 _ps.setNull(2, Types.BIGINT); 707 else 708 _ps.setLong(2, pstale_pstal_postaladdresslineid.getEntityId()); 709 710 Integer pstale_postaladdresslineelementsortorder = _value.getPostalAddressLineElementSortOrder(); 711 if(pstale_postaladdresslineelementsortorder == null) 712 _ps.setNull(3, Types.INTEGER); 713 else 714 _ps.setInt(3, pstale_postaladdresslineelementsortorder); 715 716 PostalAddressElementTypePK pstale_pstaetyp_postaladdresselementtypeid = _value.getPostalAddressElementTypePK(); 717 if(pstale_pstaetyp_postaladdresselementtypeid == null) 718 _ps.setNull(4, Types.BIGINT); 719 else 720 _ps.setLong(4, pstale_pstaetyp_postaladdresselementtypeid.getEntityId()); 721 722 String pstale_prefix = _value.getPrefix(); 723 if(pstale_prefix == null) 724 _ps.setNull(5, Types.VARCHAR); 725 else 726 _ps.setString(5, pstale_prefix); 727 728 Boolean pstale_alwaysincludeprefix = _value.getAlwaysIncludePrefix(); 729 if(pstale_alwaysincludeprefix == null) 730 _ps.setNull(6, Types.BIT); 731 else 732 _ps.setInt(6, pstale_alwaysincludeprefix? 1: 0); 733 734 String pstale_suffix = _value.getSuffix(); 735 if(pstale_suffix == null) 736 _ps.setNull(7, Types.VARCHAR); 737 else 738 _ps.setString(7, pstale_suffix); 739 740 Boolean pstale_alwaysincludesuffix = _value.getAlwaysIncludeSuffix(); 741 if(pstale_alwaysincludesuffix == null) 742 _ps.setNull(8, Types.BIT); 743 else 744 _ps.setInt(8, pstale_alwaysincludesuffix? 1: 0); 745 746 Long pstale_fromtime = _value.getFromTime(); 747 if(pstale_fromtime == null) 748 _ps.setNull(9, Types.BIGINT); 749 else 750 _ps.setLong(9, pstale_fromtime); 751 752 Long pstale_thrutime = _value.getThruTime(); 753 if(pstale_thrutime == null) 754 _ps.setNull(10, Types.BIGINT); 755 else 756 _ps.setLong(10, pstale_thrutime); 757 758 } 759 760 public PostalAddressLineElement create(Session session, PostalAddressLinePK postalAddressLinePK, Integer postalAddressLineElementSortOrder, PostalAddressElementTypePK postalAddressElementTypePK, String prefix, Boolean alwaysIncludePrefix, String suffix, Boolean alwaysIncludeSuffix, Long fromTime, Long thruTime) 761 throws PersistenceDatabaseException, PersistenceNotNullException { 762 PostalAddressLineElementPK _pk = getNextPK(); 763 PostalAddressLineElementValue _value = new PostalAddressLineElementValue(_pk, postalAddressLinePK, postalAddressLineElementSortOrder, postalAddressElementTypePK, prefix, alwaysIncludePrefix, suffix, alwaysIncludeSuffix, fromTime, thruTime); 764 765 PreparedStatement _ps = session.prepareStatement(SQL_INSERT); 766 767 try { 768 bindForCreate(_ps, _value); 769 770 if(PersistenceDebugFlags.CheckEntityInsertRowCount) { 771 int _count = _ps.executeUpdate(); 772 773 if(_count != 1) { 774 throw new PersistenceDatabaseUpdateException("insert failed, _count = " + _count); 775 } 776 } else { 777 _ps.executeUpdate(); 778 } 779 780 session.getValueCache().put(_value); 781 } catch (SQLException se) { 782 throw new PersistenceDatabaseException(se); 783 } 784 785 PostalAddressLineElement _entity = new PostalAddressLineElement(_value, EntityPermission.READ_ONLY); 786 session.putReadOnlyEntity(_pk, _entity); 787 788 return _entity; 789 } 790 791 public PostalAddressLineElement create(PostalAddressLinePK postalAddressLinePK, Integer postalAddressLineElementSortOrder, PostalAddressElementTypePK postalAddressElementTypePK, String prefix, Boolean alwaysIncludePrefix, String suffix, Boolean alwaysIncludeSuffix, Long fromTime, Long thruTime) 792 throws PersistenceDatabaseException, PersistenceNotNullException { 793 return create(ThreadSession.currentSession(), postalAddressLinePK, postalAddressLineElementSortOrder, postalAddressElementTypePK, prefix, alwaysIncludePrefix, suffix, alwaysIncludeSuffix, fromTime, thruTime); 794 } 795 796 public void create(Session session, Collection<PostalAddressLineElementValue> _values) 797 throws PersistenceDatabaseException, PersistenceNotNullException { 798 int _size = _values.size(); 799 800 if(_size > 0) { 801 PreparedStatement _ps = session.prepareStatement(SQL_INSERT); 802 List<PostalAddressLineElementValue> _cacheValues = new ArrayList<>(_size); 803 804 try { 805 for(PostalAddressLineElementValue _value : _values) { 806 _value.setEntityId(entityIdGenerator.getNextEntityId()); 807 bindForCreate(_ps, _value); 808 809 _ps.addBatch(); 810 811 _cacheValues.add(_value); 812 } 813 814 if(PersistenceDebugFlags.CheckEntityInsertRowCount) { 815 int[] _counts = _ps.executeBatch(); 816 817 for(int _countOffset = 0 ; _countOffset < _size ; _countOffset++) { 818 if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) { 819 throw new PersistenceDatabaseUpdateException("batch insert failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]); 820 } 821 } 822 } else { 823 _ps.executeBatch(); 824 } 825 826 _ps.clearBatch(); 827 } catch (SQLException se) { 828 throw new PersistenceDatabaseException(se); 829 } 830 831 _cacheValues.forEach((_cacheValue) -> { 832 PostalAddressLineElement _cacheEntity = new PostalAddressLineElement(_cacheValue, EntityPermission.READ_ONLY); 833 834 session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity); 835 }); 836 } 837 } 838 839 public void create(Collection<PostalAddressLineElementValue> _values) 840 throws PersistenceDatabaseException, PersistenceNotNullException { 841 create(ThreadSession.currentSession(), _values); 842 } 843 844 private boolean bindForStore(PreparedStatement _ps, PostalAddressLineElementValue _value) 845 throws SQLException { 846 boolean _hasBeenModified = _value.hasBeenModified(); 847 848 if(_hasBeenModified) { 849 PostalAddressLinePK pstale_pstal_postaladdresslineid = _value.getPostalAddressLinePK(); 850 if(pstale_pstal_postaladdresslineid == null) 851 _ps.setNull(1, Types.BIGINT); 852 else 853 _ps.setLong(1, pstale_pstal_postaladdresslineid.getEntityId()); 854 855 Integer pstale_postaladdresslineelementsortorder = _value.getPostalAddressLineElementSortOrder(); 856 if(pstale_postaladdresslineelementsortorder == null) 857 _ps.setNull(2, Types.INTEGER); 858 else 859 _ps.setInt(2, pstale_postaladdresslineelementsortorder); 860 861 PostalAddressElementTypePK pstale_pstaetyp_postaladdresselementtypeid = _value.getPostalAddressElementTypePK(); 862 if(pstale_pstaetyp_postaladdresselementtypeid == null) 863 _ps.setNull(3, Types.BIGINT); 864 else 865 _ps.setLong(3, pstale_pstaetyp_postaladdresselementtypeid.getEntityId()); 866 867 String pstale_prefix = _value.getPrefix(); 868 if(pstale_prefix == null) 869 _ps.setNull(4, Types.VARCHAR); 870 else 871 _ps.setString(4, pstale_prefix); 872 873 Boolean pstale_alwaysincludeprefix = _value.getAlwaysIncludePrefix(); 874 if(pstale_alwaysincludeprefix == null) 875 _ps.setNull(5, Types.BIT); 876 else 877 _ps.setInt(5, pstale_alwaysincludeprefix? 1: 0); 878 879 String pstale_suffix = _value.getSuffix(); 880 if(pstale_suffix == null) 881 _ps.setNull(6, Types.VARCHAR); 882 else 883 _ps.setString(6, pstale_suffix); 884 885 Boolean pstale_alwaysincludesuffix = _value.getAlwaysIncludeSuffix(); 886 if(pstale_alwaysincludesuffix == null) 887 _ps.setNull(7, Types.BIT); 888 else 889 _ps.setInt(7, pstale_alwaysincludesuffix? 1: 0); 890 891 Long pstale_fromtime = _value.getFromTime(); 892 if(pstale_fromtime == null) 893 _ps.setNull(8, Types.BIGINT); 894 else 895 _ps.setLong(8, pstale_fromtime); 896 897 Long pstale_thrutime = _value.getThruTime(); 898 if(pstale_thrutime == null) 899 _ps.setNull(9, Types.BIGINT); 900 else 901 _ps.setLong(9, pstale_thrutime); 902 903 _ps.setLong(10, _value.getPrimaryKey().getEntityId()); 904 905 _value.clearHasBeenModified(); 906 } 907 908 return _hasBeenModified; 909 } 910 911 @Override 912 public void store(Session session, PostalAddressLineElement entity) 913 throws PersistenceDatabaseException { 914 PreparedStatement _ps = session.prepareStatement(SQL_UPDATE); 915 916 try { 917 PostalAddressLineElementValue _value = entity.getPostalAddressLineElementValue(); 918 919 if(bindForStore(_ps, _value)) { 920 if(PersistenceDebugFlags.CheckEntityUpdateRowCount) { 921 int _count = _ps.executeUpdate(); 922 923 if(_count != 1) { 924 throw new PersistenceDatabaseUpdateException("update failed, _count = " + _count); 925 } 926 } else { 927 _ps.executeUpdate(); 928 } 929 930 session.getValueCache().put(_value); 931 } 932 } catch (SQLException se) { 933 throw new PersistenceDatabaseException(se); 934 } 935 } 936 937 @Override 938 public void store(Session session, Collection<PostalAddressLineElement> entities) 939 throws PersistenceDatabaseException { 940 if(entities.size() > 0) { 941 PreparedStatement _ps = session.prepareStatement(SQL_UPDATE); 942 int _modifiedEntities = 0; 943 944 try { 945 for(PostalAddressLineElement entity : entities) { 946 if(bindForStore(_ps, entity.getPostalAddressLineElementValue())) { 947 _ps.addBatch(); 948 _modifiedEntities++; 949 } 950 } 951 952 if(_modifiedEntities != 0) { 953 if(PersistenceDebugFlags.CheckEntityUpdateRowCount) { 954 int[] _counts = _ps.executeBatch(); 955 956 for(int _countOffset = 0 ; _countOffset < _modifiedEntities ; _countOffset++) { 957 if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) { 958 throw new PersistenceDatabaseUpdateException("batch update failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]); 959 } 960 } 961 } else { 962 _ps.executeBatch(); 963 } 964 965 _ps.clearBatch(); 966 967 entities.forEach((entity) -> { 968 session.getValueCache().put(entity.getPostalAddressLineElementValue()); 969 }); 970 } 971 } catch (SQLException se) { 972 throw new PersistenceDatabaseException(se); 973 } 974 } 975 } 976 977 @Override 978 public void store(Collection<PostalAddressLineElement> entities) 979 throws PersistenceDatabaseException { 980 store(ThreadSession.currentSession(), entities); 981 } 982 983 @Override 984 public void remove(Session session, PostalAddressLineElement entity) 985 throws PersistenceDatabaseException { 986 remove(session, entity.getPrimaryKey()); 987 } 988 989 @Override 990 public void remove(Session session, PostalAddressLineElementPK pk) 991 throws PersistenceDatabaseException { 992 PreparedStatement _ps = session.prepareStatement(SQL_DELETE); 993 long _entityId = pk.getEntityId(); 994 995 try { 996 _ps.setLong(1, _entityId); 997 998 if(PersistenceDebugFlags.CheckEntityDeleteRowCount) { 999 int _count = _ps.executeUpdate(); 1000 1001 if(_count != 1) { 1002 throw new PersistenceDatabaseUpdateException("remove failed, _count = " + _count); 1003 } 1004 } else { 1005 _ps.executeUpdate(); 1006 } 1007 1008 session.getValueCache().remove(pk); 1009 } catch (SQLException se) { 1010 throw new PersistenceDatabaseException(se); 1011 } 1012 1013 session.removed(pk, false); 1014 } 1015 1016 @Override 1017 public void remove(Session session, Collection<PostalAddressLineElementPK> pks) 1018 throws PersistenceDatabaseException { 1019 if(pks.size() > 0) { 1020 PreparedStatement _ps = session.prepareStatement(SQL_DELETE); 1021 int _modifiedEntities = 0; 1022 1023 try { 1024 for(PostalAddressLineElementPK pk : pks) { 1025 long _entityId = pk.getEntityId(); 1026 1027 _ps.setLong(1, _entityId); 1028 1029 _ps.addBatch(); 1030 _modifiedEntities++; 1031 } 1032 1033 if(_modifiedEntities != 0) { 1034 if(PersistenceDebugFlags.CheckEntityDeleteRowCount) { 1035 int[] _counts = _ps.executeBatch(); 1036 1037 for(int _countOffset = 0 ; _countOffset < _modifiedEntities ; _countOffset++) { 1038 if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) { 1039 throw new PersistenceDatabaseUpdateException("batch remove failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]); 1040 } 1041 } 1042 } else { 1043 _ps.executeBatch(); 1044 } 1045 1046 _ps.clearBatch(); 1047 1048 pks.forEach((pk) -> { 1049 session.getValueCache().remove(pk); 1050 }); 1051 } 1052 } catch (SQLException se) { 1053 throw new PersistenceDatabaseException(se); 1054 } 1055 1056 pks.forEach((pk) -> { 1057 session.removed(pk, true); 1058 }); 1059 } 1060 } 1061 1062 @Override 1063 public void remove(Collection<PostalAddressLineElementPK> pks) 1064 throws PersistenceDatabaseException { 1065 remove(ThreadSession.currentSession(), pks); 1066 } 1067 1068 @Override 1069 public boolean validPK(Session session, PostalAddressLineElementPK pk) 1070 throws PersistenceDatabaseException { 1071 boolean valid = false; 1072 PreparedStatement _ps = session.prepareStatement(SQL_VALID); 1073 ResultSet _rs = null; 1074 1075 try { 1076 _ps.setLong(1, pk.getEntityId()); 1077 1078 _rs = _ps.executeQuery(); 1079 if(_rs.next()) { 1080 long _count = _rs.getLong(1); 1081 if(_rs.wasNull()) 1082 _count = 0; 1083 1084 if(_count == 1) 1085 valid = true; 1086 } 1087 } catch (SQLException se) { 1088 throw new PersistenceDatabaseException(se); 1089 } finally { 1090 if(_rs != null) { 1091 try { 1092 _rs.close(); 1093 } catch (SQLException se) { 1094 // do nothing 1095 } 1096 } 1097 } 1098 1099 return valid; 1100 } 1101 1102}