From c0d989b24086dedbe98290d7c0bc758bb60bfc88 Mon Sep 17 00:00:00 2001 From: "Redhuan D. Oon" Date: Mon, 1 Mar 2010 00:42:03 +0000 Subject: [PATCH] FR: [ 2214883 ] Remove SQL code and Replace for Query -- JUnit test included (no failures) Link to SF Tracker: http://sourceforge.net/support/tracker.php?aid=2214883 --- base/src/org/compiere/model/MCostElement.java | 154 +++--------------- 1 file changed, 26 insertions(+), 128 deletions(-) diff --git a/base/src/org/compiere/model/MCostElement.java b/base/src/org/compiere/model/MCostElement.java index 3a8a9349eb..4f46f85342 100644 --- a/base/src/org/compiere/model/MCostElement.java +++ b/base/src/org/compiere/model/MCostElement.java @@ -16,13 +16,10 @@ *****************************************************************************/ package org.compiere.model; -import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Properties; -import java.util.logging.Level; import org.compiere.util.CCache; import org.compiere.util.CLogger; @@ -38,6 +35,8 @@ import org.compiere.util.Msg; * @author Teo Sarca, www.arhipac.ro *
  • BF [ 2664529 ] More then one Labor/Burden//Overhead is not allowed *
  • BF [ 2667470 ] MCostElement.getMaterialCostElement should check only material + * @author red1 + *
  • FR: [ 2214883 ] Remove SQL code and Replace for Query -- JUnit tested */ public class MCostElement extends X_M_CostElement { @@ -63,7 +62,7 @@ public class MCostElement extends X_M_CostElement // final String whereClause = "AD_Client_ID=? AND CostingMethod=? AND CostElementType=?"; MCostElement retValue = new Query(po.getCtx(), Table_Name, whereClause, po.get_TrxName()) - .setParameters(new Object[]{po.getAD_Client_ID(), CostingMethod, COSTELEMENTTYPE_Material}) + .setParameters(po.getAD_Client_ID(), CostingMethod, COSTELEMENTTYPE_Material) .setOrderBy("AD_Org_ID") .firstOnly(); if (retValue != null) @@ -92,37 +91,11 @@ public class MCostElement extends X_M_CostElement */ public static MCostElement getMaterialCostElement(Properties ctx, String CostingMethod) { - MCostElement retValue = null; - String sql = "SELECT * FROM M_CostElement WHERE AD_Client_ID=? AND CostingMethod=? ORDER BY AD_Org_ID"; - PreparedStatement pstmt = null; - try - { - pstmt = DB.prepareStatement (sql, null); - pstmt.setInt (1, Env.getAD_Client_ID(ctx)); - pstmt.setString(2, CostingMethod); - ResultSet rs = pstmt.executeQuery (); - if (rs.next ()) - retValue = new MCostElement (ctx, rs, null); - if (rs.next()) - s_log.info("More then one Material Cost Element for CostingMethod=" + CostingMethod); - rs.close (); - pstmt.close (); - pstmt = null; - } - catch (Exception e) - { - s_log.log (Level.SEVERE, sql, e); - } - try - { - if (pstmt != null) - pstmt.close (); - pstmt = null; - } - catch (Exception e) - { - pstmt = null; - } + final String whereClause = "AD_Client_ID=? AND CostingMethod=?"; + MCostElement retValue = new Query(ctx, I_M_CostElement.Table_Name, whereClause, null) + .setParameters(Env.getAD_Client_ID(ctx), CostingMethod) + .setOrderBy(I_M_CostElement.COLUMNNAME_AD_Org_ID) + .firstOnly(); return retValue; } // getMaterialCostElement @@ -147,36 +120,11 @@ public class MCostElement extends X_M_CostElement */ public static MCostElement[] getCostingMethods (PO po) { - ArrayList list = new ArrayList(); - String sql = "SELECT * FROM M_CostElement " - + "WHERE AD_Client_ID=?" - + " AND IsActive='Y' AND CostElementType='M' AND CostingMethod IS NOT NULL"; - PreparedStatement pstmt = null; - try - { - pstmt = DB.prepareStatement (sql, po.get_TrxName()); - pstmt.setInt (1, po.getAD_Client_ID()); - ResultSet rs = pstmt.executeQuery (); - while (rs.next ()) - list.add(new MCostElement (po.getCtx(), rs, po.get_TrxName())); - rs.close (); - pstmt.close (); - pstmt = null; - } - catch (Exception e) - { - s_log.log (Level.SEVERE, sql, e); - } - try - { - if (pstmt != null) - pstmt.close (); - pstmt = null; - } - catch (Exception e) - { - pstmt = null; - } + final String whereClause ="AD_Client_ID=? AND CostElementType='M' AND CostingMethod IS NOT NULL"; + List list = new Query(po.getCtx(), I_M_CostElement.Table_Name, whereClause, po.get_TrxName()) + .setParameters(po.getAD_Client_ID()) + .setOnlyActiveRecords(true) + .list(); // MCostElement[] retValue = new MCostElement[list.size ()]; list.toArray (retValue); @@ -191,36 +139,11 @@ public class MCostElement extends X_M_CostElement */ public static MCostElement[] getNonCostingMethods (PO po) { - ArrayList list = new ArrayList(); - String sql = "SELECT * FROM M_CostElement " - + "WHERE AD_Client_ID=?" - + " AND IsActive='Y' AND CostingMethod IS NULL"; - PreparedStatement pstmt = null; - try - { - pstmt = DB.prepareStatement (sql, po.get_TrxName()); - pstmt.setInt (1, po.getAD_Client_ID()); - ResultSet rs = pstmt.executeQuery (); - while (rs.next ()) - list.add(new MCostElement (po.getCtx(), rs, po.get_TrxName())); - rs.close (); - pstmt.close (); - pstmt = null; - } - catch (Exception e) - { - s_log.log (Level.SEVERE, sql, e); - } - try - { - if (pstmt != null) - pstmt.close (); - pstmt = null; - } - catch (Exception e) - { - pstmt = null; - } + final String whereClause = "AD_Client_ID=? AND CostingMethod IS NULL"; + Listlist = new Query(po.getCtx(),MCostElement.Table_Name, whereClause, po.get_TrxName()) + .setParameters(po.getAD_Client_ID()) + .setOnlyActiveRecords(true) + .list(); // MCostElement[] retValue = new MCostElement[list.size ()]; list.toArray (retValue); @@ -258,9 +181,9 @@ public class MCostElement extends X_M_CostElement int AD_Client_ID = Env.getAD_Client_ID(ctx); int AD_Org_ID = 0; // Org is always ZERO - see beforeSave - String whereClause = "AD_Client_ID = ? AND AD_Org_ID = ?"; + final String whereClause = "AD_Client_ID = ? AND AD_Org_ID = ?"; List list = new Query(ctx, Table_Name, whereClause, trxName) - .setParameters(new Object[]{AD_Client_ID, AD_Org_ID}) + .setParameters(AD_Client_ID, AD_Org_ID) .list(); MCostElement[] retValue = new MCostElement[list.size()]; list.toArray(retValue); @@ -279,7 +202,7 @@ public class MCostElement extends X_M_CostElement return new Query(ctx, Table_Name, whereClause, null) .setClient_ID() .setOnlyActiveRecords(true) - .setParameters(new Object[]{CostingMethod}) + .setParameters(CostingMethod) .list(); } @@ -393,37 +316,12 @@ public class MCostElement extends X_M_CostElement } // Costing Methods on PC level - String sql = "SELECT M_Product_Category_ID FROM M_Product_Category_Acct WHERE AD_Client_ID=? AND CostingMethod=?"; int M_Product_Category_ID = 0; - PreparedStatement pstmt = null; - try - { - pstmt = DB.prepareStatement (sql, null); - pstmt.setInt (1, getAD_Client_ID()); - pstmt.setString (2, getCostingMethod()); - ResultSet rs = pstmt.executeQuery (); - if (rs.next ()) - { - M_Product_Category_ID = rs.getInt(1); - } - rs.close (); - pstmt.close (); - pstmt = null; - } - catch (Exception e) - { - log.log (Level.SEVERE, sql, e); - } - try - { - if (pstmt != null) - pstmt.close (); - pstmt = null; - } - catch (Exception e) - { - pstmt = null; - } + final String whereClause ="AD_Client_ID=? AND CostingMethod=?"; + MProductCategory retValue = new Query(getCtx(), I_M_Product_Category_Acct.Table_Name, whereClause, null) + .setParameters(getAD_Client_ID(), getCostingMethod()) + .first(); + M_Product_Category_ID = retValue.getM_Product_Category_ID(); if (M_Product_Category_ID != 0) { log.saveError("CannotDeleteUsed", Msg.getElement(getCtx(), "M_Product_Category_ID")