1 24 package org.ofbiz.entity.model; 25 26 import java.io.Serializable ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 import org.ofbiz.base.config.GenericConfigException; 32 import org.ofbiz.base.config.MainResourceHandler; 33 import org.ofbiz.base.config.ResourceHandler; 34 import org.ofbiz.base.util.Debug; 35 import org.ofbiz.base.util.UtilTimer; 36 import org.ofbiz.base.util.UtilXml; 37 import org.ofbiz.base.util.cache.UtilCache; 38 import org.ofbiz.entity.config.DatasourceInfo; 39 import org.ofbiz.entity.config.EntityConfigUtil; 40 import org.ofbiz.entity.config.FieldTypeInfo; 41 import org.w3c.dom.Document ; 42 import org.w3c.dom.Element ; 43 import org.w3c.dom.Node ; 44 45 52 public class ModelFieldTypeReader implements Serializable { 53 54 public static final String module = ModelFieldTypeReader.class.getName(); 55 public static UtilCache readers = new UtilCache("entity.ModelFieldTypeReader", 0, 0); 56 57 public Map fieldTypeCache = null; 58 59 public int numEntities = 0; 60 public int numFields = 0; 61 public int numRelations = 0; 62 63 public String modelName; 64 public ResourceHandler fieldTypeResourceHandler; 65 public String entityFileName; 66 67 public static ModelFieldTypeReader getModelFieldTypeReader(String helperName) { 68 DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperName); 69 if (datasourceInfo == null) { 70 throw new IllegalArgumentException ("Could not find a datasource/helper with the name " + helperName); 71 } 72 73 String tempModelName = datasourceInfo.fieldTypeName; 74 ModelFieldTypeReader reader = (ModelFieldTypeReader) readers.get(tempModelName); 75 76 if (reader == null) { 78 synchronized (ModelFieldTypeReader.class) { 79 reader = (ModelFieldTypeReader) readers.get(tempModelName); 81 if (reader == null) { 82 reader = new ModelFieldTypeReader(tempModelName); 83 readers.put(tempModelName, reader); 84 } 85 } 86 } 87 return reader; 88 } 89 90 public ModelFieldTypeReader(String modelName) { 91 this.modelName = modelName; 92 FieldTypeInfo fieldTypeInfo = EntityConfigUtil.getFieldTypeInfo(modelName); 93 94 if (fieldTypeInfo == null) { 95 throw new IllegalStateException ("Could not find a field-type definition with name \"" + modelName + "\""); 96 } 97 fieldTypeResourceHandler = new MainResourceHandler(EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME, fieldTypeInfo.resourceElement); 98 99 getFieldTypeCache(); 101 } 102 103 public Map getFieldTypeCache() { 104 if (fieldTypeCache == null) { 106 synchronized (ModelFieldTypeReader.class) { 107 if (fieldTypeCache == null) { 110 fieldTypeCache = new HashMap (); 111 112 UtilTimer utilTimer = new UtilTimer(); 113 115 Document document = null; 116 117 try { 118 document = fieldTypeResourceHandler.getDocument(); 119 } catch (GenericConfigException e) { 120 Debug.logError(e, "Error loading field type file", module); 121 } 122 if (document == null) { 123 fieldTypeCache = null; 124 return null; 125 } 126 127 Element docElement = document.getDocumentElement(); 129 130 if (docElement == null) { 131 fieldTypeCache = null; 132 return null; 133 } 134 docElement.normalize(); 135 136 Node curChild = docElement.getFirstChild(); 137 138 int i = 0; 139 140 if (curChild != null) { 141 utilTimer.timerString("Before start of field type loop"); 142 do { 143 if (curChild.getNodeType() == Node.ELEMENT_NODE && "field-type-def".equals(curChild.getNodeName())) { 144 i++; 145 Element curFieldType = (Element ) curChild; 147 String fieldTypeName = UtilXml.checkEmpty(curFieldType.getAttribute("type"), "[No type name]"); 148 ModelFieldType fieldType = createModelFieldType(curFieldType, docElement, null); 150 151 if (fieldType != null) { 153 fieldTypeCache.put(fieldTypeName, fieldType); 154 if (Debug.verboseOn()) Debug.logVerbose("-- getModelFieldType: #" + i + " Created fieldType: " + fieldTypeName, module); 156 } else { 157 Debug.logWarning("-- -- ENTITYGEN ERROR:getModelFieldType: Could not create fieldType for fieldTypeName: " + fieldTypeName, module); 158 } 159 160 } 161 } while ((curChild = curChild.getNextSibling()) != null); 162 } else 163 Debug.logWarning("No child nodes found.", module); 164 utilTimer.timerString("FINISHED - Total Field Types: " + i + " FINISHED"); 165 } 166 } 167 } 168 return fieldTypeCache; 169 } 170 171 174 public Collection getFieldTypeNames() { 175 Map ftc = getFieldTypeCache(); 176 177 return ftc.keySet(); 178 } 179 180 183 public Collection getFieldTypes() { 184 Map ftc = getFieldTypeCache(); 185 186 return ftc.values(); 187 } 188 189 193 public ModelFieldType getModelFieldType(String fieldTypeName) { 194 Map ftc = getFieldTypeCache(); 195 196 if (ftc != null) 197 return (ModelFieldType) ftc.get(fieldTypeName); 198 else 199 return null; 200 } 201 202 ModelFieldType createModelFieldType(Element fieldTypeElement, Element docElement, UtilTimer utilTimer) { 203 if (fieldTypeElement == null) return null; 204 205 ModelFieldType field = new ModelFieldType(fieldTypeElement); 206 207 return field; 208 } 209 } 210 | Popular Tags |