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