From b4f4756a4ec919716809d00e1252ff54f54b877b Mon Sep 17 00:00:00 2001 From: teo_sarca Date: Fri, 8 Aug 2008 13:41:03 +0000 Subject: [PATCH] MPPProductBOM, MPPProductBOMLine, CalculateLowLevel, PP_Product_BOM_Check: BF [ 1874419 ] JDBC Statement not close in a finally block BF [ 2041456 ] Don't use Env.getCtx in base project * added cache support (MPPProductBOM.get()) * refactored a lot of code * removed static and pseudo-static methods and properties (like getLines, getLowLevel) * removed deprecated and not used code (too much code to maintain) * better exception handling support --- .../org/eevolution/model/MPPOrderBOMLine.java | 24 +- .../org/eevolution/model/MPPProductBOM.java | 355 +++++------------- .../eevolution/model/MPPProductBOMLine.java | 256 ++++--------- .../eevolution/process/CalculateLowLevel.java | 74 ++-- base/src/org/eevolution/process/MRP.java | 2 +- .../process/PP_Product_BOM_Check.java | 81 +--- 6 files changed, 218 insertions(+), 574 deletions(-) diff --git a/base/src/org/eevolution/model/MPPOrderBOMLine.java b/base/src/org/eevolution/model/MPPOrderBOMLine.java index fb1d1130d2..9230815469 100644 --- a/base/src/org/eevolution/model/MPPOrderBOMLine.java +++ b/base/src/org/eevolution/model/MPPOrderBOMLine.java @@ -31,6 +31,9 @@ import org.compiere.util.Env; */ public class MPPOrderBOMLine extends X_PP_Order_BOMLine { + private static final long serialVersionUID = 1L; + + /** * Default Constructor * @param ctx context @@ -92,26 +95,23 @@ public class MPPOrderBOMLine extends X_PP_Order_BOMLine return success; //Qty Ordered to Phantom BigDecimal QtyOrdered = getQtyRequiered(); - System.out.println(" Padre Product" + getM_Product_ID() + " getQtyBatch" + getQtyBatch() + " getQtyRequiered" + getQtyRequiered() + " QtyScrap" + getQtyScrap()); + log.fine(" Parent Product" + getM_Product_ID() + " getQtyBatch" + getQtyBatch() + " getQtyRequiered" + getQtyRequiered() + " QtyScrap" + getQtyScrap()); //Phantom if(getComponentType().equals(MPPProductBOMLine.COMPONENTTYPE_Phantom)) { - int PP_Product_BOM_ID = MPPProductBOM.getBOMSearchKey(getM_Product_ID()); - if (PP_Product_BOM_ID==0) + int PP_Product_BOM_ID = MPPProductBOM.getBOMSearchKey(getCtx(), getM_Product_ID()); + if (PP_Product_BOM_ID <= 0) return true; - MPPProductBOM bom = new MPPProductBOM(getCtx(),PP_Product_BOM_ID,get_TrxName()); - if (bom!= null) + MPPProductBOM bom = MPPProductBOM.get(getCtx(), PP_Product_BOM_ID); + if (bom != null) { MPPProductBOMLine[] PP_Product_BOMline = bom.getLines(); - - if (PP_Product_BOMline == null) - return true; for(int i = 0 ; i < PP_Product_BOMline.length ; i++ ) { - MPPOrderBOMLine PP_Order_BOMLine = new MPPOrderBOMLine(getCtx(),0,get_TrxName()); - MProduct product = new MProduct(getCtx(),PP_Product_BOMline[i].getM_Product_ID(),get_TrxName()); + MPPOrderBOMLine PP_Order_BOMLine = new MPPOrderBOMLine(getCtx(), 0, get_TrxName()); + MProduct product = MProduct.get(getCtx(),PP_Product_BOMline[i].getM_Product_ID()); PP_Order_BOMLine.setDescription(PP_Product_BOMline[i].getDescription()); PP_Order_BOMLine.setHelp(PP_Product_BOMline[i].getHelp()); PP_Order_BOMLine.setM_ChangeNotice_ID(PP_Product_BOMline[i].getM_ChangeNotice_ID()); @@ -137,7 +137,7 @@ public class MPPOrderBOMLine extends X_PP_Order_BOMLine if (PP_Order_BOMLine.isQtyPercentage()) { BigDecimal qty = PP_Order_BOMLine.getQtyBatch().multiply(QtyOrdered); - System.out.println("product:"+product.getName() +" Qty:"+qty + " QtyOrdered:"+ QtyOrdered + " PP_Order_BOMLine.getQtyBatch():" + PP_Order_BOMLine.getQtyBatch()); + log.fine("product:"+product.getName() +" Qty:"+qty + " QtyOrdered:"+ QtyOrdered + " PP_Order_BOMLine.getQtyBatch():" + PP_Order_BOMLine.getQtyBatch()); if(PP_Order_BOMLine.getComponentType().equals(COMPONENTTYPE_Packing)) PP_Order_BOMLine.setQtyRequiered(qty.divide(new BigDecimal(100),8,BigDecimal.ROUND_UP)); else if (PP_Order_BOMLine.getComponentType().equals(COMPONENTTYPE_Component) || PP_Order_BOMLine.getComponentType().equals(COMPONENTTYPE_Phantom)) @@ -166,7 +166,7 @@ public class MPPOrderBOMLine extends X_PP_Order_BOMLine PP_Order_BOMLine.setQtyRequiered(PP_Order_BOMLine.getQtyRequiered().divide( Env.ONE.subtract(Scrap) , 8 ,BigDecimal.ROUND_HALF_UP )); } System.out.println("Cantidad Requerida" + PP_Order_BOMLine.getQtyRequiered()); - PP_Order_BOMLine.save(); + PP_Order_BOMLine.saveEx(); } } diff --git a/base/src/org/eevolution/model/MPPProductBOM.java b/base/src/org/eevolution/model/MPPProductBOM.java index f991241fd4..2be42ea40c 100644 --- a/base/src/org/eevolution/model/MPPProductBOM.java +++ b/base/src/org/eevolution/model/MPPProductBOM.java @@ -16,7 +16,6 @@ //package org.compiere.mfg.model; package org.eevolution.model; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Timestamp; import java.util.ArrayList; @@ -26,24 +25,51 @@ import java.util.logging.Level; import org.compiere.model.MProduct; import org.compiere.model.PO; +import org.compiere.model.Query; +import org.compiere.util.CCache; import org.compiere.util.CLogger; import org.compiere.util.DB; import org.compiere.util.Env; /** - * Order Model. - * Please do not set DocStatus and C_DocType_ID directly. - * They are set in the process() method. - * Use DocAction and C_DocTypeTarget_ID instead. + * PP Product BOM Model. * * @author Victor Perez www.e-evolution.com * @version $Id: MOrder.java,v 1.40 2004/04/13 04:19:30 vpj-cd Exp $ + * + * @author Teo Sarca, SC ARHIPAC SERVICE SRL */ public class MPPProductBOM extends X_PP_Product_BOM { + private static final long serialVersionUID = 1L; - - private static CLogger log = CLogger.getCLogger(MPPProductBOM.class); + /** Static Logger */ + private static CLogger log = CLogger.getCLogger(MPPProductBOM.class); + /** Cache */ + private static CCache s_cache = new CCache(Table_Name, 40, 5); // 5 minutes + + /** + * Load/Get Product BOM by ID (cached) + * @param ctx + * @param PP_Product_BOM_ID + * @return product bom + */ + public static MPPProductBOM get(Properties ctx, int PP_Product_BOM_ID) + { + if (PP_Product_BOM_ID <= 0) + return null; + MPPProductBOM bom = s_cache.get(PP_Product_BOM_ID); + if (bom != null) + return bom; + bom = new MPPProductBOM(ctx, PP_Product_BOM_ID, null); + if (bom.get_ID() == PP_Product_BOM_ID) { + s_cache.put(PP_Product_BOM_ID, bom); + } + else { + bom = null; + } + return bom; + } /** * Default Constructor @@ -161,13 +187,12 @@ public class MPPProductBOM extends X_PP_Product_BOM * BUG #104 * @param lines */ - - List lines = null; - - public void setLines(List lines) { + private void setLines(List lines) { this.lines = lines; } + private List lines = null; + /** * BUG #? - Does not persist objects! * @param ctx @@ -181,7 +206,7 @@ public class MPPProductBOM extends X_PP_Product_BOM newBom.setDocumentNo(null); if (copyLines) { - List newLines = new ArrayList(); + List newLines = new ArrayList(); MPPProductBOMLine[] fromLines = from.getLines(); for (int i = 0; i < fromLines.length; i++) { MPPProductBOMLine line = new MPPProductBOMLine(ctx, 0,null); @@ -206,7 +231,6 @@ public class MPPProductBOM extends X_PP_Product_BOM return sb.toString (); } // toString - /** * Get BOM for Product * @param product product @@ -214,32 +238,25 @@ public class MPPProductBOM extends X_PP_Product_BOM * @param trxName Transaction Name * @return BOM */ - public static MPPProductBOM get(MProduct product , int ad_org_id, String trxName) + public static MPPProductBOM get(MProduct product, int ad_org_id, String trxName) { - MPPProductBOM bom = null; - Properties ctx = product.getCtx(); // find Default BOM in Product Data Planning if (ad_org_id > 0 ) { MPPProductPlanning pp = MPPProductPlanning.get(ctx, product.getAD_Client_ID(),ad_org_id, product.getM_Product_ID(), trxName); - - if(pp!= null && pp.getPP_Product_BOM_ID() > 0 ) + if(pp != null && pp.getPP_Product_BOM_ID() > 0 ) { bom = new MPPProductBOM(ctx, pp.getPP_Product_BOM_ID(),trxName); } } - - if (bom == null) - { + if (bom == null) + { //Find BOM with Default Logic where product = bom product and bom value = value - String sql = "SELECT PP_Product_BOM_ID FROM PP_Product_BOM WHERE M_Product_ID=? AND Value=?"; - int m_PP_Product_BOM_ID = DB.getSQLValue(trxName, sql, product.getM_Product_ID(), product.getValue()); - bom = new MPPProductBOM(product.getCtx(), m_PP_Product_BOM_ID ,product.get_TableName()); + bom = getDefault(product, trxName); } - return bom; } // getBOM @@ -252,46 +269,23 @@ public class MPPProductBOM extends X_PP_Product_BOM * @param trxName Transaction Name * @return BOM */ - public static MPPProductBOM get(MProduct product , int ad_org_id, Timestamp valid , String trxName) + public static MPPProductBOM get(MProduct product, int ad_org_id, Timestamp valid, String trxName) { - - MPPProductBOM bom = null; - - Properties ctx = product.getCtx(); - // find Default BOM in Product Data Planning - if (ad_org_id > 0 ) - { - MPPProductPlanning pp = MPPProductPlanning.get(ctx, product.getAD_Client_ID() ,ad_org_id, product.getM_Product_ID(), trxName); - - if(pp!= null && pp.getPP_Product_BOM_ID() > 0 ) - { - bom = new MPPProductBOM(ctx, pp.getPP_Product_BOM_ID(),trxName); - } - } - - if (bom == null) - { - //Find BOM with Default Logic where product = bom product and bom value = value - String sql = "SELECT PP_Product_BOM_ID FROM PP_Product_BOM WHERE M_Product_ID=? AND Value=?"; - int m_PP_Product_BOM_ID = DB.getSQLValue(trxName, sql, product.getM_Product_ID(), product.getValue()); - bom = new MPPProductBOM(product.getCtx(), m_PP_Product_BOM_ID ,product.get_TableName()); - } - + MPPProductBOM bom = get(product, ad_org_id, trxName); + // + // Validate date if (bom != null) { boolean ValidFromBOM = true; boolean ValidToBOM = true; - if (bom.getValidFrom() != null) { ValidFromBOM = valid.compareTo(bom.getValidFrom()) >= 0 ? true : false; } - if (bom.getValidTo() != null ) { ValidToBOM = valid.compareTo(bom.getValidTo()) <= 0 ? true : false; } - if(ValidFromBOM && ValidToBOM) { return bom; @@ -304,236 +298,75 @@ public class MPPProductBOM extends X_PP_Product_BOM } // getBOM - - /************************************************************************** - * Get BOM lines - * @return Array of BOM Lines - */ - public MPPProductBOMLine[] getLines() - { - return getLines (getPP_Product_BOM_ID()); - } // getLines - - - /************************************************************************** - * Get BOM lines - * @param valid date to validate - * @return Array of BOM Lines - */ - public MPPProductBOMLine[] getLines(Timestamp valid) - { - return getLines (getPP_Product_BOM_ID(), valid); - } // getLines - /** * Get BOM Lines valid date for Product BOM - * @param pp_product_bom_id BOM ID * @param valid Date to Validate * @return BOM Lines */ - public MPPProductBOMLine[] getLines (int pp_product_bom_id , Timestamp valid) + public MPPProductBOMLine[] getLines (Timestamp valid) { - - ArrayList list = new ArrayList (); - StringBuffer sql = new StringBuffer("SELECT * FROM PP_Product_BOMLine WHERE PP_Product_BOM_ID=? "); - PreparedStatement pstmt = null; - try - { - pstmt = DB.prepareStatement(sql.toString(), get_TrxName()); - pstmt.setInt(1, pp_product_bom_id); - ResultSet rs = pstmt.executeQuery(); - while (rs.next()) - { - MPPProductBOMLine bl = new MPPProductBOMLine(getCtx(), rs, get_TrxName()); - boolean ValidFromBOMLine = true; - boolean ValidToBOMLine = true; - if (bl.getValidFrom() != null) - { - ValidFromBOMLine = valid.compareTo(bl.getValidFrom()) >= 0 ? true : false; - } - - if (bl.getValidTo() != null ) - { - ValidToBOMLine = valid.compareTo(bl.getValidTo()) <= 0 ? true : false; - } - if(ValidFromBOMLine && ValidToBOMLine) - { - list.add(bl); - } + MPPProductBOMLine[] bomlines = getLines(); // All BOM Lines + List list = new ArrayList(); // Selected BOM Lines Only + for (MPPProductBOMLine bl : bomlines) { + boolean ValidFromBOMLine = true; + boolean ValidToBOMLine = true; + if (bl.getValidFrom() != null) + { + ValidFromBOMLine = valid.compareTo(bl.getValidFrom()) >= 0 ? true : false; } - rs.close(); - pstmt.close(); - pstmt = null; - } - catch (Exception e) - { - log.log(Level.SEVERE, sql.toString(), e); - } - finally - { - try - { - if (pstmt != null) - pstmt.close (); - } - catch (Exception e) - {} - pstmt = null; + if (bl.getValidTo() != null ) + { + ValidToBOMLine = valid.compareTo(bl.getValidTo()) <= 0 ? true : false; + } + if(ValidFromBOMLine && ValidToBOMLine) + { + list.add(bl); + } } // - MPPProductBOMLine[] lines = new MPPProductBOMLine[list.size ()]; - list.toArray (lines); - return lines; - + return list.toArray(new MPPProductBOMLine[list.size()]); } // getLines /** * Get BOM Lines for Product BOM - * @param pp_product_bom_id BOM ID * @return BOM Lines */ - public MPPProductBOMLine[] getLines (int PP_Product_BOM_ID) + public MPPProductBOMLine[] getLines() { - - ArrayList list = new ArrayList (); - StringBuffer sql = new StringBuffer("SELECT * FROM PP_Product_BOMLine WHERE PP_Product_BOM_ID=? "); - PreparedStatement pstmt = null; - try - { - pstmt = DB.prepareStatement(sql.toString(), get_TrxName()); - pstmt.setInt(1, PP_Product_BOM_ID); - ResultSet rs = pstmt.executeQuery(); - while (rs.next()) - { - MPPProductBOMLine bl = new MPPProductBOMLine(getCtx(), rs, get_TrxName()); - list.add(bl); - } - rs.close(); - pstmt.close(); - pstmt = null; - } - catch (Exception e) - { - log.log(Level.SEVERE, sql.toString(), e); - } - finally - { - try - { - if (pstmt != null) - pstmt.close (); - } - catch (Exception e) - {} - pstmt = null; - } - - MPPProductBOMLine[] lines = new MPPProductBOMLine[list.size ()]; - list.toArray (lines); - return lines; - + // TODO: add caching support + String whereClause = MPPProductBOMLine.COLUMNNAME_PP_Product_BOM_ID+"=?"; + List list = new Query(getCtx(), MPPProductBOMLine.Table_Name, whereClause, get_TrxName()) + .setParameters(new Object[]{getPP_Product_BOM_ID()}) + .list(); + return list.toArray(new MPPProductBOMLine[list.size()]); } // getLines - - - /** - * Create new Order by copying - * @param ctx context - * @param C_Order_ID invoice - * @param dateDoc date of the document date - * @return Order + * Get PP_Product_BOM_ID for given M_Product_ID + * @param M_Product_ID + * @return PP_Product_BOM_ID */ - public static MPPProductBOM copyFrom (Properties ctx, int PP_Product_BOM_ID, Timestamp dateDoc) + public static int getBOMSearchKey(Properties ctx, int M_Product_ID) { - MPPProductBOM from = new MPPProductBOM (ctx, PP_Product_BOM_ID,"PP_Product_BOM"); - if (from.getPP_Product_BOM_ID() == 0) - throw new IllegalArgumentException ("From Invoice not found C_Invoice_ID=" + PP_Product_BOM_ID); - // - MPPProductBOM to = new MPPProductBOM (ctx, 0,"PP_Product_BOM"); - PO.copyValues(from, to, from.getAD_Client_ID(), from.getAD_Org_ID()); - to.setPP_Product_BOM_ID(0); - //to.set_ValueNoCheck ("DocumentNo", null); - // - //to.setDocStatus (DOCSTATUS_Drafted); // Draft - //to.setDocAction(DOCACTION_Prepare); - //to.setC_DocTypeTarget_ID(to.getC_DocType_ID()); - //to.setC_DocType_ID(0); - //to.setIsSelected (false); - //to.setDateOrdered (dateDoc); - //to.setDateAcct (dateDoc); - //to.setDatePromised (dateDoc); - // - //to.setIsApproved (false); - //to.setIsCreditApproved(false); - //to.setC_Payment_ID(0); - //to.setC_CashLine_ID(0); - // Amounts are updated by trigger when adding lines - //to.setGrandTotal(Env.ZERO); - //to.setTotalLines(Env.ZERO); - // - //to.setIsDelivered(false); - //to.setIsInvoiced(false); - //to.setIsSelfService(false); - //to.setDatePrinted(null); - //to.setIsPrinted (false); - //to.setIsTransferred (false); - //to.setPosted (false); - //to.setProcessed (false); - // - if (!to.save()) - throw new IllegalStateException("Could not create Order"); + int AD_Client_ID = Env.getAD_Client_ID(ctx); - if (to.copyLinesFrom(from) == 0) - throw new IllegalStateException("Could not create Order Lines"); - - return to; - } // copyFrom - - public static int getBOMSearchKey(int M_Product_ID) - { - - int PP_Product_BOM_ID = 0; - int AD_Client_ID = Integer.parseInt(Env.getContext(Env.getCtx(), "#AD_Client_ID")); - - MProduct product = new MProduct(Env.getCtx(), M_Product_ID, "M_Product"); - String sql = "SELECT pb.PP_Product_BOM_ID FROM PP_Product_BOM pb WHERE pb.Value = ? AND pb.AD_Client_ID = ?"; - - - - PreparedStatement pstmt = null; - try - { - pstmt = DB.prepareStatement(sql); - pstmt.setString(1, product.getValue()); - pstmt.setInt(2, AD_Client_ID); - ResultSet rs = pstmt.executeQuery(); - while (rs.next()) - { - PP_Product_BOM_ID = rs.getInt(1); - break; - } - rs.close(); - pstmt.close(); - pstmt = null; - return PP_Product_BOM_ID; - } - catch (Exception e) - { - log.log(Level.SEVERE,"getProductPlanning", e); - } - try - { - if (pstmt != null) - pstmt.close(); - pstmt = null; - } - catch (Exception e) - { - pstmt = null; - } - - return 0; + MProduct product = MProduct.get(ctx, M_Product_ID); + String sql = "SELECT pb.PP_Product_BOM_ID FROM PP_Product_BOM pb" + +" WHERE pb.Value = ? AND pb.AD_Client_ID = ?"; + return DB.getSQLValue(null, sql, product.getValue(), AD_Client_ID); + } + + /** + * Get BOM with Default Logic (Product = BOM Product and BOM Value = Product Value) + * @param product + * @param trxName + * @return product BOM + */ + public static MPPProductBOM getDefault(MProduct product, String trxName) { + return new Query(product.getCtx(), Table_Name, "M_Product_ID=? AND Value=?", trxName) + .setParameters(new Object[]{product.getM_Product_ID(), product.getValue()}) + .first(); + } -} // MPP_ProductBOM +} // MPPProductBOM diff --git a/base/src/org/eevolution/model/MPPProductBOMLine.java b/base/src/org/eevolution/model/MPPProductBOMLine.java index 6b0fe690f7..d0cdfe97ef 100644 --- a/base/src/org/eevolution/model/MPPProductBOMLine.java +++ b/base/src/org/eevolution/model/MPPProductBOMLine.java @@ -20,35 +20,31 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.Hashtable; import java.util.Properties; -import java.util.logging.Level; -import javax.swing.JOptionPane; import javax.swing.tree.DefaultMutableTreeNode; +import org.adempiere.exceptions.AdempiereException; +import org.adempiere.exceptions.DBException; 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. + * PP Product BOM Line Model. * * MPPProductBOMLine l = new MPPProductBOMLine(bom); - l.setM_Product_ID(wbl.getM_Product_ID()); - l.setQty(wbl.getQuantity());; - l.save(); + * l.setM_Product_ID(wbl.getM_Product_ID()); + * l.setQty(wbl.getQuantity());; + * l.saveEx(); * * @author Victor Perez www.e-evolution.com * @version $Id: MOrderLine.java,v 1.22 2004/03/22 07:15:03 vpj-cd Exp $ + * + * @author Teo Sarca, SC ARHIPAC SERVICE SRL */ public class MPPProductBOMLine extends X_PP_Product_BOMLine { - - static private int AD_Client_ID = 0; - static Hashtable tableproduct = new Hashtable(); - private static CLogger s_log = CLogger.getCLogger(MPPProductBOMLine.class); + private static final long serialVersionUID = 1L; /** * Default Constructor @@ -63,18 +59,13 @@ public class MPPProductBOMLine extends X_PP_Product_BOMLine /** * Parent Constructor. - ol.setM_Product_ID(wbl.getM_Product_ID()); - ol.setQtyOrdered(wbl.getQuantity()); - ol.setPrice(); - ol.setPriceActual(wbl.getPrice()); - ol.setTax(); - ol.save(); - * @param order parent order + * @param bom parent BOM */ public MPPProductBOMLine(MPPProductBOM bom) { super(bom.getCtx(), 0, bom.get_TableName()); - if (bom.get_ID() == 0) throw new IllegalArgumentException("Header not saved"); + if (bom.get_ID() == 0) + throw new IllegalArgumentException("Header not saved"); setPP_Product_BOM_ID(bom.getPP_Product_BOM_ID()); // parent } @@ -89,162 +80,78 @@ public class MPPProductBOMLine extends X_PP_Product_BOMLine } // MPPProductBOMLine /** - * Set Defaults from BOM. - * Does not set Parent !! - * @param BOM BOM + * Calculate Low Level of a Product + * @param ID Product + * @return int low level */ - public void setMPPProductBOM(MPPProductBOM bom) + public int getLowLevel() { - setClientOrg(bom); + return new ProductLowLevelCalculator(getCtx(), get_TrxName()).getLowLevel(getM_Product_ID()); } /** - * String Representation - * @return info + * Calculate Low Level of a Product + * @param ID Product + * @return int low level */ - public String toString() + public static int getLowLevel(Properties ctx, int M_Product_ID, String trxName) { - StringBuffer sb = new StringBuffer("MPPProductBOMLine[").append(get_ID()).append("]"); - return sb.toString(); + return new ProductLowLevelCalculator(ctx, trxName).getLowLevel(M_Product_ID); } - /** - * String Description - * @return info + /************************************************************************** + * After Save + * @param newRecord new + * @return save */ - public String getDescriptionText() + protected boolean afterSave(boolean newRecord, boolean success) { - return super.getDescription(); - } // getDescriptionText + if (success) + { + int lowlevel = getLowLevel(); + MProduct product = new MProduct(getCtx(), getM_Product_ID(), get_TrxName()); + product.setLowLevel(lowlevel); //update lowlevel + product.saveEx(); + } + return true; + } +} +class ProductLowLevelCalculator { + private Hashtable tableproduct = new Hashtable(); + private Properties m_ctx = null; + private String m_trxName = null; + + public ProductLowLevelCalculator(Properties ctx, String trxName) + { + m_ctx = ctx; + m_trxName = trxName; + } + /** * get low level of a Product * @param ID Product * @return int low level */ - public int getLowLevel(int M_Product_ID) throws Exception + public int getLowLevel(int M_Product_ID) { - AD_Client_ID = Integer.valueOf(getCtx().getProperty("#AD_Client_ID")); + int AD_Client_ID = Env.getAD_Client_ID(m_ctx); 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 + ibom = iparent(AD_Client_ID, M_Product_ID, 0); //start traversing tree return ibom.getDepth(); } - - /** - * get a implotion the a prduct - * @param ID Product - * @param ID BOM - * @return DefaultMutableTreeNode Tree with all parent product - */ - 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; - ResultSet rs = null; - try - { - pstmt = DB.prepareStatement(sql, get_TrxName()); - pstmt.setInt(1, Env.getAD_Client_ID(Env.getCtx())); - pstmt.setInt(2, M_Product_ID); - rs = pstmt.executeQuery(); - while (rs.next()) - { - DefaultMutableTreeNode bom = component(rs.getInt(1), M_Product_ID, parent); - if (bom != null) - { - parent.add(bom); - } - - } - return parent; - } - catch (Exception e) - { - s_log.log(Level.SEVERE, "doIt - " + sql + e); - - } - finally { - DB.close(rs, pstmt); - rs = null; - pstmt = null; - } - 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; - 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; - } - - - 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 + private DefaultMutableTreeNode iparent(int AD_Client_ID, int M_Product_ID, int PP_Product_BOM_ID) { DefaultMutableTreeNode parent = new DefaultMutableTreeNode(Integer.toString(M_Product_ID) + "|" + Integer.toString(PP_Product_BOM_ID)); @@ -257,7 +164,7 @@ public class MPPProductBOMLine extends X_PP_Product_BOMLine ResultSet rs = null; try { - pstmt = DB.prepareStatement(sql, get_TrxName()); + pstmt = DB.prepareStatement(sql, m_trxName); pstmt.setInt(1, AD_Client_ID); pstmt.setInt(2, M_Product_ID); rs = pstmt.executeQuery(); @@ -270,17 +177,16 @@ public class MPPProductBOMLine extends X_PP_Product_BOMLine 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); + DefaultMutableTreeNode bom = icomponent(AD_Client_ID, rs.getInt(1), M_Product_ID, parent); if (bom != null) { parent.add(bom); } } - return parent; } catch (SQLException e) { - s_log.log(Level.SEVERE, "iparent - " + sql + e); + throw new DBException(e); } finally { DB.close(rs, pstmt); @@ -296,17 +202,17 @@ public class MPPProductBOMLine extends X_PP_Product_BOMLine * @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 + private DefaultMutableTreeNode icomponent(int AD_Client_ID, int PP_Product_BOMLine_ID, int M_Product_ID, DefaultMutableTreeNode bom) { - String sql = new String( + String sql = "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 = ? "); + + " 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 = DB.prepareStatement(sql, m_trxName); pstmt.setInt(1, AD_Client_ID); pstmt.setInt(2, PP_Product_BOMLine_ID); rs = pstmt.executeQuery(); @@ -316,26 +222,24 @@ public class MPPProductBOMLine extends X_PP_Product_BOMLine { //BOM Loop Error if (!tableproduct(rs.getInt(1), rs.getInt(3))) - bom.add(iparent(rs.getInt(1), rs.getInt(3))); + bom.add(iparent(AD_Client_ID, 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()); + throw new AdempiereException("Cycle BOM & Formula:" + rs.getString(2) + "(" + rs.getString(3) + ")"); } } 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()); + MProduct product = MProduct.get(m_ctx, M_Product_ID); + throw new AdempiereException("Cycle BOM & Formula:" + rs.getString(2) + "(" + rs.getString(3) +")" + + " - Component: " + product.getValue() + "(" + product.getM_Product_ID() + ")"); } } } catch (SQLException e) { - s_log.log(Level.SEVERE, "doIt - " + sql + e); + throw new DBException(e); } finally { DB.close(rs, pstmt); @@ -351,7 +255,7 @@ public class MPPProductBOMLine extends X_PP_Product_BOMLine * @param ID BOM * @return true if product is found */ - private static boolean tableproduct(int M_Product_ID, int PP_Product_BOM_ID) + private 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); @@ -363,30 +267,4 @@ public class MPPProductBOMLine extends X_PP_Product_BOMLine 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()); - int lowlevel = 0; - try - { - lowlevel = getLowLevel(getM_Product_ID()); - } - catch (Exception e) - { - log.saveError("Error", Msg.parseTranslation(getCtx(), e.getMessage()), false); - return false; - } - product.setLowLevel(lowlevel); //update lowlevel - product.save(get_TrxName()); - } - return true; - } } diff --git a/base/src/org/eevolution/process/CalculateLowLevel.java b/base/src/org/eevolution/process/CalculateLowLevel.java index c9a3658006..2da104ae1d 100644 --- a/base/src/org/eevolution/process/CalculateLowLevel.java +++ b/base/src/org/eevolution/process/CalculateLowLevel.java @@ -16,14 +16,12 @@ package org.eevolution.process; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.util.logging.Level; import org.compiere.model.MProduct; -import org.compiere.process.ProcessInfoParameter; +import org.compiere.model.POResultSet; +import org.compiere.model.Query; import org.compiere.process.SvrProcess; -import org.compiere.util.DB; import org.compiere.util.Env; import org.eevolution.model.MPPProductBOMLine; @@ -32,63 +30,43 @@ import org.eevolution.model.MPPProductBOMLine; * * @author Victor Perez, e-Evolution, S.C. * @version $Id: CalculateLowLevel.java,v 1.1 2004/06/22 05:24:03 vpj-cd Exp $ + * + * @author Teo Sarca, SC ARHIPAC SERVICE SRL */ public class CalculateLowLevel extends SvrProcess { - /** */ - - private int AD_Client_ID = 0; - /** * Prepare - e.g., get Parameters. */ protected void prepare() { - AD_Client_ID = Integer.parseInt(Env.getContext(Env.getCtx(), "#AD_Client_ID")); - ProcessInfoParameter[] para = getParameter(); } // prepare protected String doIt() throws Exception { - - String sql = "SELECT p.M_Product_ID FROM M_Product p WHERE AD_Client_ID = " + AD_Client_ID; - PreparedStatement pstmt = null; - ResultSet rs = null; - try - { - pstmt = DB.prepareStatement(sql, get_TrxName()); - rs = pstmt.executeQuery(); - while (rs.next()) - { - - int m_M_Product_ID = rs.getInt(1); - if (m_M_Product_ID != 0) - { - MProduct product = new MProduct(getCtx(), m_M_Product_ID, get_TrxName()); - MPPProductBOMLine bomline = new MPPProductBOMLine(getCtx(), 0, get_TrxName()); - int lowlevel = bomline.getLowLevel(m_M_Product_ID); - product.setLowLevel(lowlevel); - product.save(); - } + int count_ok = 0; + int count_err = 0; + // + POResultSet rs = new Query(getCtx(), MProduct.Table_Name, "AD_Client_ID=?", get_TrxName()) + .setParameters(new Object[]{Env.getAD_Client_ID(getCtx())}) + .setOrderBy(MProduct.COLUMNNAME_M_Product_ID) + .scroll(); + rs.setCloseOnError(true); + while(rs.hasNext()) { + MProduct product = rs.next(); + try { + int lowlevel = MPPProductBOMLine.getLowLevel(getCtx(), product.get_ID(), get_TrxName()); + product.setLowLevel(lowlevel); + product.saveEx(); + count_ok++; + } + catch(Exception e) { + log.log(Level.SEVERE, e.getLocalizedMessage(), e); + count_err++; } } - catch (SQLException e) - { - log.severe("Error: " + e.getLocalizedMessage() + sql); - return "@Error@"; - } - catch (Exception e) - { - log.severe("Error: " + e.getLocalizedMessage()); - return "@Error@"; - } - finally - { - DB.close(rs, pstmt); - rs = null; - pstmt = null; - } + rs.close(); - return "OK"; + return "@Ok@ #"+count_ok+" @Error@ #"+count_err; } } diff --git a/base/src/org/eevolution/process/MRP.java b/base/src/org/eevolution/process/MRP.java index 02c73582fb..0844c9fbb1 100644 --- a/base/src/org/eevolution/process/MRP.java +++ b/base/src/org/eevolution/process/MRP.java @@ -514,7 +514,7 @@ public class MRP extends SvrProcess //Find the BOM to this Product if (m_product_planning.getPP_Product_BOM_ID() == 0 && product.isBOM()) { - m_product_planning.setPP_Product_BOM_ID(MPPProductBOM.getBOMSearchKey(m_product_planning.getM_Product_ID())); + m_product_planning.setPP_Product_BOM_ID(MPPProductBOM.getBOMSearchKey(getCtx(), m_product_planning.getM_Product_ID())); } if (m_product_planning.getPlanner_ID() == 0 ) { diff --git a/base/src/org/eevolution/process/PP_Product_BOM_Check.java b/base/src/org/eevolution/process/PP_Product_BOM_Check.java index 88b3d5d966..414c3ccc51 100755 --- a/base/src/org/eevolution/process/PP_Product_BOM_Check.java +++ b/base/src/org/eevolution/process/PP_Product_BOM_Check.java @@ -18,12 +18,9 @@ *****************************************************************************/ package org.eevolution.process; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.logging.Level; -import org.compiere.model.X_M_Product; +import org.compiere.model.MProduct; import org.compiere.process.ProcessInfoParameter; import org.compiere.process.SvrProcess; import org.compiere.util.AdempiereUserError; @@ -39,7 +36,7 @@ import org.eevolution.model.MPPProductBOMLine; * BOMs which are already referenced * * @author Tony Snook (tspc) - * + * @author Teo Sarca, SC ARHIPAC SERVICE SRL */ public class PP_Product_BOM_Check extends SvrProcess { @@ -72,14 +69,10 @@ public class PP_Product_BOM_Check extends SvrProcess */ protected String doIt() throws Exception { - - int bomid = 0; - int lowlevel = 0; - log.info("Check BOM Structure"); // Record ID is M_Product_ID of product to be tested - X_M_Product xp = new X_M_Product(Env.getCtx(), p_Record_ID, get_TrxName()); + MProduct xp = new MProduct(Env.getCtx(), p_Record_ID, get_TrxName()); if (!xp.isBOM()) { @@ -89,67 +82,28 @@ public class PP_Product_BOM_Check extends SvrProcess } // Check Parent Level - MPPProductBOMLine bomline = new MPPProductBOMLine(getCtx(), 0, get_TrxName()); - - try - { - lowlevel = bomline.getLowLevel(p_Record_ID); - } - catch (Exception e) - { - raiseError("BOM Loop Error: ", e.getCause().getLocalizedMessage()); - } + int lowlevel = MPPProductBOMLine.getLowLevel(getCtx(), p_Record_ID, get_TrxName()); xp.setLowLevel(lowlevel); xp.setIsVerified(true); - xp.save(get_TrxName()); + xp.saveEx(); - // Get BOM ID for default BOM (where BOM search key equals Product - // search key) - String sql = new String("SELECT PP_Product_BOM_ID FROM PP_Product_BOM pb " + "LEFT JOIN M_Product p ON pb.M_Product_ID = p.M_Product_ID " - + "WHERE pb.M_Product_ID = ? AND pb.value = p.value "); - PreparedStatement pstmt = null; - ResultSet rs = null; - try - { - pstmt = DB.prepareStatement(sql, get_TrxName()); - pstmt.setInt(1, p_Record_ID); - rs = pstmt.executeQuery(); - if (rs.next()) - bomid = rs.getInt(1); - else - raiseError("No Default BOM found: ", "Check BOM Parent search key"); + // Get Default BOM from this product + MPPProductBOM tbom = MPPProductBOM.getDefault(xp, get_TrxName()); + if (tbom == null) { + raiseError("No Default BOM found: ", "Check BOM Parent search key"); } - catch (SQLException e) - { - log.severe("Error getting BOM_ID for product: " + e + "\n" + sql); - return "@Error@"; - } - finally - { - rs.close(); - pstmt.close(); - } - + // Check All BOM Lines - MPPProductBOM tbom = new MPPProductBOM(getCtx(), bomid, get_TrxName()); if (tbom.getM_Product_ID() != 0) { MPPProductBOMLine[] tbomlines = tbom.getLines(); for (MPPProductBOMLine tbomline : tbomlines) { - xp = new X_M_Product(Env.getCtx(), tbomline.getM_Product_ID(), get_TrxName()); - lowlevel = 0; - try - { - lowlevel = bomline.getLowLevel(tbomline.getM_Product_ID()); - } - catch (Exception e) - { - raiseError("BOM Loop Error: ", e.getLocalizedMessage()); - } - xp.setLowLevel(lowlevel); - xp.setIsVerified(true); - xp.save(get_TrxName()); + lowlevel = tbomline.getLowLevel(); + MProduct p = new MProduct(getCtx(), tbomline.getM_Product_ID(), get_TrxName()); + p.setLowLevel(lowlevel); + p.setIsVerified(true); + p.saveEx(); } } return "OK"; @@ -158,12 +112,13 @@ public class PP_Product_BOM_Check extends SvrProcess private void raiseError(String string, String hint) throws Exception { DB.rollback(false, get_TrxName()); - X_M_Product xp = new X_M_Product(getCtx(), p_Record_ID, null); // parent + MProduct xp = new MProduct(getCtx(), p_Record_ID, null); // parent xp.setIsVerified(false); // set BOM not verified xp.save(); String msg = string; ValueNamePair pp = CLogger.retrieveError(); - if (pp != null) msg = pp.getName() + " - "; + if (pp != null) + msg = pp.getName() + " - "; msg += hint; throw new AdempiereUserError(msg); }