KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > model > ModelGroupReader


1 /*
2  * $Id: ModelGroupReader.java 5720 2005-09-13 03:10:59Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.entity.model;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.TreeSet JavaDoc;
36
37 import org.ofbiz.base.component.ComponentConfig;
38 import org.ofbiz.base.config.GenericConfigException;
39 import org.ofbiz.base.config.MainResourceHandler;
40 import org.ofbiz.base.config.ResourceHandler;
41 import org.ofbiz.entity.GenericEntityConfException;
42 import org.ofbiz.entity.config.DelegatorInfo;
43 import org.ofbiz.entity.config.EntityConfigUtil;
44 import org.ofbiz.entity.config.EntityGroupReaderInfo;
45 import org.ofbiz.base.util.Debug;
46 import org.ofbiz.base.util.cache.UtilCache;
47 import org.ofbiz.base.util.UtilTimer;
48 import org.ofbiz.base.util.UtilXml;
49 import org.w3c.dom.Document JavaDoc;
50 import org.w3c.dom.Element JavaDoc;
51 import org.w3c.dom.Node JavaDoc;
52
53 /**
54  * Generic Entity - Entity Group Definition Reader
55  *
56  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
57  * @version $Rev: 5720 $
58  * @since 2.0
59  */

60 public class ModelGroupReader implements Serializable JavaDoc {
61
62     public static final String JavaDoc module = ModelGroupReader.class.getName();
63     public static UtilCache readers = new UtilCache("entity.ModelGroupReader", 0, 0);
64
65     private Map JavaDoc groupCache = null;
66     private Set JavaDoc groupNames = null;
67
68     public String JavaDoc modelName;
69     public List JavaDoc entityGroupResourceHandlers = new LinkedList JavaDoc();
70
71     public static ModelGroupReader getModelGroupReader(String JavaDoc delegatorName) throws GenericEntityConfException {
72         DelegatorInfo delegatorInfo = EntityConfigUtil.getDelegatorInfo(delegatorName);
73
74         if (delegatorInfo == null) {
75             throw new GenericEntityConfException("Could not find a delegator with the name " + delegatorName);
76         }
77
78         String JavaDoc tempModelName = delegatorInfo.entityGroupReader;
79         ModelGroupReader reader = (ModelGroupReader) readers.get(tempModelName);
80
81         if (reader == null) { // don't want to block here
82
synchronized (ModelGroupReader.class) {
83                 // must check if null again as one of the blocked threads can still enter
84
reader = (ModelGroupReader) readers.get(tempModelName);
85                 if (reader == null) {
86                     reader = new ModelGroupReader(tempModelName);
87                     readers.put(tempModelName, reader);
88                 }
89             }
90         }
91         return reader;
92     }
93
94     public ModelGroupReader(String JavaDoc modelName) throws GenericEntityConfException {
95         this.modelName = modelName;
96         EntityGroupReaderInfo entityGroupReaderInfo = EntityConfigUtil.getEntityGroupReaderInfo(modelName);
97
98         if (entityGroupReaderInfo == null) {
99             throw new GenericEntityConfException("Cound not find an entity-group-reader with the name " + modelName);
100         }
101         Iterator JavaDoc resourceElementIter = entityGroupReaderInfo.resourceElements.iterator();
102         while (resourceElementIter.hasNext()) {
103             Element JavaDoc resourceElement = (Element JavaDoc) resourceElementIter.next();
104             this.entityGroupResourceHandlers.add(new MainResourceHandler(EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME, resourceElement));
105         }
106
107         // get all of the component resource group stuff, ie specified in each ofbiz-component.xml file
108
List JavaDoc componentResourceInfos = ComponentConfig.getAllEntityResourceInfos("group");
109         Iterator JavaDoc componentResourceInfoIter = componentResourceInfos.iterator();
110         while (componentResourceInfoIter.hasNext()) {
111             ComponentConfig.EntityResourceInfo componentResourceInfo = (ComponentConfig.EntityResourceInfo) componentResourceInfoIter.next();
112             if (modelName.equals(componentResourceInfo.readerName)) {
113                 this.entityGroupResourceHandlers.add(componentResourceInfo.createResourceHandler());
114             }
115         }
116
117         // preload caches...
118
getGroupCache();
119     }
120
121     public Map JavaDoc getGroupCache() {
122         if (this.groupCache == null) // don't want to block here
123
{
124             synchronized (ModelGroupReader.class) {
125                 // must check if null again as one of the blocked threads can still enter
126
if (this.groupCache == null) {
127                     // now it's safe
128
this.groupCache = new HashMap JavaDoc();
129                     this.groupNames = new TreeSet JavaDoc();
130
131                     UtilTimer utilTimer = new UtilTimer();
132                     // utilTimer.timerString("[ModelGroupReader.getGroupCache] Before getDocument");
133

134                     int i = 0;
135                     Iterator JavaDoc entityGroupResourceHandlerIter = this.entityGroupResourceHandlers.iterator();
136                     while (entityGroupResourceHandlerIter.hasNext()) {
137                         ResourceHandler entityGroupResourceHandler = (ResourceHandler) entityGroupResourceHandlerIter.next();
138                         Document JavaDoc document = null;
139
140                         try {
141                             document = entityGroupResourceHandler.getDocument();
142                         } catch (GenericConfigException e) {
143                             Debug.logError(e, "Error loading entity group model", module);
144                         }
145                         if (document == null) {
146                             this.groupCache = null;
147                             return null;
148                         }
149
150                         // utilTimer.timerString("[ModelGroupReader.getGroupCache] Before getDocumentElement");
151
Element JavaDoc docElement = document.getDocumentElement();
152                         if (docElement == null) {
153                             continue;
154                         }
155                         docElement.normalize();
156
157                         Node JavaDoc curChild = docElement.getFirstChild();
158                         if (curChild != null) {
159                             utilTimer.timerString("[ModelGroupReader.getGroupCache] Before start of entity loop");
160                             do {
161                                 if (curChild.getNodeType() == Node.ELEMENT_NODE && "entity-group".equals(curChild.getNodeName())) {
162                                     Element JavaDoc curEntity = (Element JavaDoc) curChild;
163                                     String JavaDoc entityName = UtilXml.checkEmpty(curEntity.getAttribute("entity"));
164                                     String JavaDoc groupName = UtilXml.checkEmpty(curEntity.getAttribute("group"));
165
166                                     if (groupName == null || entityName == null) continue;
167                                     this.groupNames.add(groupName);
168                                     this.groupCache.put(entityName, groupName);
169                                     // utilTimer.timerString(" After entityEntityName -- " + i + " --");
170
i++;
171                                 }
172                             } while ((curChild = curChild.getNextSibling()) != null);
173                         } else {
174                             Debug.logWarning("[ModelGroupReader.getGroupCache] No child nodes found.", module);
175                         }
176                     }
177                     utilTimer.timerString("[ModelGroupReader.getGroupCache] FINISHED - Total Entity-Groups: " + i + " FINISHED");
178                 }
179             }
180         }
181         return this.groupCache;
182     }
183
184     /** Gets a group name based on a definition from the specified XML Entity Group descriptor file.
185      * @param entityName The entityName of the Entity Group definition to use.
186      * @return A group name
187      */

188     public String JavaDoc getEntityGroupName(String JavaDoc entityName) {
189         Map JavaDoc gc = getGroupCache();
190
191         if (gc != null)
192             return (String JavaDoc) gc.get(entityName);
193         else
194             return null;
195     }
196
197     /** Creates a Collection with all of the groupNames defined in the specified XML Entity Group Descriptor file.
198      * @return A Collection of groupNames Strings
199      */

200     public Collection JavaDoc getGroupNames() {
201         getGroupCache();
202         if (this.groupNames == null) return null;
203         return new ArrayList JavaDoc(this.groupNames);
204     }
205
206     /** Creates a Collection with names of all of the entities for a given group
207      * @param groupName
208      * @return A Collection of entityName Strings
209      */

210     public Collection JavaDoc getEntityNamesByGroup(String JavaDoc groupName) {
211         Map JavaDoc gc = getGroupCache();
212         Collection JavaDoc enames = new LinkedList JavaDoc();
213
214         if (groupName == null || groupName.length() <= 0) return enames;
215         if (gc == null || gc.size() < 0) return enames;
216         Set JavaDoc gcEntries = gc.entrySet();
217         Iterator JavaDoc gcIter = gcEntries.iterator();
218
219         while (gcIter.hasNext()) {
220             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) gcIter.next();
221
222             if (groupName.equals(entry.getValue())) enames.add(entry.getKey());
223         }
224         return enames;
225     }
226 }
227
Popular Tags