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