details:   https://code.openbravo.com/erp/devel/pi/rev/785864c62605
changeset: 25698:785864c62605
user:      Ander Iraceburu <ander.iraceburu <at> openbravo.com>
date:      Mon Jan 12 12:34:31 2015 +0100
summary:   Fixes issue 28467: Code refactoring to FIN_TransactionProcess.java & 
FIN_PaymentProcess.java
In order to remove the commitAndClose every time the method processPayment 
fails a refactoring has been made. Taking advantage of this refactoring also 
the use of ProcessBundle, OBError, ConnectionProvider and VariablesSecureApps 
has been reduced to a minimum.

details:   https://code.openbravo.com/erp/devel/pi/rev/d409ba0d336b
changeset: 25699:d409ba0d336b
user:      Ander Iraceburu <ander.iraceburu <at> openbravo.com>
date:      Mon Jan 12 16:20:56 2015 +0100
summary:   Related to issue 28467: FIN_PaymentProcess.java Bugfix

diffstat:

 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentProcess.java
     |  387 +++------
 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_TransactionProcess.java
 |  323 ++++----
 2 files changed, 280 insertions(+), 430 deletions(-)

diffs (truncated from 1075 to 300 lines):

diff -r 11a4bc84965f -r d409ba0d336b 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentProcess.java
--- 
a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentProcess.java
     Mon Jan 12 18:07:06 2015 +0100
+++ 
b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentProcess.java
     Mon Jan 12 16:20:56 2015 +0100
@@ -22,7 +22,6 @@
 import java.math.RoundingMode;
 import java.util.ArrayList;
 import java.util.Date;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -35,7 +34,7 @@
 import org.openbravo.advpaymentmngt.utility.FIN_Utility;
 import org.openbravo.base.exception.OBException;
 import org.openbravo.base.provider.OBProvider;
-import org.openbravo.base.secureApp.VariablesSecureApp;
+import org.openbravo.base.weld.WeldUtils;
 import org.openbravo.client.kernel.RequestContext;
 import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.core.OBContext;
@@ -45,7 +44,6 @@
 import org.openbravo.erpCommon.utility.OBDateUtils;
 import org.openbravo.erpCommon.utility.OBError;
 import org.openbravo.erpCommon.utility.OBMessageUtils;
-import org.openbravo.erpCommon.utility.Utility;
 import org.openbravo.model.common.businesspartner.BusinessPartner;
 import org.openbravo.model.common.currency.ConversionRate;
 import org.openbravo.model.common.currency.ConversionRateDoc;
@@ -63,20 +61,20 @@
 import org.openbravo.model.financialmgmt.payment.FinAccPaymentMethod;
 import org.openbravo.model.financialmgmt.payment.PaymentExecutionProcess;
 import org.openbravo.scheduling.ProcessBundle;
-import org.openbravo.service.db.DalConnectionProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class FIN_PaymentProcess implements org.openbravo.scheduling.Process {
   private static AdvPaymentMngtDao dao;
 
   public BigDecimal ZERO = BigDecimal.ZERO;
+  static Logger log4j = LoggerFactory.getLogger(FIN_PaymentProcess.class);
 
   public void execute(ProcessBundle bundle) throws Exception {
-    dao = new AdvPaymentMngtDao();
-    final String language = bundle.getContext().getLanguage();
 
     OBError msg = new OBError();
     msg.setType("Success");
-    msg.setTitle(Utility.messageBD(bundle.getConnection(), "Success", 
language));
+    msg.setTitle(OBMessageUtils.messageBD("Success"));
 
     try {
       // retrieve custom params
@@ -84,25 +82,48 @@
       final String comingFrom = (String) bundle.getParams().get("comingFrom");
       // retrieve standard params
       final String recordID = (String) 
bundle.getParams().get("Fin_Payment_ID");
-      final FIN_Payment payment = dao.getObject(FIN_Payment.class, recordID);
-      final VariablesSecureApp vars = bundle.getContext().toVars();
+      final FIN_Payment payment = OBDal.getInstance().get(FIN_Payment.class, 
recordID);
+      final Boolean isPosOrder;
+      if (bundle.getParams().get("isPOSOrder") == null) {
+        isPosOrder = false;
+      } else {
+        isPosOrder = bundle.getParams().get("isPOSOrder").equals("Y");
+      }
+      final String paymentDate = (String) 
bundle.getParams().get("paymentdate");
+      processPayment(payment, strAction, isPosOrder, paymentDate, comingFrom);
+      bundle.setResult(msg);
+    } catch (Exception e) {
+      log4j.error(e.getMessage());
+      msg.setType("Error");
+      msg.setTitle(OBMessageUtils.messageBD("Error"));
+      msg.setMessage(FIN_Utility.getExceptionMessage(e));
+      bundle.setResult(msg);
+      OBDal.getInstance().getConnection().rollback();
+    }
+  }
 
-      final ConnectionProvider conProvider = bundle.getConnection();
+  // ProcessPayment without a return type
+  public static void doProcessPayment(FIN_Payment payment, String strAction, 
Boolean isPosOrder,
+      String paymentDate, String comingFrom) throws OBException {
+    FIN_PaymentProcess fpp = 
WeldUtils.getInstanceFromStaticBeanManager(FIN_PaymentProcess.class);
+    fpp.processPayment(payment, strAction, isPosOrder, paymentDate, 
comingFrom);
+  }
+
+  private void processPayment(FIN_Payment payment, String strAction, Boolean 
isPosOrder,
+      String paymentDate, String comingFrom) throws OBException {
+    dao = new AdvPaymentMngtDao();
+    String msg = "";
+    try {
       final boolean isReceipt = payment.isReceipt();
-
       if (strAction.equals("P") || strAction.equals("D")) {
         if (payment.getBusinessPartner() != null) {
           if 
(FIN_Utility.isBlockedBusinessPartner(payment.getBusinessPartner().getId(), 
isReceipt,
               4)) {
             // If the Business Partner is blocked for Payments, the Payment 
will not be completed.
-            msg.setType("Error");
-            msg.setTitle(Utility.messageBD(conProvider, "Error", language));
-            msg.setMessage(OBMessageUtils.messageBD("ThebusinessPartner") + " "
+            msg = OBMessageUtils.messageBD("ThebusinessPartner") + " "
                 + payment.getBusinessPartner().getIdentifier() + " "
-                + OBMessageUtils.messageBD("BusinessPartnerBlocked"));
-            bundle.setResult(msg);
-            OBDal.getInstance().rollbackAndClose();
-            return;
+                + OBMessageUtils.messageBD("BusinessPartnerBlocked");
+            throw new OBException(msg);
           }
         } else {
           OBContext.setAdminMode(true);
@@ -120,14 +141,10 @@
                         4)) {
                   // If the Business Partner is blocked for Payments, the 
Payment will not be
                   // completed.
-                  msg.setType("Error");
-                  msg.setTitle(Utility.messageBD(conProvider, "Error", 
language));
-                  
msg.setMessage(OBMessageUtils.messageBD("ThebusinessPartner") + " "
+                  msg = OBMessageUtils.messageBD("ThebusinessPartner") + " "
                       + bPartner.getIdentifier() + " "
-                      + OBMessageUtils.messageBD("BusinessPartnerBlocked"));
-                  bundle.setResult(msg);
-                  OBDal.getInstance().rollbackAndClose();
-                  return;
+                      + OBMessageUtils.messageBD("BusinessPartnerBlocked");
+                  throw new OBException(msg);
                 }
               }
             }
@@ -159,38 +176,23 @@
           payment.setUsedCredit(BigDecimal.ZERO);
           OBDal.getInstance().save(payment);
         }
-        // Set APRM_Ready preference
-        if (vars.getSessionValue("APRMT_MigrationToolRunning", "N").equals("Y")
-            && !dao.existsAPRMReadyPreference()) {
-          dao.createAPRMReadyPreference();
-        }
 
-        boolean orgLegalWithAccounting = 
FIN_Utility.periodControlOpened(payment.TABLE_NAME,
-            payment.getId(), payment.TABLE_NAME + "_ID", "LE");
-        boolean documentEnabled = getDocumentConfirmation(conProvider, 
payment.getId());
+        boolean orgLegalWithAccounting = 
FIN_Utility.periodControlOpened(FIN_Payment.TABLE_NAME,
+            payment.getId(), FIN_Payment.TABLE_NAME + "_ID", "LE");
+        boolean documentEnabled = getDocumentConfirmation(null, 
payment.getId());
         if (documentEnabled
             && !FIN_Utility.isPeriodOpen(payment.getClient().getId(), 
payment.getDocumentType()
                 .getDocumentCategory(), payment.getOrganization().getId(), 
OBDateUtils
                 .formatDate(payment.getPaymentDate())) && 
orgLegalWithAccounting) {
-          msg.setType("Error");
-          msg.setTitle(Utility.messageBD(conProvider, "Error", language));
-          msg.setMessage(Utility.parseTranslation(conProvider, vars, language,
-              "@PeriodNotAvailable@"));
-          bundle.setResult(msg);
-          OBDal.getInstance().rollbackAndClose();
-          return;
+          msg = OBMessageUtils.messageBD("PeriodNotAvailable");
+          throw new OBException(msg);
         }
         Set<String> documentOrganizations = OBContext.getOBContext()
             .getOrganizationStructureProvider(payment.getClient().getId())
             .getNaturalTree(payment.getOrganization().getId());
         if 
(!documentOrganizations.contains(payment.getAccount().getOrganization().getId()))
 {
-          msg.setType("Error");
-          msg.setTitle(Utility.messageBD(conProvider, "Error", language));
-          msg.setMessage(Utility.parseTranslation(conProvider, vars, language,
-              "@APRM_FinancialAccountNotInNaturalTree@"));
-          bundle.setResult(msg);
-          OBDal.getInstance().rollbackAndClose();
-          return;
+          msg = 
OBMessageUtils.messageBD("APRM_FinancialAccountNotInNaturalTree");
+          throw new OBException(msg);
         }
         Set<String> invoiceDocNos = new TreeSet<String>();
         Set<String> orderDocNos = new TreeSet<String>();
@@ -208,13 +210,8 @@
 
           // Show error message when payment has no lines
           if (paymentDetails.size() == 0) {
-            msg.setType("Error");
-            msg.setTitle(Utility.messageBD(conProvider, "Error", language));
-            msg.setMessage(Utility.parseTranslation(conProvider, vars, 
language,
-                "@APRM_PaymentNoLines@"));
-            bundle.setResult(msg);
-            OBDal.getInstance().rollbackAndClose();
-            return;
+            msg = OBMessageUtils.messageBD("APRM_PaymentNoLines");
+            throw new OBException(msg);
           }
           for (FIN_PaymentDetail paymentDetail : paymentDetails) {
             for (FIN_PaymentScheduleDetail paymentScheduleDetail : 
paymentDetail
@@ -238,9 +235,9 @@
                   && paymentScheduleDetail.getOrderPaymentSchedule() == null
                   && paymentScheduleDetail.getPaymentDetails().getGLItem() == 
null) {
                 if (paymentDetail.isRefund())
-                  strRefundCredit = Utility.messageBD(conProvider, 
"APRM_RefundAmount", language);
+                  strRefundCredit = 
OBMessageUtils.messageBD("APRM_RefundAmount");
                 else {
-                  strRefundCredit = Utility.messageBD(conProvider, 
"APRM_CreditAmount", language);
+                  strRefundCredit = 
OBMessageUtils.messageBD("APRM_CreditAmount");
                   payment.setGeneratedCredit(paymentDetail.getAmount());
                 }
                 strRefundCredit += ": " + paymentDetail.getAmount().toString();
@@ -250,26 +247,25 @@
               glitems.add(paymentDetail.getGLItem().getName());
           }
           // Set description
-          if (bundle.getParams().get("isPOSOrder") == null
-              || !bundle.getParams().get("isPOSOrder").equals("Y")) {
+          if (!isPosOrder) {
             StringBuffer description = new StringBuffer();
 
             if (payment.getDescription() != null && 
!payment.getDescription().equals(""))
               description.append(payment.getDescription()).append("\n");
             if (!invoiceDocNos.isEmpty()) {
-              description.append(Utility.messageBD(conProvider, 
"InvoiceDocumentno", language));
+              
description.append(OBMessageUtils.messageBD("InvoiceDocumentno"));
               description.append(": ").append(
                   invoiceDocNos.toString().substring(1, 
invoiceDocNos.toString().length() - 1));
               description.append("\n");
             }
             if (!orderDocNos.isEmpty()) {
-              description.append(Utility.messageBD(conProvider, 
"OrderDocumentno", language));
+              description.append(OBMessageUtils.messageBD("OrderDocumentno"));
               description.append(": ").append(
                   orderDocNos.toString().substring(1, 
orderDocNos.toString().length() - 1));
               description.append("\n");
             }
             if (!glitems.isEmpty()) {
-              description.append(Utility.messageBD(conProvider, "APRM_GLItem", 
language));
+              description.append(OBMessageUtils.messageBD("APRM_GLItem"));
               description.append(": ").append(
                   glitems.toString().substring(1, glitems.toString().length() 
- 1));
               description.append("\n");
@@ -299,13 +295,8 @@
               || BigDecimal.ZERO.compareTo(payment.getGeneratedCredit()) != 0) 
{
             BusinessPartner businessPartner = payment.getBusinessPartner();
             if (businessPartner == null) {
-              msg.setType("Error");
-              msg.setTitle(Utility.messageBD(conProvider, "Error", language));
-              msg.setMessage(Utility.parseTranslation(conProvider, vars, 
language,
-                  "@APRM_CreditWithoutBPartner@"));
-              bundle.setResult(msg);
-              OBDal.getInstance().rollbackAndClose();
-              return;
+              msg = OBMessageUtils.messageBD("APRM_CreditWithoutBPartner");
+              throw new OBException(msg);
             }
             String currency = null;
             if (businessPartner.getCurrency() == null) {
@@ -315,14 +306,9 @@
               currency = businessPartner.getCurrency().getId();
             }
             if (!payment.getCurrency().getId().equals(currency)) {
-              msg.setType("Error");
-              msg.setTitle(Utility.messageBD(conProvider, "Error", language));
-              msg.setMessage(String.format(
-                  Utility.parseTranslation(conProvider, vars, language, 
"@APRM_CreditCurrency@"),
-                  businessPartner.getCurrency().getISOCode()));
-              bundle.setResult(msg);
-              OBDal.getInstance().rollbackAndClose();
-              return;
+              msg = 
String.format(OBMessageUtils.messageBD("APRM_CreditCurrency"), businessPartner
+                  .getCurrency().getISOCode());
+              throw new OBException(msg);
             }
           }
           // Execution Process
@@ -342,34 +328,22 @@
                       payment.getOrganization());
                   OBError result = executePayment.execute();
                   if ("Error".equals(result.getType())) {
-                    msg.setType("Warning");
-                    msg.setMessage(Utility.parseTranslation(conProvider, vars, 
language,
-                        result.getMessage()));
+                    msg = OBMessageUtils.messageBD(result.getMessage());
                   } else if (!"".equals(result.getMessage())) {
-                    String execProcessMsg = 
Utility.parseTranslation(conProvider, vars, language,
-                        result.getMessage());
-                    if (!"".equals(msg.getMessage()))
-                      msg.setMessage(msg.getMessage() + "<br>");
-                    msg.setMessage(msg.getMessage() + execProcessMsg);
+                    String execProcessMsg = 
OBMessageUtils.messageBD(result.getMessage());
+                    if (!"".equals(msg)) {
+                      msg += "<br>";
+                    }
+                    msg += execProcessMsg;
                   }
                 }
               }
             } catch (final NoExecutionProcessFoundException e) {
-              e.printStackTrace(System.err);
-              msg.setType("Warning");
-              msg.setMessage(Utility.parseTranslation(conProvider, vars, 
language,
-                  "@NoExecutionProcessFound@"));
-              bundle.setResult(msg);
-              OBDal.getInstance().rollbackAndClose();
-              return;

------------------------------------------------------------------------------
New Year. New Location. New Benefits. New Data Center in Ashburn, VA.
GigeNET is offering a free month of service with a new server in Ashburn.
Choose from 2 high performing configs, both with 100TB of bandwidth.
Higher redundancy.Lower latency.Increased capacity.Completely compliant.
http://p.sf.net/sfu/gigenet
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to