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
This commit is contained in:
Redhuan D. Oon 2010-03-01 00:42:03 +00:00
parent a3cf7e8d37
commit c0d989b240
1 changed files with 26 additions and 128 deletions

View File

@ -16,13 +16,10 @@
*****************************************************************************/ *****************************************************************************/
package org.compiere.model; package org.compiere.model;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level;
import org.compiere.util.CCache; import org.compiere.util.CCache;
import org.compiere.util.CLogger; import org.compiere.util.CLogger;
@ -38,6 +35,8 @@ import org.compiere.util.Msg;
* @author Teo Sarca, www.arhipac.ro * @author Teo Sarca, www.arhipac.ro
* <li>BF [ 2664529 ] More then one Labor/Burden//Overhead is not allowed * <li>BF [ 2664529 ] More then one Labor/Burden//Overhead is not allowed
* <li>BF [ 2667470 ] MCostElement.getMaterialCostElement should check only material * <li>BF [ 2667470 ] MCostElement.getMaterialCostElement should check only material
* @author red1
* <li>FR: [ 2214883 ] Remove SQL code and Replace for Query -- JUnit tested
*/ */
public class MCostElement extends X_M_CostElement 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=?"; final String whereClause = "AD_Client_ID=? AND CostingMethod=? AND CostElementType=?";
MCostElement retValue = new Query(po.getCtx(), Table_Name, whereClause, po.get_TrxName()) 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") .setOrderBy("AD_Org_ID")
.firstOnly(); .firstOnly();
if (retValue != null) if (retValue != null)
@ -92,37 +91,11 @@ public class MCostElement extends X_M_CostElement
*/ */
public static MCostElement getMaterialCostElement(Properties ctx, String CostingMethod) public static MCostElement getMaterialCostElement(Properties ctx, String CostingMethod)
{ {
MCostElement retValue = null; final String whereClause = "AD_Client_ID=? AND CostingMethod=?";
String sql = "SELECT * FROM M_CostElement WHERE AD_Client_ID=? AND CostingMethod=? ORDER BY AD_Org_ID"; MCostElement retValue = new Query(ctx, I_M_CostElement.Table_Name, whereClause, null)
PreparedStatement pstmt = null; .setParameters(Env.getAD_Client_ID(ctx), CostingMethod)
try .setOrderBy(I_M_CostElement.COLUMNNAME_AD_Org_ID)
{ .firstOnly();
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;
}
return retValue; return retValue;
} // getMaterialCostElement } // getMaterialCostElement
@ -147,36 +120,11 @@ public class MCostElement extends X_M_CostElement
*/ */
public static MCostElement[] getCostingMethods (PO po) public static MCostElement[] getCostingMethods (PO po)
{ {
ArrayList<MCostElement> list = new ArrayList<MCostElement>(); final String whereClause ="AD_Client_ID=? AND CostElementType='M' AND CostingMethod IS NOT NULL";
String sql = "SELECT * FROM M_CostElement " List<MCostElement> list = new Query(po.getCtx(), I_M_CostElement.Table_Name, whereClause, po.get_TrxName())
+ "WHERE AD_Client_ID=?" .setParameters(po.getAD_Client_ID())
+ " AND IsActive='Y' AND CostElementType='M' AND CostingMethod IS NOT NULL"; .setOnlyActiveRecords(true)
PreparedStatement pstmt = null; .list();
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;
}
// //
MCostElement[] retValue = new MCostElement[list.size ()]; MCostElement[] retValue = new MCostElement[list.size ()];
list.toArray (retValue); list.toArray (retValue);
@ -191,36 +139,11 @@ public class MCostElement extends X_M_CostElement
*/ */
public static MCostElement[] getNonCostingMethods (PO po) public static MCostElement[] getNonCostingMethods (PO po)
{ {
ArrayList<MCostElement> list = new ArrayList<MCostElement>(); final String whereClause = "AD_Client_ID=? AND CostingMethod IS NULL";
String sql = "SELECT * FROM M_CostElement " List<MCostElement>list = new Query(po.getCtx(),MCostElement.Table_Name, whereClause, po.get_TrxName())
+ "WHERE AD_Client_ID=?" .setParameters(po.getAD_Client_ID())
+ " AND IsActive='Y' AND CostingMethod IS NULL"; .setOnlyActiveRecords(true)
PreparedStatement pstmt = null; .list();
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;
}
// //
MCostElement[] retValue = new MCostElement[list.size ()]; MCostElement[] retValue = new MCostElement[list.size ()];
list.toArray (retValue); 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_Client_ID = Env.getAD_Client_ID(ctx);
int AD_Org_ID = 0; // Org is always ZERO - see beforeSave 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<MCostElement> list = new Query(ctx, Table_Name, whereClause, trxName) List<MCostElement> 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(); .list();
MCostElement[] retValue = new MCostElement[list.size()]; MCostElement[] retValue = new MCostElement[list.size()];
list.toArray(retValue); list.toArray(retValue);
@ -279,7 +202,7 @@ public class MCostElement extends X_M_CostElement
return new Query(ctx, Table_Name, whereClause, null) return new Query(ctx, Table_Name, whereClause, null)
.setClient_ID() .setClient_ID()
.setOnlyActiveRecords(true) .setOnlyActiveRecords(true)
.setParameters(new Object[]{CostingMethod}) .setParameters(CostingMethod)
.list(); .list();
} }
@ -393,37 +316,12 @@ public class MCostElement extends X_M_CostElement
} }
// Costing Methods on PC level // 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; int M_Product_Category_ID = 0;
PreparedStatement pstmt = null; final String whereClause ="AD_Client_ID=? AND CostingMethod=?";
try MProductCategory retValue = new Query(getCtx(), I_M_Product_Category_Acct.Table_Name, whereClause, null)
{ .setParameters(getAD_Client_ID(), getCostingMethod())
pstmt = DB.prepareStatement (sql, null); .first();
pstmt.setInt (1, getAD_Client_ID()); M_Product_Category_ID = retValue.getM_Product_Category_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;
}
if (M_Product_Category_ID != 0) if (M_Product_Category_ID != 0)
{ {
log.saveError("CannotDeleteUsed", Msg.getElement(getCtx(), "M_Product_Category_ID") log.saveError("CannotDeleteUsed", Msg.getElement(getCtx(), "M_Product_Category_ID")