BF [ 1874419 ] JDBC Statement not close in a finally block

organize imports
+ re-format
This commit is contained in:
tspc 2008-07-14 03:51:38 +00:00
parent fded7d0218
commit b4750dad0a
1 changed files with 291 additions and 288 deletions

View File

@ -15,101 +15,99 @@
*****************************************************************************/ *****************************************************************************/
package org.eevolution.model; package org.eevolution.model;
import java.util.*; import java.sql.PreparedStatement;
import java.sql.*; import java.sql.ResultSet;
import javax.swing.*; import java.sql.SQLException;
import javax.swing.tree.*; import java.util.Hashtable;
import java.util.logging.*; import java.util.Properties;
import java.util.logging.Level;
import org.compiere.util.*; import javax.swing.JOptionPane;
import org.compiere.model.*; import javax.swing.tree.DefaultMutableTreeNode;
import org.compiere.model.MProduct;
import org.compiere.model.X_M_Product;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
/** /**
* Manufacturing Order Line Model. * Manufacturing Order Line Model.
* <code> * <code>
* MPPProductBOMLine l = new MPPProductBOMLine(bom); * MPPProductBOMLine l = new MPPProductBOMLine(bom);
l.setM_Product_ID(wbl.getM_Product_ID()); l.setM_Product_ID(wbl.getM_Product_ID());
l.setQty(wbl.getQuantity());; l.setQty(wbl.getQuantity());;
l.save(); l.save();
* </code> * </code>
* @author Victor Perez www.e-evolution.com * @author Victor Perez www.e-evolution.com
* @version $Id: MOrderLine.java,v 1.22 2004/03/22 07:15:03 vpj-cd Exp $ * @version $Id: MOrderLine.java,v 1.22 2004/03/22 07:15:03 vpj-cd Exp $
*/ */
public class MPPProductBOMLine extends X_PP_Product_BOMLine public class MPPProductBOMLine extends X_PP_Product_BOMLine
{ {
static private int AD_Client_ID = 0; static private int AD_Client_ID = 0;
static Hashtable<Integer, Integer> tableproduct = new Hashtable<Integer, Integer>(); static Hashtable<Integer, Integer> tableproduct = new Hashtable<Integer, Integer>();
private static CLogger s_log = CLogger.getCLogger (MPPProductBOMLine.class); private static CLogger s_log = CLogger.getCLogger(MPPProductBOMLine.class);
/** /**
* Default Constructor * Default Constructor
* @param ctx context * @param ctx context
* @param PP_Product_BOMLine BOM line to load * @param PP_Product_BOMLine BOM line to load
* @param Transaction Line * @param Transaction Line
*/ */
public MPPProductBOMLine(Properties ctx, int PP_Product_BOMLine,String trxName) public MPPProductBOMLine(Properties ctx, int PP_Product_BOMLine, String trxName)
{ {
super (ctx, PP_Product_BOMLine,trxName); super(ctx, PP_Product_BOMLine, trxName);
} // MPPProductBOMLine } // MPPProductBOMLine
/** /**
* Parent Constructor. * Parent Constructor.
ol.setM_Product_ID(wbl.getM_Product_ID()); ol.setM_Product_ID(wbl.getM_Product_ID());
ol.setQtyOrdered(wbl.getQuantity()); ol.setQtyOrdered(wbl.getQuantity());
ol.setPrice(); ol.setPrice();
ol.setPriceActual(wbl.getPrice()); ol.setPriceActual(wbl.getPrice());
ol.setTax(); ol.setTax();
ol.save(); ol.save();
* @param order parent order * @param order parent order
*/ */
public MPPProductBOMLine(MPPProductBOM bom) public MPPProductBOMLine(MPPProductBOM bom)
{ {
super (bom.getCtx(), 0,bom.get_TableName()); super(bom.getCtx(), 0, bom.get_TableName());
if (bom.get_ID() == 0) if (bom.get_ID() == 0) throw new IllegalArgumentException("Header not saved");
throw new IllegalArgumentException("Header not saved"); setPP_Product_BOM_ID(bom.getPP_Product_BOM_ID()); // parent
setPP_Product_BOM_ID (bom.getPP_Product_BOM_ID()); // parent }
}
/** /**
* Load Constructor * Load Constructor
* @param ctx context * @param ctx context
* @param rs result set record * @param rs result set record
*/ */
public MPPProductBOMLine(Properties ctx, ResultSet rs,String trxName) public MPPProductBOMLine(Properties ctx, ResultSet rs, String trxName)
{ {
super (ctx, rs,trxName); super(ctx, rs, trxName);
} // MPPProductBOMLine } // MPPProductBOMLine
/** /**
* Set Defaults from BOM. * Set Defaults from BOM.
* Does not set Parent !! * Does not set Parent !!
* @param BOM BOM * @param BOM BOM
*/ */
public void setMPPProductBOM (MPPProductBOM bom) public void setMPPProductBOM(MPPProductBOM bom)
{ {
setClientOrg(bom); setClientOrg(bom);
} }
/** /**
* String Representation * String Representation
* @return info * @return info
*/ */
public String toString () public String toString()
{ {
StringBuffer sb = new StringBuffer ("MPPProductBOMLine[") StringBuffer sb = new StringBuffer("MPPProductBOMLine[").append(get_ID()).append("]");
.append(get_ID()) return sb.toString();
.append ("]");
return sb.toString ();
} }
/** /**
* String Description * String Description
* @return info * @return info
@ -117,266 +115,271 @@ public class MPPProductBOMLine extends X_PP_Product_BOMLine
public String getDescriptionText() public String getDescriptionText()
{ {
return super.getDescription(); return super.getDescription();
} // getDescriptionText } // getDescriptionText
/**
* get low level of a Product
* @param ID Product
* @return int low level
*/
public int getLowLevel(int M_Product_ID) throws Exception
{
AD_Client_ID = Integer.valueOf(getCtx().getProperty("#AD_Client_ID"));
tableproduct.clear(); //reset tableproduct cache
DefaultMutableTreeNode ibom = null;
tableproduct.put(M_Product_ID, 0); //insert parent into cache /**
ibom = iparent(M_Product_ID, 0); //start traversing tree * get low level of a Product
* @param ID Product
return ibom.getDepth(); * @return int low level
*/
public int getLowLevel(int M_Product_ID) throws Exception
{
AD_Client_ID = Integer.valueOf(getCtx().getProperty("#AD_Client_ID"));
tableproduct.clear(); //reset tableproduct cache
DefaultMutableTreeNode ibom = null;
tableproduct.put(M_Product_ID, 0); //insert parent into cache
ibom = iparent(M_Product_ID, 0); //start traversing tree
return ibom.getDepth();
} }
/** /**
* get a implotion the a prduct * get a implotion the a prduct
* @param ID Product * @param ID Product
* @param ID BOM * @param ID BOM
* @return DefaultMutableTreeNode Tree with all parent product * @return DefaultMutableTreeNode Tree with all parent product
*/ */
private DefaultMutableTreeNode parent(int M_Product_ID, int PP_Product_BOM_ID) private DefaultMutableTreeNode parent(int M_Product_ID, int PP_Product_BOM_ID)
{ {
DefaultMutableTreeNode parent = new DefaultMutableTreeNode(Integer.toString(M_Product_ID) +"|"+ Integer.toString(PP_Product_BOM_ID));
String sql = new String("SELECT pbom.PP_Product_BOM_ID FROM PP_Product_BOM pbom WHERE pbom.IsActive = 'Y' AND pbom.AD_Client_ID= ? AND pbom.M_Product_ID = ? ");
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, get_TrxName());
pstmt.setInt(1, Env.getAD_Client_ID(Env.getCtx()));
pstmt.setInt(2, M_Product_ID);
ResultSet rs = pstmt.executeQuery ();
while (rs.next())
{
DefaultMutableTreeNode bom = component(rs.getInt(1), M_Product_ID , parent);
if (bom != null)
{
parent.add(bom);
}
} DefaultMutableTreeNode parent = new DefaultMutableTreeNode(Integer.toString(M_Product_ID) + "|" + Integer.toString(PP_Product_BOM_ID));
rs.close();
pstmt.close();
return parent;
} String sql = new String(
catch (Exception e) "SELECT pbom.PP_Product_BOM_ID FROM PP_Product_BOM pbom WHERE pbom.IsActive = 'Y' AND pbom.AD_Client_ID= ? AND pbom.M_Product_ID = ? ");
{
s_log.log(Level.SEVERE ,"doIt - " + sql + e);
}
return parent;
}
/**
* get a explotion the a product
* @param ID BOM
* @param ID Product
* @param DefaultMutableTreeNode Tree BOM
* @return DefaultMutableTreeNode Tree with all components product
*/
private DefaultMutableTreeNode component(int M_Product_BOM_ID, int M_Product_ID , DefaultMutableTreeNode bom)
{
String sql = new String("SELECT pboml.M_Product_ID , pbom.Value , pboml.PP_Product_BOMLine_ID , pbom.PP_Product_BOM_ID FROM PP_Product_BOM pbom INNER JOIN PP_Product_BOMLine pboml ON (pbom.PP_Product_BOM_ID = pboml.PP_Product_BOM_ID) WHERE pbom.IsActive= 'Y' AND pboml.IsActive= 'Y' AND pbom.AD_Client_ID= ? AND pbom.PP_Product_BOM_ID = ? ");
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql,get_TrxName());
pstmt.setInt(1, Env.getAD_Client_ID(Env.getCtx()));
pstmt.setInt(2, M_Product_BOM_ID);
ResultSet rs = pstmt.executeQuery ();
while (rs.next())
{
if (M_Product_ID != rs.getInt(1))
{
bom.add(parent(rs.getInt(1), rs.getInt(4)));
}
else
{
JOptionPane.showMessageDialog(null,"Componet will be deactivated for BOM & Formula:" + rs.getString(2) + "(" + rs.getString(3) + ")", "Error Cycle BOM" , JOptionPane.ERROR_MESSAGE);
MPPProductBOMLine PP_Product_BOMLine = new MPPProductBOMLine(Env.getCtx(), rs.getInt(3),get_TrxName());
PP_Product_BOMLine.setIsActive(false);
PP_Product_BOMLine.save();
}
} PreparedStatement pstmt = null;
if (rs.getRow() == 0) ResultSet rs = null;
{ try
DefaultMutableTreeNode parent = new DefaultMutableTreeNode(Integer.toString(M_Product_ID) + "|0"); {
bom.add(parent); pstmt = DB.prepareStatement(sql, get_TrxName());
return bom; pstmt.setInt(1, Env.getAD_Client_ID(Env.getCtx()));
} pstmt.setInt(2, M_Product_ID);
rs = pstmt.executeQuery();
rs.close(); while (rs.next())
pstmt.close(); {
DefaultMutableTreeNode bom = component(rs.getInt(1), M_Product_ID, parent);
if (bom != null)
{
parent.add(bom);
}
} }
catch (Exception e) return parent;
{ }
s_log.log(Level.SEVERE ,"doIt - " + sql + e); catch (Exception e)
} {
s_log.log(Level.SEVERE, "doIt - " + sql + e);
return null;
}
/**
* get an implotion the product
* @param ID Product
* @param ID BOM
* @return DefaultMutableTreeNode Tree with all parent product
*/
private DefaultMutableTreeNode iparent(int M_Product_ID , int PP_Product_BOM_ID) throws Exception
{
DefaultMutableTreeNode parent = new DefaultMutableTreeNode(Integer.toString(M_Product_ID) +"|"+ Integer.toString(PP_Product_BOM_ID));
String sql = new String("SELECT pboml.PP_Product_BOMLine_ID FROM PP_Product_BOMLine pboml WHERE pboml.IsActive= 'Y' AND pboml.AD_Client_ID = ? AND pboml.M_Product_ID = ? ");
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, get_TrxName());
pstmt.setInt(1, AD_Client_ID);
pstmt.setInt(2, M_Product_ID);
ResultSet rs = pstmt.executeQuery ();
while (rs.next())
{
// If not the first bom line at this level
if(rs.getRow() > 1)
{
//need to reset tableproduct cache
tableproduct.clear();
tableproduct.put(M_Product_ID, PP_Product_BOM_ID); //insert parent into cache
}
DefaultMutableTreeNode bom = icomponent(rs.getInt(1), M_Product_ID , parent);
if (bom != null)
{
parent.add(bom);
}
} }
finally {
DB.close(rs, pstmt);
rs.close(); rs = null;
pstmt.close(); pstmt = null;
}
return parent; return parent;
}
} /**
catch (Exception e) * get a explotion the a product
{ * @param ID BOM
s_log.log(Level.SEVERE, "iparent - " + sql + e); * @param ID Product
} * @param DefaultMutableTreeNode Tree BOM
return parent; * @return DefaultMutableTreeNode Tree with all components product
} */
private DefaultMutableTreeNode component(int M_Product_BOM_ID, int M_Product_ID, DefaultMutableTreeNode bom)
/** {
* get an implotion the product
* @param ID Product
* @param ID BOM
* @return DefaultMutableTreeNode Tree with all parent product
*/
private DefaultMutableTreeNode icomponent(int PP_Product_BOMLine_ID, int M_Product_ID , DefaultMutableTreeNode bom) throws Exception
{
String sql = new String("SELECT pbom.M_Product_ID , pbom.Value , pbom.PP_Product_BOM_ID FROM PP_Product_BOMLine pboml INNER JOIN PP_Product_BOM pbom ON (pbom.PP_Product_BOM_ID = pboml.PP_Product_BOM_ID) WHERE pbom.IsActive= 'Y' AND pboml.IsActive= 'Y' AND pboml.AD_Client_ID =? AND pboml.PP_Product_BOMLine_ID = ? ");
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql,get_TrxName());
pstmt.setInt(1, AD_Client_ID);
pstmt.setInt(2, PP_Product_BOMLine_ID);
ResultSet rs = pstmt.executeQuery ();
while (rs.next())
{
if (M_Product_ID != rs.getInt(1))
{
//BOM Loop Error
if(!tableproduct(rs.getInt(1),rs.getInt(3)))
bom.add(iparent(rs.getInt(1),rs.getInt(3)));
else
{
s_log.saveError("Error", "Cycle BOM & Formula:" + rs.getString(2) + "(" + rs.getString(3) + ")");
throw new Exception(CLogger.retrieveError().toString());
}
}
else
{
//Child = Parent error
X_M_Product product = new X_M_Product(getCtx(), M_Product_ID, null);
s_log.saveError("Error", "Cycle BOM & Formula:" + rs.getString(2) + "(" + rs.getString(3) + ") - Component: "
+ product.getValue() + "(" + product.getM_Product_ID() + ")");
throw new Exception(CLogger.retrieveError().toString());
}
} String sql = new String(
rs.close(); "SELECT pboml.M_Product_ID , pbom.Value , pboml.PP_Product_BOMLine_ID , pbom.PP_Product_BOM_ID FROM"
pstmt.close(); + " PP_Product_BOM pbom INNER JOIN PP_Product_BOMLine pboml ON (pbom.PP_Product_BOM_ID = pboml.PP_Product_BOM_ID)"
+ " WHERE pbom.IsActive= 'Y' AND pboml.IsActive= 'Y' AND pbom.AD_Client_ID= ? AND pbom.PP_Product_BOM_ID = ? ");
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql, get_TrxName());
pstmt.setInt(1, Env.getAD_Client_ID(Env.getCtx()));
pstmt.setInt(2, M_Product_BOM_ID);
rs = pstmt.executeQuery();
while (rs.next())
{
if (M_Product_ID != rs.getInt(1))
{
bom.add(parent(rs.getInt(1), rs.getInt(4)));
}
else
{
JOptionPane.showMessageDialog(null, "Componet will be deactivated for BOM & Formula:" + rs.getString(2) + "(" + rs.getString(3)
+ ")", "Error Cycle BOM", JOptionPane.ERROR_MESSAGE);
MPPProductBOMLine PP_Product_BOMLine = new MPPProductBOMLine(Env.getCtx(), rs.getInt(3), get_TrxName());
PP_Product_BOMLine.setIsActive(false);
PP_Product_BOMLine.save();
}
}
if (rs.getRow() == 0)
{
DefaultMutableTreeNode parent = new DefaultMutableTreeNode(Integer.toString(M_Product_ID) + "|0");
bom.add(parent);
return bom;
}
}
catch (Exception e)
{
s_log.log(Level.SEVERE, "doIt - " + sql + e);
}
finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
}
catch (SQLException e) return null;
{ }
s_log.log(Level.SEVERE ,"doIt - " + sql + e);
} /**
* get an implotion the product
return null; * @param ID Product
} * @param ID BOM
* @return DefaultMutableTreeNode Tree with all parent product
/** */
* find a product in cache private DefaultMutableTreeNode iparent(int M_Product_ID, int PP_Product_BOM_ID) throws Exception
* @param ID Product {
* @param ID BOM
* @return true if product is found DefaultMutableTreeNode parent = new DefaultMutableTreeNode(Integer.toString(M_Product_ID) + "|" + Integer.toString(PP_Product_BOM_ID));
*/
private static boolean tableproduct(int M_Product_ID, int PP_Product_BOM_ID) String sql = new String(
{ "SELECT pboml.PP_Product_BOMLine_ID FROM PP_Product_BOMLine pboml"
Integer p = new Integer(M_Product_ID); + " WHERE pboml.IsActive= 'Y' AND pboml.AD_Client_ID = ? AND pboml.M_Product_ID = ? ");
Integer bom = new Integer(PP_Product_BOM_ID);
PreparedStatement pstmt = null;
if (tableproduct.containsKey(p)) ResultSet rs = null;
{ try
return true; {
} pstmt = DB.prepareStatement(sql, get_TrxName());
tableproduct.put(p , bom); pstmt.setInt(1, AD_Client_ID);
return false; pstmt.setInt(2, M_Product_ID);
rs = pstmt.executeQuery();
} while (rs.next())
{
/************************************************************************** // If not the first bom line at this level
* After Save if (rs.getRow() > 1)
* @param newRecord new {
* @return save //need to reset tableproduct cache
*/ tableproduct.clear();
protected boolean afterSave(boolean newRecord, boolean success) tableproduct.put(M_Product_ID, PP_Product_BOM_ID); //insert parent into cache
{ }
if (success) DefaultMutableTreeNode bom = icomponent(rs.getInt(1), M_Product_ID, parent);
if (bom != null)
{
parent.add(bom);
}
}
return parent;
}
catch (Exception e)
{
s_log.log(Level.SEVERE, "iparent - " + sql + e);
}
finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
return parent;
}
/**
* get an implotion the product
* @param ID Product
* @param ID BOM
* @return DefaultMutableTreeNode Tree with all parent product
*/
private DefaultMutableTreeNode icomponent(int PP_Product_BOMLine_ID, int M_Product_ID, DefaultMutableTreeNode bom) throws Exception
{
String sql = new String(
"SELECT pbom.M_Product_ID , pbom.Value , pbom.PP_Product_BOM_ID FROM PP_Product_BOMLine pboml"
+ "INNER JOIN PP_Product_BOM pbom ON (pbom.PP_Product_BOM_ID = pboml.PP_Product_BOM_ID)"
+ "WHERE pbom.IsActive= 'Y' AND pboml.IsActive= 'Y' AND pboml.AD_Client_ID =? AND pboml.PP_Product_BOMLine_ID = ? ");
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql, get_TrxName());
pstmt.setInt(1, AD_Client_ID);
pstmt.setInt(2, PP_Product_BOMLine_ID);
rs = pstmt.executeQuery();
while (rs.next())
{
if (M_Product_ID != rs.getInt(1))
{
//BOM Loop Error
if (!tableproduct(rs.getInt(1), rs.getInt(3)))
bom.add(iparent(rs.getInt(1), rs.getInt(3)));
else
{
s_log.saveError("Error", "Cycle BOM & Formula:" + rs.getString(2) + "(" + rs.getString(3) + ")");
throw new Exception(CLogger.retrieveError().toString());
}
}
else
{
//Child = Parent error
X_M_Product product = new X_M_Product(getCtx(), M_Product_ID, null);
s_log.saveError("Error", "Cycle BOM & Formula:" + rs.getString(2) + "(" + rs.getString(3) + ") - Component: "
+ product.getValue() + "(" + product.getM_Product_ID() + ")");
throw new Exception(CLogger.retrieveError().toString());
}
}
}
catch (SQLException e)
{
s_log.log(Level.SEVERE, "doIt - " + sql + e);
}
finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
return null;
}
/**
* find a product in cache
* @param ID Product
* @param ID BOM
* @return true if product is found
*/
private static boolean tableproduct(int M_Product_ID, int PP_Product_BOM_ID)
{
Integer p = new Integer(M_Product_ID);
Integer bom = new Integer(PP_Product_BOM_ID);
if (tableproduct.containsKey(p))
{
return true;
}
tableproduct.put(p, bom);
return false;
}
/**************************************************************************
* After Save
* @param newRecord new
* @return save
*/
protected boolean afterSave(boolean newRecord, boolean success)
{
if (success)
{ {
MProduct product = new MProduct(getCtx(), getM_Product_ID(), get_TrxName()); MProduct product = new MProduct(getCtx(), getM_Product_ID(), get_TrxName());
int lowlevel = 0; int lowlevel = 0;
try try
{ {
lowlevel = getLowLevel(getM_Product_ID()); lowlevel = getLowLevel(getM_Product_ID());
} }
catch (Exception e) catch (Exception e)
{ {
log.saveError("Error", Msg.parseTranslation(getCtx(), e.getMessage()), false); log.saveError("Error", Msg.parseTranslation(getCtx(), e.getMessage()), false);
return false; return false;