fixed issue 39229: source path is required to import Chart Of Accounts
authorAsier Lostalé <asier.lostale@openbravo.com>
Fri, 07 Sep 2018 11:37:29 +0200
changeset 34762 038384250ba9
parent 34761 692305cb1996
child 34763 701d003b887b
fixed issue 39229: source path is required to import Chart Of Accounts

Now it is deployed as resource accessible from Tomcat.
src-db/database/sourcedata/AD_MESSAGE.xml
src/build.xml
src/org/openbravo/erpCommon/businessUtility/COAUtility.java
src/org/openbravo/erpCommon/businessUtility/InitialClientSetup.java
src/org/openbravo/erpCommon/businessUtility/InitialOrgSetup.java
--- a/src-db/database/sourcedata/AD_MESSAGE.xml	Fri Sep 07 09:51:56 2018 +0200
+++ b/src-db/database/sourcedata/AD_MESSAGE.xml	Fri Sep 07 11:37:29 2018 +0200
@@ -21082,6 +21082,18 @@
 <!--6775556A78264AB192CAD83F25E37E7B-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
 <!--6775556A78264AB192CAD83F25E37E7B--></AD_MESSAGE>
 
+<!--67CC7B2901FC47D3BBE259F2E125764C--><AD_MESSAGE>
+<!--67CC7B2901FC47D3BBE259F2E125764C-->  <AD_MESSAGE_ID><![CDATA[67CC7B2901FC47D3BBE259F2E125764C]]></AD_MESSAGE_ID>
+<!--67CC7B2901FC47D3BBE259F2E125764C-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--67CC7B2901FC47D3BBE259F2E125764C-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--67CC7B2901FC47D3BBE259F2E125764C-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--67CC7B2901FC47D3BBE259F2E125764C-->  <VALUE><![CDATA[FileDoesNotExist]]></VALUE>
+<!--67CC7B2901FC47D3BBE259F2E125764C-->  <MSGTEXT><![CDATA[Could not find file]]></MSGTEXT>
+<!--67CC7B2901FC47D3BBE259F2E125764C-->  <MSGTYPE><![CDATA[I]]></MSGTYPE>
+<!--67CC7B2901FC47D3BBE259F2E125764C-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--67CC7B2901FC47D3BBE259F2E125764C-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
+<!--67CC7B2901FC47D3BBE259F2E125764C--></AD_MESSAGE>
+
 <!--68082E2D67E94A7E8FB1416A7FCEBC47--><AD_MESSAGE>
 <!--68082E2D67E94A7E8FB1416A7FCEBC47-->  <AD_MESSAGE_ID><![CDATA[68082E2D67E94A7E8FB1416A7FCEBC47]]></AD_MESSAGE_ID>
 <!--68082E2D67E94A7E8FB1416A7FCEBC47-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src/build.xml	Fri Sep 07 09:51:56 2018 +0200
+++ b/src/build.xml	Fri Sep 07 11:37:29 2018 +0200
@@ -420,11 +420,12 @@
       </fileset>
     </copy>
 
-    <copy todir="${base.context}/referencedata/standard">
+    <copy todir="${base.context}/referencedata">
       <fileset dir="${base.modules}">
         <include name="*/referencedata/standard/*.xml" />
+        <include name="*/referencedata/accounts/COA.csv" />
       </fileset>
-      <mapper type="regexp" from="(.*\${file.separator})(referencedata\${file.separator}standard\${file.separator})(.*)" to="\1\3" />
+      <mapper type="regexp" from="(.*\${file.separator})referencedata\${file.separator}(standard|accounts)\${file.separator}(.*)" to="\2${file.separator}\1\3" />
     </copy>
 
     <antcall target="build.web.folder" inheritAll="true" inheritrefs="true" />
--- a/src/org/openbravo/erpCommon/businessUtility/COAUtility.java	Fri Sep 07 09:51:56 2018 +0200
+++ b/src/org/openbravo/erpCommon/businessUtility/COAUtility.java	Fri Sep 07 11:37:29 2018 +0200
@@ -18,6 +18,7 @@
  */
 package org.openbravo.erpCommon.businessUtility;
 
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.math.BigDecimal;
@@ -31,11 +32,13 @@
 import org.apache.log4j.Logger;
 import org.openbravo.base.VariablesBase;
 import org.openbravo.base.secureApp.VariablesSecureApp;
+import org.openbravo.client.kernel.RequestContext;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.data.FieldProvider;
 import org.openbravo.erpCommon.utility.OBError;
 import org.openbravo.erpCommon.utility.Utility;
+import org.openbravo.model.ad.module.Module;
 import org.openbravo.model.ad.system.Client;
 import org.openbravo.model.ad.utility.Tree;
 import org.openbravo.model.ad.utility.TreeNode;
@@ -176,6 +179,29 @@
     return obeResult;
   }
 
+  /**
+   * Returns the COA.csv resource as an {@link InputStream}, obtained from deployed reources
+   * 
+   * @param coaModule
+   *          Module containing chart of accounts
+   * @return {@link InputStream} to chart of accounts file
+   * @throws FileNotFoundException
+   *           In case expected COA.csv file cannot be found
+   */
+  public static InputStream getCOAResource(Module coaModule) throws FileNotFoundException {
+    OBContext.setAdminMode();
+    try {
+      String coaPath = "/referencedata/accounts/" + coaModule.getJavaPackage() + "/COA.csv";
+      InputStream inputStream = RequestContext.getServletContext().getResourceAsStream(coaPath);
+      if (inputStream == null) {
+        throw new FileNotFoundException(RequestContext.getServletContext().getRealPath(coaPath));
+      }
+      return inputStream;
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+  }
+
   private String setAccountType(COAData data) {
     if (data == null)
       return null;
@@ -860,8 +886,8 @@
             log4j.debug("updateOperands() - Procesing operand " + strOperand[j][0]
                 + ", of the account " + data[i].accountValue);
             ElementValueOperand operandElement = InitialSetupUtility.insertOperand(operand,
-                elementValue, Long.valueOf((strOperand[j][1].equals("+") ? "1" : "-1")), Long.valueOf(
-                    strSeqNo));
+                elementValue, Long.valueOf((strOperand[j][1].equals("+") ? "1" : "-1")),
+                Long.valueOf(strSeqNo));
             strSeqNo = nextSeqNo(strSeqNo);
             if (operandElement == null)
               logEvent("@OperandNotInserted@. @Account_ID@ = " + data[i].accountValue
--- a/src/org/openbravo/erpCommon/businessUtility/InitialClientSetup.java	Fri Sep 07 09:51:56 2018 +0200
+++ b/src/org/openbravo/erpCommon/businessUtility/InitialClientSetup.java	Fri Sep 07 11:37:29 2018 +0200
@@ -11,14 +11,14 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2010-2015 Openbravo SLU
+ * All portions are Copyright (C) 2010-2018 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
  */
 package org.openbravo.erpCommon.businessUtility;
 
-import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -580,17 +580,20 @@
         logEvent(NEW_LINE + "@ProcessingAccountingModule@ " + modCoA.getName());
         log4j.debug("createReferenceData() - Processing Chart of Accounts module "
             + modCoA.getName());
-        String strPath = vars.getSessionValue("#SOURCEPATH") + "/modules/"
-            + modCoA.getJavaPackage() + "/referencedata/accounts/COA.csv";
-        COAUtility coaUtility = new COAUtility(client, treeAccount);
-        FileInputStream inputStream = new FileInputStream(strPath);
-        obeResult = coaUtility.createAccounting(vars, inputStream, bBPartner, bProduct, bProject,
-            bCampaign, bSalesRegion, strAccountText, "US", "A", strCalendarText, currency);
-        strLog.append(coaUtility.getLog());
-      } else
+        try (InputStream coaFile = COAUtility.getCOAResource(modCoA)) {
+          COAUtility coaUtility = new COAUtility(client, treeAccount);
+          obeResult = coaUtility.createAccounting(vars, coaFile, bBPartner, bProduct, bProject,
+              bCampaign, bSalesRegion, strAccountText, "US", "A", strCalendarText, currency);
+          strLog.append(coaUtility.getLog());
+        } catch (FileNotFoundException e) {
+          logEvent("@FileDoesNotExist@ " + e.getMessage());
+          throw new OBException("Could not find resource", e);
+        }
+      } else {
         return logErrorAndRollback(
             "@CreateReferenceDataFailed@. @CreateAccountingButNoCoAProvided@",
             "createReferenceData() - Create accounting option was active, but no file was provided, and no accoutning module was chosen");
+      }
     } catch (Exception e) {
       return logErrorAndRollback("@CreateReferenceDataFailed@",
           "createReferenceData() - Exception while processing accounting modules", e);
--- a/src/org/openbravo/erpCommon/businessUtility/InitialOrgSetup.java	Fri Sep 07 09:51:56 2018 +0200
+++ b/src/org/openbravo/erpCommon/businessUtility/InitialOrgSetup.java	Fri Sep 07 11:37:29 2018 +0200
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2010-2016 Openbravo SLU
+ * All portions are Copyright (C) 2010-2018 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -19,7 +19,7 @@
 
 package org.openbravo.erpCommon.businessUtility;
 
-import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -27,6 +27,7 @@
 
 import org.apache.commons.fileupload.FileItem;
 import org.apache.log4j.Logger;
+import org.openbravo.base.exception.OBException;
 import org.openbravo.base.secureApp.VariablesSecureApp;
 import org.openbravo.base.structure.BaseOBObject;
 import org.openbravo.dal.core.OBContext;
@@ -438,25 +439,23 @@
         logEvent(NEW_LINE + "@ProcessingAccountingModule@ " + modCoA.getName());
         log4j.debug("createReferenceData() - Processing Chart of Accounts module "
             + modCoA.getName());
-        String strPath = "";
-        try {
-          OBContext.setAdminMode();
-          strPath = strSourcePath + "/modules/" + modCoA.getJavaPackage()
-              + "/referencedata/accounts/COA.csv";
-        } finally {
-          OBContext.restorePreviousMode();
+
+        try (InputStream coaFile = COAUtility.getCOAResource(modCoA)) {
+          COAUtility coaUtility = new COAUtility(client, org, accountTree);
+          obeResult = coaUtility.createAccounting(null, coaFile, bBPartner, bProduct, bProject,
+              bCampaign, bSalesRegion, strAccountText, "US", "A", strCalendarText,
+              InitialSetupUtility.getCurrency(strCurrency));
+          strLog.append(coaUtility.getLog());
+        } catch (FileNotFoundException e) {
+          logEvent("@FileDoesNotExist@ " + e.getMessage());
+          throw new OBException("Could not find resource", e);
         }
-        COAUtility coaUtility = new COAUtility(client, org, accountTree);
-        FileInputStream inputStream = new FileInputStream(strPath);
-        obeResult = coaUtility.createAccounting(null, inputStream, bBPartner, bProduct, bProject,
-            bCampaign, bSalesRegion, strAccountText, "US", "A", strCalendarText,
-            InitialSetupUtility.getCurrency(strCurrency));
-        strLog.append(coaUtility.getLog());
-      } else
+      } else {
         return logErrorAndRollback(
             "@CreateReferenceDataFailed@. @CreateAccountingButNoCoAProvided@",
             "createReferenceData() - Create accounting option was active, but no file was provided, and no accoutning module was chosen",
             null);
+      }
     } catch (Exception e) {
       return logErrorAndRollback("@CreateReferenceDataFailed@",
           "createReferenceData() - Exception while processing accounting modules", e);