1 25 package org.ofbiz.entityext.data; 26 27 import java.net.URL ; 28 import java.net.MalformedURLException ; 29 import java.text.NumberFormat ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.ArrayList ; 34 import java.io.File ; 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 56 public class EntityDataLoadContainer implements Container { 57 58 public static final String module = EntityDataLoadContainer.class.getName(); 59 60 protected String overrideDelegator = null; 61 protected String overrideGroup = null; 62 protected String configFile = null; 63 protected String readers = null; 64 protected String directory = null; 65 protected String 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 78 public void init(String [] args, String configFile) throws ContainerException { 79 this.configFile = configFile; 80 ServiceDispatcher.enableJM(false); 82 ServiceDispatcher.enableJMS(false); 83 ServiceDispatcher.enableSvcs(false); 84 85 98 if (args != null) { 99 for (int i = 0; i < args.length; i++) { 100 String argument = args[i]; 101 if (argument.startsWith("-")) { 103 int subIdx = 1; 104 if (argument.startsWith("--")) { 105 subIdx = 2; 106 } 107 argument = argument.substring(subIdx); 108 } 109 110 if (argument.indexOf("=") != -1) { 112 String argumentName = argument.substring(0, argument.indexOf("=")); 113 String 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 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 if (this.readers == null && (this.file != null || this.directory != null)) { 142 this.readers = "none"; 143 } 144 } 145 } 146 } 147 148 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 delegatorName = null; 157 String 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 List readerNames = null; 173 if (this.readers != null && !"none".equalsIgnoreCase(this.readers)) { 174 if (this.readers.indexOf(",") == -1) { 175 readerNames = new LinkedList (); 176 readerNames.add(this.readers); 177 } else { 178 readerNames = StringUtil.split(this.readers, ","); 179 } 180 } 181 182 String delegatorNameToUse = overrideDelegator != null ? overrideDelegator : delegatorName; 183 String 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 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 List 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 if (urlList == null) { 204 urlList = new ArrayList (); 205 } 206 207 if (this.file != null) { 209 URL fileUrl = UtilURL.fromResource(this.file); 210 if (fileUrl != null) { 211 urlList.add(fileUrl); 212 } 213 } 214 215 if (this.directory != null) { 217 File dir = new File (this.directory); 218 if (dir.exists() && dir.isDirectory() && dir.canRead()) { 219 File [] 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 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 NumberFormat changedFormat = NumberFormat.getIntegerInstance(); 236 changedFormat.setMinimumIntegerDigits(5); 237 changedFormat.setGroupingUsed(false); 238 239 List errorMessages = new LinkedList (); 240 List infoMessages = new LinkedList (); 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 urlIter = urlList.iterator(); 245 while (urlIter.hasNext()) { 246 URL dataUrl = (URL ) 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 dataUrl = (URL ) 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 infoIter = infoMessages.iterator(); 270 while (infoIter.hasNext()){ 271 Debug.logImportant((String ) 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 errIter = errorMessages.iterator(); 278 while (errIter.hasNext()){ 279 Debug.logImportant((String ) 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 291 public void stop() throws ContainerException { 292 } 293 } 294 | Popular Tags |