001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.entityext.data;
019:
020: import java.net.URL;
021: import java.net.MalformedURLException;
022: import java.text.NumberFormat;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.List;
026: import java.util.ArrayList;
027: import java.io.File;
028:
029: import org.ofbiz.base.container.Container;
030: import org.ofbiz.base.container.ContainerConfig;
031: import org.ofbiz.base.container.ContainerException;
032: import org.ofbiz.base.util.Debug;
033: import org.ofbiz.base.util.StringUtil;
034: import org.ofbiz.base.util.UtilURL;
035: import org.ofbiz.entity.GenericDelegator;
036: import org.ofbiz.entity.GenericEntityException;
037: import org.ofbiz.entity.util.EntityDataLoader;
038: import org.ofbiz.service.ServiceDispatcher;
039:
040: /**
041: * Some utility routines for loading seed data.
042: */
043: public class EntityDataLoadContainer implements Container {
044:
045: public static final String module = EntityDataLoadContainer.class
046: .getName();
047:
048: protected String overrideDelegator = null;
049: protected String overrideGroup = null;
050: protected String configFile = null;
051: protected String readers = null;
052: protected String directory = null;
053: protected String file = null;
054: protected String component = null;
055: protected boolean useDummyFks = false;
056: protected boolean maintainTxs = false;
057: protected boolean tryInserts = false;
058: protected int txTimeout = -1;
059:
060: public EntityDataLoadContainer() {
061: super ();
062: }
063:
064: /**
065: * @see org.ofbiz.base.container.Container#init(java.lang.String[], java.lang.String)
066: */
067: public void init(String[] args, String configFile)
068: throws ContainerException {
069: this .configFile = configFile;
070: // disable job scheduler, JMS listener and startup services
071: ServiceDispatcher.enableJM(false);
072: ServiceDispatcher.enableJMS(false);
073: ServiceDispatcher.enableSvcs(false);
074:
075: /*
076: install arguments:
077: readers (none, all, seed, demo, ext, etc - configured in entityengine.xml and associated via ofbiz-component.xml)
078: timeout (transaction timeout default 7200)
079: delegator (overrides the delegator name configured for the container)
080: group (overrides the entity group name configured for the container)
081: dir (imports all XML files in a directory)
082: file (import a specific XML file)
083:
084: Example:
085: $ java -jar ofbiz.jar -install -readers=seed,demo,ext -timeout=7200 -delegator=default -group=org.ofbiz
086: $ java -jar ofbiz.jar -install -file=/tmp/dataload.xml
087: */
088: if (args != null) {
089: for (int i = 0; i < args.length; i++) {
090: String argument = args[i];
091: // arguments can prefix w/ a '-'. Just strip them off
092: if (argument.startsWith("-")) {
093: int subIdx = 1;
094: if (argument.startsWith("--")) {
095: subIdx = 2;
096: }
097: argument = argument.substring(subIdx);
098: }
099:
100: // parse the arguments
101: String argumentName;
102: String argumentVal;
103: if (argument.indexOf("=") != -1) {
104: argumentName = argument.substring(0, argument
105: .indexOf("="));
106: argumentVal = argument.substring(argument
107: .indexOf("=") + 1);
108: } else {
109: argumentName = argument;
110: argumentVal = "";
111: }
112: Debug.log("Install Argument - " + argumentName + " = "
113: + argumentVal, module);
114:
115: if ("readers".equalsIgnoreCase(argumentName)) {
116: this .readers = argumentVal;
117: } else if ("timeout".equalsIgnoreCase(argumentName)) {
118: try {
119: this .txTimeout = Integer.parseInt(argumentVal);
120: } catch (Exception e) {
121: this .txTimeout = -1;
122: }
123: } else if ("component".equalsIgnoreCase(argumentName)) {
124: this .component = argumentVal;
125: } else if ("delegator".equalsIgnoreCase(argumentName)) {
126: this .overrideDelegator = argumentVal;
127: } else if ("group".equalsIgnoreCase(argumentName)) {
128: this .overrideGroup = argumentVal;
129: } else if ("file".equalsIgnoreCase(argumentName)) {
130: this .file = argumentVal;
131: } else if ("dir".equalsIgnoreCase(argumentName)) {
132: this .directory = argumentVal;
133: } else if ("createfks".equalsIgnoreCase(argumentName)) {
134: this .useDummyFks = "true"
135: .equalsIgnoreCase(argumentVal);
136: } else if ("maintainTxs".equalsIgnoreCase(argumentName)) {
137: this .maintainTxs = "true"
138: .equalsIgnoreCase(argumentVal);
139: } else if ("inserts".equalsIgnoreCase(argumentName)) {
140: this .tryInserts = "true"
141: .equalsIgnoreCase(argumentVal);
142: } else if ("help".equalsIgnoreCase(argumentName)) {
143: Debug.log("--------------------------------------",
144: module);
145: Debug.log("java -jar ofbiz.jar -install [options]",
146: module);
147: Debug
148: .log("-component=[name] .... only load from a specific component");
149: Debug
150: .log(
151: "-delegator=[name] .... use the defined delegator (default-no-eca",
152: module);
153: Debug
154: .log(
155: "-group=[name] ........ override the entity group (org.ofbiz)",
156: module);
157: Debug
158: .log(
159: "-file=[path] ......... load a single from from location",
160: module);
161: Debug
162: .log(
163: "-createfks ........... create dummy (placeholder) FKs",
164: module);
165: Debug
166: .log(
167: "-maintainTxs ......... maintain timestamps in data file",
168: module);
169: Debug
170: .log(
171: "-inserts ............. use mostly inserts option",
172: module);
173: Debug
174: .log(
175: "-help ................ display this information",
176: module);
177: System.exit(1);
178: }
179:
180: // special case
181: if (this .readers == null
182: && (this .file != null || this .directory != null)) {
183: this .readers = "none";
184: }
185: }
186: }
187: }
188:
189: /**
190: * @see org.ofbiz.base.container.Container#start()
191: */
192: public boolean start() throws ContainerException {
193: ContainerConfig.Container cfg = ContainerConfig.getContainer(
194: "dataload-container", configFile);
195: ContainerConfig.Container.Property delegatorNameProp = cfg
196: .getProperty("delegator-name");
197: ContainerConfig.Container.Property entityGroupNameProp = cfg
198: .getProperty("entity-group-name");
199:
200: String delegatorName = null;
201: String entityGroupName = null;
202:
203: if (delegatorNameProp == null
204: || delegatorNameProp.value == null
205: || delegatorNameProp.value.length() == 0) {
206: throw new ContainerException(
207: "Invalid delegator-name defined in container configuration");
208: } else {
209: delegatorName = delegatorNameProp.value;
210: }
211:
212: if (entityGroupNameProp == null
213: || entityGroupNameProp.value == null
214: || entityGroupNameProp.value.length() == 0) {
215: throw new ContainerException(
216: "Invalid entity-group-name defined in container configuration");
217: } else {
218: entityGroupName = entityGroupNameProp.value;
219: }
220:
221: // parse the pass in list of readers to use
222: List readerNames = null;
223: if (this .readers != null
224: && !"none".equalsIgnoreCase(this .readers)) {
225: if (this .readers.indexOf(",") == -1) {
226: readerNames = new LinkedList();
227: readerNames.add(this .readers);
228: } else {
229: readerNames = StringUtil.split(this .readers, ",");
230: }
231: }
232:
233: String delegatorNameToUse = overrideDelegator != null ? overrideDelegator
234: : delegatorName;
235: String groupNameToUse = overrideGroup != null ? overrideGroup
236: : entityGroupName;
237: GenericDelegator delegator = GenericDelegator
238: .getGenericDelegator(delegatorNameToUse);
239: if (delegator == null) {
240: throw new ContainerException("Invalid delegator name!");
241: }
242:
243: String helperName = delegator
244: .getGroupHelperName(groupNameToUse);
245: if (helperName == null) {
246: throw new ContainerException(
247: "Unable to locate the datasource helper for the group ["
248: + groupNameToUse + "]");
249: }
250:
251: // get the reader name URLs first
252: List urlList = null;
253: if (readerNames != null) {
254: urlList = EntityDataLoader.getUrlList(helperName,
255: component, readerNames);
256: } else if (!"none".equalsIgnoreCase(this .readers)) {
257: urlList = EntityDataLoader
258: .getUrlList(helperName, component);
259: }
260:
261: // need a list if it is empty
262: if (urlList == null) {
263: urlList = new ArrayList();
264: }
265:
266: // add in the defined extra file
267: if (this .file != null) {
268: URL fileUrl = UtilURL.fromResource(this .file);
269: if (fileUrl != null) {
270: urlList.add(fileUrl);
271: }
272: }
273:
274: // next check for a directory of files
275: if (this .directory != null) {
276: File dir = new File(this .directory);
277: if (dir.exists() && dir.isDirectory() && dir.canRead()) {
278: File[] fileArray = dir.listFiles();
279: if (fileArray != null && fileArray.length > 0) {
280: for (int i = 0; i < fileArray.length; i++) {
281: if (fileArray[i].getName().toLowerCase()
282: .endsWith(".xml")) {
283: try {
284: urlList.add(fileArray[i].toURI()
285: .toURL());
286: } catch (MalformedURLException e) {
287: Debug
288: .logError(
289: e,
290: "Unable to load file ("
291: + fileArray[i]
292: .getName()
293: + "); not a valid URL.",
294: module);
295: }
296: }
297: }
298: }
299: }
300: }
301:
302: // process the list of files
303: NumberFormat changedFormat = NumberFormat.getIntegerInstance();
304: changedFormat.setMinimumIntegerDigits(5);
305: changedFormat.setGroupingUsed(false);
306:
307: List errorMessages = new LinkedList();
308: List infoMessages = new LinkedList();
309: int totalRowsChanged = 0;
310: if (urlList != null && urlList.size() > 0) {
311: Debug
312: .logImportant(
313: "=-=-=-=-=-=-= Doing a data load with the following files:",
314: module);
315: Iterator urlIter = urlList.iterator();
316: while (urlIter.hasNext()) {
317: URL dataUrl = (URL) urlIter.next();
318: Debug.logImportant(dataUrl.toExternalForm(), module);
319: }
320:
321: Debug.logImportant(
322: "=-=-=-=-=-=-= Starting the data load...", module);
323:
324: urlIter = urlList.iterator();
325: while (urlIter.hasNext()) {
326: URL dataUrl = (URL) urlIter.next();
327: try {
328: int rowsChanged = EntityDataLoader.loadData(
329: dataUrl, helperName, delegator,
330: errorMessages, txTimeout, useDummyFks,
331: maintainTxs, tryInserts);
332: totalRowsChanged += rowsChanged;
333: infoMessages.add(changedFormat.format(rowsChanged)
334: + " of "
335: + changedFormat.format(totalRowsChanged)
336: + " from " + dataUrl.toExternalForm());
337: } catch (GenericEntityException e) {
338: Debug.logError(e, "Error loading data file: "
339: + dataUrl.toExternalForm(), module);
340: }
341: }
342: } else {
343: Debug.logImportant(
344: "=-=-=-=-=-=-= No data load files found.", module);
345: }
346:
347: if (infoMessages.size() > 0) {
348: Debug
349: .logImportant(
350: "=-=-=-=-=-=-= Here is a summary of the data load:",
351: module);
352: Iterator infoIter = infoMessages.iterator();
353: while (infoIter.hasNext()) {
354: Debug.logImportant((String) infoIter.next(), module);
355: }
356: }
357:
358: if (errorMessages.size() > 0) {
359: Debug.logImportant(
360: "The following errors occured in the data load:",
361: module);
362: Iterator errIter = errorMessages.iterator();
363: while (errIter.hasNext()) {
364: Debug.logImportant((String) errIter.next(), module);
365: }
366: }
367:
368: Debug.logImportant("=-=-=-=-=-=-= Finished the data load with "
369: + totalRowsChanged + " rows changed.", module);
370:
371: return true;
372: }
373:
374: /**
375: * @see org.ofbiz.base.container.Container#stop()
376: */
377: public void stop() throws ContainerException {
378: }
379: }
|