details:   https://code.openbravo.com/erp/devel/pi/rev/32bab2f8df54
changeset: 32982:32bab2f8df54
user:      Atul Gaware <atul.gaware <at> openbravo.com>
date:      Fri Oct 06 11:41:37 2017 +0530
summary:   Fixes Issue 36747:Avoid query more than once in several classes

** In ReportReconciliation.java, method getUnreconciledBankStatmentLinesTotal,
getOutstandingPaymentAndDepositTotal, 
getTransactionsTotalAfterReconciliationEndDate
executes query only once by avoiding calls to list method multiple times

** Use IsEmpty method instead of size() > 0 to check if the list is not empty

details:   https://code.openbravo.com/erp/devel/pi/rev/9cf1f8c7de28
changeset: 32983:9cf1f8c7de28
user:      Atul Gaware <atul.gaware <at> openbravo.com>
date:      Tue Oct 10 13:49:09 2017 +0530
summary:   Related to Issue 36747:Avoid query more than once in 
AdvPaymentMngtDao.java

** Use IsEmpty() for list instead if size() > 0
** Use count() to get the size of list instead of size()
** Instead of get(0) for any list, use setMaxResults(1) and uniqueResult()
   for OBCriteria

details:   https://code.openbravo.com/erp/devel/pi/rev/59128cd57e89
changeset: 32984:59128cd57e89
user:      Atul Gaware <atul.gaware <at> openbravo.com>
date:      Tue Oct 10 13:51:31 2017 +0530
summary:   Related to Issue 36747:Avoid query more than once in 
FIN_PaymentMonitorProcess.java

** Instead of get(0) for any list, use setMaxResults(1) and uniqueResult()
   for OBCriteria
** Use StringUtils to compare module id
** Update copyright year information

details:   https://code.openbravo.com/erp/devel/pi/rev/75b558d70612
changeset: 32985:75b558d70612
user:      Atul Gaware <atul.gaware <at> openbravo.com>
date:      Wed Oct 11 14:47:07 2017 +0530
summary:   Related to Issue 36747:Avoid query more than once in 
InventoryStatusTest.java

** Use count() instead of list().size()

details:   https://code.openbravo.com/erp/devel/pi/rev/14205180042a
changeset: 32986:14205180042a
user:      Atul Gaware <atul.gaware <at> openbravo.com>
date:      Wed Oct 11 14:38:14 2017 +0530
summary:   Related to Issue 36747:Avoid query more than once in 
InventoryStatusUtils.java

** Avoid list().size() and use count()

details:   https://code.openbravo.com/erp/devel/pi/rev/ee94fdf7171e
changeset: 32987:ee94fdf7171e
user:      Atul Gaware <atul.gaware <at> openbravo.com>
date:      Wed Oct 11 01:23:38 2017 +0530
summary:   Related to Issue 36747:Avoid query more than once in 
SE_Locator_Activate.java

** Use setMaxResult(1) and uniqueResult() in storageIsNotEmpty method
   just one fetch a single record and decide whether the storage bin
   is empty or not instead of fetching all the record and checking
   with size() > 0

details:   https://code.openbravo.com/erp/devel/pi/rev/44e45c93cd40
changeset: 32988:44e45c93cd40
user:      Atul Gaware <atul.gaware <at> openbravo.com>
date:      Wed Oct 11 14:42:38 2017 +0530
summary:   Related to Issue 36747:Avoid query more than once in TestCosting.java

** Avoid mutliple call to list() method in assertDocumentPost
** Use setMaxResult(1) and uniqueResult()
   in OBCriteria instead of get(0) from the list.
** Use count() instead of list().size()

details:   https://code.openbravo.com/erp/devel/pi/rev/218bc353d82b
changeset: 32989:218bc353d82b
user:      Atul Gaware <atul.gaware <at> openbravo.com>
date:      Thu Nov 09 17:58:26 2017 +0100
summary:   Related to Issue 36747:Avoid query more than once in 
TransactionsDao.java

** Avoid multiple call to list() and list().size() in getAccTrxData method
** Use uniqueResult() instead of get(0) to get Last FIN_Reconciliation
** Use count() instead of size() in getPendingToMatchCount method
** Avoid multiple calls to list() and use setMaxResult(1) and uniqueResult()
   in getPeriod method instead of get(0)

details:   https://code.openbravo.com/erp/devel/pi/rev/fd6282f18182
changeset: 32990:fd6282f18182
user:      Atul Gaware <atul.gaware <at> openbravo.com>
date:      Wed Oct 11 01:56:44 2017 +0530
summary:   Related to Issue 36747:Avoid query more than once in Utility.java

** Avoid mutliple call to list() method and use setMaxResult(1) and 
uniqueResult()
   in OBCriteria instead of get(0) from the list.

details:   https://code.openbravo.com/erp/devel/pi/rev/929418684af7
changeset: 32991:929418684af7
user:      David Miguelez <david.miguelez <at> openbravo.com>
date:      Fri Nov 10 10:24:55 2017 +0100
summary:   Related to Issue 36747. Code review changes

* Added comments to code
* Moved some code to a different line
* Added uniqueResults condition to missing clause in SE_Locator_Activate

diffstat:

 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_reports/ReportReconciliation.java
   |  23 +-
 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/AdvPaymentMngtDao.java
             |  75 ++++----
 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/TransactionsDao.java
               |  13 +-
 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentMonitorProcess.java
 |  25 +-
 src-test/src/org/openbravo/test/costing/TestCosting.java                       
                              |  87 ++++++---
 src-test/src/org/openbravo/test/inventoryStatus/InventoryStatusTest.java       
                              |   6 +-
 src/org/openbravo/erpCommon/ad_callouts/SE_Invoice_BPartner.java               
                              |   2 +-
 src/org/openbravo/erpCommon/ad_callouts/SE_Locator_Activate.java               
                              |   8 +-
 src/org/openbravo/erpCommon/utility/InventoryStatusUtils.java                  
                              |   2 +-
 src/org/openbravo/erpCommon/utility/Utility.java                               
                              |  24 +-
 10 files changed, 148 insertions(+), 117 deletions(-)

diffs (truncated from 717 to 300 lines):

diff -r 873652fe3cd4 -r 929418684af7 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_reports/ReportReconciliation.java
--- 
a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_reports/ReportReconciliation.java
        Fri Nov 10 11:49:58 2017 +0100
+++ 
b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_reports/ReportReconciliation.java
        Fri Nov 10 10:24:55 2017 +0100
@@ -197,7 +197,7 @@
           recon.getEndingDate()));
       List<FIN_Reconciliation> afterReconciliations = MatchTransactionDao
           .getReconciliationListAfterDate(recon);
-      if (afterReconciliations.size() > 0) {
+      if (!afterReconciliations.isEmpty()) {
         obcTrans.add(Restrictions.or(
             Restrictions.isNull(FIN_FinaccTransaction.PROPERTY_RECONCILIATION),
             Restrictions.in(FIN_FinaccTransaction.PROPERTY_RECONCILIATION, 
afterReconciliations)));
@@ -209,10 +209,9 @@
       
projections.add(Projections.sum(FIN_FinaccTransaction.PROPERTY_PAYMENTAMOUNT));
       
projections.add(Projections.sum(FIN_FinaccTransaction.PROPERTY_DEPOSITAMOUNT));
       obcTrans.setProjection(projections);
-
-      if (obcTrans.list() != null && obcTrans.list().size() > 0) {
-        @SuppressWarnings("rawtypes")
-        List o = obcTrans.list();
+      @SuppressWarnings("rawtypes")
+      List o = obcTrans.list();
+      if (o != null && !o.isEmpty()) {
         Object[] resultSet = (Object[]) o.get(0);
         BigDecimal paymentAmt = (resultSet[0] != null) ? (BigDecimal) 
resultSet[0]
             : BigDecimal.ZERO;
@@ -255,7 +254,7 @@
           recon.getEndingDate()));
       List<FIN_Reconciliation> afterReconciliations = MatchTransactionDao
           .getReconciliationListAfterDate(recon);
-      if (afterReconciliations.size() > 0) {
+      if (!afterReconciliations.isEmpty()) {
         obcBsl.add(Restrictions.or(Restrictions
             
.isNull(FIN_BankStatementLine.PROPERTY_FINANCIALACCOUNTTRANSACTION), 
Restrictions.in(
             "tr." + FIN_FinaccTransaction.PROPERTY_RECONCILIATION, 
afterReconciliations)));
@@ -269,9 +268,9 @@
       
projections.add(Projections.sum(FIN_BankStatementLine.PROPERTY_DRAMOUNT));
       obcBsl.setProjection(projections);
 
-      if (obcBsl.list() != null && obcBsl.list().size() > 0) {
-        @SuppressWarnings("rawtypes")
-        List o = obcBsl.list();
+      @SuppressWarnings("rawtypes")
+      List o = obcBsl.list();
+      if (o != null && !o.isEmpty()) {
         Object[] resultSet = (Object[]) o.get(0);
         BigDecimal credit = (resultSet[0] != null) ? (BigDecimal) resultSet[0] 
: BigDecimal.ZERO;
         BigDecimal debit = (resultSet[1] != null) ? (BigDecimal) resultSet[1] 
: BigDecimal.ZERO;
@@ -309,9 +308,9 @@
       
projections.add(Projections.sum(FIN_FinaccTransaction.PROPERTY_DEPOSITAMOUNT));
       obcTrans.setProjection(projections);
 
-      if (obcTrans.list() != null && obcTrans.list().size() > 0) {
-        @SuppressWarnings("rawtypes")
-        List o = obcTrans.list();
+      @SuppressWarnings("rawtypes")
+      List o = obcTrans.list();
+      if (o != null && !o.isEmpty()) {
         Object[] resultSet = (Object[]) o.get(0);
         BigDecimal paymentAmt = (resultSet[0] != null) ? (BigDecimal) 
resultSet[0]
             : BigDecimal.ZERO;
diff -r 873652fe3cd4 -r 929418684af7 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/AdvPaymentMngtDao.java
--- 
a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/AdvPaymentMngtDao.java
  Fri Nov 10 11:49:58 2017 +0100
+++ 
b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/AdvPaymentMngtDao.java
  Fri Nov 10 10:24:55 2017 +0100
@@ -259,7 +259,7 @@
       whereClause.append(" and (oinfo is null or oinfo.active = true)");
 
       // remove selected payments
-      if (selectedScheduledPaymentDetails != null && 
selectedScheduledPaymentDetails.size() > 0) {
+      if (selectedScheduledPaymentDetails != null && 
!selectedScheduledPaymentDetails.isEmpty()) {
         String strSelectedPaymentDetails = 
Utility.getInStrList(selectedScheduledPaymentDetails);
         whereClause.append(" and psd not in (");
         whereClause.append(strSelectedPaymentDetails);
@@ -270,9 +270,10 @@
       final OBCriteria<FIN_PaymentPropDetail> obc = 
OBDal.getInstance().createCriteria(
           FIN_PaymentPropDetail.class);
       
obc.add(Restrictions.isNotNull(FIN_PaymentPropDetail.PROPERTY_FINPAYMENTSCHEDULEDETAIL));
-      if (obc.list() != null && obc.list().size() > 0) {
+      List<FIN_PaymentPropDetail> paymentPropDetailList = obc.list();
+      if (paymentPropDetailList != null && !paymentPropDetailList.isEmpty()) {
         List<FIN_PaymentScheduleDetail> aux = new 
ArrayList<FIN_PaymentScheduleDetail>();
-        for (FIN_PaymentPropDetail ppd : obc.list()) {
+        for (FIN_PaymentPropDetail ppd : paymentPropDetailList) {
           aux.add(ppd.getFINPaymentScheduledetail());
         }
         whereClause.append(" and psd.id not in (" + Utility.getInStrList(aux) 
+ ")");
@@ -1153,7 +1154,7 @@
           FIN_FinaccTransaction.class, whereClause.toString());
       obqFATrx.setFirstResult(offset);
       obqFATrx.setMaxResult(maxrowspergridpage);
-      return obqFATrx.list().size();
+      return obqFATrx.count();
 
     } finally {
       OBContext.restorePreviousMode();
@@ -1545,28 +1546,34 @@
     }
     obc.addOrder(org.hibernate.criterion.Order.asc("pm." + 
FIN_PaymentMethod.PROPERTY_NAME));
     obc.setFilterOnReadableOrganization(false);
-    List<FinAccPaymentMethod> defaults = obc.list();
-    if (defaults.size() > 0) {
-      return obc.list().get(0).getPaymentMethod().getId();
+    obc.setMaxResults(1);
+    FinAccPaymentMethod paymentMethod = (FinAccPaymentMethod) 
obc.uniqueResult();
+    if (paymentMethod != null) {
+      return paymentMethod.getPaymentMethod().getId();
     } else {
       return "";
     }
   }
 
+  /**
+   * This method adds a filter to the Query to filter by Payment Methods Id's. 
However, since the
+   * list can be bigger than 1000 records, it is necessary to split it to 
create several
+   * "paymentMethod.id in ('')" clauses that have less than 1000 records.
+   */
   private void addPaymentMethodList(OBCriteria<FIN_PaymentMethod> obc, 
List<String> paymentMethods) {
-    List<String> paymentMethodsToRemove;
     Criterion compoundExp = null;
-    while (paymentMethods.size() > 999) {
-      paymentMethodsToRemove = new ArrayList<String>(paymentMethods.subList(0, 
999));
+    int paymentMethodsSize = paymentMethods.size();
+    while (paymentMethodsSize > 999) {
+      List<String> paymentMethodsToRemove = paymentMethods.subList(0, 999);
       if (compoundExp == null) {
-        compoundExp = Restrictions.in("id", paymentMethods.subList(0, 999));
+        compoundExp = Restrictions.in("id", paymentMethodsToRemove);
       } else {
-        compoundExp = Restrictions.or(compoundExp,
-            Restrictions.in("id", paymentMethods.subList(0, 999)));
+        compoundExp = Restrictions.or(compoundExp, Restrictions.in("id", 
paymentMethodsToRemove));
       }
       paymentMethods.removeAll(paymentMethodsToRemove);
+      paymentMethodsSize = paymentMethodsSize - 999;
     }
-    if (paymentMethods.size() > 0) {
+    if (paymentMethodsSize > 0) {
       if (compoundExp == null) {
         compoundExp = Restrictions.in("id", paymentMethods);
       } else {
@@ -1676,14 +1683,14 @@
     }
 
     public Criterion getCriterion() {
-      if (finAccs.size() > 0) {
+      if (!finAccs.isEmpty()) {
         refresh();
       }
       return compoundexp;
     }
 
     private void refresh() {
-      // finAccs.size() must be > 0
+      // finAccs size must be > 0
       if (compoundexp == null) {
         compoundexp = Restrictions.in("id", finAccs);
       } else {
@@ -1879,9 +1886,10 @@
     OBCriteria<APRMPendingPaymentFromInvoice> ppfiCriteria = 
OBDal.getInstance().createCriteria(
         APRMPendingPaymentFromInvoice.class);
     
ppfiCriteria.add(Restrictions.eq(APRMPendingPaymentFromInvoice.PROPERTY_PAYMENT,
 payment));
-    List<APRMPendingPaymentFromInvoice> pendingPayments = ppfiCriteria.list();
-    if (pendingPayments != null && pendingPayments.size() > 0) {
-      APRMPendingPaymentFromInvoice pendingPayment = pendingPayments.get(0);
+    ppfiCriteria.setMaxResults(1);
+    APRMPendingPaymentFromInvoice pendingPayment = 
(APRMPendingPaymentFromInvoice) ppfiCriteria
+        .uniqueResult();
+    if (pendingPayment != null) {
       pendingPayment.setProcessNow(executing);
       OBDal.getInstance().flush();
       OBDal.getInstance().save(pendingPayment);
@@ -1892,11 +1900,14 @@
     OBCriteria<APRMPendingPaymentFromInvoice> ppfiCriteria = 
OBDal.getInstance().createCriteria(
         APRMPendingPaymentFromInvoice.class);
     
ppfiCriteria.add(Restrictions.eq(APRMPendingPaymentFromInvoice.PROPERTY_PAYMENT,
 payment));
-    List<APRMPendingPaymentFromInvoice> pendingPayments = ppfiCriteria.list();
-    if (pendingPayments != null && pendingPayments.size() > 0) {
-      return pendingPayments.get(0).isProcessNow();
-    } else
+    ppfiCriteria.setMaxResults(1);
+    APRMPendingPaymentFromInvoice pendingPayment = 
(APRMPendingPaymentFromInvoice) ppfiCriteria
+        .uniqueResult();
+    if (pendingPayment != null) {
+      return pendingPayment.isProcessNow();
+    } else {
       return false;
+    }
 
   }
 
@@ -2047,15 +2058,9 @@
     obc.add(Restrictions.in("organization.id", OBContext.getOBContext()
         .getOrganizationStructureProvider().getNaturalTree(strOrgId)));
     obc.setFilterOnReadableOrganization(false);
-
     obc.add(Restrictions.eq(FIN_FinancialAccount.PROPERTY_DEFAULT, true));
-
-    final List<FIN_FinancialAccount> defaultAccounts = obc.list();
-    if (defaultAccounts.size() > 0) {
-      return defaultAccounts.get(0);
-    } else {
-      return null;
-    }
+    obc.setMaxResults(1);
+    return (FIN_FinancialAccount) obc.uniqueResult();
   }
 
   public boolean existsAPRMReadyPreference() {
@@ -2111,13 +2116,7 @@
           
.getOrganizationStructureProvider().getNaturalTree(organization.getId())));
       obcFinPayment.addOrderBy(FIN_Payment.PROPERTY_PAYMENTDATE, true);
       obcFinPayment.setMaxResults(1);
-
-      final List<FIN_Payment> finPayments = obcFinPayment.list();
-      if (finPayments.size() > 0) {
-        return finPayments.get(0);
-      } else {
-        return null;
-      }
+      return (FIN_Payment) obcFinPayment.uniqueResult();
     } finally {
       OBContext.restorePreviousMode();
     }
diff -r 873652fe3cd4 -r 929418684af7 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/TransactionsDao.java
--- 
a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/TransactionsDao.java
    Fri Nov 10 11:49:58 2017 +0100
+++ 
b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/TransactionsDao.java
    Fri Nov 10 10:24:55 2017 +0100
@@ -82,10 +82,8 @@
       final OBQuery<FIN_FinaccTransaction> obQuery = 
OBDal.getInstance().createQuery(
           FIN_FinaccTransaction.class, whereClause.toString());
       obQuery.setParameters(parameters);
-      OBObjectFieldProvider[] objectFieldProvider = null;
-      if (obQuery != null && obQuery.list().size() > 0) {
-        objectFieldProvider = 
OBObjectFieldProvider.createOBObjectFieldProvider(obQuery.list());
-      }
+      OBObjectFieldProvider[] objectFieldProvider = OBObjectFieldProvider
+          .createOBObjectFieldProvider(obQuery.list());
       return objectFieldProvider;
     } finally {
       OBContext.restorePreviousMode();
@@ -216,10 +214,7 @@
       obc.addOrderBy(FIN_Reconciliation.PROPERTY_ENDINGDATE, false);
       obc.addOrderBy(FIN_Reconciliation.PROPERTY_CREATIONDATE, false);
       obc.setMaxResults(1);
-      final List<FIN_Reconciliation> rec = obc.list();
-      if (rec.size() == 0)
-        return null;
-      return rec.get(0);
+      return (FIN_Reconciliation) obc.uniqueResult();
     } finally {
       OBContext.restorePreviousMode();
     }
@@ -240,7 +235,7 @@
           .append(" is null ");
       final OBQuery<FIN_FinaccTransaction> obqFATrx = 
OBDal.getInstance().createQuery(
           FIN_FinaccTransaction.class, whereClause.toString());
-      return obqFATrx.list().size();
+      return obqFATrx.count();
 
     } finally {
       OBContext.restorePreviousMode();
diff -r 873652fe3cd4 -r 929418684af7 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentMonitorProcess.java
--- 
a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentMonitorProcess.java
      Fri Nov 10 11:49:58 2017 +0100
+++ 
b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentMonitorProcess.java
      Fri Nov 10 10:24:55 2017 +0100
@@ -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-2015 Openbravo SLU
+ * All portions are Copyright (C) 2010-2017 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  *************************************************************************
@@ -26,6 +26,7 @@
 import java.util.HashMap;
 import java.util.List;
 
+import org.apache.commons.lang.StringUtils;
 import org.hibernate.ScrollMode;
 import org.hibernate.ScrollableResults;
 import org.hibernate.criterion.Projections;
@@ -202,7 +203,8 @@
     }
     obc.add(Restrictions.eq(FIN_PaymentSchedInvV.PROPERTY_INVOICE, invoice));
     
obc.setProjection(Projections.max(FIN_PaymentSchedInvV.PROPERTY_LASTPAYMENT));
-    Object o = obc.list().get(0);
+    obc.setMaxResults(1);
+    Object o = obc.uniqueResult();
     if (o != null) {
       return ((Date) o);
     } else {
@@ -307,7 +309,8 @@
     obc.add(Restrictions.eq(FIN_PaymentSchedule.PROPERTY_INVOICE, invoice));
     obc.add(Restrictions.ne(FIN_PaymentSchedule.PROPERTY_OUTSTANDINGAMOUNT, 
BigDecimal.ZERO));
     obc.setProjection(Projections.min(FIN_PaymentSchedule.PROPERTY_DUEDATE));
-    Object o = obc.list().get(0);
+    obc.setMaxResults(1);
+    Object o = obc.uniqueResult();
     if (o != null) {
       return (FIN_Utility.getDaysToDue((Date) o));
     } else {

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to