KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entityext > data > EntityDataLoadContainer


1 /*
2  * $Id: EntityDataLoadContainer.java 6614 2006-01-30 20:19:11Z jaz $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.entityext.data;
26
27 import java.net.URL JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.text.NumberFormat JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.io.File JavaDoc;
35
36 import org.ofbiz.base.container.Container;
37 import org.ofbiz.base.container.ContainerConfig;
38 import org.ofbiz.base.container.ContainerException;
39 import org.ofbiz.base.util.Debug;
40 import org.ofbiz.base.util.StringUtil;
41 import org.ofbiz.base.util.UtilURL;
42 import org.ofbiz.entity.GenericDelegator;
43 import org.ofbiz.entity.GenericEntityException;
44 import org.ofbiz.entity.util.EntityDataLoader;
45 import org.ofbiz.service.ServiceDispatcher;
46
47
48 /**
49  * Some utility routines for loading seed data.
50  *
51  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
52  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
53  * @version $Rev: 6614 $
54  * @since 3.1
55  */

56 public class EntityDataLoadContainer implements Container {
57
58     public static final String JavaDoc module = EntityDataLoadContainer.class.getName();
59
60     protected String JavaDoc overrideDelegator = null;
61     protected String JavaDoc overrideGroup = null;
62     protected String JavaDoc configFile = null;
63     protected String JavaDoc readers = null;
64     protected String JavaDoc directory = null;
65     protected String JavaDoc file = null;
66     protected boolean useDummyFks = false;
67     protected boolean maintainTxs = false;
68     protected boolean tryInserts = false;
69     protected int txTimeout = -1;
70
71     public EntityDataLoadContainer() {
72         super();
73     }
74
75     /**
76      * @see org.ofbiz.base.container.Container#init(java.lang.String[], java.lang.String)
77      */

78     public void init(String JavaDoc[] args, String JavaDoc configFile) throws ContainerException {
79         this.configFile = configFile;
80         // disable job scheduler, JMS listener and startup services
81
ServiceDispatcher.enableJM(false);
82         ServiceDispatcher.enableJMS(false);
83         ServiceDispatcher.enableSvcs(false);
84
85         /*
86            install arguments:
87            readers (none, all, seed, demo, ext, etc - configured in entityengine.xml and associated via ofbiz-component.xml)
88            timeout (transaction timeout default 7200)
89            delegator (overrides the delegator name configured for the container)
90            group (overrides the entity group name configured for the container)
91            dir (imports all XML files in a directory)
92            file (import a specific XML file)
93
94            Example:
95            $ java -jar ofbiz.jar -install -readers=seed,demo,ext -timeout=7200 -delegator=default -group=org.ofbiz
96            $ java -jar ofbiz.jar -install -file=/tmp/dataload.xml
97         */

98         if (args != null) {
99             for (int i = 0; i < args.length; i++) {
100                 String JavaDoc argument = args[i];
101                 // arguments can prefix w/ a '-'. Just strip them off
102
if (argument.startsWith("-")) {
103                     int subIdx = 1;
104                     if (argument.startsWith("--")) {
105                         subIdx = 2;
106                     }
107                     argument = argument.substring(subIdx);
108                 }
109     
110                 // parse the arguments
111
if (argument.indexOf("=") != -1) {
112                     String JavaDoc argumentName = argument.substring(0, argument.indexOf("="));
113                     String JavaDoc argumentVal = argument.substring(argument.indexOf("=") + 1);
114                     Debug.log("Install Argument - " + argumentName + " = " + argumentVal, module);
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 JavaDoc e) {
121                             this.txTimeout = -1;
122                         }
123                     } else if ("delegator".equalsIgnoreCase(argumentName)) {
124                         this.overrideDelegator = argumentVal;
125                     } else if ("group".equalsIgnoreCase(argumentName)) {
126                         this.overrideGroup = argumentVal;
127                     } else if ("file".equalsIgnoreCase(argumentName)) {
128                         this.file = argumentVal;
129                     } else if ("dir".equalsIgnoreCase(argumentName)) {
130                         this.directory = argumentVal;
131                     } else if ("createfks".equalsIgnoreCase(argumentName)) {
132                         this.useDummyFks = "true".equalsIgnoreCase(argumentVal);
133                     } else if ("maintainTxs".equalsIgnoreCase(argumentName)) {
134                         this.maintainTxs = "true".equalsIgnoreCase(argumentVal);
135                     } else if ("inserts".equalsIgnoreCase(argumentName)) {
136                         this.tryInserts = "true".equalsIgnoreCase(argumentVal);
137                     }
138                 }
139     
140                 // special case
141
if (this.readers == null && (this.file != null || this.directory != null)) {
142                     this.readers = "none";
143                 }
144             }
145         }
146     }
147
148     /**
149      * @see org.ofbiz.base.container.Container#start()
150      */

151     public boolean start() throws ContainerException {
152         ContainerConfig.Container cfg = ContainerConfig.getContainer("dataload-container", configFile);
153         ContainerConfig.Container.Property delegatorNameProp = cfg.getProperty("delegator-name");
154         ContainerConfig.Container.Property entityGroupNameProp = cfg.getProperty("entity-group-name");
155
156         String JavaDoc delegatorName = null;
157         String JavaDoc entityGroupName = null;
158
159         if (delegatorNameProp == null || delegatorNameProp.value == null || delegatorNameProp.value.length() == 0) {
160             throw new ContainerException("Invalid delegator-name defined in container configuration");
161         } else {
162             delegatorName = delegatorNameProp.value;
163         }
164
165         if (entityGroupNameProp == null || entityGroupNameProp.value == null || entityGroupNameProp.value.length() == 0) {
166             throw new ContainerException("Invalid entity-group-name defined in container configuration");
167         } else {
168             entityGroupName = entityGroupNameProp.value;
169         }
170
171         // parse the pass in list of readers to use
172
List JavaDoc readerNames = null;
173         if (this.readers != null && !"none".equalsIgnoreCase(this.readers)) {
174             if (this.readers.indexOf(",") == -1) {
175                 readerNames = new LinkedList JavaDoc();
176                 readerNames.add(this.readers);
177             } else {
178                 readerNames = StringUtil.split(this.readers, ",");
179             }
180         }
181
182         String JavaDoc delegatorNameToUse = overrideDelegator != null ? overrideDelegator : delegatorName;
183         String JavaDoc groupNameToUse = overrideGroup != null ? overrideGroup : entityGroupName;
184         GenericDelegator delegator = GenericDelegator.getGenericDelegator(delegatorNameToUse);
185         if (delegator == null) {
186             throw new ContainerException("Invalid delegator name!");
187         }
188
189         String JavaDoc helperName = delegator.getGroupHelperName(groupNameToUse);
190         if (helperName == null) {
191             throw new ContainerException("Unable to locate the datasource helper for the group [" + groupNameToUse + "]");
192         }
193
194         // get the reader name URLs first
195
List JavaDoc urlList = null;
196         if (readerNames != null) {
197             urlList = EntityDataLoader.getUrlList(helperName, readerNames);
198         } else if (!"none".equalsIgnoreCase(this.readers)) {
199             urlList = EntityDataLoader.getUrlList(helperName);
200         }
201
202         // need a list if it is empty
203
if (urlList == null) {
204             urlList = new ArrayList JavaDoc();
205         }
206
207         // add in the defined extra file
208
if (this.file != null) {
209             URL JavaDoc fileUrl = UtilURL.fromResource(this.file);
210             if (fileUrl != null) {
211                 urlList.add(fileUrl);
212             }
213         }
214
215         // next check for a directory of files
216
if (this.directory != null) {
217             File JavaDoc dir = new File JavaDoc(this.directory);
218             if (dir.exists() && dir.isDirectory() && dir.canRead()) {
219                 File JavaDoc[] fileArray = dir.listFiles();
220                 if (fileArray != null && fileArray.length > 0) {
221                     for (int i = 0; i < fileArray.length; i++) {
222                         if (fileArray[i].getName().toLowerCase().endsWith(".xml")) {
223                             try {
224                                 urlList.add(fileArray[i].toURL());
225                             } catch (MalformedURLException JavaDoc e) {
226                                 Debug.logError(e, "Unable to load file (" + fileArray[i].getName() + "); not a valid URL.", module);
227                             }
228                         }
229                     }
230                 }
231             }
232         }
233
234         // process the list of files
235
NumberFormat JavaDoc changedFormat = NumberFormat.getIntegerInstance();
236         changedFormat.setMinimumIntegerDigits(5);
237         changedFormat.setGroupingUsed(false);
238         
239         List JavaDoc errorMessages = new LinkedList JavaDoc();
240         List JavaDoc infoMessages = new LinkedList JavaDoc();
241         int totalRowsChanged = 0;
242         if (urlList != null && urlList.size() > 0) {
243             Debug.logImportant("=-=-=-=-=-=-= Doing a data load with the following files:", module);
244             Iterator JavaDoc urlIter = urlList.iterator();
245             while (urlIter.hasNext()) {
246                 URL JavaDoc dataUrl = (URL JavaDoc) urlIter.next();
247                 Debug.logImportant(dataUrl.toExternalForm(), module);
248             }
249
250             Debug.logImportant("=-=-=-=-=-=-= Starting the data load...", module);
251
252             urlIter = urlList.iterator();
253             while (urlIter.hasNext()) {
254                 URL JavaDoc dataUrl = (URL JavaDoc) urlIter.next();
255                 try {
256                     int rowsChanged = EntityDataLoader.loadData(dataUrl, helperName, delegator, errorMessages, txTimeout, useDummyFks, maintainTxs, tryInserts);
257                     totalRowsChanged += rowsChanged;
258                     infoMessages.add(changedFormat.format(rowsChanged) + " of " + changedFormat.format(totalRowsChanged) + " from " + dataUrl.toExternalForm());
259                 } catch (GenericEntityException e) {
260                     Debug.logError(e, "Error loading data file: " + dataUrl.toExternalForm(), module);
261                 }
262             }
263         } else {
264             Debug.logImportant("=-=-=-=-=-=-= No data load files found.", module);
265         }
266
267         if (infoMessages.size() > 0) {
268             Debug.logImportant("=-=-=-=-=-=-= Here is a summary of the data load:", module);
269             Iterator JavaDoc infoIter = infoMessages.iterator();
270             while (infoIter.hasNext()){
271               Debug.logImportant((String JavaDoc) infoIter.next(), module);
272             }
273         }
274         
275         if (errorMessages.size() > 0) {
276             Debug.logImportant("The following errors occured in the data load:", module);
277             Iterator JavaDoc errIter = errorMessages.iterator();
278             while (errIter.hasNext()){
279               Debug.logImportant((String JavaDoc) errIter.next(), module);
280             }
281         }
282
283         Debug.logImportant("=-=-=-=-=-=-= Finished the data load with " + totalRowsChanged + " rows changed.", module);
284         
285         return true;
286     }
287
288     /**
289      * @see org.ofbiz.base.container.Container#stop()
290      */

291     public void stop() throws ContainerException {
292     }
293 }
294
Popular Tags