1 24 package org.ofbiz.entity.model; 25 26 import java.io.Serializable ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Set ; 35 import java.util.TreeSet ; 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 ; 50 import org.w3c.dom.Element ; 51 import org.w3c.dom.Node ; 52 53 60 public class ModelGroupReader implements Serializable { 61 62 public static final String module = ModelGroupReader.class.getName(); 63 public static UtilCache readers = new UtilCache("entity.ModelGroupReader", 0, 0); 64 65 private Map groupCache = null; 66 private Set groupNames = null; 67 68 public String modelName; 69 public List entityGroupResourceHandlers = new LinkedList (); 70 71 public static ModelGroupReader getModelGroupReader(String 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 tempModelName = delegatorInfo.entityGroupReader; 79 ModelGroupReader reader = (ModelGroupReader) readers.get(tempModelName); 80 81 if (reader == null) { synchronized (ModelGroupReader.class) { 83 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 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 resourceElementIter = entityGroupReaderInfo.resourceElements.iterator(); 102 while (resourceElementIter.hasNext()) { 103 Element resourceElement = (Element ) resourceElementIter.next(); 104 this.entityGroupResourceHandlers.add(new MainResourceHandler(EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME, resourceElement)); 105 } 106 107 List componentResourceInfos = ComponentConfig.getAllEntityResourceInfos("group"); 109 Iterator 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 getGroupCache(); 119 } 120 121 public Map getGroupCache() { 122 if (this.groupCache == null) { 124 synchronized (ModelGroupReader.class) { 125 if (this.groupCache == null) { 127 this.groupCache = new HashMap (); 129 this.groupNames = new TreeSet (); 130 131 UtilTimer utilTimer = new UtilTimer(); 132 134 int i = 0; 135 Iterator entityGroupResourceHandlerIter = this.entityGroupResourceHandlers.iterator(); 136 while (entityGroupResourceHandlerIter.hasNext()) { 137 ResourceHandler entityGroupResourceHandler = (ResourceHandler) entityGroupResourceHandlerIter.next(); 138 Document 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 Element docElement = document.getDocumentElement(); 152 if (docElement == null) { 153 continue; 154 } 155 docElement.normalize(); 156 157 Node 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 curEntity = (Element ) curChild; 163 String entityName = UtilXml.checkEmpty(curEntity.getAttribute("entity")); 164 String 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 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 188 public String getEntityGroupName(String entityName) { 189 Map gc = getGroupCache(); 190 191 if (gc != null) 192 return (String ) gc.get(entityName); 193 else 194 return null; 195 } 196 197 200 public Collection getGroupNames() { 201 getGroupCache(); 202 if (this.groupNames == null) return null; 203 return new ArrayList (this.groupNames); 204 } 205 206 210 public Collection getEntityNamesByGroup(String groupName) { 211 Map gc = getGroupCache(); 212 Collection enames = new LinkedList (); 213 214 if (groupName == null || groupName.length() <= 0) return enames; 215 if (gc == null || gc.size() < 0) return enames; 216 Set gcEntries = gc.entrySet(); 217 Iterator gcIter = gcEntries.iterator(); 218 219 while (gcIter.hasNext()) { 220 Map.Entry entry = (Map.Entry ) gcIter.next(); 221 222 if (groupName.equals(entry.getValue())) enames.add(entry.getKey()); 223 } 224 return enames; 225 } 226 } 227 | Popular Tags |