MProduct:

* use new Query API
* MProduct.get(Properties ctx, int M_Product_ID) should return null if M_Product_ID is <= 0
This commit is contained in:
teo_sarca 2008-09-17 06:21:16 +00:00
parent 1cf32f9c6e
commit ec95527738
1 changed files with 45 additions and 106 deletions

View File

@ -17,14 +17,11 @@
package org.compiere.model; package org.compiere.model;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.util.ArrayList; 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.DB; import org.compiere.util.DB;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.compiere.util.Msg; import org.compiere.util.Msg;
@ -53,17 +50,25 @@ public class MProduct extends X_M_Product
* Get MProduct from Cache * Get MProduct from Cache
* @param ctx context * @param ctx context
* @param M_Product_ID id * @param M_Product_ID id
* @return MProduct * @return MProduct or null
*/ */
public static MProduct get (Properties ctx, int M_Product_ID) public static MProduct get (Properties ctx, int M_Product_ID)
{ {
if (M_Product_ID <= 0)
{
return null;
}
Integer key = new Integer (M_Product_ID); Integer key = new Integer (M_Product_ID);
MProduct retValue = (MProduct) s_cache.get (key); MProduct retValue = (MProduct) s_cache.get (key);
if (retValue != null) if (retValue != null)
{
return retValue; return retValue;
}
retValue = new MProduct (ctx, M_Product_ID, null); retValue = new MProduct (ctx, M_Product_ID, null);
if (retValue.get_ID () != 0) if (retValue.get_ID () != 0)
{
s_cache.put (key, retValue); s_cache.put (key, retValue);
}
return retValue; return retValue;
} // get } // get
@ -76,40 +81,11 @@ public class MProduct extends X_M_Product
*/ */
public static MProduct[] get (Properties ctx, String whereClause, String trxName) public static MProduct[] get (Properties ctx, String whereClause, String trxName)
{ {
String sql = "SELECT * FROM M_Product";
if (whereClause != null && whereClause.length() > 0)
sql += " WHERE AD_Client_ID=? AND " + whereClause;
ArrayList<MProduct> list = new ArrayList<MProduct>();
int AD_Client_ID = Env.getAD_Client_ID(ctx); int AD_Client_ID = Env.getAD_Client_ID(ctx);
PreparedStatement pstmt = null; List<MProduct> list = new Query(ctx, Table_Name, "AD_Client_ID=? AND "+whereClause, trxName)
try .setParameters(new Object[]{AD_Client_ID})
{ .list();
pstmt = DB.prepareStatement (sql, trxName); return list.toArray(new MProduct[list.size()]);
pstmt.setInt(1, AD_Client_ID);
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
list.add (new MProduct (ctx, rs, 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;
}
MProduct[] retValue = new MProduct[list.size ()];
list.toArray (retValue);
return retValue;
} // get } // get
@ -127,9 +103,6 @@ public class MProduct extends X_M_Product
/** Cache */ /** Cache */
private static CCache<Integer,MProduct> s_cache = new CCache<Integer,MProduct>("M_Product", 40, 5); // 5 minutes private static CCache<Integer,MProduct> s_cache = new CCache<Integer,MProduct>("M_Product", 40, 5); // 5 minutes
/** Static Logger */
private static CLogger s_log = CLogger.getCLogger (MProduct.class);
/************************************************************************** /**************************************************************************
* Standard Constructor * Standard Constructor
@ -452,6 +425,7 @@ public class MProduct extends X_M_Product
* Product is an Item and Stocked * Product is an Item and Stocked
* @return true if stocked and item * @return true if stocked and item
*/ */
@Override
public boolean isStocked () public boolean isStocked ()
{ {
return super.isStocked() && isItem(); return super.isStocked() && isItem();
@ -489,39 +463,12 @@ public class MProduct extends X_M_Product
if (m_downloads != null && !requery) if (m_downloads != null && !requery)
return m_downloads; return m_downloads;
// //
ArrayList<MProductDownload> list = new ArrayList<MProductDownload>(); List<MProductDownload> list = new Query(getCtx(), MProductDownload.Table_Name, "M_Product_ID=?", get_TrxName())
String sql = "SELECT * FROM M_ProductDownload " .setOnlyActiveRecords(true)
+ "WHERE M_Product_ID=? AND IsActive='Y' ORDER BY Name"; .setOrderBy(MProductDownload.COLUMNNAME_Name)
// .setParameters(new Object[]{get_ID()})
PreparedStatement pstmt = null; .list();
try m_downloads = list.toArray(new MProductDownload[list.size()]);
{
pstmt = DB.prepareStatement (sql, get_TrxName());
pstmt.setInt (1, getM_Product_ID());
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
list.add (new MProductDownload (getCtx(), rs, get_TrxName()));
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;
}
//
m_downloads = new MProductDownload[list.size ()];
list.toArray (m_downloads);
return m_downloads; return m_downloads;
} // getProductDownloads } // getProductDownloads
@ -531,14 +478,10 @@ public class MProduct extends X_M_Product
*/ */
public boolean hasDownloads() public boolean hasDownloads()
{ {
getProductDownloads(false); return getProductDownloads(false).length > 0;
return m_downloads != null && m_downloads.length > 0;
} // hasDownloads } // hasDownloads
/** @Override
* String Representation
* @return info
*/
public String toString() public String toString()
{ {
StringBuffer sb = new StringBuffer("MProduct["); StringBuffer sb = new StringBuffer("MProduct[");
@ -547,12 +490,7 @@ public class MProduct extends X_M_Product
return sb.toString(); return sb.toString();
} // toString } // toString
@Override
/**
* Before Save
* @param newRecord new
* @return true
*/
protected boolean beforeSave (boolean newRecord) protected boolean beforeSave (boolean newRecord)
{ {
// Check Storage // Check Storage
@ -609,26 +547,34 @@ public class MProduct extends X_M_Product
* HasInventoryOrCost * HasInventoryOrCost
* @return true if it has Inventory or Cost * @return true if it has Inventory or Cost
*/ */
protected boolean hasInventoryOrCost () { protected boolean hasInventoryOrCost ()
{
//check if it has transactions //check if it has transactions
int trans = DB.getSQLValue(null,"SELECT COUNT(*) FROM M_Transaction WHERE M_Product_ID=? AND IsActive='Y'", getM_Product_ID()); boolean hasTrx = new Query(getCtx(), MTransaction.Table_Name,
if (trans > 0) MTransaction.COLUMNNAME_M_Product_ID+"=?", get_TrxName())
.setOnlyActiveRecords(true)
.setParameters(new Object[]{get_ID()})
.match();
if (hasTrx)
{
return true; return true;
}
//check if it has cost //check if it has cost
int cost = DB.getSQLValue(null,"SELECT COUNT(*) FROM M_CostDetail WHERE M_Product_ID=? AND IsActive='Y'", getM_Product_ID()); boolean hasCosts = new Query(getCtx(), MCostDetail.Table_Name,
if (cost > 0) MCostDetail.COLUMNNAME_M_Product_ID+"=?", get_TrxName())
.setOnlyActiveRecords(true)
.setParameters(new Object[]{get_ID()})
.match();
if (hasCosts)
{
return true; return true;
}
return false; return false;
} }
/** @Override
* After Save
* @param newRecord new
* @param success success
* @return success
*/
protected boolean afterSave (boolean newRecord, boolean success) protected boolean afterSave (boolean newRecord, boolean success)
{ {
if (!success) if (!success)
@ -676,10 +622,7 @@ public class MProduct extends X_M_Product
return success; return success;
} // afterSave } // afterSave
/** @Override
* Before Delete
* @return true if it can be deleted
*/
protected boolean beforeDelete () protected boolean beforeDelete ()
{ {
// Check Storage // Check Storage
@ -742,11 +685,7 @@ public class MProduct extends X_M_Product
return delete_Accounting("M_Product_Acct"); return delete_Accounting("M_Product_Acct");
} // beforeDelete } // beforeDelete
/** @Override
* After Delete
* @param success
* @return deleted
*/
protected boolean afterDelete (boolean success) protected boolean afterDelete (boolean success)
{ {
if (success) if (success)