1 25 package org.ofbiz.entity; 26 27 import java.io.FileNotFoundException ; 28 import java.io.IOException ; 29 import java.net.URL ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Set ; 35 import java.util.TreeSet ; 36 import javax.xml.parsers.ParserConfigurationException ; 37 38 import javolution.util.FastList; 39 import javolution.util.FastMap; 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.Element ; 42 import org.w3c.dom.Node ; 43 import org.xml.sax.SAXException ; 44 45 import org.ofbiz.base.util.Debug; 46 import org.ofbiz.base.util.UtilFormatOut; 47 import org.ofbiz.base.util.UtilMisc; 48 import org.ofbiz.base.util.UtilValidate; 49 import org.ofbiz.base.util.UtilXml; 50 import org.ofbiz.base.util.cache.CacheLine; 51 import org.ofbiz.base.util.cache.UtilCache; 52 import org.ofbiz.entity.cache.Cache; 53 import org.ofbiz.entity.condition.EntityCondition; 54 import org.ofbiz.entity.condition.EntityConditionList; 55 import org.ofbiz.entity.condition.EntityExpr; 56 import org.ofbiz.entity.condition.EntityFieldMap; 57 import org.ofbiz.entity.condition.EntityOperator; 58 import org.ofbiz.entity.config.DatasourceInfo; 59 import org.ofbiz.entity.config.DelegatorInfo; 60 import org.ofbiz.entity.config.EntityConfigUtil; 61 import org.ofbiz.entity.datasource.GenericHelper; 62 import org.ofbiz.entity.datasource.GenericHelperFactory; 63 import org.ofbiz.entity.eca.EntityEcaHandler; 64 import org.ofbiz.entity.model.*; 65 import org.ofbiz.entity.serialize.SerializeException; 66 import org.ofbiz.entity.serialize.XmlSerializer; 67 import org.ofbiz.entity.transaction.GenericTransactionException; 68 import org.ofbiz.entity.transaction.TransactionUtil; 69 import org.ofbiz.entity.util.DistributedCacheClear; 70 import org.ofbiz.entity.util.EntityCrypto; 71 import org.ofbiz.entity.util.EntityFindOptions; 72 import org.ofbiz.entity.util.EntityListIterator; 73 import org.ofbiz.entity.util.SequenceUtil; 74 75 84 public class GenericDelegator implements DelegatorInterface { 85 86 public static final String module = GenericDelegator.class.getName(); 87 88 89 public static final boolean keepLocalReaders = true; 90 protected ModelReader modelReader = null; 91 protected ModelGroupReader modelGroupReader = null; 92 93 94 public static final boolean alwaysUseTransaction = true; 95 96 98 protected static Map delegatorCache = FastMap.newInstance(); 99 protected String delegatorName = null; 100 protected DelegatorInfo delegatorInfo = null; 101 102 protected Cache cache = null; 103 104 protected Map andCacheFieldSets = FastMap.newInstance(); 106 107 protected DistributedCacheClear distributedCacheClear = null; 108 protected EntityEcaHandler entityEcaHandler = null; 109 protected SequenceUtil sequencer = null; 110 protected EntityCrypto crypto = null; 111 112 public static GenericDelegator getGenericDelegator(String delegatorName) { 113 if (delegatorName == null) { 114 delegatorName = "default"; 115 Debug.logWarning("Got a getGenericDelegator call with a null delegatorName, assuming default for the name.", module); 116 } 117 GenericDelegator delegator = (GenericDelegator) delegatorCache.get(delegatorName); 118 119 if (delegator == null) { 120 synchronized (GenericDelegator.class) { 121 delegator = (GenericDelegator) delegatorCache.get(delegatorName); 123 if (delegator == null) { 124 if (Debug.infoOn()) Debug.logInfo("Creating new delegator [" + delegatorName + "] (" + Thread.currentThread().getName() + ")", module); 125 try { 127 delegator = new GenericDelegator(delegatorName); 128 } catch (GenericEntityException e) { 129 Debug.logError(e, "Error creating delegator", module); 130 } 131 if (delegator != null) { 132 delegatorCache.put(delegatorName, delegator); 133 } else { 134 Debug.logError("Could not create delegator with name " + delegatorName + ", constructor failed (got null value) not sure why/how.", module); 135 } 136 } 137 } 138 } 139 return delegator; 140 } 141 142 143 protected GenericDelegator() {} 144 145 146 protected GenericDelegator(String delegatorName) throws GenericEntityException { 147 149 this.delegatorName = delegatorName; 150 if (keepLocalReaders) { 151 modelReader = ModelReader.getModelReader(delegatorName); 152 modelGroupReader = ModelGroupReader.getModelGroupReader(delegatorName); 153 } 154 155 cache = new Cache(delegatorName); 156 157 List warningList = FastList.newInstance(); 159 Debug.logImportant("Doing entity definition check...", module); 160 ModelEntityChecker.checkEntities(this, warningList); 161 if (warningList.size() > 0) { 162 Debug.logWarning("=-=-=-=-= Found " + warningList.size() + " warnings when checking the entity definitions:", module); 163 Iterator warningIter = warningList.iterator(); 164 while (warningIter.hasNext()) { 165 String warning = (String ) warningIter.next(); 166 Debug.logWarning(warning, module); 167 } 168 } 169 170 Iterator groups = UtilMisc.toIterator(getModelGroupReader().getGroupNames()); 172 while (groups != null && groups.hasNext()) { 173 String groupName = (String ) groups.next(); 174 String helperName = this.getGroupHelperName(groupName); 175 176 if (Debug.infoOn()) Debug.logInfo("Delegator \"" + delegatorName + "\" initializing helper \"" + 177 helperName + "\" for entity group \"" + groupName + "\".", module); 178 TreeSet helpersDone = new TreeSet (); 179 if (helperName != null && helperName.length() > 0) { 180 if (helpersDone.contains(helperName)) { 182 if (Debug.infoOn()) Debug.logInfo("Helper \"" + helperName + "\" already initialized, not re-initializing.", module); 183 continue; 184 } 185 helpersDone.add(helperName); 186 ModelFieldTypeReader.getModelFieldTypeReader(helperName); 188 GenericHelper helper = GenericHelperFactory.getHelper(helperName); 190 191 DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperName); 192 if (datasourceInfo.checkOnStart) { 193 if (Debug.infoOn()) Debug.logInfo("Doing database check as requested in entityengine.xml with addMissing=" + datasourceInfo.addMissingOnStart, module); 194 try { 195 helper.checkDataSource(this.getModelEntityMapByGroup(groupName), null, datasourceInfo.addMissingOnStart); 196 } catch (GenericEntityException e) { 197 Debug.logWarning(e, e.getMessage(), module); 198 } 199 } 200 } 201 } 202 203 delegatorCache.put(delegatorName, this); 206 207 this.crypto = new EntityCrypto(this); 209 210 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 212 213 if (getDelegatorInfo().useDistributedCacheClear) { 217 String distributedCacheClearClassName = getDelegatorInfo().distributedCacheClearClassName; 219 220 try { 221 Class dccClass = loader.loadClass(distributedCacheClearClassName); 222 this.distributedCacheClear = (DistributedCacheClear) dccClass.newInstance(); 223 this.distributedCacheClear.setDelegator(this, getDelegatorInfo().distributedCacheClearUserLoginId); 224 } catch (ClassNotFoundException e) { 225 Debug.logWarning(e, "DistributedCacheClear class with name " + distributedCacheClearClassName + " was not found, distributed cache clearing will be disabled", module); 226 } catch (InstantiationException e) { 227 Debug.logWarning(e, "DistributedCacheClear class with name " + distributedCacheClearClassName + " could not be instantiated, distributed cache clearing will be disabled", module); 228 } catch (IllegalAccessException e) { 229 Debug.logWarning(e, "DistributedCacheClear class with name " + distributedCacheClearClassName + " could not be accessed (illegal), distributed cache clearing will be disabled", module); 230 } catch (ClassCastException e) { 231 Debug.logWarning(e, "DistributedCacheClear class with name " + distributedCacheClearClassName + " does not implement the DistributedCacheClear interface, distributed cache clearing will be disabled", module); 232 } 233 } else { 234 Debug.logInfo("Distributed Cache Clear System disabled for delegator [" + delegatorName + "]", module); 235 } 236 237 if (getDelegatorInfo().useEntityEca) { 239 String entityEcaHandlerClassName = getDelegatorInfo().entityEcaHandlerClassName; 241 242 try { 243 Class eecahClass = loader.loadClass(entityEcaHandlerClassName); 244 this.entityEcaHandler = (EntityEcaHandler) eecahClass.newInstance(); 245 this.entityEcaHandler.setDelegator(this); 246 } catch (ClassNotFoundException e) { 247 Debug.logWarning(e, "EntityEcaHandler class with name " + entityEcaHandlerClassName + " was not found, Entity ECA Rules will be disabled", module); 248 } catch (InstantiationException e) { 249 Debug.logWarning(e, "EntityEcaHandler class with name " + entityEcaHandlerClassName + " could not be instantiated, Entity ECA Rules will be disabled", module); 250 } catch (IllegalAccessException e) { 251 Debug.logWarning(e, "EntityEcaHandler class with name " + entityEcaHandlerClassName + " could not be accessed (illegal), Entity ECA Rules will be disabled", module); 252 } catch (ClassCastException e) { 253 Debug.logWarning(e, "EntityEcaHandler class with name " + entityEcaHandlerClassName + " does not implement the EntityEcaHandler interface, Entity ECA Rules will be disabled", module); 254 } 255 } else { 256 Debug.logInfo("Entity ECA Handler disabled for delegator [" + delegatorName + "]", module); 257 } 258 } 259 260 263 public String getDelegatorName() { 264 return this.delegatorName; 265 } 266 267 protected DelegatorInfo getDelegatorInfo() { 268 if (delegatorInfo == null) { 269 delegatorInfo = EntityConfigUtil.getDelegatorInfo(this.delegatorName); 270 } 271 return delegatorInfo; 272 } 273 274 277 public ModelReader getModelReader() { 278 if (keepLocalReaders) { 279 return this.modelReader; 280 } else { 281 try { 282 return ModelReader.getModelReader(delegatorName); 283 } catch (GenericEntityException e) { 284 Debug.logError(e, "Error loading entity model", module); 285 return null; 286 } 287 } 288 } 289 290 293 public ModelGroupReader getModelGroupReader() { 294 if (keepLocalReaders) { 295 return this.modelGroupReader; 296 } else { 297 try { 298 return ModelGroupReader.getModelGroupReader(delegatorName); 299 } catch (GenericEntityException e) { 300 Debug.logError(e, "Error loading entity group model", module); 301 return null; 302 } 303 } 304 } 305 306 310 public ModelEntity getModelEntity(String entityName) { 311 try { 312 return getModelReader().getModelEntity(entityName); 313 } catch (GenericEntityException e) { 314 Debug.logError(e, "Error getting entity definition from model", module); 315 return null; 316 } 317 } 318 319 323 public String getEntityGroupName(String entityName) { 324 String groupName = getModelGroupReader().getEntityGroupName(entityName); 325 326 return groupName; 327 } 328 329 333 public List getModelEntitiesByGroup(String groupName) { 334 Iterator enames = UtilMisc.toIterator(getModelGroupReader().getEntityNamesByGroup(groupName)); 335 List entities = FastList.newInstance(); 336 337 if (enames == null || !enames.hasNext()) 338 return entities; 339 while (enames.hasNext()) { 340 String ename = (String ) enames.next(); 341 ModelEntity entity = this.getModelEntity(ename); 342 343 if (entity != null) 344 entities.add(entity); 345 } 346 return entities; 347 } 348 349 353 public Map getModelEntityMapByGroup(String groupName) { 354 Iterator enames = UtilMisc.toIterator(getModelGroupReader().getEntityNamesByGroup(groupName)); 355 Map entities = FastMap.newInstance(); 356 357 if (enames == null || !enames.hasNext()) { 358 return entities; 359 } 360 361 int errorCount = 0; 362 while (enames.hasNext()) { 363 String ename = (String ) enames.next(); 364 try { 365 ModelEntity entity = getModelReader().getModelEntity(ename); 366 if (entity != null) { 367 entities.put(entity.getEntityName(), entity); 368 } else { 369 throw new IllegalStateException ("Could not find entity with name " + ename); 370 } 371 } catch (GenericEntityException ex) { 372 errorCount++; 373 Debug.logError("Entity " + ename + " named in Entity Group with name " + groupName + " are not defined in any Entity Definition file", module); 374 } 375 } 376 377 if (errorCount > 0) { 378 Debug.logError(errorCount + " entities were named in ModelGroup but not defined in any EntityModel", module); 379 } 380 381 return entities; 382 } 383 384 388 public String getGroupHelperName(String groupName) { 389 return (String ) this.getDelegatorInfo().groupMap.get(groupName); 390 } 391 392 396 public String getEntityHelperName(String entityName) { 397 String groupName = getModelGroupReader().getEntityGroupName(entityName); 398 399 return this.getGroupHelperName(groupName); 400 } 401 402 406 public String getEntityHelperName(ModelEntity entity) { 407 if (entity == null) 408 return null; 409 return getEntityHelperName(entity.getEntityName()); 410 } 411 412 416 public GenericHelper getEntityHelper(String entityName) throws GenericEntityException { 417 String helperName = getEntityHelperName(entityName); 418 419 if (helperName != null && helperName.length() > 0) 420 return GenericHelperFactory.getHelper(helperName); 421 else 422 throw new GenericEntityException("Helper name not found for entity " + entityName); 423 } 424 425 429 public GenericHelper getEntityHelper(ModelEntity entity) throws GenericEntityException { 430 return getEntityHelper(entity.getEntityName()); 431 } 432 433 438 public ModelFieldType getEntityFieldType(ModelEntity entity, String type) throws GenericEntityException { 439 String helperName = getEntityHelperName(entity); 440 441 if (helperName == null || helperName.length() <= 0) 442 return null; 443 ModelFieldTypeReader modelFieldTypeReader = ModelFieldTypeReader.getModelFieldTypeReader(helperName); 444 445 if (modelFieldTypeReader == null) { 446 throw new GenericEntityException("ModelFieldTypeReader not found for entity " + entity.getEntityName() + " with helper name " + helperName); 447 } 448 return modelFieldTypeReader.getModelFieldType(type); 449 } 450 451 455 public Collection getEntityFieldTypeNames(ModelEntity entity) throws GenericEntityException { 456 String helperName = getEntityHelperName(entity); 457 458 if (helperName == null || helperName.length() <= 0) 459 return null; 460 ModelFieldTypeReader modelFieldTypeReader = ModelFieldTypeReader.getModelFieldTypeReader(helperName); 461 462 if (modelFieldTypeReader == null) { 463 throw new GenericEntityException("ModelFieldTypeReader not found for entity " + entity.getEntityName() + " with helper name " + helperName); 464 } 465 return modelFieldTypeReader.getFieldTypeNames(); 466 } 467 468 469 public GenericValue makeValue(String entityName, Map fields) { 470 ModelEntity entity = this.getModelEntity(entityName); 471 if (entity == null) { 472 throw new IllegalArgumentException ("[GenericDelegator.makeValue] could not find entity for entityName: " + entityName); 473 } 474 GenericValue value = GenericValue.create(entity, fields); 475 value.setDelegator(this); 476 return value; 477 } 478 479 480 public GenericValue makeValidValue(String entityName, Map fields) { 481 ModelEntity entity = this.getModelEntity(entityName); 482 if (entity == null) { 483 throw new IllegalArgumentException ("[GenericDelegator.makeValidValue] could not find entity for entityName: " + entityName); 484 } 485 GenericValue value = GenericValue.create(entity, null); 486 value.setPKFields(fields, true); 487 value.setNonPKFields(fields, true); 488 value.setDelegator(this); 489 return value; 490 } 491 492 493 public GenericPK makePK(String entityName, Map fields) { 494 ModelEntity entity = this.getModelEntity(entityName); 495 if (entity == null) { 496 throw new IllegalArgumentException ("[GenericDelegator.makePK] could not find entity for entityName: " + entityName); 497 } 498 GenericPK pk = GenericPK.create(entity, fields); 499 500 pk.setDelegator(this); 501 return pk; 502 } 503 504 508 public GenericValue create(GenericPK primaryKey) throws GenericEntityException { 509 return this.create(primaryKey, true); 510 } 511 512 517 public GenericValue create(GenericPK primaryKey, boolean doCacheClear) throws GenericEntityException { 518 if (primaryKey == null) { 519 throw new GenericEntityException("Cannot create from a null primaryKey"); 520 } 521 522 return this.create(GenericValue.create(primaryKey), doCacheClear); 523 } 524 525 528 public GenericValue create(String entityName, Map fields) throws GenericEntityException { 529 if (entityName == null || fields == null) { 530 return null; 531 } 532 ModelEntity entity = this.getModelReader().getModelEntity(entityName); 533 GenericValue genericValue = GenericValue.create(entity, fields); 534 535 return this.create(genericValue, true); 536 } 537 538 542 public GenericValue create(GenericValue value) throws GenericEntityException { 543 return this.create(value, true); 544 } 545 546 551 public GenericValue create(GenericValue value, boolean doCacheClear) throws GenericEntityException { 552 boolean beganTransaction = false; 553 try { 554 if (alwaysUseTransaction) { 555 beganTransaction = TransactionUtil.begin(); 556 } 557 558 Map ecaEventMap = this.getEcaEntityEventMap(value.getEntityName()); 559 this.evalEcaRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_CREATE, value, ecaEventMap, (ecaEventMap == null), false); 560 561 if (value == null) { 562 throw new GenericEntityException("Cannot create a null value"); 563 } 564 GenericHelper helper = getEntityHelper(value.getEntityName()); 565 566 this.evalEcaRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_CREATE, value, ecaEventMap, (ecaEventMap == null), false); 567 568 value.setDelegator(this); 569 this.encryptFields(value); 570 value = helper.create(value); 571 572 if (value != null) { 573 value.setDelegator(this); 574 if (value.lockEnabled()) { 575 refresh(value, doCacheClear); 576 } else { 577 if (doCacheClear) { 578 this.evalEcaRules(EntityEcaHandler.EV_CACHE_CLEAR, EntityEcaHandler.OP_CREATE, value, ecaEventMap, (ecaEventMap == null), false); 579 this.clearCacheLine(value); 580 } 581 } 582 } 583 584 this.evalEcaRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_CREATE, value, ecaEventMap, (ecaEventMap == null), false); 585 return value; 586 } catch (GenericEntityException e) { 587 String errMsg = "Failure in create operation for entity [" + value.getEntityName() + "]: " + e.toString() + ". Rolling back transaction."; 588 Debug.logError(e, errMsg, module); 589 try { 590 TransactionUtil.rollback(beganTransaction, errMsg, e); 592 } catch (GenericEntityException e2) { 593 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 594 } 595 throw e; 597 } finally { 598 TransactionUtil.commit(beganTransaction); 600 } 601 } 602 603 608 public GenericValue createOrStore(GenericValue value, boolean doCacheClear) throws GenericEntityException { 609 boolean beganTransaction = false; 610 try { 611 if (alwaysUseTransaction) { 612 beganTransaction = TransactionUtil.begin(); 613 } 614 615 GenericValue checkValue = this.findByPrimaryKey(value.getPrimaryKey()); 616 if (checkValue != null) { 617 this.store(value, doCacheClear); 618 } else { 619 this.create(value, doCacheClear); 620 } 621 if (value.lockEnabled()) { 622 this.refresh(value); 623 } 624 625 return value; 626 } catch (GenericEntityException e) { 627 String errMsg = "Failure in createOrStore operation for entity [" + value.getEntityName() + "]: " + e.toString() + ". Rolling back transaction."; 628 Debug.logError(e, errMsg, module); 629 try { 630 TransactionUtil.rollback(beganTransaction, errMsg, e); 632 } catch (GenericEntityException e2) { 633 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 634 } 635 throw e; 637 } finally { 638 TransactionUtil.commit(beganTransaction); 640 } 641 } 642 643 647 public GenericValue createOrStore(GenericValue value) throws GenericEntityException { 648 return createOrStore(value, true); 649 } 650 651 protected void saveEntitySyncRemoveInfo(GenericEntity dummyPK) throws GenericEntityException { 652 if (dummyPK.getModelEntity().getNoAutoStamp()) { 654 return; 655 } 656 657 if (dummyPK.getIsFromEntitySync()) { 659 return; 660 } 661 662 String serializedPK = null; 663 try { 664 serializedPK = XmlSerializer.serialize(dummyPK); 665 } catch (SerializeException e) { 666 Debug.logError(e, "Could not serialize primary key to save EntitySyncRemove", module); 667 } catch (FileNotFoundException e) { 668 Debug.logError(e, "Could not serialize primary key to save EntitySyncRemove", module); 669 } catch (IOException e) { 670 Debug.logError(e, "Could not serialize primary key to save EntitySyncRemove", module); 671 } 672 673 if (serializedPK != null) { 674 GenericValue entitySyncRemove = this.makeValue("EntitySyncRemove", null); 675 entitySyncRemove.set("entitySyncRemoveId", this.getNextSeqId("EntitySyncRemove")); 676 entitySyncRemove.set("primaryKeyRemoved", serializedPK); 677 entitySyncRemove.create(); 678 } 679 } 680 681 685 public int removeByPrimaryKey(GenericPK primaryKey) throws GenericEntityException { 686 int retVal = this.removeByPrimaryKey(primaryKey, true); 687 return retVal; 688 } 689 690 695 public int removeByPrimaryKey(GenericPK primaryKey, boolean doCacheClear) throws GenericEntityException { 696 boolean beganTransaction = false; 697 try { 698 if (alwaysUseTransaction) { 699 beganTransaction = TransactionUtil.begin(); 700 } 701 702 Map ecaEventMap = this.getEcaEntityEventMap(primaryKey.getEntityName()); 703 this.evalEcaRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_REMOVE, primaryKey, ecaEventMap, (ecaEventMap == null), false); 704 705 GenericHelper helper = getEntityHelper(primaryKey.getEntityName()); 706 707 if (doCacheClear) { 708 this.evalEcaRules(EntityEcaHandler.EV_CACHE_CLEAR, EntityEcaHandler.OP_REMOVE, primaryKey, ecaEventMap, (ecaEventMap == null), false); 710 this.clearCacheLine(primaryKey); 711 } 712 713 this.evalEcaRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_REMOVE, primaryKey, ecaEventMap, (ecaEventMap == null), false); 714 int num = helper.removeByPrimaryKey(primaryKey); 715 this.saveEntitySyncRemoveInfo(primaryKey); 716 717 this.evalEcaRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_REMOVE, primaryKey, ecaEventMap, (ecaEventMap == null), false); 718 return num; 719 } catch (GenericEntityException e) { 720 String errMsg = "Failure in removeByPrimaryKey operation for entity [" + primaryKey.getEntityName() + "]: " + e.toString() + ". Rolling back transaction."; 721 Debug.logError(e, errMsg, module); 722 try { 723 TransactionUtil.rollback(beganTransaction, errMsg, e); 725 } catch (GenericEntityException e2) { 726 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 727 } 728 throw e; 730 } finally { 731 TransactionUtil.commit(beganTransaction); 733 } 734 } 735 736 740 public int removeValue(GenericValue value) throws GenericEntityException { 741 return this.removeValue(value, true); 742 } 743 744 749 public int removeValue(GenericValue value, boolean doCacheClear) throws GenericEntityException { 750 boolean beganTransaction = false; 752 try { 753 if (alwaysUseTransaction) { 754 beganTransaction = TransactionUtil.begin(); 755 } 756 757 Map ecaEventMap = this.getEcaEntityEventMap(value.getEntityName()); 758 this.evalEcaRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_REMOVE, value, ecaEventMap, (ecaEventMap == null), false); 759 760 GenericHelper helper = getEntityHelper(value.getEntityName()); 761 762 if (doCacheClear) { 763 this.evalEcaRules(EntityEcaHandler.EV_CACHE_CLEAR, EntityEcaHandler.OP_REMOVE, value, ecaEventMap, (ecaEventMap == null), false); 764 this.clearCacheLine(value); 765 } 766 767 this.evalEcaRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_REMOVE, value, ecaEventMap, (ecaEventMap == null), false); 768 int num = helper.removeByPrimaryKey(value.getPrimaryKey()); 769 this.saveEntitySyncRemoveInfo(value.getPrimaryKey()); 770 771 this.evalEcaRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_REMOVE, value, ecaEventMap, (ecaEventMap == null), false); 772 return num; 773 } catch (GenericEntityException e) { 774 String errMsg = "Failure in removeValue operation for entity [" + value.getEntityName() + "]: " + e.toString() + ". Rolling back transaction."; 775 Debug.logError(e, errMsg, module); 776 try { 777 TransactionUtil.rollback(beganTransaction, errMsg, e); 779 } catch (GenericEntityException e2) { 780 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 781 } 782 throw e; 784 } finally { 785 TransactionUtil.commit(beganTransaction); 787 } 788 } 789 790 795 public int removeByAnd(String entityName, Map fields) throws GenericEntityException { 796 return this.removeByAnd(entityName, fields, true); 797 } 798 799 805 public int removeByAnd(String entityName, Map fields, boolean doCacheClear) throws GenericEntityException { 806 EntityCondition ecl = new EntityFieldMap(fields, EntityOperator.AND); 807 return removeByCondition(entityName, ecl, doCacheClear); 808 } 809 810 815 public int removeByCondition(String entityName, EntityCondition condition) throws GenericEntityException { 816 return this.removeByCondition(entityName, condition, true); 817 } 818 819 825 public int removeByCondition(String entityName, EntityCondition condition, boolean doCacheClear) throws GenericEntityException { 826 boolean beganTransaction = false; 827 try { 828 if (alwaysUseTransaction) { 829 beganTransaction = TransactionUtil.begin(); 830 } 831 832 if (doCacheClear) { 833 this.clearCacheLineByCondition(entityName, condition); 835 } 836 ModelEntity modelEntity = getModelReader().getModelEntity(entityName); 837 GenericHelper helper = getEntityHelper(entityName); 838 839 return helper.removeByCondition(modelEntity, condition); 840 } catch (GenericEntityException e) { 841 String errMsg = "Failure in removeByCondition operation for entity [" + entityName + "]: " + e.toString() + ". Rolling back transaction."; 842 Debug.logError(e, errMsg, module); 843 try { 844 TransactionUtil.rollback(beganTransaction, errMsg, e); 846 } catch (GenericEntityException e2) { 847 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 848 } 849 throw e; 851 } finally { 852 TransactionUtil.commit(beganTransaction); 854 } 855 } 856 857 864 public int removeRelated(String relationName, GenericValue value) throws GenericEntityException { 865 return this.removeRelated(relationName, value, true); 866 } 867 868 876 public int removeRelated(String relationName, GenericValue value, boolean doCacheClear) throws GenericEntityException { 877 ModelEntity modelEntity = value.getModelEntity(); 878 ModelRelation relation = modelEntity.getRelation(relationName); 879 880 if (relation == null) { 881 throw new GenericModelException("Could not find relation for relationName: " + relationName + " for value " + value); 882 } 883 884 Map fields = FastMap.newInstance(); 885 for (int i = 0; i < relation.getKeyMapsSize(); i++) { 886 ModelKeyMap keyMap = relation.getKeyMap(i); 887 fields.put(keyMap.getRelFieldName(), value.get(keyMap.getFieldName())); 888 } 889 890 return this.removeByAnd(relation.getRelEntityName(), fields, doCacheClear); 891 } 892 893 896 public void refresh(GenericValue value) throws GenericEntityException { 897 this.refresh(value, true); 898 } 899 900 904 public void refresh(GenericValue value, boolean doCacheClear) throws GenericEntityException { 905 if (doCacheClear) { 906 clearCacheLine(value); 908 } 909 GenericPK pk = value.getPrimaryKey(); 910 GenericValue newValue = findByPrimaryKey(pk); 911 value.refreshFromValue(newValue); 912 } 913 914 917 public void refreshFromCache(GenericValue value) throws GenericEntityException { 918 GenericPK pk = value.getPrimaryKey(); 919 GenericValue newValue = findByPrimaryKeyCache(pk); 920 value.refreshFromValue(newValue); 921 } 922 923 930 public int storeByCondition(String entityName, Map fieldsToSet, EntityCondition condition) throws GenericEntityException { 931 return storeByCondition(entityName, fieldsToSet, condition, true); 932 } 933 934 942 public int storeByCondition(String entityName, Map fieldsToSet, EntityCondition condition, boolean doCacheClear) throws GenericEntityException { 943 boolean beganTransaction = false; 944 try { 945 if (alwaysUseTransaction) { 946 beganTransaction = TransactionUtil.begin(); 947 } 948 949 if (doCacheClear) { 950 this.clearCacheLineByCondition(entityName, condition); 952 } 953 ModelEntity modelEntity = getModelReader().getModelEntity(entityName); 954 GenericHelper helper = getEntityHelper(entityName); 955 956 return helper.storeByCondition(modelEntity, fieldsToSet, condition); 957 } catch (GenericEntityException e) { 958 String errMsg = "Failure in storeByCondition operation for entity [" + entityName + "]: " + e.toString() + ". Rolling back transaction."; 959 Debug.logError(e, errMsg, module); 960 try { 961 TransactionUtil.rollback(beganTransaction, errMsg, e); 963 } catch (GenericEntityException e2) { 964 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 965 } 966 throw e; 968 } finally { 969 TransactionUtil.commit(beganTransaction); 971 } 972 } 973 974 978 public int store(GenericValue value) throws GenericEntityException { 979 return this.store(value, true); 980 } 981 982 987 public int store(GenericValue value, boolean doCacheClear) throws GenericEntityException { 988 boolean beganTransaction = false; 989 try { 990 if (alwaysUseTransaction) { 991 beganTransaction = TransactionUtil.begin(); 992 } 993 994 Map ecaEventMap = this.getEcaEntityEventMap(value.getEntityName()); 995 this.evalEcaRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_STORE, value, ecaEventMap, (ecaEventMap == null), false); 996 GenericHelper helper = getEntityHelper(value.getEntityName()); 997 998 if (doCacheClear) { 999 this.evalEcaRules(EntityEcaHandler.EV_CACHE_CLEAR, EntityEcaHandler.OP_STORE, value, ecaEventMap, (ecaEventMap == null), false); 1001 this.clearCacheLine(value); 1002 } 1003 1004 this.evalEcaRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_STORE, value, ecaEventMap, (ecaEventMap == null), false); 1005 this.encryptFields(value); 1006 int retVal = helper.store(value); 1007 1008 if (value.lockEnabled()) { 1010 refresh(value, doCacheClear); 1011 } 1012 1013 this.evalEcaRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_STORE, value, ecaEventMap, (ecaEventMap == null), false); 1014 return retVal; 1015 } catch (GenericEntityException e) { 1016 String errMsg = "Failure in store operation for entity [" + value.getEntityName() + "]: " + e.toString() + ". Rolling back transaction."; 1017 Debug.logError(e, errMsg, module); 1018 try { 1019 TransactionUtil.rollback(beganTransaction, errMsg, e); 1021 } catch (GenericEntityException e2) { 1022 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 1023 } 1024 throw e; 1026 } finally { 1027 TransactionUtil.commit(beganTransaction); 1029 } 1030 } 1031 1032 1042 public int storeAll(List values) throws GenericEntityException { 1043 return this.storeAll(values, true); 1044 } 1045 1046 1057 public int storeAll(List values, boolean doCacheClear) throws GenericEntityException { 1058 return this.storeAll(values, doCacheClear, false); 1059 } 1060 1061 1073 public int storeAll(List values, boolean doCacheClear, boolean createDummyFks) throws GenericEntityException { 1074 if (values == null) { 1075 return 0; 1076 } 1077 1078 int numberChanged = 0; 1079 1080 boolean beganTransaction = false; 1081 try { 1082 beganTransaction = TransactionUtil.begin(); 1083 1084 Iterator viter = values.iterator(); 1085 while (viter.hasNext()) { 1086 GenericValue value = (GenericValue) viter.next(); 1087 String entityName = value.getEntityName(); 1088 GenericPK primaryKey = value.getPrimaryKey(); 1089 Map ecaEventMap = this.getEcaEntityEventMap(entityName); 1090 GenericHelper helper = getEntityHelper(entityName); 1091 1092 if (!primaryKey.isPrimaryKey()) { 1095 throw new GenericModelException("[GenericDelegator.storeAll] One of the passed primary keys is not a valid primary key: " + primaryKey); 1096 } 1097 GenericValue existing = null; 1098 try { 1099 existing = helper.findByPrimaryKey(primaryKey); 1100 this.decryptFields(existing); 1101 } catch (GenericEntityNotFoundException e) { 1102 existing = null; 1103 } 1104 1105 if (existing == null) { 1106 if (createDummyFks) { 1107 value.checkFks(true); 1108 } 1109 this.create(value, doCacheClear); 1110 numberChanged++; 1111 } else { 1112 ModelEntity modelEntity = value.getModelEntity(); 1114 GenericValue toStore = GenericValue.create(modelEntity, value.getPrimaryKey()); 1115 toStore.setDelegator(this); 1116 boolean atLeastOneField = false; 1117 Iterator nonPksIter = modelEntity.getNopksIterator(); 1118 while (nonPksIter.hasNext()) { 1119 ModelField modelField = (ModelField) nonPksIter.next(); 1120 String fieldName = modelField.getName(); 1121 if (value.containsKey(fieldName)) { 1122 Object fieldValue = value.get(fieldName); 1123 Object oldValue = existing.get(fieldName); 1124 if ((fieldValue == null && oldValue != null) || (fieldValue != null && !fieldValue.equals(oldValue))) { 1125 toStore.put(fieldName, fieldValue); 1126 atLeastOneField = true; 1127 } 1128 } 1129 } 1130 1131 if (atLeastOneField) { 1132 if (createDummyFks) { 1133 value.checkFks(true); 1134 } 1135 numberChanged += this.store(toStore, doCacheClear); 1136 } 1137 } 1138 } 1139 1140 return numberChanged; 1141 } catch (GenericEntityException e) { 1142 String errMsg = "Failure in storeAll operation: " + e.toString() + ". Rolling back transaction."; 1143 Debug.logError(e, errMsg, module); 1144 try { 1145 TransactionUtil.rollback(beganTransaction, errMsg, e); 1147 } catch (GenericEntityException e2) { 1148 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 1149 } 1150 throw e; 1152 } finally { 1153 TransactionUtil.commit(beganTransaction); 1155 } 1156 } 1157 1158 1169 public int removeAll(List dummyPKs) throws GenericEntityException { 1170 return this.removeAll(dummyPKs, true); 1171 } 1172 1173 1185 public int removeAll(List dummyPKs, boolean doCacheClear) throws GenericEntityException { 1186 if (dummyPKs == null) { 1187 return 0; 1188 } 1189 1190 boolean beganTransaction = false; 1191 int numRemoved = 0; 1192 1193 try { 1194 Iterator viter = dummyPKs.iterator(); 1195 while (viter.hasNext()) { 1196 GenericEntity value = (GenericEntity) viter.next(); 1197 if (value.containsPrimaryKey()) { 1198 numRemoved += this.removeByPrimaryKey(value.getPrimaryKey(), doCacheClear); 1199 } else { 1200 numRemoved += this.removeByAnd(value.getEntityName(), value.getAllFields(), doCacheClear); 1201 } 1202 } 1203 1204 return numRemoved; 1205 } catch (GenericEntityException e) { 1206 String errMsg = "Failure in removeAll operation: " + e.toString() + ". Rolling back transaction."; 1207 Debug.logError(e, errMsg, module); 1208 try { 1209 TransactionUtil.rollback(beganTransaction, errMsg, e); 1211 } catch (GenericEntityException e2) { 1212 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 1213 } 1214 throw e; 1216 } finally { 1217 TransactionUtil.commit(beganTransaction); 1219 } 1220 } 1221 1222 1226 1230 public GenericValue findByPrimaryKey(GenericPK primaryKey) throws GenericEntityException { 1231 boolean beganTransaction = false; 1232 try { 1233 if (alwaysUseTransaction) { 1234 beganTransaction = TransactionUtil.begin(); 1235 } 1236 1237 Map ecaEventMap = this.getEcaEntityEventMap(primaryKey.getEntityName()); 1238 this.evalEcaRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false); 1239 1240 GenericHelper helper = getEntityHelper(primaryKey.getEntityName()); 1241 GenericValue value = null; 1242 1243 if (!primaryKey.isPrimaryKey()) { 1244 throw new GenericModelException("[GenericDelegator.findByPrimaryKey] Passed primary key is not a valid primary key: " + primaryKey); 1245 } 1246 this.evalEcaRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false); 1247 try { 1248 value = helper.findByPrimaryKey(primaryKey); 1249 } catch (GenericEntityNotFoundException e) { 1250 value = null; 1251 } 1252 if (value != null) { 1253 value.setDelegator(this); 1254 this.decryptFields(value); 1255 } 1256 1257 this.evalEcaRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false); 1258 return value; 1259 } catch (GenericEntityException e) { 1260 String errMsg = "Failure in findByPrimaryKey operation for entity [" + primaryKey.getEntityName() + "]: " + e.toString() + ". Rolling back transaction."; 1261 Debug.logError(e, errMsg, module); 1262 try { 1263 TransactionUtil.rollback(beganTransaction, errMsg, e); 1265 } catch (GenericEntityException e2) { 1266 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 1267 } 1268 throw e; 1270 } finally { 1271 TransactionUtil.commit(beganTransaction); 1273 } 1274 } 1275 1276 1280 public GenericValue findByPrimaryKeyCache(GenericPK primaryKey) throws GenericEntityException { 1281 Map ecaEventMap = this.getEcaEntityEventMap(primaryKey.getEntityName()); 1282 this.evalEcaRules(EntityEcaHandler.EV_CACHE_CHECK, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false); 1283 1284 GenericValue value = this.getFromPrimaryKeyCache(primaryKey); 1285 if (value instanceof GenericEntity.NULL) return null; 1286 if (value == null) { 1287 value = findByPrimaryKey(primaryKey); 1288 if (value != null) { 1289 this.evalEcaRules(EntityEcaHandler.EV_CACHE_PUT, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false); 1290 this.putInPrimaryKeyCache(primaryKey, value); 1291 } else { 1292 this.putInPrimaryKeyCache(primaryKey, GenericValue.NULL_VALUE); 1293 } 1294 } 1295 return value; 1296 } 1297 1298 1303 public GenericValue findByPrimaryKey(String entityName, Map fields) throws GenericEntityException { 1304 return findByPrimaryKey(makePK(entityName, fields)); 1305 } 1306 1307 1312 public GenericValue findByPrimaryKeyCache(String entityName, Map fields) throws GenericEntityException { 1313 return findByPrimaryKeyCache(makePK(entityName, fields)); 1314 } 1315 1316 1321 public GenericValue findByPrimaryKeyPartial(GenericPK primaryKey, Set keys) throws GenericEntityException { 1322 boolean beganTransaction = false; 1323 try { 1324 if (alwaysUseTransaction) { 1325 beganTransaction = TransactionUtil.begin(); 1326 } 1327 1328 Map ecaEventMap = this.getEcaEntityEventMap(primaryKey.getEntityName()); 1329 this.evalEcaRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false); 1330 1331 GenericHelper helper = getEntityHelper(primaryKey.getEntityName()); 1332 GenericValue value = null; 1333 1334 if (!primaryKey.isPrimaryKey()) { 1335 throw new GenericModelException("[GenericDelegator.findByPrimaryKey] Passed primary key is not a valid primary key: " + primaryKey); 1336 } 1337 1338 this.evalEcaRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false); 1339 try { 1340 value = helper.findByPrimaryKeyPartial(primaryKey, keys); 1341 } catch (GenericEntityNotFoundException e) { 1342 value = null; 1343 } 1344 if (value != null) value.setDelegator(this); 1345 1346 this.evalEcaRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false); 1347 return value; 1348 } catch (GenericEntityException e) { 1349 String errMsg = "Failure in findByPrimaryKeyPartial operation for entity [" + primaryKey.getEntityName() + "]: " + e.toString() + ". Rolling back transaction."; 1350 Debug.logError(e, errMsg, module); 1351 try { 1352 TransactionUtil.rollback(beganTransaction, errMsg, e); 1354 } catch (GenericEntityException e2) { 1355 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 1356 } 1357 throw e; 1359 } finally { 1360 TransactionUtil.commit(beganTransaction); 1362 } 1363 } 1364 1365 1369 public List findAllByPrimaryKeys(Collection primaryKeys) throws GenericEntityException { 1370 boolean beganTransaction = false; 1371 try { 1372 if (alwaysUseTransaction) { 1373 beganTransaction = TransactionUtil.begin(); 1374 } 1375 1376 if (primaryKeys == null) return null; 1379 List results = FastList.newInstance(); 1380 1381 Map pksPerHelper = FastMap.newInstance(); 1384 Iterator pkiter = primaryKeys.iterator(); 1385 while (pkiter.hasNext()) { 1386 GenericPK curPK = (GenericPK) pkiter.next(); 1387 String helperName = this.getEntityHelperName(curPK.getEntityName()); 1388 List pks = (List ) pksPerHelper.get(helperName); 1389 1390 if (pks == null) { 1391 pks = FastList.newInstance(); 1392 pksPerHelper.put(helperName, pks); 1393 } 1394 pks.add(curPK); 1395 } 1396 1397 Iterator helperIter = pksPerHelper.entrySet().iterator(); 1398 1399 while (helperIter.hasNext()) { 1400 Map.Entry curEntry = (Map.Entry ) helperIter.next(); 1401 String helperName = (String ) curEntry.getKey(); 1402 GenericHelper helper = GenericHelperFactory.getHelper(helperName); 1403 List values = helper.findAllByPrimaryKeys((List ) curEntry.getValue()); 1404 1405 results.addAll(values); 1406 } 1407 1408 this.decryptFields(results); 1409 return results; 1410 } catch (GenericEntityException e) { 1411 String errMsg = "Failure in findAllByPrimaryKeys operation, rolling back transaction"; 1412 Debug.logError(e, errMsg, module); 1413 try { 1414 TransactionUtil.rollback(beganTransaction, errMsg, e); 1416 } catch (GenericEntityException e2) { 1417 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 1418 } 1419 throw e; 1421 } finally { 1422 TransactionUtil.commit(beganTransaction); 1424 } 1425 } 1426 1427 1434 public List findAllByPrimaryKeysCache(Collection primaryKeys) throws GenericEntityException { 1435 boolean beganTransaction = false; 1436 try { 1437 if (alwaysUseTransaction) { 1438 beganTransaction = TransactionUtil.begin(); 1439 } 1440 1441 if (primaryKeys == null) 1444 return null; 1445 List results = FastList.newInstance(); 1446 1447 Map pksPerHelper = FastMap.newInstance(); 1450 Iterator pkiter = primaryKeys.iterator(); 1451 1452 while (pkiter.hasNext()) { 1453 GenericPK curPK = (GenericPK) pkiter.next(); 1454 1455 GenericValue value = this.getFromPrimaryKeyCache(curPK); 1456 1457 if (value != null) { 1458 results.add(value); 1460 } else { 1461 String helperName = this.getEntityHelperName(curPK.getEntityName()); 1463 List pks = (List ) pksPerHelper.get(helperName); 1464 1465 if (pks == null) { 1466 pks = FastList.newInstance(); 1467 pksPerHelper.put(helperName, pks); 1468 } 1469 pks.add(curPK); 1470 } 1471 } 1472 1473 Iterator helperIter = pksPerHelper.entrySet().iterator(); 1474 1475 while (helperIter.hasNext()) { 1476 Map.Entry curEntry = (Map.Entry ) helperIter.next(); 1477 String helperName = (String ) curEntry.getKey(); 1478 GenericHelper helper = GenericHelperFactory.getHelper(helperName); 1479 List values = helper.findAllByPrimaryKeys((List ) curEntry.getValue()); 1480 1481 this.putAllInPrimaryKeyCache(values); 1482 results.addAll(values); 1483 } 1484 1485 this.decryptFields(results); 1486 return results; 1487 } catch (GenericEntityException e) { 1488 String errMsg = "Failure in findAllByPrimaryKeysCache operation, rolling back transaction"; 1489 Debug.logError(e, errMsg, module); 1490 try { 1491 TransactionUtil.rollback(beganTransaction, errMsg, e); 1493 } catch (GenericEntityException e2) { 1494 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 1495 } 1496 throw e; 1498 } finally { 1499 TransactionUtil.commit(beganTransaction); 1501 } 1502 } 1503 1504 1508 public List findAll(String entityName) throws GenericEntityException { 1509 return this.findByAnd(entityName, FastMap.newInstance(), null); 1510 } 1511 1512 1517 public List findAll(String entityName, List orderBy) throws GenericEntityException { 1518 return this.findByAnd(entityName, FastMap.newInstance(), orderBy); 1519 } 1520 1521 1525 public List findAllCache(String entityName) throws GenericEntityException { 1526 return this.findAllCache(entityName, null); 1527 } 1528 1529 1534 public List findAllCache(String entityName, List orderBy) throws GenericEntityException { 1535 GenericValue dummyValue = makeValue(entityName, null); 1536 Map ecaEventMap = this.getEcaEntityEventMap(entityName); 1537 this.evalEcaRules(EntityEcaHandler.EV_CACHE_CHECK, EntityEcaHandler.OP_FIND, dummyValue, ecaEventMap, (ecaEventMap == null), false); 1538 1539 List lst = cache.get(entityName, null, orderBy); 1540 1541 if (lst == null) { 1542 lst = findAll(entityName, orderBy); 1543 if (lst != null) { 1544 this.evalEcaRules(EntityEcaHandler.EV_CACHE_PUT, EntityEcaHandler.OP_FIND, dummyValue, ecaEventMap, (ecaEventMap == null), false); 1545 cache.put(entityName, null, orderBy, lst); 1546 } 1547 } 1548 return lst; 1549 } 1550 1551 1556 public List findByAnd(String entityName, Map fields) throws GenericEntityException { 1557 return this.findByAnd(entityName, fields, null); 1558 } 1559 1560 1565 public List findByOr(String entityName, Map fields) throws GenericEntityException { 1566 return this.findByOr(entityName, fields, null); 1567 } 1568 1569 1576 public List findByAnd(String entityName, Map fields, List orderBy) throws GenericEntityException { 1577 EntityCondition ecl = new EntityFieldMap(fields, EntityOperator.AND); 1578 return findByCondition(entityName, ecl, null, orderBy); 1579 } 1580 1581 1585 1586 1593 public List findByOr(String entityName, Map fields, List orderBy) throws GenericEntityException { 1594 EntityCondition ecl = new EntityFieldMap(fields, EntityOperator.OR); 1595 return findByCondition(entityName, ecl, null, orderBy); 1596 } 1597 1598 1603 public List findByAndCache(String entityName, Map fields) throws GenericEntityException { 1604 return this.findByAndCache(entityName, fields, null); 1605 } 1606 1607 1613 public List findByAndCache(String entityName, Map fields, List orderBy) throws GenericEntityException { 1614 return findByConditionCache(entityName, new EntityFieldMap(fields, EntityOperator.AND), null, orderBy); 1615 } 1616 1617 1622 public List findByAnd(String entityName, List expressions) throws GenericEntityException { 1623 EntityConditionList ecl = new EntityConditionList(expressions, EntityOperator.AND); 1624 return findByCondition(entityName, ecl, null, null); 1625 } 1626 1627 1633 public List findByAnd(String entityName, List expressions, List orderBy) throws GenericEntityException { 1634 EntityConditionList ecl = new EntityConditionList(expressions, EntityOperator.AND); 1635 return findByCondition(entityName, ecl, null, orderBy); 1636 } 1637 1638 1643 public List findByOr(String entityName, List expressions) throws GenericEntityException { 1644 EntityConditionList ecl = new EntityConditionList(expressions, EntityOperator.OR); 1645 return findByCondition(entityName, ecl, null, null); 1646 } 1647 1648 1654 public List findByOr(String entityName, List expressions, List orderBy) throws GenericEntityException { 1655 EntityConditionList ecl = new EntityConditionList(expressions, EntityOperator.OR); 1656 return findByCondition(entityName, ecl, null, orderBy); 1657 } 1658 1659 public List findByLike(String entityName, Map fields) throws GenericEntityException { 1660 return findByLike(entityName, fields, null); 1661 } 1662 1663 public List findByLike(String entityName, Map fields, List orderBy) throws GenericEntityException { 1664 List likeExpressions = FastList.newInstance(); 1665 if (fields != null) { 1666 Iterator fieldEntries = fields.entrySet().iterator(); 1667 while (fieldEntries.hasNext()) { 1668 Map.Entry fieldEntry = (Map.Entry ) fieldEntries.next(); 1669 likeExpressions.add(new EntityExpr((String ) fieldEntry.getKey(), EntityOperator.LIKE, fieldEntry.getValue())); 1670 } 1671 } 1672 EntityConditionList ecl = new EntityConditionList(likeExpressions, EntityOperator.AND); 1673 return findByCondition(entityName, ecl, null, orderBy); 1674 } 1675 1676 1683 public List findByCondition(String entityName, EntityCondition entityCondition, Collection fieldsToSelect, List orderBy) throws GenericEntityException { 1684 return this.findByCondition(entityName, entityCondition, null, fieldsToSelect, orderBy, null); 1685 } 1686 1687 1696 public List findByCondition(String entityName, EntityCondition whereEntityCondition, 1697 EntityCondition havingEntityCondition, Collection fieldsToSelect, List orderBy, EntityFindOptions findOptions) 1698 throws GenericEntityException { 1699 boolean beganTransaction = false; 1700 try { 1701 if (alwaysUseTransaction) { 1702 beganTransaction = TransactionUtil.begin(); 1703 } 1704 1705 EntityListIterator eli = this.findListIteratorByCondition(entityName, whereEntityCondition, havingEntityCondition, fieldsToSelect, orderBy, findOptions); 1706 eli.setDelegator(this); 1707 List list = eli.getCompleteList(); 1708 eli.close(); 1709 1710 return list; 1711 } catch (GenericEntityException e) { 1712 String errMsg = "Failure in findByCondition operation for entity [" + entityName + "]: " + e.toString() + ". Rolling back transaction."; 1713 Debug.logError(e, errMsg, module); 1714 try { 1715 TransactionUtil.rollback(beganTransaction, errMsg, e); 1717 } catch (GenericEntityException e2) { 1718 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 1719 } 1720 throw e; 1722 } finally { 1723 TransactionUtil.commit(beganTransaction); 1725 } 1726 } 1727 1728 1735 public List findByConditionCache(String entityName, EntityCondition entityCondition, Collection fieldsToSelect, List orderBy) throws GenericEntityException { 1736 ModelEntity modelEntity = getModelReader().getModelEntity(entityName); 1737 GenericValue dummyValue = GenericValue.create(modelEntity); 1738 Map ecaEventMap = this.getEcaEntityEventMap(entityName); 1739 this.evalEcaRules(EntityEcaHandler.EV_CACHE_CHECK, EntityEcaHandler.OP_FIND, dummyValue, ecaEventMap, (ecaEventMap == null), false); 1740 1741 List lst = cache.get(entityName, entityCondition, orderBy); 1742 1743 if (lst == null) { 1744 lst = findByCondition(entityName, entityCondition, fieldsToSelect, orderBy); 1745 if (lst != null) { 1746 this.evalEcaRules(EntityEcaHandler.EV_CACHE_PUT, EntityEcaHandler.OP_FIND, dummyValue, ecaEventMap, (ecaEventMap == null), false); 1747 cache.put(entityName, entityCondition, orderBy, lst); 1748 } 1749 } 1750 return lst; 1751 } 1752 1753 1761 public EntityListIterator findListIteratorByCondition(String entityName, EntityCondition entityCondition, 1762 Collection fieldsToSelect, List orderBy) throws GenericEntityException { 1763 return this.findListIteratorByCondition(entityName, entityCondition, null, fieldsToSelect, orderBy, null); 1764 } 1765 1766 1776 public EntityListIterator findListIteratorByCondition(String entityName, EntityCondition whereEntityCondition, 1777 EntityCondition havingEntityCondition, Collection fieldsToSelect, List orderBy, EntityFindOptions findOptions) 1778 throws GenericEntityException { 1779 1780 if (!TransactionUtil.isTransactionInPlace()) { 1782 1784 Exception newE = new Exception ("Stack Trace"); 1786 Debug.logError(newE, "ERROR: Cannot do a find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.", module); 1787 } 1788 1789 ModelEntity modelEntity = getModelReader().getModelEntity(entityName); 1790 GenericValue dummyValue = GenericValue.create(modelEntity); 1791 Map ecaEventMap = this.getEcaEntityEventMap(modelEntity.getEntityName()); 1792 this.evalEcaRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_FIND, dummyValue, ecaEventMap, (ecaEventMap == null), false); 1793 1794 if (whereEntityCondition != null) { 1795 whereEntityCondition.checkCondition(modelEntity); 1796 whereEntityCondition.encryptConditionFields(modelEntity, this); 1797 } 1798 if (havingEntityCondition != null) { 1799 havingEntityCondition.checkCondition(modelEntity); 1800 havingEntityCondition.encryptConditionFields(modelEntity, this); 1801 } 1802 1803 this.evalEcaRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_FIND, dummyValue, ecaEventMap, (ecaEventMap == null), false); 1804 GenericHelper helper = getEntityHelper(modelEntity.getEntityName()); 1805 EntityListIterator eli = helper.findListIteratorByCondition(modelEntity, whereEntityCondition, 1806 havingEntityCondition, fieldsToSelect, orderBy, findOptions); 1807 eli.setDelegator(this); 1808 1809 this.evalEcaRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_FIND, dummyValue, ecaEventMap, (ecaEventMap == null), false); 1810 return eli; 1811 } 1812 1813 1823 public EntityListIterator findListIteratorByCondition(DynamicViewEntity dynamicViewEntity, EntityCondition whereEntityCondition, 1824 EntityCondition havingEntityCondition, Collection fieldsToSelect, List orderBy, EntityFindOptions findOptions) 1825 throws GenericEntityException { 1826 1827 if (!TransactionUtil.isTransactionInPlace()) { 1829 1831 Exception newE = new Exception ("Stack Trace"); 1833 Debug.logError(newE, "ERROR: Cannot do a find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.", module); 1834 } 1835 1836 ModelViewEntity modelViewEntity = dynamicViewEntity.makeModelViewEntity(this); 1837 if (whereEntityCondition != null) whereEntityCondition.checkCondition(modelViewEntity); 1838 if (havingEntityCondition != null) havingEntityCondition.checkCondition(modelViewEntity); 1839 1840 GenericHelper helper = getEntityHelper(dynamicViewEntity.getOneRealEntityName()); 1841 EntityListIterator eli = helper.findListIteratorByCondition(modelViewEntity, whereEntityCondition, 1842 havingEntityCondition, fieldsToSelect, orderBy, findOptions); 1843 eli.setDelegator(this); 1844 return eli; 1846 } 1847 1848 public long findCountByAnd(String entityName, Map fields) throws GenericEntityException { 1849 return findCountByCondition(entityName, new EntityFieldMap(fields, EntityOperator.AND), null); 1850 } 1851 1852 public long findCountByCondition(String entityName, EntityCondition whereEntityCondition, 1853 EntityCondition havingEntityCondition) throws GenericEntityException { 1854 1855 boolean beganTransaction = false; 1856 try { 1857 if (alwaysUseTransaction) { 1858 beganTransaction = TransactionUtil.begin(); 1859 } 1860 1861 ModelEntity modelEntity = getModelReader().getModelEntity(entityName); 1862 GenericValue dummyValue = GenericValue.create(modelEntity); 1863 Map ecaEventMap = this.getEcaEntityEventMap(modelEntity.getEntityName()); 1864 this.evalEcaRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_FIND, dummyValue, ecaEventMap, (ecaEventMap == null), false); 1865 1866 if (whereEntityCondition != null) { 1867 whereEntityCondition.checkCondition(modelEntity); 1868 whereEntityCondition.encryptConditionFields(modelEntity, this); 1869 } 1870 if (havingEntityCondition != null) { 1871 havingEntityCondition.checkCondition(modelEntity); 1872 havingEntityCondition.encryptConditionFields(modelEntity, this); 1873 } 1874 1875 this.evalEcaRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_FIND, dummyValue, ecaEventMap, (ecaEventMap == null), false); 1876 GenericHelper helper = getEntityHelper(modelEntity.getEntityName()); 1877 long count = helper.findCountByCondition(modelEntity, whereEntityCondition, havingEntityCondition); 1878 1879 this.evalEcaRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_FIND, dummyValue, ecaEventMap, (ecaEventMap == null), false); 1880 return count; 1881 } catch (GenericEntityException e) { 1882 String errMsg = "Failure in findListIteratorByCondition operation for entity [DynamicView]: " + e.toString() + ". Rolling back transaction."; 1883 Debug.logError(e, errMsg, module); 1884 try { 1885 TransactionUtil.rollback(beganTransaction, errMsg, e); 1887 } catch (GenericEntityException e2) { 1888 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 1889 } 1890 throw e; 1892 } finally { 1893 TransactionUtil.commit(beganTransaction); 1895 } 1896 } 1897 1898 1910 public List getMultiRelation(GenericValue value, String relationNameOne, String relationNameTwo, List orderBy) throws GenericEntityException { 1911 boolean beganTransaction = false; 1912 try { 1913 if (alwaysUseTransaction) { 1914 beganTransaction = TransactionUtil.begin(); 1915 } 1916 1917 ModelEntity modelEntity = value.getModelEntity(); 1920 ModelRelation modelRelationOne = modelEntity.getRelation(relationNameOne); 1921 ModelEntity modelEntityOne = getModelEntity(modelRelationOne.getRelEntityName()); 1922 ModelRelation modelRelationTwo = modelEntityOne.getRelation(relationNameTwo); 1923 ModelEntity modelEntityTwo = getModelEntity(modelRelationTwo.getRelEntityName()); 1924 1925 GenericHelper helper = getEntityHelper(modelEntity); 1926 1927 return helper.findByMultiRelation(value, modelRelationOne, modelEntityOne, modelRelationTwo, modelEntityTwo, orderBy); 1928 } catch (GenericEntityException e) { 1929 String errMsg = "Failure in getMultiRelation operation for entity [" + value.getEntityName() + "]: " + e.toString() + ". Rolling back transaction."; 1930 Debug.logError(e, errMsg, module); 1931 try { 1932 TransactionUtil.rollback(beganTransaction, errMsg, e); 1934 } catch (GenericEntityException e2) { 1935 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 1936 } 1937 throw e; 1939 } finally { 1940 TransactionUtil.commit(beganTransaction); 1942 } 1943 } 1944 1945 1955 public List getMultiRelation(GenericValue value, String relationNameOne, String relationNameTwo) throws GenericEntityException { 1956 return getMultiRelation(value, relationNameOne, relationNameTwo, null); 1957 } 1958 1959 1966 public List getRelated(String relationName, GenericValue value) throws GenericEntityException { 1967 return getRelated(relationName, null, null, value); 1968 } 1969 1970 1978 public List getRelatedByAnd(String relationName, Map byAndFields, GenericValue value) throws GenericEntityException { 1979 return this.getRelated(relationName, byAndFields, null, value); 1980 } 1981 1982 1991 public List getRelatedOrderBy(String relationName, List orderBy, GenericValue value) throws GenericEntityException { 1992 return this.getRelated(relationName, null, orderBy, value); 1993 } 1994 1995 2005 public List getRelated(String relationName, Map byAndFields, List orderBy, GenericValue value) throws GenericEntityException { 2006 ModelEntity modelEntity = value.getModelEntity(); 2007 ModelRelation relation = modelEntity.getRelation(relationName); 2008 2009 if (relation == null) { 2010 throw new GenericModelException("Could not find relation for relationName: " + relationName + " for value " + value); 2011 } 2012 2013 Map fields = FastMap.newInstance(); 2016 if (byAndFields != null) fields.putAll(byAndFields); 2017 for (int i = 0; i < relation.getKeyMapsSize(); i++) { 2018 ModelKeyMap keyMap = relation.getKeyMap(i); 2019 fields.put(keyMap.getRelFieldName(), value.get(keyMap.getFieldName())); 2020 } 2021 2022 return this.findByAnd(relation.getRelEntityName(), fields, orderBy); 2023 } 2024 2025 2033 public GenericPK getRelatedDummyPK(String relationName, Map byAndFields, GenericValue value) throws GenericEntityException { 2034 ModelEntity modelEntity = value.getModelEntity(); 2035 ModelRelation relation = modelEntity.getRelation(relationName); 2036 2037 if (relation == null) { 2038 throw new GenericModelException("Could not find relation for relationName: " + relationName + " for value " + value); 2039 } 2040 ModelEntity relatedEntity = getModelReader().getModelEntity(relation.getRelEntityName()); 2041 2042 Map fields = FastMap.newInstance(); 2045 if (byAndFields != null) fields.putAll(byAndFields); 2046 for (int i = 0; i < relation.getKeyMapsSize(); i++) { 2047 ModelKeyMap keyMap = relation.getKeyMap(i); 2048 fields.put(keyMap.getRelFieldName(), value.get(keyMap.getFieldName())); 2049 } 2050 2051 GenericPK dummyPK = GenericPK.create(relatedEntity, fields); 2052 dummyPK.setDelegator(this); 2053 return dummyPK; 2054 } 2055 2056 2063 public List getRelatedCache(String relationName, GenericValue value) throws GenericEntityException { 2064 ModelEntity modelEntity = value.getModelEntity(); 2065 ModelRelation relation = modelEntity.getRelation(relationName); 2066 2067 if (relation == null) { 2068 throw new GenericModelException("Could not find relation for relationName: " + relationName + " for value " + value); 2069 } 2070 2071 Map fields = FastMap.newInstance(); 2072 for (int i = 0; i < relation.getKeyMapsSize(); i++) { 2073 ModelKeyMap keyMap = relation.getKeyMap(i); 2074 fields.put(keyMap.getRelFieldName(), value.get(keyMap.getFieldName())); 2075 } 2076 2077 return this.findByAndCache(relation.getRelEntityName(), fields, null); 2078 } 2079 2080 2083 public GenericValue getRelatedOne(String relationName, GenericValue value) throws GenericEntityException { 2084 ModelRelation relation = value.getModelEntity().getRelation(relationName); 2085 2086 if (relation == null) { 2087 throw new GenericModelException("Could not find relation for relationName: " + relationName + " for value " + value); 2088 } 2089 if (!"one".equals(relation.getType()) && !"one-nofk".equals(relation.getType())) { 2090 throw new GenericModelException("Relation is not a 'one' or a 'one-nofk' relation: " + relationName + " of entity " + value.getEntityName()); 2091 } 2092 2093 Map fields = FastMap.newInstance(); 2094 for (int i = 0; i < relation.getKeyMapsSize(); i++) { 2095 ModelKeyMap keyMap = relation.getKeyMap(i); 2096 fields.put(keyMap.getRelFieldName(), value.get(keyMap.getFieldName())); 2097 } 2098 2099 return this.findByPrimaryKey(relation.getRelEntityName(), fields); 2100 } 2101 2102 2105 public GenericValue getRelatedOneCache(String relationName, GenericValue value) throws GenericEntityException { 2106 ModelEntity modelEntity = value.getModelEntity(); 2107 ModelRelation relation = modelEntity.getRelation(relationName); 2108 2109 if (relation == null) { 2110 throw new GenericModelException("Could not find relation for relationName: " + relationName + " for value " + value); 2111 } 2112 if (!"one".equals(relation.getType()) && !"one-nofk".equals(relation.getType())) { 2113 throw new GenericModelException("Relation is not a 'one' or a 'one-nofk' relation: " + relationName + " of entity " + value.getEntityName()); 2114 } 2115 2116 Map fields = FastMap.newInstance(); 2117 for (int i = 0; i < relation.getKeyMapsSize(); i++) { 2118 ModelKeyMap keyMap = relation.getKeyMap(i); 2119 fields.put(keyMap.getRelFieldName(), value.get(keyMap.getFieldName())); 2120 } 2121 2122 return this.findByPrimaryKeyCache(relation.getRelEntityName(), fields); 2123 } 2124 2125 2126 2130 2133 public void clearAllCaches() { 2134 this.clearAllCaches(true); 2135 } 2136 2137 public void clearAllCaches(boolean distribute) { 2138 cache.clear(); 2139 2140 if (distribute && this.distributedCacheClear != null) { 2141 this.distributedCacheClear.clearAllCaches(); 2142 } 2143 } 2144 2145 2149 public void clearCacheLine(String entityName, Map fields) { 2150 if (fields == null) { 2152 cache.remove(entityName); 2153 return; 2154 } 2155 2156 ModelEntity entity = this.getModelEntity(entityName); 2157 if (entity == null) { 2158 throw new IllegalArgumentException ("[GenericDelegator.clearCacheLine] could not find entity for entityName: " + entityName); 2159 } 2160 if (entity.getNeverCache()) return; 2162 2163 GenericValue dummyValue = GenericValue.create(entity, fields); 2164 dummyValue.setDelegator(this); 2165 this.clearCacheLineFlexible(dummyValue); 2166 } 2167 2168 2175 public void clearCacheLineFlexible(GenericEntity dummyPK) { 2176 this.clearCacheLineFlexible(dummyPK, true); 2177 } 2178 2179 public void clearCacheLineFlexible(GenericEntity dummyPK, boolean distribute) { 2180 if (dummyPK != null) { 2181 if (dummyPK.getModelEntity().getNeverCache()) return; 2183 2184 cache.remove(dummyPK); 2185 2186 if (distribute && this.distributedCacheClear != null) { 2187 this.distributedCacheClear.distributedClearCacheLineFlexible(dummyPK); 2188 } 2189 } 2190 } 2191 2192 protected void clearCacheValues(UtilCache cache, String entityName, EntityCondition condition) { 2193 Iterator iterator = cache.cacheLineTable.values().iterator(); 2194 while (iterator.hasNext()) { 2195 CacheLine line = (CacheLine) iterator.next(); 2196 GenericValue value = (GenericValue) line.getValue(); 2197 if (value != null && value.getEntityName().equals(entityName) && condition.entityMatches(value)) { 2198 iterator.remove(); 2199 } 2200 } 2201 } 2202 2203 public void clearCacheLineByCondition(String entityName, EntityCondition condition) { 2204 clearCacheLineByCondition(entityName, condition, true); 2205 } 2206 2207 public void clearCacheLineByCondition(String entityName, EntityCondition condition, boolean distribute) { 2208 if (entityName != null) { 2209 if (getModelEntity(entityName).getNeverCache()) return; 2211 2212 cache.remove(entityName, condition); 2213 2214 if (distribute && this.distributedCacheClear != null) { 2215 this.distributedCacheClear.distributedClearCacheLineByCondition(entityName, condition); 2216 } 2217 } 2218 } 2219 2220 2225 public void clearCacheLine(GenericPK primaryKey) { 2226 this.clearCacheLine(primaryKey, true); 2227 } 2228 2229 public void clearCacheLine(GenericPK primaryKey, boolean distribute) { 2230 if (primaryKey == null) return; 2231 2232 if (primaryKey.getModelEntity().getNeverCache()) return; 2234 2235 cache.remove(primaryKey); 2236 2237 if (distribute && this.distributedCacheClear != null) { 2238 this.distributedCacheClear.distributedClearCacheLine(primaryKey); 2239 } 2240 } 2241 2242 2248 public void clearCacheLine(GenericValue value) { 2249 this.clearCacheLine(value, true); 2250 } 2251 2252 public void clearCacheLine(GenericValue value, boolean distribute) { 2253 2258 if (value == null) return; 2260 2261 if (value.getModelEntity().getNeverCache()) return; 2263 2264 cache.remove(value); 2265 2266 if (distribute && this.distributedCacheClear != null) { 2267 this.distributedCacheClear.distributedClearCacheLine(value); 2268 } 2269 } 2270 2271 public void clearAllCacheLinesByDummyPK(Collection dummyPKs) { 2272 if (dummyPKs == null) return; 2273 Iterator iter = dummyPKs.iterator(); 2274 2275 while (iter.hasNext()) { 2276 GenericEntity entity = (GenericEntity) iter.next(); 2277 2278 this.clearCacheLineFlexible(entity); 2279 } 2280 } 2281 2282 public void clearAllCacheLinesByValue(Collection values) { 2283 if (values == null) return; 2284 Iterator iter = values.iterator(); 2285 2286 while (iter.hasNext()) { 2287 GenericValue value = (GenericValue) iter.next(); 2288 2289 this.clearCacheLine(value); 2290 } 2291 } 2292 2293 public GenericValue getFromPrimaryKeyCache(GenericPK primaryKey) { 2294 if (primaryKey == null) return null; 2295 return (GenericValue) cache.get(primaryKey); 2296 } 2297 2298 public void putInPrimaryKeyCache(GenericPK primaryKey, GenericValue value) { 2299 if (primaryKey == null) return; 2300 2301 if (primaryKey.getModelEntity().getNeverCache()) { 2302 Debug.logWarning("Tried to put a value of the " + value.getEntityName() + " entity in the BY PRIMARY KEY cache but this entity has never-cache set to true, not caching.", module); 2303 return; 2304 } 2305 2306 value.setImmutable(); 2308 cache.put(primaryKey, value); 2309 } 2310 2311 public void putAllInPrimaryKeyCache(List values) { 2312 if (values == null) return; 2313 Iterator iter = values.iterator(); 2314 while (iter.hasNext()) { 2315 GenericValue value = (GenericValue) iter.next(); 2316 this.putInPrimaryKeyCache(value.getPrimaryKey(), value); 2317 } 2318 } 2319 2320 public void setDistributedCacheClear(DistributedCacheClear distributedCacheClear) { 2321 this.distributedCacheClear = distributedCacheClear; 2322 } 2323 2324 public List readXmlDocument(URL url) throws SAXException , ParserConfigurationException , java.io.IOException { 2326 if (url == null) return null; 2327 return this.makeValues(UtilXml.readXmlDocument(url, false)); 2328 } 2329 2330 public List makeValues(Document document) { 2331 if (document == null) return null; 2332 List values = FastList.newInstance(); 2333 2334 Element docElement = document.getDocumentElement(); 2335 2336 if (docElement == null) 2337 return null; 2338 if (!"entity-engine-xml".equals(docElement.getTagName())) { 2339 Debug.logError("[GenericDelegator.makeValues] Root node was not <entity-engine-xml>", module); 2340 throw new java.lang.IllegalArgumentException ("Root node was not <entity-engine-xml>"); 2341 } 2342 docElement.normalize(); 2343 Node curChild = docElement.getFirstChild(); 2344 2345 if (curChild != null) { 2346 do { 2347 if (curChild.getNodeType() == Node.ELEMENT_NODE) { 2348 Element element = (Element ) curChild; 2349 GenericValue value = this.makeValue(element); 2350 2351 if (value != null) 2352 values.add(value); 2353 } 2354 } while ((curChild = curChild.getNextSibling()) != null); 2355 } else { 2356 Debug.logWarning("[GenericDelegator.makeValues] No child nodes found in document.", module); 2357 } 2358 2359 return values; 2360 } 2361 2362 public GenericPK makePK(Element element) { 2363 GenericValue value = makeValue(element); 2364 2365 return value.getPrimaryKey(); 2366 } 2367 2368 public GenericValue makeValue(Element element) { 2369 if (element == null) return null; 2370 String entityName = element.getTagName(); 2371 2372 if (entityName.indexOf('-') > 0) 2374 entityName = entityName.substring(entityName.indexOf('-') + 1); 2375 if (entityName.indexOf(':') > 0) 2376 entityName = entityName.substring(entityName.indexOf(':') + 1); 2377 GenericValue value = this.makeValue(entityName, null); 2378 2379 ModelEntity modelEntity = value.getModelEntity(); 2380 2381 Iterator modelFields = modelEntity.getFieldsIterator(); 2382 2383 while (modelFields.hasNext()) { 2384 ModelField modelField = (ModelField) modelFields.next(); 2385 String name = modelField.getName(); 2386 String attr = element.getAttribute(name); 2387 2388 if (attr != null && attr.length() > 0) { 2389 value.setString(name, attr); 2390 } else { 2391 Element subElement = UtilXml.firstChildElement(element, name); 2393 2394 if (subElement != null) { 2395 value.setString(name, UtilXml.elementValue(subElement)); 2396 } 2397 } 2398 } 2399 2400 return value; 2401 } 2402 2403 2405 protected Map getEcaEntityEventMap(String entityName) { 2406 if (this.entityEcaHandler == null) return null; 2407 Map ecaEventMap = this.entityEcaHandler.getEntityEventMap(entityName); 2408 return ecaEventMap; 2410 } 2411 2412 protected void evalEcaRules(String event, String currentOperation, GenericEntity value, Map eventMap, boolean noEventMapFound, boolean isError) throws GenericEntityException { 2413 if (noEventMapFound) return; 2415 if (this.entityEcaHandler == null) return; 2416 this.entityEcaHandler.evalRules(currentOperation, eventMap, event, value, isError); 2418 } 2419 2420 public void setEntityEcaHandler(EntityEcaHandler entityEcaHandler) { 2421 this.entityEcaHandler = entityEcaHandler; 2422 } 2423 2424 public EntityEcaHandler getEntityEcaHandler() { 2425 return this.entityEcaHandler; 2426 } 2427 2428 2433 public String getNextSeqId(String seqName) { 2434 return this.getNextSeqId(seqName, 1); 2435 } 2436 2437 2443 public String getNextSeqId(String seqName, long staggerMax) { 2444 boolean beganTransaction = false; 2445 try { 2446 if (alwaysUseTransaction) { 2447 beganTransaction = TransactionUtil.begin(); 2448 } 2449 2450 Long nextSeqLong = this.getNextSeqIdLong(seqName, staggerMax); 2451 2452 if (nextSeqLong == null) { 2453 throw new IllegalArgumentException ("Could not get next sequenced ID for sequence name: " + seqName); 2454 } 2455 2456 if (UtilValidate.isNotEmpty(this.getDelegatorInfo().sequencedIdPrefix)) { 2457 return this.getDelegatorInfo().sequencedIdPrefix + nextSeqLong.toString(); 2458 } else { 2459 return nextSeqLong.toString(); 2460 } 2461 } catch (Exception e) { 2462 String errMsg = "Failure in getNextSeqId operation for seqName [" + seqName + "]: " + e.toString() + ". Rolling back transaction"; 2463 Debug.logError(e, errMsg, module); 2464 try { 2465 TransactionUtil.rollback(beganTransaction, errMsg, e); 2467 } catch (GenericEntityException e2) { 2468 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 2469 } 2470 Debug.logError(e, "[GenericDelegator] Error getting next sequence ID: " + e.toString(), module); 2471 return null; 2472 } finally { 2473 try { 2474 TransactionUtil.commit(beganTransaction); 2476 } catch (GenericTransactionException e1) { 2477 Debug.logError(e1, "[GenericDelegator] Could not commit transaction: " + e1.toString(), module); 2478 } 2479 } 2480 } 2481 2482 2487 public Long getNextSeqIdLong(String seqName) { 2488 return this.getNextSeqIdLong(seqName, 1); 2489 } 2490 2491 2497 public Long getNextSeqIdLong(String seqName, long staggerMax) { 2498 boolean beganTransaction = false; 2499 try { 2500 if (alwaysUseTransaction) { 2501 beganTransaction = TransactionUtil.begin(); 2502 } 2503 2504 if (sequencer == null) { 2505 synchronized (this) { 2506 if (sequencer == null) { 2507 String helperName = this.getEntityHelperName("SequenceValueItem"); 2508 ModelEntity seqEntity = this.getModelEntity("SequenceValueItem"); 2509 sequencer = new SequenceUtil(helperName, seqEntity, "seqName", "seqId"); 2510 } 2511 } 2512 } 2513 2514 Long newSeqId = sequencer == null ? null : sequencer.getNextSeqId(seqName, staggerMax); 2515 2516 TransactionUtil.commit(beganTransaction); 2518 2519 return newSeqId; 2520 } catch (GenericEntityException e) { 2521 String errMsg = "Failure in getNextSeqIdLong operation for seqName [" + seqName + "]: " + e.toString() + ". Rolling back transaction."; 2522 Debug.logError(e, errMsg, module); 2523 try { 2524 TransactionUtil.rollback(beganTransaction, errMsg, e); 2526 } catch (GenericEntityException e2) { 2527 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 2528 } 2529 Debug.logError(e, "[GenericDelegator] Error getting next sequence ID: " + e.toString(), module); 2530 return null; 2531 } 2532 } 2533 2534 2536 public void setSequencer(SequenceUtil sequencer) { 2537 this.sequencer = sequencer; 2538 } 2539 2540 2541 public void refreshSequencer() { 2542 this.sequencer = null; 2543 } 2544 2545 2546 2547 public void setNextSubSeqId(GenericValue value, String seqFieldName, int numericPadding, int incrementBy) { 2548 if (value != null && UtilValidate.isEmpty(value.getString(seqFieldName))) { 2549 String sequencedIdPrefix = this.getDelegatorInfo().sequencedIdPrefix; 2550 2551 value.remove(seqFieldName); 2552 GenericValue lookupValue = this.makeValue(value.getEntityName(), null); 2553 lookupValue.setPKFields(value); 2554 2555 boolean beganTransaction = false; 2556 try { 2557 if (alwaysUseTransaction) { 2558 beganTransaction = TransactionUtil.begin(); 2559 } 2560 2561 List allValues = this.findByAnd(value.getEntityName(), lookupValue, null); 2563 Iterator allValueIter = allValues.iterator(); 2565 Integer highestSeqVal = null; 2566 while (allValueIter.hasNext()) { 2567 GenericValue curValue = (GenericValue) allValueIter.next(); 2568 String currentSeqId = curValue.getString(seqFieldName); 2569 if (currentSeqId != null) { 2570 if (UtilValidate.isNotEmpty(sequencedIdPrefix)) { 2571 if (currentSeqId.startsWith(sequencedIdPrefix)) { 2572 currentSeqId = currentSeqId.substring(sequencedIdPrefix.length()); 2573 } else { 2574 continue; 2575 } 2576 } 2577 try { 2578 int seqVal = Integer.parseInt(currentSeqId); 2579 if (highestSeqVal == null || seqVal > highestSeqVal.intValue()) { 2580 highestSeqVal = new Integer (seqVal); 2581 } 2582 } catch (Exception e) { 2583 Debug.logWarning("Error in make-next-seq-id converting SeqId [" + currentSeqId + "] to a number: " + e.toString(), module); 2584 } 2585 } 2586 } 2587 2588 int seqValToUse = (highestSeqVal == null ? 1 : highestSeqVal.intValue() + incrementBy); 2589 String newSeqId = sequencedIdPrefix + UtilFormatOut.formatPaddedNumber(seqValToUse, numericPadding); 2590 value.set(seqFieldName, newSeqId); 2591 2592 TransactionUtil.commit(beganTransaction); 2594 } catch (Exception e) { 2595 String errMsg = "Failure in setNextSubSeqId operation for entity [" + value.getEntityName() + "]: " + e.toString() + ". Rolling back transaction."; 2596 Debug.logError(e, errMsg, module); 2597 try { 2598 TransactionUtil.rollback(beganTransaction, errMsg, e); 2600 } catch (GenericEntityException e2) { 2601 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module); 2602 } 2603 Debug.logError(e, "Error making next seqId", module); 2604 } 2605 } 2606 } 2607 2608 public void encryptFields(List entities) throws GenericEntityException { 2609 if (entities != null) { 2610 for (int i = 0; i < entities.size(); i++) { 2611 GenericEntity entity = (GenericEntity) entities.get(i); 2612 this.encryptFields(entity); 2613 } 2614 } 2615 } 2616 2617 public void encryptFields(GenericEntity entity) throws GenericEntityException { 2618 ModelEntity model = entity.getModelEntity(); 2619 String entityName = model.getEntityName(); 2620 2621 Iterator i = model.getFieldsIterator(); 2622 while (i.hasNext()) { 2623 ModelField field = (ModelField) i.next(); 2624 if (field.getEncrypt()) { 2625 Object obj = entity.get(field.getName()); 2626 if (obj != null) { 2627 if (obj instanceof String && UtilValidate.isEmpty((String ) obj)) { 2628 continue; 2629 } 2630 entity.dangerousSetNoCheckButFast(field, this.encryptFieldValue(entityName, obj)); 2631 } 2632 } 2633 } 2634 } 2635 2636 public Object encryptFieldValue(String entityName, Object fieldValue) throws EntityCryptoException { 2637 if (fieldValue != null) { 2638 if (fieldValue instanceof String && UtilValidate.isEmpty((String ) fieldValue)) { 2639 return fieldValue; 2640 } 2641 return this.crypto.encrypt(entityName, fieldValue); 2642 } 2643 return fieldValue; 2644 } 2645 2646 public void decryptFields(List entities) throws GenericEntityException { 2647 if (entities != null) { 2648 for (int i = 0; i < entities.size(); i++) { 2649 GenericEntity entity = (GenericEntity) entities.get(i); 2650 this.decryptFields(entity); 2651 } 2652 } 2653 } 2654 2655 public void decryptFields(GenericEntity entity) throws GenericEntityException { 2656 ModelEntity model = entity.getModelEntity(); 2657 String entityName = model.getEntityName(); 2658 2659 Iterator i = model.getFieldsIterator(); 2660 while (i.hasNext()) { 2661 ModelField field = (ModelField) i.next(); 2662 if (field.getEncrypt()) { 2663 String encHex = (String ) entity.get(field.getName()); 2664 if (UtilValidate.isNotEmpty(encHex)) { 2665 try { 2666 entity.dangerousSetNoCheckButFast(field, crypto.decrypt(entityName, encHex)); 2667 } catch (EntityCryptoException e) { 2668 Debug.logWarning(e, "Problem decrypting field [" + entityName + " / " + field.getName() + "]", module); 2670 } 2671 } 2672 } 2673 } 2674 } 2675 2676 public void setEntityCrypto(EntityCrypto crypto) { 2677 this.crypto = crypto; 2678 } 2679 2680 protected void absorbList(List lst) { 2681 if (lst == null) return; 2682 Iterator iter = lst.iterator(); 2683 2684 while (iter.hasNext()) { 2685 GenericValue value = (GenericValue) iter.next(); 2686 2687 value.setDelegator(this); 2688 } 2689 } 2690 2691 public Cache getCache() { 2692 return cache; 2693 } 2694 2695 public GenericDelegator cloneDelegator(String delegatorName) { 2696 GenericDelegator newDelegator = new GenericDelegator(); 2700 newDelegator.modelReader = this.modelReader; 2701 newDelegator.modelGroupReader = this.modelGroupReader; 2702 newDelegator.delegatorName = delegatorName; 2703 newDelegator.delegatorInfo = this.delegatorInfo; 2704 newDelegator.cache = this.cache; 2705 newDelegator.andCacheFieldSets = this.andCacheFieldSets; 2706 newDelegator.distributedCacheClear = this.distributedCacheClear; 2707 newDelegator.entityEcaHandler = this.entityEcaHandler; 2708 newDelegator.crypto = this.crypto; 2709 2711 return newDelegator; 2712 } 2713 2714 public GenericDelegator cloneDelegator() { 2715 return this.cloneDelegator(this.delegatorName); 2716 } 2717} 2718 | Popular Tags |