From 7f3483d363e7ae9b3ad72d2dd7f57d90c1a16c47 Mon Sep 17 00:00:00 2001 From: tspc Date: Mon, 14 Jul 2008 04:23:53 +0000 Subject: [PATCH] BF2003790 BOM Verify - root insert error --- .../process/PP_Product_BOM_Check.java | 171 ++++++++++++++++++ .../351a-trunk/228_BF2003790_BOM_Verify.sql | 25 +++ .../postgresql/228_BF2003790_BOM_Verify.sql | 25 +++ 3 files changed, 221 insertions(+) create mode 100755 base/src/org/eevolution/process/PP_Product_BOM_Check.java create mode 100755 migration/351a-trunk/228_BF2003790_BOM_Verify.sql create mode 100755 migration/351a-trunk/postgresql/228_BF2003790_BOM_Verify.sql diff --git a/base/src/org/eevolution/process/PP_Product_BOM_Check.java b/base/src/org/eevolution/process/PP_Product_BOM_Check.java new file mode 100755 index 0000000000..88b3d5d966 --- /dev/null +++ b/base/src/org/eevolution/process/PP_Product_BOM_Check.java @@ -0,0 +1,171 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * For the text or an alternative of this public license, you may reach us * + * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA * + * or via info@compiere.org or http://www.compiere.org/license.html * + * Portions created by Carlos Ruiz are Copyright (C) 2005 QSS Ltda. + * Contributor(s): Carlos Ruiz (globalqss) + *****************************************************************************/ +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.process.ProcessInfoParameter; +import org.compiere.process.SvrProcess; +import org.compiere.util.AdempiereUserError; +import org.compiere.util.CLogger; +import org.compiere.util.DB; +import org.compiere.util.Env; +import org.compiere.util.ValueNamePair; +import org.eevolution.model.MPPProductBOM; +import org.eevolution.model.MPPProductBOMLine; + +/** + * Title: Check BOM Structure (free of cycles) Description: Tree cannot contain + * BOMs which are already referenced + * + * @author Tony Snook (tspc) + * + */ +public class PP_Product_BOM_Check extends SvrProcess +{ + + /** The Record */ + private int p_Record_ID = 0; + + /** + * Prepare - e.g., get Parameters. + */ + protected void prepare() + { + ProcessInfoParameter[] para = getParameter(); + for (int i = 0; i < para.length; i++) + { + String name = para[i].getParameterName(); + if (para[i].getParameter() == null) + ; + else + log.log(Level.SEVERE, "Unknown Parameter: " + name); + } + p_Record_ID = getRecord_ID(); + } // prepare + + /** + * Process + * + * @return message + * @throws Exception + */ + 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()); + + if (!xp.isBOM()) + { + log.info("Product is not a BOM"); + // No BOM - should not happen, but no problem + return "OK"; + } + + // 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()); + } + xp.setLowLevel(lowlevel); + xp.setIsVerified(true); + xp.save(get_TrxName()); + + // 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"); + } + 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()); + } + } + return "OK"; + } // doIt + + 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 + xp.setIsVerified(false); // set BOM not verified + xp.save(); + String msg = string; + ValueNamePair pp = CLogger.retrieveError(); + if (pp != null) msg = pp.getName() + " - "; + msg += hint; + throw new AdempiereUserError(msg); + } + +} // M_Product_BOM_Check diff --git a/migration/351a-trunk/228_BF2003790_BOM_Verify.sql b/migration/351a-trunk/228_BF2003790_BOM_Verify.sql new file mode 100755 index 0000000000..5a8bcb9b67 --- /dev/null +++ b/migration/351a-trunk/228_BF2003790_BOM_Verify.sql @@ -0,0 +1,25 @@ +-- Jul 7, 2008 1:24:00 PM EST +-- Default comment for updating dictionary +UPDATE AD_Process SET Classname='org.eevolution.process.PP_Product_BOM_Check', Value='PP_Product_BOM',Updated=TO_DATE('2008-07-07 13:24:00','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Process_ID=136 +; + +-- Jul 7, 2008 1:26:22 PM EST +-- Default comment for updating dictionary +UPDATE AD_Process SET Description='Verify BOM Structure and Update Low Level',Updated=TO_DATE('2008-07-07 13:26:22','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Process_ID=136 +; + +-- Jul 7, 2008 1:26:22 PM EST +-- Default comment for updating dictionary +UPDATE AD_Process_Trl SET IsTranslated='N' WHERE AD_Process_ID=136 +; + +-- Jul 7, 2008 1:28:22 PM EST +-- Default comment for updating dictionary +UPDATE AD_Field SET Description='Verify BOM Structure and Update Low Level', DisplayLogic='@IsSummary@=''N'' & @IsBOM@=''Y''',Updated=TO_DATE('2008-07-07 13:28:22','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Field_ID=3747 +; + +-- Jul 7, 2008 1:28:22 PM EST +-- Default comment for updating dictionary +UPDATE AD_Field_Trl SET IsTranslated='N' WHERE AD_Field_ID=3747 +; + diff --git a/migration/351a-trunk/postgresql/228_BF2003790_BOM_Verify.sql b/migration/351a-trunk/postgresql/228_BF2003790_BOM_Verify.sql new file mode 100755 index 0000000000..6a0d24c606 --- /dev/null +++ b/migration/351a-trunk/postgresql/228_BF2003790_BOM_Verify.sql @@ -0,0 +1,25 @@ +-- Jul 7, 2008 1:24:00 PM EST +-- Default comment for updating dictionary +UPDATE AD_Process SET Classname='org.eevolution.process.PP_Product_BOM_Check', Value='PP_Product_BOM',Updated=TO_TIMESTAMP('2008-07-07 13:24:00','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Process_ID=136 +; + +-- Jul 7, 2008 1:26:22 PM EST +-- Default comment for updating dictionary +UPDATE AD_Process SET Description='Verify BOM Structure and Update Low Level',Updated=TO_TIMESTAMP('2008-07-07 13:26:22','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Process_ID=136 +; + +-- Jul 7, 2008 1:26:22 PM EST +-- Default comment for updating dictionary +UPDATE AD_Process_Trl SET IsTranslated='N' WHERE AD_Process_ID=136 +; + +-- Jul 7, 2008 1:28:22 PM EST +-- Default comment for updating dictionary +UPDATE AD_Field SET Description='Verify BOM Structure and Update Low Level', DisplayLogic='@IsSummary@=''N'' & @IsBOM@=''Y''',Updated=TO_TIMESTAMP('2008-07-07 13:28:22','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Field_ID=3747 +; + +-- Jul 7, 2008 1:28:22 PM EST +-- Default comment for updating dictionary +UPDATE AD_Field_Trl SET IsTranslated='N' WHERE AD_Field_ID=3747 +; +