fixed issue 39229: source path is required to import reference data
authorAsier Lostalé <asier.lostale@openbravo.com>
Fri, 07 Sep 2018 09:51:56 +0200
changeset 34761 692305cb1996
parent 34760 686664db43e1
child 34762 038384250ba9
fixed issue 39229: source path is required to import reference data

Reference data is now deployed to WebContent which can be accessed from Tomcat
as a resource.
src/build.xml
src/org/openbravo/erpCommon/businessUtility/InitialSetupUtility.java
--- a/src/build.xml	Thu Sep 06 10:14:53 2018 +0200
+++ b/src/build.xml	Fri Sep 07 09:51:56 2018 +0200
@@ -413,6 +413,20 @@
       </fileset>
       <mapper type="regexp" from="(.*\${file.separator}lib\${file.separator}runtime)(.*)" to="\2" />
     </copy>
+
+    <copy todir="${base.context}/referencedata/standard/org.openbravo">
+      <fileset dir="${basedir}/../referencedata/standard">
+        <include name="*.xml" />
+      </fileset>
+    </copy>
+
+    <copy todir="${base.context}/referencedata/standard">
+      <fileset dir="${base.modules}">
+        <include name="*/referencedata/standard/*.xml" />
+      </fileset>
+      <mapper type="regexp" from="(.*\${file.separator})(referencedata\${file.separator}standard\${file.separator})(.*)" to="\1\3" />
+    </copy>
+
     <antcall target="build.web.folder" inheritAll="true" inheritrefs="true" />
   </target>
 
--- a/src/org/openbravo/erpCommon/businessUtility/InitialSetupUtility.java	Thu Sep 06 10:14:53 2018 +0200
+++ b/src/org/openbravo/erpCommon/businessUtility/InitialSetupUtility.java	Fri Sep 07 09:51:56 2018 +0200
@@ -18,7 +18,7 @@
  */
 package org.openbravo.erpCommon.businessUtility;
 
-import java.io.File;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -26,13 +26,13 @@
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
 import org.hibernate.criterion.Order;
 import org.hibernate.criterion.Restrictions;
-import org.openbravo.base.exception.OBException;
 import org.openbravo.base.provider.OBProvider;
-import org.openbravo.base.session.OBPropertiesProvider;
+import org.openbravo.client.kernel.RequestContext;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
@@ -1679,30 +1679,28 @@
     if (orgProvided == null) {
       if ((organization = getZeroOrg()) == null)
         return null;
-    } else
+    } else {
       organization = orgProvided;
+    }
     ImportResult myResult = null;
-    String strSourcePath = OBPropertiesProvider.getInstance().getOpenbravoProperties()
-        .getProperty("source.path");
-    if (strSourcePath == null || strSourcePath.equals("")) {
-      throw new OBException("@NoSourcePath@");
-    }
-    String strPath = "";
-    File datasetFile;
+
     OBContext.setAdminMode();
     try {
-      if (dataset.getModule().getJavaPackage().equals("org.openbravo")) {
-        strPath = strSourcePath + "/referencedata/standard";
-      } else {
-        strPath = strSourcePath + "/modules/" + dataset.getModule().getJavaPackage()
-            + "/referencedata/standard";
+      String xmlPath = "/referencedata/standard/" + dataset.getModule().getJavaPackage() + "/"
+          + Utility.wikifiedName(dataset.getName()) + ".xml";
+
+      String strXml = null;
+      try (InputStream r = RequestContext.getServletContext().getResourceAsStream(xmlPath)) {
+        if (r == null) {
+          log4j.error("Could not find reference data resource " + xmlPath + " at "
+              + RequestContext.getServletContext().getRealPath(xmlPath));
+          return myResult;
+        }
+        strXml = IOUtils.toString(r);
       }
-      datasetFile = new File(strPath + "/" + Utility.wikifiedName(dataset.getName()) + ".xml");
-      if (!datasetFile.exists()) {
-        return myResult;
-      }
+
       DataImportService myData = DataImportService.getInstance();
-      String strXml = Utility.fileToString(datasetFile.getPath());
+
       boolean filterOrganizations = true;
       myResult = myData.importDataFromXML(client, organization, strXml, dataset.getModule(),
           filterOrganizations);
@@ -1712,9 +1710,9 @@
         return myResult;
       }
       if (organization.getId().equals(getZeroOrg().getId())
-          && getClientModuleList(client, dataset.getModule()).size() == 0) {
+          && getClientModuleList(client, dataset.getModule()).isEmpty()) {
         insertClientModule(client, dataset.getModule());
-      } else if (getOrgModuleList(client, organization, dataset.getModule()).size() == 0) {
+      } else if (getOrgModuleList(client, organization, dataset.getModule()).isEmpty()) {
         insertOrgModule(client, organization, dataset.getModule());
       }
     } finally {