📄 modelreader.java
字号:
/* * $Id: ModelReader.java 5720 2005-09-13 03:10:59Z jonesde $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.entity.model;import java.io.Serializable;import java.util.Collection;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.TreeSet;import javolution.util.FastList;import javolution.util.FastMap;import javolution.util.FastSet;import org.ofbiz.base.component.ComponentConfig;import org.ofbiz.base.config.GenericConfigException;import org.ofbiz.base.config.MainResourceHandler;import org.ofbiz.base.config.ResourceHandler;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilTimer;import org.ofbiz.base.util.UtilXml;import org.ofbiz.base.util.cache.UtilCache;import org.ofbiz.entity.GenericEntityConfException;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericModelException;import org.ofbiz.entity.config.DelegatorInfo;import org.ofbiz.entity.config.EntityConfigUtil;import org.ofbiz.entity.config.EntityModelReaderInfo;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;/** * Generic Entity - Entity Definition Reader * * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 5720 $ * @since 2.0 */public class ModelReader implements Serializable { public static final String module = ModelReader.class.getName(); public static UtilCache readers = new UtilCache("entity.ModelReader", 0, 0); protected Map entityCache = null; protected int numEntities = 0; protected int numViewEntities = 0; protected int numFields = 0; protected int numRelations = 0; protected int numAutoRelations = 0; protected String modelName; /** collection of filenames for entity definitions */ protected Collection entityResourceHandlers; /** contains a collection of entity names for each ResourceHandler, populated as they are loaded */ protected Map resourceHandlerEntities; /** for each entity contains a map to the ResourceHandler that the entity came from */ protected Map entityResourceHandlerMap; public static ModelReader getModelReader(String delegatorName) throws GenericEntityException { DelegatorInfo delegatorInfo = EntityConfigUtil.getDelegatorInfo(delegatorName); if (delegatorInfo == null) { throw new GenericEntityConfException("Could not find a delegator with the name " + delegatorName); } String tempModelName = delegatorInfo.entityModelReader; ModelReader reader = (ModelReader) readers.get(tempModelName); if (reader == null) { // don't want to block here synchronized (ModelReader.class) { // must check if null again as one of the blocked threads can still enter reader = (ModelReader) readers.get(tempModelName); if (reader == null) { reader = new ModelReader(tempModelName); // preload caches... reader.getEntityCache(); readers.put(tempModelName, reader); } } } return reader; } public ModelReader(String modelName) throws GenericEntityException { this.modelName = modelName; entityResourceHandlers = FastList.newInstance(); resourceHandlerEntities = FastMap.newInstance(); entityResourceHandlerMap = FastMap.newInstance(); EntityModelReaderInfo entityModelReaderInfo = EntityConfigUtil.getEntityModelReaderInfo(modelName); if (entityModelReaderInfo == null) { throw new GenericEntityConfException("Cound not find an entity-model-reader with the name " + modelName); } // get all of the main resource model stuff, ie specified in the entityengine.xml file List resourceElements = entityModelReaderInfo.resourceElements; Iterator resIter = resourceElements.iterator(); while (resIter.hasNext()) { Element resourceElement = (Element) resIter.next(); ResourceHandler handler = new MainResourceHandler(EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME, resourceElement); entityResourceHandlers.add(handler); } // get all of the component resource model stuff, ie specified in each ofbiz-component.xml file List componentResourceInfos = ComponentConfig.getAllEntityResourceInfos("model"); Iterator componentResourceInfoIter = componentResourceInfos.iterator(); while (componentResourceInfoIter.hasNext()) { ComponentConfig.EntityResourceInfo componentResourceInfo = (ComponentConfig.EntityResourceInfo) componentResourceInfoIter.next(); if (modelName.equals(componentResourceInfo.readerName)) { entityResourceHandlers.add(componentResourceInfo.createResourceHandler()); } } } public Map getEntityCache() throws GenericEntityException { if (entityCache == null) { // don't want to block here synchronized (ModelReader.class) { // must check if null again as one of the blocked threads can still enter if (entityCache == null) { // now it's safe numEntities = 0; numViewEntities = 0; numFields = 0; numRelations = 0; numAutoRelations = 0; entityCache = FastMap.newInstance(); List tempViewEntityList = FastList.newInstance(); UtilTimer utilTimer = new UtilTimer(); Iterator rhIter = entityResourceHandlers.iterator(); while (rhIter.hasNext()) { ResourceHandler entityResourceHandler = (ResourceHandler) rhIter.next(); // utilTimer.timerString("Before getDocument in file " + entityFileName); Document document = null; try { document = entityResourceHandler.getDocument(); } catch (GenericConfigException e) { throw new GenericEntityConfException("Error getting document from resource handler", e); } if (document == null) { throw new GenericEntityConfException("Could not get document for " + entityResourceHandler.toString()); } // utilTimer.timerString("Before getDocumentElement in " + entityResourceHandler.toString()); Element docElement = document.getDocumentElement(); if (docElement == null) { entityCache = null; return null; } docElement.normalize(); Node curChild = docElement.getFirstChild(); ModelInfo def = new ModelInfo(); def.populateFromElements(docElement); int i = 0; if (curChild != null) { utilTimer.timerString("Before start of entity loop in " + entityResourceHandler.toString()); do { boolean isEntity = "entity".equals(curChild.getNodeName()); boolean isViewEntity = "view-entity".equals(curChild.getNodeName()); if ((isEntity || isViewEntity) && curChild.getNodeType() == Node.ELEMENT_NODE) { i++; Element curEntity = (Element) curChild; String entityName = UtilXml.checkEmpty(curEntity.getAttribute("entity-name")); // add entityName to appropriate resourceHandlerEntities collection Collection resourceHandlerEntityNames = (Collection) resourceHandlerEntities.get(entityResourceHandler); if (resourceHandlerEntityNames == null) { resourceHandlerEntityNames = FastList.newInstance(); resourceHandlerEntities.put(entityResourceHandler, resourceHandlerEntityNames); } resourceHandlerEntityNames.add(entityName); // check to see if entity with same name has already been read if (entityCache.containsKey(entityName)) { Debug.logWarning("WARNING: Entity " + entityName + " is defined more than once, most recent will over-write " + "previous definition(s)", module); Debug.logWarning("WARNING: Entity " + entityName + " was found in " + entityResourceHandler + ", but was already defined in " + entityResourceHandlerMap.get(entityName).toString(), module); } // add entityName, entityFileName pair to entityResourceHandlerMap map entityResourceHandlerMap.put(entityName, entityResourceHandler); // utilTimer.timerString(" After entityEntityName -- " + i + " --"); // ModelEntity entity = createModelEntity(curEntity, utilTimer); ModelEntity entity = null; if (isEntity) { entity = createModelEntity(curEntity, null, def); } else { entity = createModelViewEntity(curEntity, null, def); // put the view entity in a list to get ready for the second pass to populate fields... tempViewEntityList.add(entity); } // utilTimer.timerString(" After createModelEntity -- " + i + " --"); if (entity != null) { entityCache.put(entityName, entity); // utilTimer.timerString(" After entityCache.put -- " + i + " --"); if (isEntity) { if (Debug.verboseOn()) Debug.logVerbose("-- [Entity]: #" + i + ": " + entityName, module); } else { if (Debug.verboseOn()) Debug.logVerbose("-- [ViewEntity]: #" + i + ": " + entityName, module); } } else { Debug.logWarning("-- -- ENTITYGEN ERROR:getModelEntity: Could not create " + "entity for entityName: " + entityName, module); } } } while ((curChild = curChild.getNextSibling()) != null); } else { Debug.logWarning("No child nodes found.", module); } utilTimer.timerString("Finished " + entityResourceHandler.toString() + " - Total Entities: " + i + " FINISHED"); } // do a pass on all of the view entities now that all of the entities have // loaded and populate the fields for (int velInd = 0; velInd < tempViewEntityList.size(); velInd++) { ModelViewEntity curViewEntity = (ModelViewEntity) tempViewEntityList.get(velInd); curViewEntity.populateFields(this); List memberEntities = curViewEntity.getAllModelMemberEntities(); for (int j = 0; j < memberEntities.size(); j++) { ModelViewEntity.ModelMemberEntity mve = (ModelViewEntity.ModelMemberEntity) memberEntities.get(j); ModelEntity me = (ModelEntity) entityCache.get(mve.getEntityName()); if (me == null) throw new GenericEntityConfException("View " + curViewEntity.getEntityName() + " references non-existant entity: " + mve.getEntityName());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -