001: /*
002: * $Id: EntityDataLoader.java,v 1.1 2003/08/20 22:58:44 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: */
025: package org.ofbiz.entity.util;
026:
027: import java.io.File;
028: import java.net.URL;
029: import java.util.Collection;
030: import java.util.Collections;
031: import java.util.Iterator;
032: import java.util.LinkedList;
033: import java.util.List;
034: import java.util.StringTokenizer;
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.base.util.Debug;
041: import org.ofbiz.base.util.UtilMisc;
042: import org.ofbiz.entity.GenericDelegator;
043: import org.ofbiz.entity.GenericEntityException;
044: import org.ofbiz.entity.config.EntityConfigUtil;
045: import org.ofbiz.entity.model.ModelEntity;
046: import org.ofbiz.entity.model.ModelReader;
047: import org.ofbiz.entity.model.ModelUtil;
048: import org.ofbiz.entity.model.ModelViewEntity;
049: import org.w3c.dom.Element;
050:
051: /**
052: * Some utility routines for loading seed data.
053: *
054: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
055: * @version $Revision: 1.1 $
056: * @since 3.0
057: */
058: public class EntityDataLoader {
059:
060: public static final String module = EntityDataLoader.class
061: .getName();
062:
063: public static String getPathsString(String helperName) {
064: StringBuffer pathBuffer = new StringBuffer();
065: if (helperName != null && helperName.length() > 0) {
066: EntityConfigUtil.DatasourceInfo datasourceInfo = EntityConfigUtil
067: .getDatasourceInfo(helperName);
068: List sqlLoadPathElements = datasourceInfo.sqlLoadPaths;
069: Iterator slpIter = sqlLoadPathElements.iterator();
070: while (slpIter.hasNext()) {
071: Element sqlLoadPathElement = (Element) slpIter.next();
072: String prependEnv = sqlLoadPathElement
073: .getAttribute("prepend-env");
074: pathBuffer.append(pathBuffer.length() == 0 ? "" : ";");
075: if (prependEnv != null && prependEnv.length() > 0) {
076: pathBuffer.append(System.getProperty(prependEnv));
077: pathBuffer.append("/");
078: }
079: pathBuffer.append(sqlLoadPathElement
080: .getAttribute("path"));
081: }
082: }
083: return pathBuffer.toString();
084: }
085:
086: public static List getUrlList(String helperName) {
087: String paths = getPathsString(helperName);
088: List urlList = new LinkedList();
089:
090: // first get files from resources
091:
092: EntityConfigUtil.DatasourceInfo datasourceInfo = EntityConfigUtil
093: .getDatasourceInfo(helperName);
094: if (datasourceInfo != null) {
095: Iterator readDataIter = datasourceInfo.readDatas.iterator();
096: while (readDataIter.hasNext()) {
097: Element readDataElement = (Element) readDataIter.next();
098: String readerName = readDataElement
099: .getAttribute("reader-name");
100:
101: // get all of the main resource model stuff, ie specified in the entityengine.xml file
102: EntityConfigUtil.EntityDataReaderInfo entityDataReaderInfo = EntityConfigUtil
103: .getEntityDataReaderInfo(readerName);
104:
105: if (entityDataReaderInfo != null) {
106: List resourceElements = entityDataReaderInfo.resourceElements;
107: Iterator resIter = resourceElements.iterator();
108: while (resIter.hasNext()) {
109: Element resourceElement = (Element) resIter
110: .next();
111: ResourceHandler handler = new MainResourceHandler(
112: EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME,
113: resourceElement);
114: try {
115: urlList.add(handler.getURL());
116: } catch (GenericConfigException e) {
117: String errorMsg = "Could not get URL for Main ResourceHandler: "
118: + e.toString();
119: Debug.logWarning(errorMsg, module);
120: }
121: }
122:
123: // get all of the component resource model stuff, ie specified in each ofbiz-component.xml file
124: List componentResourceInfos = ComponentConfig
125: .getAllEntityResourceInfos("data");
126: Iterator componentResourceInfoIter = componentResourceInfos
127: .iterator();
128: while (componentResourceInfoIter.hasNext()) {
129: ComponentConfig.EntityResourceInfo componentResourceInfo = (ComponentConfig.EntityResourceInfo) componentResourceInfoIter
130: .next();
131: if (readerName
132: .equals(componentResourceInfo.readerName)) {
133: ResourceHandler handler = componentResourceInfo
134: .createResourceHandler();
135: try {
136: urlList.add(handler.getURL());
137: } catch (GenericConfigException e) {
138: String errorMsg = "Could not get URL for Component ResourceHandler: "
139: + e.toString();
140: Debug.logWarning(errorMsg, module);
141: }
142: }
143: }
144: } else {
145: String errorMsg = "Could not find entity-date-reader named: "
146: + readerName;
147: Debug.logWarning(errorMsg, module);
148: }
149: }
150: } else {
151: String errorMsg = "Could not find datasource named: "
152: + helperName;
153: Debug.logWarning(errorMsg, module);
154: }
155:
156: // get files from the paths string
157: if (paths != null && paths.length() > 0) {
158: StringTokenizer tokenizer = new StringTokenizer(paths, ";");
159: while (tokenizer.hasMoreTokens()) {
160: String path = tokenizer.nextToken().toLowerCase();
161: File loadDir = new File(path);
162: if (loadDir.exists() && loadDir.isDirectory()) {
163: File[] files = loadDir.listFiles();
164: List tempFileList = new LinkedList();
165: for (int i = 0; i < files.length; i++) {
166: if (files[i].getName().toLowerCase().endsWith(
167: ".xml")) {
168: tempFileList.add(files[i]);
169: }
170: }
171: Collections.sort(tempFileList);
172: Iterator tempFileIter = tempFileList.iterator();
173: while (tempFileIter.hasNext()) {
174: File dataFile = (File) tempFileIter.next();
175: if (dataFile.exists()) {
176: URL url = null;
177: try {
178: url = dataFile.toURL();
179: urlList.add(url);
180: } catch (java.net.MalformedURLException e) {
181: String xmlError = "Error loading XML file \""
182: + dataFile.getAbsolutePath()
183: + "\"; Error was: "
184: + e.getMessage();
185: Debug.logError(xmlError, module);
186: }
187: } else {
188: String errorMsg = "Could not find file: \""
189: + dataFile.getAbsolutePath() + "\"";
190: Debug.logError(errorMsg, module);
191: }
192: }
193: }
194: }
195: }
196: return urlList;
197: }
198:
199: public static int loadData(URL dataUrl, String helperName,
200: GenericDelegator delegator, List errorMessages)
201: throws GenericEntityException {
202: int rowsChanged = 0;
203:
204: Debug.logInfo("[install.loadData] Loading XML Resource: \""
205: + dataUrl.toExternalForm() + "\"", module);
206:
207: try {
208: /* The OLD way
209: List toBeStored = delegator.readXmlDocument(url);
210: delegator.storeAll(toBeStored);
211: rowsChanged += toBeStored.size();
212: */
213: EntitySaxReader reader = new EntitySaxReader(delegator);
214: rowsChanged += reader.parse(dataUrl);
215: } catch (Exception e) {
216: String xmlError = "[install.loadData]: Error loading XML Resource \""
217: + dataUrl.toExternalForm()
218: + "\"; Error was: "
219: + e.getMessage();
220: errorMessages.add(xmlError);
221: Debug.logError(e, xmlError, module);
222: }
223:
224: return rowsChanged;
225: }
226:
227: public static int generateData(GenericDelegator delegator,
228: List errorMessages) throws GenericEntityException {
229: int rowsChanged = 0;
230: ModelReader reader = delegator.getModelReader();
231: Collection entityCol = reader.getEntityNames();
232: Iterator classNamesIterator = entityCol.iterator();
233: while (classNamesIterator != null
234: && classNamesIterator.hasNext()) {
235: ModelEntity entity = reader
236: .getModelEntity((String) classNamesIterator.next());
237: String baseName = entity.getPlainTableName();
238: if (entity instanceof ModelViewEntity) {
239: baseName = ModelUtil.javaNameToDbName(entity
240: .getEntityName());
241: }
242:
243: if (baseName != null) {
244: try {
245: List toBeStored = new LinkedList();
246: toBeStored.add(delegator.makeValue(
247: "SecurityPermission", UtilMisc.toMap(
248: "permissionId",
249: baseName + "_ADMIN", "description",
250: "Permission to Administer a "
251: + entity.getEntityName()
252: + " entity.")));
253: toBeStored.add(delegator.makeValue(
254: "SecurityGroupPermission", UtilMisc
255: .toMap("groupId", "FULLADMIN",
256: "permissionId", baseName
257: + "_ADMIN")));
258: rowsChanged += delegator.storeAll(toBeStored);
259: } catch (GenericEntityException e) {
260: errorMessages
261: .add("[install.generateData] ERROR: Failed Security Generation for entity \""
262: + baseName + "\"");
263: }
264:
265: /*
266: toStore.add(delegator.makeValue("SecurityPermission", UtilMisc.toMap("permissionId", baseName + "_VIEW", "description", "Permission to View a " + entity.getEntityName() + " entity.")));
267: toStore.add(delegator.makeValue("SecurityPermission", UtilMisc.toMap("permissionId", baseName + "_CREATE", "description", "Permission to Create a " + entity.getEntityName() + " entity.")));
268: toStore.add(delegator.makeValue("SecurityPermission", UtilMisc.toMap("permissionId", baseName + "_UPDATE", "description", "Permission to Update a " + entity.getEntityName() + " entity.")));
269: toStore.add(delegator.makeValue("SecurityPermission", UtilMisc.toMap("permissionId", baseName + "_DELETE", "description", "Permission to Delete a " + entity.getEntityName() + " entity.")));
270:
271: toStore.add(delegator.makeValue("SecurityGroupPermission", UtilMisc.toMap("groupId", "FLEXADMIN", "permissionId", baseName + "_VIEW")));
272: toStore.add(delegator.makeValue("SecurityGroupPermission", UtilMisc.toMap("groupId", "FLEXADMIN", "permissionId", baseName + "_CREATE")));
273: toStore.add(delegator.makeValue("SecurityGroupPermission", UtilMisc.toMap("groupId", "FLEXADMIN", "permissionId", baseName + "_UPDATE")));
274: toStore.add(delegator.makeValue("SecurityGroupPermission", UtilMisc.toMap("groupId", "FLEXADMIN", "permissionId", baseName + "_DELETE")));
275: */
276: }
277: }
278:
279: return rowsChanged;
280: }
281: }
|