001: /*
002: * $Id: ModelGroupReader.java,v 1.4 2003/11/25 07:48:14 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.entity.model;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.LinkedList;
031: import java.util.List;
032: import java.util.Map;
033: import java.util.Set;
034: import java.util.TreeSet;
035:
036: import org.ofbiz.base.component.ComponentConfig;
037: import org.ofbiz.base.config.GenericConfigException;
038: import org.ofbiz.base.config.MainResourceHandler;
039: import org.ofbiz.base.config.ResourceHandler;
040: import org.ofbiz.entity.GenericEntityConfException;
041: import org.ofbiz.entity.config.EntityConfigUtil;
042: import org.ofbiz.base.util.Debug;
043: import org.ofbiz.base.util.UtilCache;
044: import org.ofbiz.base.util.UtilTimer;
045: import org.ofbiz.base.util.UtilXml;
046: import org.w3c.dom.Document;
047: import org.w3c.dom.Element;
048: import org.w3c.dom.Node;
049:
050: /**
051: * Generic Entity - Entity Group Definition Reader
052: *
053: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
054: * @version $Revision: 1.4 $
055: * @since 2.0
056: */
057: public class ModelGroupReader {
058:
059: public static final String module = ModelGroupReader.class
060: .getName();
061: public static UtilCache readers = new UtilCache(
062: "entity.ModelGroupReader", 0, 0);
063:
064: private Map groupCache = null;
065: private Set groupNames = null;
066:
067: public String modelName;
068: public List entityGroupResourceHandlers = new LinkedList();
069:
070: public static ModelGroupReader getModelGroupReader(
071: String delegatorName) throws GenericEntityConfException {
072: EntityConfigUtil.DelegatorInfo delegatorInfo = EntityConfigUtil
073: .getDelegatorInfo(delegatorName);
074:
075: if (delegatorInfo == null) {
076: throw new GenericEntityConfException(
077: "Could not find a delegator with the name "
078: + delegatorName);
079: }
080:
081: String tempModelName = delegatorInfo.entityGroupReader;
082: ModelGroupReader reader = (ModelGroupReader) readers
083: .get(tempModelName);
084:
085: if (reader == null) { // don't want to block here
086: synchronized (ModelGroupReader.class) {
087: // must check if null again as one of the blocked threads can still enter
088: reader = (ModelGroupReader) readers.get(tempModelName);
089: if (reader == null) {
090: reader = new ModelGroupReader(tempModelName);
091: readers.put(tempModelName, reader);
092: }
093: }
094: }
095: return reader;
096: }
097:
098: public ModelGroupReader(String modelName)
099: throws GenericEntityConfException {
100: this .modelName = modelName;
101: EntityConfigUtil.EntityGroupReaderInfo entityGroupReaderInfo = EntityConfigUtil
102: .getEntityGroupReaderInfo(modelName);
103:
104: if (entityGroupReaderInfo == null) {
105: throw new GenericEntityConfException(
106: "Cound not find an entity-group-reader with the name "
107: + modelName);
108: }
109: Iterator resourceElementIter = entityGroupReaderInfo.resourceElements
110: .iterator();
111: while (resourceElementIter.hasNext()) {
112: Element resourceElement = (Element) resourceElementIter
113: .next();
114: this .entityGroupResourceHandlers
115: .add(new MainResourceHandler(
116: EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME,
117: resourceElement));
118: }
119:
120: // get all of the component resource group stuff, ie specified in each ofbiz-component.xml file
121: List componentResourceInfos = ComponentConfig
122: .getAllEntityResourceInfos("group");
123: Iterator componentResourceInfoIter = componentResourceInfos
124: .iterator();
125: while (componentResourceInfoIter.hasNext()) {
126: ComponentConfig.EntityResourceInfo componentResourceInfo = (ComponentConfig.EntityResourceInfo) componentResourceInfoIter
127: .next();
128: if (modelName.equals(componentResourceInfo.readerName)) {
129: this .entityGroupResourceHandlers
130: .add(componentResourceInfo
131: .createResourceHandler());
132: }
133: }
134:
135: // preload caches...
136: getGroupCache();
137: }
138:
139: public Map getGroupCache() {
140: if (this .groupCache == null) // don't want to block here
141: {
142: synchronized (ModelGroupReader.class) {
143: // must check if null again as one of the blocked threads can still enter
144: if (this .groupCache == null) {
145: // now it's safe
146: this .groupCache = new HashMap();
147: this .groupNames = new TreeSet();
148:
149: UtilTimer utilTimer = new UtilTimer();
150: // utilTimer.timerString("[ModelGroupReader.getGroupCache] Before getDocument");
151:
152: int i = 0;
153: Iterator entityGroupResourceHandlerIter = this .entityGroupResourceHandlers
154: .iterator();
155: while (entityGroupResourceHandlerIter.hasNext()) {
156: ResourceHandler entityGroupResourceHandler = (ResourceHandler) entityGroupResourceHandlerIter
157: .next();
158: Document document = null;
159:
160: try {
161: document = entityGroupResourceHandler
162: .getDocument();
163: } catch (GenericConfigException e) {
164: Debug.logError(e,
165: "Error loading entity group model",
166: module);
167: }
168: if (document == null) {
169: this .groupCache = null;
170: return null;
171: }
172:
173: // utilTimer.timerString("[ModelGroupReader.getGroupCache] Before getDocumentElement");
174: Element docElement = document
175: .getDocumentElement();
176: if (docElement == null) {
177: continue;
178: }
179: docElement.normalize();
180:
181: Node curChild = docElement.getFirstChild();
182: if (curChild != null) {
183: utilTimer
184: .timerString("[ModelGroupReader.getGroupCache] Before start of entity loop");
185: do {
186: if (curChild.getNodeType() == Node.ELEMENT_NODE
187: && "entity-group"
188: .equals(curChild
189: .getNodeName())) {
190: Element curEntity = (Element) curChild;
191: String entityName = UtilXml
192: .checkEmpty(curEntity
193: .getAttribute("entity"));
194: String groupName = UtilXml
195: .checkEmpty(curEntity
196: .getAttribute("group"));
197:
198: if (groupName == null
199: || entityName == null)
200: continue;
201: this .groupNames.add(groupName);
202: this .groupCache.put(entityName,
203: groupName);
204: // utilTimer.timerString(" After entityEntityName -- " + i + " --");
205: i++;
206: }
207: } while ((curChild = curChild
208: .getNextSibling()) != null);
209: } else {
210: Debug
211: .logWarning(
212: "[ModelGroupReader.getGroupCache] No child nodes found.",
213: module);
214: }
215: }
216: utilTimer
217: .timerString("[ModelGroupReader.getGroupCache] FINISHED - Total Entity-Groups: "
218: + i + " FINISHED");
219: }
220: }
221: }
222: return this .groupCache;
223: }
224:
225: /** Gets a group name based on a definition from the specified XML Entity Group descriptor file.
226: * @param entityName The entityName of the Entity Group definition to use.
227: * @return A group name
228: */
229: public String getEntityGroupName(String entityName) {
230: Map gc = getGroupCache();
231:
232: if (gc != null)
233: return (String) gc.get(entityName);
234: else
235: return null;
236: }
237:
238: /** Creates a Collection with all of the groupNames defined in the specified XML Entity Group Descriptor file.
239: * @return A Collection of groupNames Strings
240: */
241: public Collection getGroupNames() {
242: getGroupCache();
243: if (this .groupNames == null)
244: return null;
245: return new ArrayList(this .groupNames);
246: }
247:
248: /** Creates a Collection with names of all of the entities for a given group
249: * @param groupName
250: * @return A Collection of entityName Strings
251: */
252: public Collection getEntityNamesByGroup(String groupName) {
253: Map gc = getGroupCache();
254: Collection enames = new LinkedList();
255:
256: if (groupName == null || groupName.length() <= 0)
257: return enames;
258: if (gc == null || gc.size() < 0)
259: return enames;
260: Set gcEntries = gc.entrySet();
261: Iterator gcIter = gcEntries.iterator();
262:
263: while (gcIter.hasNext()) {
264: Map.Entry entry = (Map.Entry) gcIter.next();
265:
266: if (groupName.equals(entry.getValue()))
267: enames.add(entry.getKey());
268: }
269: return enames;
270: }
271: }
|