[reconciliation refactor] automatic matching algorithm information message
authorVíctor Martínez Romanos <victor.martinez@openbravo.com>
Tue, 26 Aug 2014 10:16:23 +0200
changeset 24581 be551ee759e1
parent 24580 8a907e904481
child 24582 b65e6c363895
[reconciliation refactor] automatic matching algorithm information message
modules/org.openbravo.advpaymentmngt/src-db/database/sourcedata/AD_MESSAGE.xml
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/actionHandler/MatchStatementOnLoadActionHandler.java
--- a/modules/org.openbravo.advpaymentmngt/src-db/database/sourcedata/AD_MESSAGE.xml	Mon Aug 25 18:36:57 2014 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src-db/database/sourcedata/AD_MESSAGE.xml	Tue Aug 26 10:16:23 2014 +0200
@@ -132,6 +132,18 @@
 <!--167679F2C257479E9129C3C51B8F426D-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
 <!--167679F2C257479E9129C3C51B8F426D--></AD_MESSAGE>
 
+<!--19141BEA79154C24BC0DEB49B61C5C4B--><AD_MESSAGE>
+<!--19141BEA79154C24BC0DEB49B61C5C4B-->  <AD_MESSAGE_ID><![CDATA[19141BEA79154C24BC0DEB49B61C5C4B]]></AD_MESSAGE_ID>
+<!--19141BEA79154C24BC0DEB49B61C5C4B-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--19141BEA79154C24BC0DEB49B61C5C4B-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--19141BEA79154C24BC0DEB49B61C5C4B-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--19141BEA79154C24BC0DEB49B61C5C4B-->  <VALUE><![CDATA[APRM_AutomaticMatchedLines]]></VALUE>
+<!--19141BEA79154C24BC0DEB49B61C5C4B-->  <MSGTEXT><![CDATA[The automatic matching algorithm has matched %d transaction(s)]]></MSGTEXT>
+<!--19141BEA79154C24BC0DEB49B61C5C4B-->  <MSGTYPE><![CDATA[S]]></MSGTYPE>
+<!--19141BEA79154C24BC0DEB49B61C5C4B-->  <AD_MODULE_ID><![CDATA[A918E3331C404B889D69AA9BFAFB23AC]]></AD_MODULE_ID>
+<!--19141BEA79154C24BC0DEB49B61C5C4B-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
+<!--19141BEA79154C24BC0DEB49B61C5C4B--></AD_MESSAGE>
+
 <!--19D77522B7A4470FBA505D39A6AA051C--><AD_MESSAGE>
 <!--19D77522B7A4470FBA505D39A6AA051C-->  <AD_MESSAGE_ID><![CDATA[19D77522B7A4470FBA505D39A6AA051C]]></AD_MESSAGE_ID>
 <!--19D77522B7A4470FBA505D39A6AA051C-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
@@ -505,6 +517,18 @@
 <!--628371BCC5C54C88A4D5CFA24B6FEE1D-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
 <!--628371BCC5C54C88A4D5CFA24B6FEE1D--></AD_MESSAGE>
 
+<!--64B44368CEC441619C3462E67A07F9DB--><AD_MESSAGE>
+<!--64B44368CEC441619C3462E67A07F9DB-->  <AD_MESSAGE_ID><![CDATA[64B44368CEC441619C3462E67A07F9DB]]></AD_MESSAGE_ID>
+<!--64B44368CEC441619C3462E67A07F9DB-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--64B44368CEC441619C3462E67A07F9DB-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--64B44368CEC441619C3462E67A07F9DB-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--64B44368CEC441619C3462E67A07F9DB-->  <VALUE><![CDATA[APRM_NoAutomaticMatchedLines]]></VALUE>
+<!--64B44368CEC441619C3462E67A07F9DB-->  <MSGTEXT><![CDATA[The automatic matching algorithm couldn't match any transaction]]></MSGTEXT>
+<!--64B44368CEC441619C3462E67A07F9DB-->  <MSGTYPE><![CDATA[W]]></MSGTYPE>
+<!--64B44368CEC441619C3462E67A07F9DB-->  <AD_MODULE_ID><![CDATA[A918E3331C404B889D69AA9BFAFB23AC]]></AD_MODULE_ID>
+<!--64B44368CEC441619C3462E67A07F9DB-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
+<!--64B44368CEC441619C3462E67A07F9DB--></AD_MESSAGE>
+
 <!--65A7E4455E2F41F1A3D5C71221E01DE5--><AD_MESSAGE>
 <!--65A7E4455E2F41F1A3D5C71221E01DE5-->  <AD_MESSAGE_ID><![CDATA[65A7E4455E2F41F1A3D5C71221E01DE5]]></AD_MESSAGE_ID>
 <!--65A7E4455E2F41F1A3D5C71221E01DE5-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/actionHandler/MatchStatementOnLoadActionHandler.java	Mon Aug 25 18:36:57 2014 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/actionHandler/MatchStatementOnLoadActionHandler.java	Tue Aug 26 10:16:23 2014 +0200
@@ -25,6 +25,7 @@
 import java.util.Map;
 
 import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
 import org.hibernate.ScrollableResults;
 import org.hibernate.criterion.Order;
@@ -58,6 +59,8 @@
   @Override
   protected JSONObject execute(Map<String, Object> parameters, String content) {
     JSONObject jsonResponse = new JSONObject();
+    JSONArray actions = new JSONArray();
+
     String strReconciliationId = null;
     try {
       OBContext.setAdminMode(true);
@@ -87,43 +90,29 @@
       if (executeAutoMatchingAlgm) {
         /* Verify we have something left to match */
         if (MatchTransactionDao.getUnMatchedBankStatementLines(financialAccount).isEmpty()) {
-          OBError message = OBMessageUtils.translateError("@APRM_NoStatementsToMatch@");
-          JSONArray actions = new JSONArray();
-          JSONObject msg = new JSONObject();
-          // FIXME warning
-          msg.put("msgType", "error");
-          msg.put("msgTitle", message.getTitle());
-          msg.put("msgText", message.getMessage());
-          msg.put("force", true);
-          JSONObject msgTotalAction = new JSONObject();
-          msgTotalAction.put("showMsgInProcessView", msg);
-          actions.put(msgTotalAction);
-
+          actions = createMessage("@APRM_NoStatementsToMatch@", "warning");
           jsonResponse.put("responseActions", actions);
           return jsonResponse;
         }
+
         /* Run the automatic matching algorithm */
-        runAutoMatchingAlgorithm(strReconciliationId, strFinancialAccountId, financialAccount,
-            reconciliation);
+        int matchedLines = runAutoMatchingAlgorithm(strReconciliationId, strFinancialAccountId,
+            financialAccount, reconciliation);
+        if (matchedLines > 0) {
+          actions = createMessage("@APRM_AutomaticMatchedLines@", "success", matchedLines);
+        } else {
+          actions = createMessage("@APRM_NoAutomaticMatchedLines@", "warning");
+        }
+        jsonResponse.put("responseActions", actions);
       }
-
     } catch (Exception e) {
       OBDal.getInstance().rollbackAndClose();
       log.error("Exception handling the match statement", e);
 
       try {
-        JSONArray actions = new JSONArray();
         jsonResponse = new JSONObject();
         Throwable ex = DbUtility.getUnderlyingSQLException(e);
-        String message = OBMessageUtils.translateError(ex.getMessage()).getMessage();
-        JSONObject errorMessage = new JSONObject();
-        errorMessage.put("msgType", "error");
-        errorMessage.put("msgTitle", "Error");
-        errorMessage.put("msgText", message);
-        errorMessage.put("force", true);
-        JSONObject msgErrorlAction = new JSONObject();
-        msgErrorlAction.put("showMsgInProcessView", errorMessage);
-        actions.put(msgErrorlAction);
+        actions = createMessage(ex.getMessage(), "error");
         jsonResponse.put("responseActions", actions);
       } catch (Exception ignore) {
       }
@@ -131,10 +120,11 @@
       OBContext.restorePreviousMode();
       APRM_MatchingUtility.setNotProcessingReconciliation(strReconciliationId);
     }
+
     return jsonResponse;
   }
 
-  private void runAutoMatchingAlgorithm(String strReconciliationId,
+  private int runAutoMatchingAlgorithm(String strReconciliationId,
       final String strFinancialAccountId, final FIN_FinancialAccount financialAccount,
       FIN_Reconciliation reconciliation) throws InterruptedException, SQLException {
     APRM_MatchingUtility.setProcessingReconciliation(reconciliation);
@@ -144,6 +134,7 @@
     final ScrollableResults bankLinesSR = APRM_MatchingUtility
         .getPendingToBeMatchedBankStatementLines(strFinancialAccountId, strReconciliationId);
     final List<FIN_FinaccTransaction> excluded = new ArrayList<FIN_FinaccTransaction>();
+    int matchedLines = 0;
     try {
       while (bankLinesSR.next()) {
         final FIN_BankStatementLine bankStatementLine = (FIN_BankStatementLine) bankLinesSR.get(0);
@@ -161,11 +152,14 @@
             && APRM_MatchingUtility.matchBankStatementLine(bankStatementLine, transaction,
                 reconciliation, matched.getMatchLevel())) {
           excluded.add(transaction);
+          matchedLines++;
         }
       }
     } finally {
       bankLinesSR.close();
     }
+
+    return matchedLines;
   }
 
   private boolean updateReconciliation(final FIN_Reconciliation reconciliation,
@@ -217,4 +211,22 @@
     obc.setMaxResults(1);
     return ((FIN_ReconciliationLine_v) obc.uniqueResult()).getTransactionDate();
   }
+
+  private JSONArray createMessage(final String messageSearchKey, final String msgType,
+      Object... messageParams) throws JSONException {
+    final OBError message = OBMessageUtils.translateError(messageSearchKey);
+
+    final JSONObject msg = new JSONObject();
+    msg.put("msgType", msgType);
+    msg.put("msgTitle", message.getTitle());
+    msg.put("msgText", String.format(message.getMessage(), messageParams));
+    msg.put("force", true);
+
+    final JSONObject msgTotalAction = new JSONObject();
+    msgTotalAction.put("showMsgInProcessView", msg);
+
+    final JSONArray actions = new JSONArray();
+    actions.put(msgTotalAction);
+    return actions;
+  }
 }