1 24 package org.ofbiz.entity.model; 25 26 import java.io.Serializable ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 import java.util.TreeSet ; 33 34 import javolution.util.FastList; 35 import javolution.util.FastMap; 36 import javolution.util.FastSet; 37 38 import org.ofbiz.base.component.ComponentConfig; 39 import org.ofbiz.base.config.GenericConfigException; 40 import org.ofbiz.base.config.MainResourceHandler; 41 import org.ofbiz.base.config.ResourceHandler; 42 import org.ofbiz.base.util.Debug; 43 import org.ofbiz.base.util.UtilTimer; 44 import org.ofbiz.base.util.UtilXml; 45 import org.ofbiz.base.util.cache.UtilCache; 46 import org.ofbiz.entity.GenericEntityConfException; 47 import org.ofbiz.entity.GenericEntityException; 48 import org.ofbiz.entity.GenericModelException; 49 import org.ofbiz.entity.config.DelegatorInfo; 50 import org.ofbiz.entity.config.EntityConfigUtil; 51 import org.ofbiz.entity.config.EntityModelReaderInfo; 52 import org.w3c.dom.Document ; 53 import org.w3c.dom.Element ; 54 import org.w3c.dom.Node ; 55 56 64 public class ModelReader implements Serializable { 65 66 public static final String module = ModelReader.class.getName(); 67 public static UtilCache readers = new UtilCache("entity.ModelReader", 0, 0); 68 69 protected Map entityCache = null; 70 71 protected int numEntities = 0; 72 protected int numViewEntities = 0; 73 protected int numFields = 0; 74 protected int numRelations = 0; 75 protected int numAutoRelations = 0; 76 77 protected String modelName; 78 79 80 protected Collection entityResourceHandlers; 81 82 83 protected Map resourceHandlerEntities; 84 85 86 protected Map entityResourceHandlerMap; 87 88 public static ModelReader getModelReader(String delegatorName) throws GenericEntityException { 89 DelegatorInfo delegatorInfo = EntityConfigUtil.getDelegatorInfo(delegatorName); 90 91 if (delegatorInfo == null) { 92 throw new GenericEntityConfException("Could not find a delegator with the name " + delegatorName); 93 } 94 95 String tempModelName = delegatorInfo.entityModelReader; 96 ModelReader reader = (ModelReader) readers.get(tempModelName); 97 98 if (reader == null) { synchronized (ModelReader.class) { 100 reader = (ModelReader) readers.get(tempModelName); 102 if (reader == null) { 103 reader = new ModelReader(tempModelName); 104 reader.getEntityCache(); 106 readers.put(tempModelName, reader); 107 } 108 } 109 } 110 return reader; 111 } 112 113 public ModelReader(String modelName) throws GenericEntityException { 114 this.modelName = modelName; 115 entityResourceHandlers = FastList.newInstance(); 116 resourceHandlerEntities = FastMap.newInstance(); 117 entityResourceHandlerMap = FastMap.newInstance(); 118 119 EntityModelReaderInfo entityModelReaderInfo = EntityConfigUtil.getEntityModelReaderInfo(modelName); 120 121 if (entityModelReaderInfo == null) { 122 throw new GenericEntityConfException("Cound not find an entity-model-reader with the name " + modelName); 123 } 124 125 List resourceElements = entityModelReaderInfo.resourceElements; 127 Iterator resIter = resourceElements.iterator(); 128 while (resIter.hasNext()) { 129 Element resourceElement = (Element ) resIter.next(); 130 ResourceHandler handler = new MainResourceHandler(EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME, resourceElement); 131 entityResourceHandlers.add(handler); 132 } 133 134 List componentResourceInfos = ComponentConfig.getAllEntityResourceInfos("model"); 136 Iterator componentResourceInfoIter = componentResourceInfos.iterator(); 137 while (componentResourceInfoIter.hasNext()) { 138 ComponentConfig.EntityResourceInfo componentResourceInfo = (ComponentConfig.EntityResourceInfo) componentResourceInfoIter.next(); 139 if (modelName.equals(componentResourceInfo.readerName)) { 140 entityResourceHandlers.add(componentResourceInfo.createResourceHandler()); 141 } 142 } 143 } 144 145 public Map getEntityCache() throws GenericEntityException { 146 if (entityCache == null) { synchronized (ModelReader.class) { 148 if (entityCache == null) { numEntities = 0; 151 numViewEntities = 0; 152 numFields = 0; 153 numRelations = 0; 154 numAutoRelations = 0; 155 156 entityCache = FastMap.newInstance(); 157 List tempViewEntityList = FastList.newInstance(); 158 159 UtilTimer utilTimer = new UtilTimer(); 160 161 Iterator rhIter = entityResourceHandlers.iterator(); 162 while (rhIter.hasNext()) { 163 ResourceHandler entityResourceHandler = (ResourceHandler) rhIter.next(); 164 165 Document document = null; 167 168 try { 169 document = entityResourceHandler.getDocument(); 170 } catch (GenericConfigException e) { 171 throw new GenericEntityConfException("Error getting document from resource handler", e); 172 } 173 if (document == null) { 174 throw new GenericEntityConfException("Could not get document for " + entityResourceHandler.toString()); 175 } 176 177 Element docElement = document.getDocumentElement(); 179 180 if (docElement == null) { 181 entityCache = null; 182 return null; 183 } 184 docElement.normalize(); 185 Node curChild = docElement.getFirstChild(); 186 187 ModelInfo def = new ModelInfo(); 188 def.populateFromElements(docElement); 189 int i = 0; 190 191 if (curChild != null) { 192 utilTimer.timerString("Before start of entity loop in " + entityResourceHandler.toString()); 193 do { 194 boolean isEntity = "entity".equals(curChild.getNodeName()); 195 boolean isViewEntity = "view-entity".equals(curChild.getNodeName()); 196 197 if ((isEntity || isViewEntity) && curChild.getNodeType() == Node.ELEMENT_NODE) { 198 i++; 199 Element curEntity = (Element ) curChild; 200 String entityName = UtilXml.checkEmpty(curEntity.getAttribute("entity-name")); 201 202 Collection resourceHandlerEntityNames = (Collection ) resourceHandlerEntities.get(entityResourceHandler); 204 205 if (resourceHandlerEntityNames == null) { 206 resourceHandlerEntityNames = FastList.newInstance(); 207 resourceHandlerEntities.put(entityResourceHandler, resourceHandlerEntityNames); 208 } 209 resourceHandlerEntityNames.add(entityName); 210 211 if (entityCache.containsKey(entityName)) { 213 Debug.logWarning("WARNING: Entity " + entityName + 214 " is defined more than once, most recent will over-write " + 215 "previous definition(s)", module); 216 Debug.logWarning("WARNING: Entity " + entityName + " was found in " + 217 entityResourceHandler + ", but was already defined in " + 218 entityResourceHandlerMap.get(entityName).toString(), module); 219 } 220 221 entityResourceHandlerMap.put(entityName, entityResourceHandler); 223 224 227 ModelEntity entity = null; 228 229 if (isEntity) { 230 entity = createModelEntity(curEntity, null, def); 231 } else { 232 entity = createModelViewEntity(curEntity, null, def); 233 tempViewEntityList.add(entity); 235 } 236 237 if (entity != null) { 239 entityCache.put(entityName, entity); 240 if (isEntity) { 242 if (Debug.verboseOn()) Debug.logVerbose("-- [Entity]: #" + i + ": " + entityName, module); 243 } else { 244 if (Debug.verboseOn()) Debug.logVerbose("-- [ViewEntity]: #" + i + ": " + entityName, module); 245 } 246 } else { 247 Debug.logWarning("-- -- ENTITYGEN ERROR:getModelEntity: Could not create " + 248 "entity for entityName: " + entityName, module); 249 } 250 251 } 252 } while ((curChild = curChild.getNextSibling()) != null); 253 } else { 254 Debug.logWarning("No child nodes found.", module); 255 } 256 utilTimer.timerString("Finished " + entityResourceHandler.toString() + " - Total Entities: " + i + " FINISHED"); 257 } 258 259 for (int velInd = 0; velInd < tempViewEntityList.size(); velInd++) { 262 ModelViewEntity curViewEntity = (ModelViewEntity) tempViewEntityList.get(velInd); 263 curViewEntity.populateFields(this); 264 List memberEntities = curViewEntity.getAllModelMemberEntities(); 265 for (int j = 0; j < memberEntities.size(); j++) { 266 ModelViewEntity.ModelMemberEntity mve = (ModelViewEntity.ModelMemberEntity) memberEntities.get(j); 267 ModelEntity me = (ModelEntity) entityCache.get(mve.getEntityName()); 268 if (me == null) throw new GenericEntityConfException("View " + curViewEntity.getEntityName() + " references non-existant entity: " + mve.getEntityName()); 269 me.addViewEntity(curViewEntity); 270 } 271 } 272 273 TreeSet orderedMessages = new TreeSet (); 275 Iterator entityNamesIter = new TreeSet (this.getEntityNames()).iterator(); 276 while (entityNamesIter.hasNext()) { 277 String curEntityName = (String ) entityNamesIter.next(); 278 ModelEntity curModelEntity = this.getModelEntity(curEntityName); 279 if (curModelEntity instanceof ModelViewEntity) { 280 282 } else { 283 285 List newSameEntityRelations = FastList.newInstance(); 287 288 Iterator relationsIter = curModelEntity.getRelationsIterator(); 289 while (relationsIter.hasNext()) { 290 ModelRelation modelRelation = (ModelRelation) relationsIter.next(); 291 if (("one".equals(modelRelation.getType()) || "one-nofk".equals(modelRelation.getType())) && !modelRelation.isAutoRelation()) { 292 ModelEntity relatedEnt = null; 293 try { 294 relatedEnt = this.getModelEntity(modelRelation.getRelEntityName()); 295 } catch (GenericModelException e) { 296 throw new GenericModelException("Error getting related entity [" + modelRelation.getRelEntityName() + "] definition from entity [" + curEntityName + "]", e); 297 } 298 if (relatedEnt != null) { 299 String targetTitle = modelRelation.getTitle(); 301 if (curModelEntity.getEntityName().equals(relatedEnt.getEntityName()) && "Parent".equals(targetTitle)) { 302 targetTitle = "Child"; 303 } 304 305 ModelRelation newRel = new ModelRelation(); 307 newRel.setModelEntity(relatedEnt); 308 newRel.setRelEntityName(curModelEntity.getEntityName()); 309 newRel.setTitle(targetTitle); 310 newRel.setAutoRelation(true); 311 Set curEntityKeyFields = FastSet.newInstance(); 312 for (int kmn = 0; kmn < modelRelation.getKeyMapsSize(); kmn++) { 313 ModelKeyMap curkm = modelRelation.getKeyMap(kmn); 314 ModelKeyMap newkm = new ModelKeyMap(); 315 newRel.addKeyMap(newkm); 316 newkm.setFieldName(curkm.getRelFieldName()); 317 newkm.setRelFieldName(curkm.getFieldName()); 318 curEntityKeyFields.add(curkm.getFieldName()); 319 } 320 if (curModelEntity.containsAllPkFieldNames(curEntityKeyFields)) { 322 newRel.setType("one-nofk"); 324 325 List curPkFieldNames = curModelEntity.getPkFieldNames(); 327 Iterator nrkmIter = newRel.getKeyMapsIterator(); 328 while (nrkmIter.hasNext()) { 329 ModelKeyMap nrkm = (ModelKeyMap) nrkmIter.next(); 330 String checkField = nrkm.getRelFieldName(); 331 if (!curPkFieldNames.contains(checkField)) { 332 nrkmIter.remove(); 333 } 334 } 335 } else { 336 newRel.setType("many"); 337 } 338 339 ModelRelation existingRelation = relatedEnt.getRelation(targetTitle + curModelEntity.getEntityName()); 340 if (existingRelation == null) { 341 numAutoRelations++; 342 if (curModelEntity.getEntityName().equals(relatedEnt.getEntityName())) { 343 newSameEntityRelations.add(newRel); 344 } else { 345 relatedEnt.addRelation(newRel); 346 } 347 } else { 348 if (newRel.equals(existingRelation)) { 349 if (!(targetTitle + curModelEntity.getEntityName()).equals(modelRelation.getTitle() + modelRelation.getRelEntityName())) { 351 String message = "Entity [" + relatedEnt.getPackageName() + ":" + relatedEnt.getEntityName() + "] already has identical relationship to entity [" + 353 curModelEntity.getEntityName() + "] title [" + targetTitle + "]; would auto-create: type [" + 354 newRel.getType() + "] and fields [" + newRel.keyMapString(",", "") + "]"; 355 orderedMessages.add(message); 356 } 357 } else { 358 String message = "Existing relationship with the same name, but different specs found from what would be auto-created for Entity [" + relatedEnt.getEntityName() + "] ant relationship to entity [" + 359 curModelEntity.getEntityName() + "] title [" + targetTitle + "]; would auto-create: type [" + 360 newRel.getType() + "] and fields [" + newRel.keyMapString(",", "") + "]"; 361 } 363 } 364 } else { 365 String errorMsg = "Could not find related entity [" 366 + modelRelation.getRelEntityName() + "], no reverse relation added."; 367 Debug.logWarning(errorMsg, module); 368 } 369 } 370 } 371 372 if (newSameEntityRelations.size() > 0) { 373 Iterator newRelsIter = newSameEntityRelations.iterator(); 374 while (newRelsIter.hasNext()) { 375 ModelRelation newRel = (ModelRelation) newRelsIter.next(); 376 curModelEntity.addRelation(newRel); 377 } 378 } 379 } 380 } 381 382 Iterator omIter = orderedMessages.iterator(); 383 while (omIter.hasNext()) { 384 Debug.logInfo((String ) omIter.next(), module); 385 } 386 387 Debug.log("FINISHED LOADING ENTITIES - ALL FILES; #Entities=" + numEntities + " #ViewEntities=" + 388 numViewEntities + " #Fields=" + numFields + " #Relationships=" + numRelations + " #AutoRelationships=" + numAutoRelations, module); 389 } 390 } 391 } 392 return entityCache; 393 } 394 395 399 public void rebuildResourceHandlerEntities() { 400 resourceHandlerEntities = FastMap.newInstance(); 401 Iterator entityResourceIter = entityResourceHandlerMap.entrySet().iterator(); 402 403 while (entityResourceIter.hasNext()) { 404 Map.Entry entry = (Map.Entry ) entityResourceIter.next(); 405 Collection resourceHandlerEntityNames = (Collection ) resourceHandlerEntities.get(entry.getValue()); 407 408 if (resourceHandlerEntityNames == null) { 409 resourceHandlerEntityNames = FastList.newInstance(); 410 resourceHandlerEntities.put(entry.getValue(), resourceHandlerEntityNames); 411 } 412 resourceHandlerEntityNames.add(entry.getKey()); 413 } 414 } 415 416 public Iterator getResourceHandlerEntitiesKeyIterator() { 417 if (resourceHandlerEntities == null) return null; 418 return resourceHandlerEntities.keySet().iterator(); 419 } 420 421 public Collection getResourceHandlerEntities(ResourceHandler resourceHandler) { 422 if (resourceHandlerEntities == null) return null; 423 return (Collection ) resourceHandlerEntities.get(resourceHandler); 424 } 425 426 public void addEntityToResourceHandler(String entityName, String loaderName, String location) { 427 entityResourceHandlerMap.put(entityName, new MainResourceHandler(EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME, loaderName, location)); 428 } 429 430 public ResourceHandler getEntityResourceHandler(String entityName) { 431 return (ResourceHandler) entityResourceHandlerMap.get(entityName); 432 } 433 434 438 public ModelEntity getModelEntity(String entityName) throws GenericEntityException { 439 if (entityName == null) { 440 throw new IllegalArgumentException ("Tried to find entity definition for a null entityName"); 441 } 442 Map ec = getEntityCache(); 443 if (ec == null) { 444 throw new GenericEntityConfException("ERROR: Unable to load Entity Cache"); 445 } 446 ModelEntity modelEntity = (ModelEntity) ec.get(entityName); 447 if (modelEntity == null) { 448 throw new GenericModelException("Could not find definition for entity name " + entityName); 449 } 450 return modelEntity; 451 } 452 453 public ModelEntity getModelEntityNoCheck(String entityName) { 454 Map ec = null; 455 try { 456 ec = getEntityCache(); 457 } catch (GenericEntityException e) { 458 Debug.logError(e, "Error getting entity cache", module); 459 } 460 if (ec == null) { 461 return null; 462 } 463 ModelEntity modelEntity = (ModelEntity) ec.get(entityName); 464 return modelEntity; 465 } 466 467 470 public Iterator getEntityNamesIterator() throws GenericEntityException { 471 Collection collection = getEntityNames(); 472 if (collection != null) { 473 return collection.iterator(); 474 } else { 475 return null; 476 } 477 } 478 479 482 public Collection getEntityNames() throws GenericEntityException { 483 Map ec = getEntityCache(); 484 if (ec == null) { 485 throw new GenericEntityConfException("ERROR: Unable to load Entity Cache"); 486 } 487 return ec.keySet(); 488 } 489 490 ModelEntity createModelEntity(Element entityElement, UtilTimer utilTimer, ModelInfo def) { 491 if (entityElement == null) return null; 492 this.numEntities++; 493 ModelEntity entity = new ModelEntity(this, entityElement, utilTimer, def); 494 return entity; 495 } 496 497 ModelEntity createModelViewEntity(Element entityElement, UtilTimer utilTimer, ModelInfo def) { 498 if (entityElement == null) return null; 499 this.numViewEntities++; 500 ModelViewEntity entity = new ModelViewEntity(this, entityElement, utilTimer, def); 501 return entity; 502 } 503 504 public ModelRelation createRelation(ModelEntity entity, Element relationElement) { 505 this.numRelations++; 506 ModelRelation relation = new ModelRelation(entity, relationElement); 507 return relation; 508 } 509 510 public ModelField findModelField(ModelEntity entity, String fieldName) { 511 for (int i = 0; i < entity.fields.size(); i++) { 512 ModelField field = (ModelField) entity.fields.get(i); 513 if (field.name.compareTo(fieldName) == 0) { 514 return field; 515 } 516 } 517 return null; 518 } 519 520 public ModelField createModelField(String name, String type, String colName, boolean isPk) { 521 this.numFields++; 522 ModelField field = new ModelField(name, type, colName, isPk); 523 return field; 524 } 525 526 public ModelField createModelField(Element fieldElement) { 527 if (fieldElement == null) { 528 return null; 529 } 530 531 this.numFields++; 532 ModelField field = new ModelField(fieldElement); 533 return field; 534 } 535 } 536 | Popular Tags |