1 25 package org.ofbiz.entity.util; 26 27 import java.io.File ; 28 import java.net.URL ; 29 import java.util.Collection ; 30 import java.util.Collections ; 31 import java.util.Iterator ; 32 import java.util.LinkedList ; 33 import java.util.List ; 34 import java.util.StringTokenizer ; 35 36 import org.w3c.dom.Element ; 37 38 import org.ofbiz.base.component.ComponentConfig; 39 import org.ofbiz.base.config.GenericConfigException; 40 import org.ofbiz.base.config.MainResourceHandler; 41 import org.ofbiz.base.config.ResourceHandler; 42 import org.ofbiz.base.util.Debug; 43 import org.ofbiz.base.util.UtilMisc; 44 import org.ofbiz.entity.GenericDelegator; 45 import org.ofbiz.entity.GenericEntityException; 46 import org.ofbiz.entity.config.DatasourceInfo; 47 import org.ofbiz.entity.config.EntityConfigUtil; 48 import org.ofbiz.entity.config.EntityDataReaderInfo; 49 import org.ofbiz.entity.model.ModelEntity; 50 import org.ofbiz.entity.model.ModelReader; 51 import org.ofbiz.entity.model.ModelUtil; 52 import org.ofbiz.entity.model.ModelViewEntity; 53 54 62 public class EntityDataLoader { 63 64 public static final String module = EntityDataLoader.class.getName(); 65 66 public static String getPathsString(String helperName) { 67 StringBuffer pathBuffer = new StringBuffer (); 68 if (helperName != null && helperName.length() > 0) { 69 DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperName); 70 List sqlLoadPathElements = datasourceInfo.sqlLoadPaths; 71 Iterator slpIter = sqlLoadPathElements.iterator(); 72 while (slpIter.hasNext()) { 73 Element sqlLoadPathElement = (Element ) slpIter.next(); 74 String prependEnv = sqlLoadPathElement.getAttribute("prepend-env"); 75 pathBuffer.append(pathBuffer.length() == 0 ? "" : ";"); 76 if (prependEnv != null && prependEnv.length() > 0) { 77 pathBuffer.append(System.getProperty(prependEnv)); 78 pathBuffer.append("/"); 79 } 80 pathBuffer.append(sqlLoadPathElement.getAttribute("path")); 81 } 82 } 83 return pathBuffer.toString(); 84 } 85 86 public static List getUrlList(String helperName) { 87 DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperName); 88 return getUrlList(helperName, datasourceInfo.readDatas); 89 } 90 91 public static List getUrlList(String helperName, List readerNames) { 92 String paths = getPathsString(helperName); 93 List urlList = new LinkedList (); 94 95 if (readerNames != null) { 97 Iterator readDataIter = readerNames.iterator(); 98 while (readDataIter.hasNext()) { 99 Object readerInfo = readDataIter.next(); 100 String readerName = null; 101 if (readerInfo instanceof String ) { 102 readerName = (String ) readerInfo; 103 } else if (readerInfo instanceof Element ) { 104 readerName = ((Element ) readerInfo).getAttribute("reader-name"); 105 } else { 106 throw new IllegalArgumentException ("Reader name list does not contain String(s) or Element(s)"); 107 } 108 109 EntityDataReaderInfo entityDataReaderInfo = EntityConfigUtil.getEntityDataReaderInfo(readerName); 111 112 if (entityDataReaderInfo != null) { 113 List resourceElements = entityDataReaderInfo.resourceElements; 114 Iterator resIter = resourceElements.iterator(); 115 while (resIter.hasNext()) { 116 Element resourceElement = (Element ) resIter.next(); 117 ResourceHandler handler = new MainResourceHandler(EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME, resourceElement); 118 try { 119 urlList.add(handler.getURL()); 120 } catch (GenericConfigException e) { 121 String errorMsg = "Could not get URL for Main ResourceHandler: " + e.toString(); 122 Debug.logWarning(errorMsg, module); 123 } 124 } 125 126 List componentResourceInfos = ComponentConfig.getAllEntityResourceInfos("data"); 128 Iterator componentResourceInfoIter = componentResourceInfos.iterator(); 129 while (componentResourceInfoIter.hasNext()) { 130 ComponentConfig.EntityResourceInfo componentResourceInfo = (ComponentConfig.EntityResourceInfo) componentResourceInfoIter.next(); 131 if (readerName.equals(componentResourceInfo.readerName)) { 132 ResourceHandler handler = componentResourceInfo.createResourceHandler(); 133 try { 134 urlList.add(handler.getURL()); 135 } catch (GenericConfigException e) { 136 String errorMsg = "Could not get URL for Component ResourceHandler: " + e.toString(); 137 Debug.logWarning(errorMsg, module); 138 } 139 } 140 } 141 } else { 142 String errorMsg = "Could not find entity-date-reader named: " + readerName; 143 Debug.logWarning(errorMsg, module); 144 } 145 } 146 } else { 147 String errorMsg = "Could not find datasource named: " + helperName; 148 Debug.logWarning(errorMsg, module); 149 } 150 151 if (paths != null && paths.length() > 0) { 153 StringTokenizer tokenizer = new StringTokenizer (paths, ";"); 154 while (tokenizer.hasMoreTokens()) { 155 String path = tokenizer.nextToken().toLowerCase(); 156 File loadDir = new File (path); 157 if (loadDir.exists() && loadDir.isDirectory()) { 158 File [] files = loadDir.listFiles(); 159 List tempFileList = new LinkedList (); 160 for (int i = 0; i < files.length; i++) { 161 if (files[i].getName().toLowerCase().endsWith(".xml")) { 162 tempFileList.add(files[i]); 163 } 164 } 165 Collections.sort(tempFileList); 166 Iterator tempFileIter = tempFileList.iterator(); 167 while (tempFileIter.hasNext()) { 168 File dataFile = (File ) tempFileIter.next(); 169 if (dataFile.exists()) { 170 URL url = null; 171 try { 172 url = dataFile.toURL(); 173 urlList.add(url); 174 } catch (java.net.MalformedURLException e) { 175 String xmlError = "Error loading XML file \"" + dataFile.getAbsolutePath() + "\"; Error was: " + e.getMessage(); 176 Debug.logError(xmlError, module); 177 } 178 } else { 179 String errorMsg = "Could not find file: \"" + dataFile.getAbsolutePath() + "\""; 180 Debug.logError(errorMsg, module); 181 } 182 } 183 } 184 } 185 } 186 187 return urlList; 188 } 189 190 public static int loadData(URL dataUrl, String helperName, GenericDelegator delegator, List errorMessages) throws GenericEntityException { 191 return loadData(dataUrl, helperName, delegator, errorMessages, -1); 192 } 193 194 public static int loadData(URL dataUrl, String helperName, GenericDelegator delegator, List errorMessages, int txTimeout) throws GenericEntityException { 195 return loadData(dataUrl, helperName, delegator, errorMessages, txTimeout, false, false, false); 196 } 197 198 public static int loadData(URL dataUrl, String helperName, GenericDelegator delegator, List errorMessages, int txTimeout, boolean dummyFks, boolean maintainTxs, boolean tryInsert) throws GenericEntityException { 199 int rowsChanged = 0; 200 201 if (dataUrl == null) { 202 String errMsg = "Cannot load data, dataUrl was null"; 203 errorMessages.add(errMsg); 204 Debug.logError(errMsg, module); 205 return 0; 206 } 207 208 Debug.logVerbose("[install.loadData] Loading XML Resource: \"" + dataUrl.toExternalForm() + "\"", module); 209 210 try { 211 216 217 EntitySaxReader reader = null; 218 if (txTimeout > 0) { 219 reader = new EntitySaxReader(delegator, txTimeout); 220 } else { 221 reader = new EntitySaxReader(delegator); 222 } 223 reader.setCreateDummyFks(dummyFks); 224 reader.setMaintainTxStamps(maintainTxs); 225 rowsChanged += reader.parse(dataUrl); 226 } catch (Exception e) { 227 String xmlError = "[install.loadData]: Error loading XML Resource \"" + dataUrl.toExternalForm() + "\"; Error was: " + e.getMessage(); 228 errorMessages.add(xmlError); 229 Debug.logError(e, xmlError, module); 230 } 231 232 return rowsChanged; 233 } 234 235 public static int generateData(GenericDelegator delegator, List errorMessages) throws GenericEntityException { 236 int rowsChanged = 0; 237 ModelReader reader = delegator.getModelReader(); 238 Collection entityCol = reader.getEntityNames(); 239 Iterator classNamesIterator = entityCol.iterator(); 240 while (classNamesIterator != null && classNamesIterator.hasNext()) { 241 ModelEntity entity = reader.getModelEntity((String ) classNamesIterator.next()); 242 String baseName = entity.getPlainTableName(); 243 if (entity instanceof ModelViewEntity) { 244 baseName = ModelUtil.javaNameToDbName(entity.getEntityName()); 245 } 246 247 if (baseName != null) { 248 try { 249 List toBeStored = new LinkedList (); 250 toBeStored.add( 251 delegator.makeValue( 252 "SecurityPermission", 253 UtilMisc.toMap( 254 "permissionId", 255 baseName + "_ADMIN", 256 "description", 257 "Permission to Administer a " + entity.getEntityName() + " entity."))); 258 toBeStored.add(delegator.makeValue("SecurityGroupPermission", UtilMisc.toMap("groupId", "FULLADMIN", "permissionId", baseName + "_ADMIN"))); 259 rowsChanged += delegator.storeAll(toBeStored); 260 } catch (GenericEntityException e) { 261 errorMessages.add("[install.generateData] ERROR: Failed Security Generation for entity \"" + baseName + "\""); 262 } 263 264 275 } 276 } 277 278 return rowsChanged; 279 } 280 } 281 | Popular Tags |