Fixes bug 29030: Match Statement does not fail when a line throws an exception
authorUnai Martirena <unai.martirena@openbravo.com>
Mon, 23 Feb 2015 17:47:46 +0100
changeset 26067 a169e46939db
parent 26066 aba0f61a4140
child 26068 9b796d39efb6
Fixes bug 29030: Match Statement does not fail when a line throws an exception

In Matching Algorithm in Match Statement, if a line to be matched fails for any reason (period closed for example) a rollback occurs. This rollback was not being properly managed in Matching Algorithm after the refactor of Match Statement window, so the process was failing instead of skipping that line and going forward. Now it skips and the process finished successfully.
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/actionHandler/MatchStatementOnLoadActionHandler.java
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/actionHandler/MatchStatementOnLoadActionHandler.java	Tue Feb 24 12:38:51 2015 +0100
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/actionHandler/MatchStatementOnLoadActionHandler.java	Mon Feb 23 17:47:46 2015 +0100
@@ -124,11 +124,19 @@
     final ScrollableResults bankLinesSR = APRM_MatchingUtility
         .getPendingToBeMatchedBankStatementLines(strFinancialAccountId, strReconciliationId);
     final List<FIN_FinaccTransaction> excluded = new ArrayList<FIN_FinaccTransaction>();
-    int matchedLines = 0;
+    int matchedLines = 0, i = 0;
     try {
+      List<String> bankLines = new ArrayList<String>();
       while (bankLinesSR.next()) {
         final FIN_BankStatementLine bankStatementLine = (FIN_BankStatementLine) bankLinesSR.get(0);
-
+        bankLines.add(bankStatementLine.getId());
+        i++;
+      }
+      bankLinesSR.close();
+      i = 0;
+      for (i = 0; i < bankLines.size(); i++) {
+        final FIN_BankStatementLine bankStatementLine = OBDal.getInstance().get(
+            FIN_BankStatementLine.class, bankLines.get(i));
         FIN_MatchedTransaction matched;
         // try to match if exception is thrown continue
         try {
@@ -143,16 +151,20 @@
                 reconciliation, matched.getMatchLevel(), false)) {
           excluded.add(transaction);
           matchedLines++;
-          // Required to persist current matching so that it is not rollbacked afterwards because of
+          // Required to persist current matching so that it is not rollbacked afterwards because
+          // of
           // a
           // future error
           OBDal.getInstance().getConnection().commit();
         }
+
+        if ((i % 100) == 0) {
+          OBDal.getInstance().flush();
+          OBDal.getInstance().getSession().clear();
+        }
       }
     } catch (Exception e) {
       OBDal.getInstance().rollbackAndClose();
-    } finally {
-      bankLinesSR.close();
     }
 
     return matchedLines;