ComponentChange: refactor & clean code

This commit is contained in:
teo_sarca 2008-10-14 09:44:28 +00:00
parent 3cd4bf8698
commit f2ccd24d3c
1 changed files with 139 additions and 164 deletions

View File

@ -12,18 +12,24 @@
* For the text or an alternative of this public license, you may reach us * * For the text or an alternative of this public license, you may reach us *
* Copyright (C) 2003-2007 e-Evolution,SC. All Rights Reserved. * * Copyright (C) 2003-2007 e-Evolution,SC. All Rights Reserved. *
* Contributor(s): Victor Perez www.e-evolution.com * * Contributor(s): Victor Perez www.e-evolution.com *
* Teo Sarca, www.arhipac.ro *
*****************************************************************************/ *****************************************************************************/
package org.eevolution.process; package org.eevolution.process;
import java.math.*; import java.math.BigDecimal;
import java.sql.*; import java.sql.Timestamp;
import java.util.*; import java.util.ArrayList;
import java.util.logging.*; import java.util.List;
import org.compiere.util.*; import java.util.logging.Level;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.FillMandatoryException;
import org.compiere.model.MRefList;
import org.compiere.model.Query; import org.compiere.model.Query;
import org.compiere.process.*; import org.compiere.process.ProcessInfoParameter;
import org.eevolution.model.*; import org.compiere.process.SvrProcess;
import org.eevolution.model.MPPProductBOMLine;
/** /**
@ -31,180 +37,149 @@ import org.eevolution.model.*;
* *
* @author victor.perez@e-evolution.com * @author victor.perez@e-evolution.com
* @version $Id: ComponentChange.java * @version $Id: ComponentChange.java
*
* @author Teo Sarca, www.arhipac.ro
*/ */
public class ComponentChange extends SvrProcess public class ComponentChange extends SvrProcess
{ {
/** The Order */ private static final int ACTION_AD_Reference_ID = 53227;
private int p_M_Product_ID = 0; private static final String ACTION_Add = "A";
private Timestamp p_ValidTo = null; private static final String ACTION_Deactivate = "D";
private Timestamp p_ValidFrom = null; private static final String ACTION_Expire = "E";
private String p_Action; private static final String ACTION_Replace = "R";
private int p_New_M_Product_ID =0; private static final String ACTION_ReplaceAndExpire = "RE";
private BigDecimal p_Qty = null;
private int p_M_ChangeNotice_ID=0; private int p_M_Product_ID = 0;
private int morepara = 0; private Timestamp p_ValidTo = null;
/** private Timestamp p_ValidFrom = null;
* Prepare - e.g., get Parameters. private String p_Action;
*/ private int p_New_M_Product_ID =0;
private BigDecimal p_Qty = null;
private int p_M_ChangeNotice_ID=0;
@Override
protected void prepare() protected void prepare()
{ {
ProcessInfoParameter[] para = getParameter(); int morepara = 0;
for (int i = 0; i < para.length; i++) for (ProcessInfoParameter para : getParameter())
{ {
String name = para[i].getParameterName(); String name = para.getParameterName();
if (para[i].getParameter() == null) if (para.getParameter() == null)
; ;
else if (name.equals("M_Product_ID") && morepara == 0) else if (name.equals("M_Product_ID") && morepara == 0)
{ {
p_M_Product_ID = para[i].getParameterAsInt(); p_M_Product_ID = para.getParameterAsInt();
morepara = 1; morepara = 1;
} }
else if (name.equals("ValidTo")) else if (name.equals("ValidTo"))
p_ValidTo = ((Timestamp)para[i].getParameter()); p_ValidTo = ((Timestamp)para.getParameter());
else if (name.equals("ValidFrom")) else if (name.equals("ValidFrom"))
p_ValidFrom = ((Timestamp)para[i].getParameter()); p_ValidFrom = ((Timestamp)para.getParameter());
else if (name.equals("Action")) else if (name.equals("Action"))
p_Action = ((String)para[i].getParameter()); p_Action = ((String)para.getParameter());
else if (name.equals("M_Product_ID")) else if (name.equals("M_Product_ID"))
p_New_M_Product_ID = para[i].getParameterAsInt(); p_New_M_Product_ID = para.getParameterAsInt();
else if (name.equals("Qty")) else if (name.equals("Qty"))
p_Qty = ((BigDecimal)para[i].getParameter()); p_Qty = ((BigDecimal)para.getParameter());
else if (name.equals("M_ChangeNotice_ID")) else if (name.equals("M_ChangeNotice_ID"))
p_M_ChangeNotice_ID = para[i].getParameterAsInt(); p_M_ChangeNotice_ID = para.getParameterAsInt();
else else
log.log(Level.SEVERE,"prepare - Unknown Parameter: " + name); log.log(Level.SEVERE,"prepare - Unknown Parameter: " + name);
} }
} // prepare } // prepare
/** @Override
* Perform process.
* @return Message
* @throws Exception if not successful
*/
protected String doIt() throws Exception protected String doIt() throws Exception
{ {
log.info("Existing Product" + p_M_Product_ID ); if (p_Action == null)
log.info("ValidTo" + p_ValidTo); {
log.info("ValidFrom" + p_ValidFrom); throw new FillMandatoryException("Action");
log.info("Action" + p_Action); }
log.info("New Product" + p_New_M_Product_ID);
log.info("Qty" + p_Qty );
String whereClause = "M_Product_ID = ?"; List<Object> params = new ArrayList<Object>();
if (p_ValidTo !=null) StringBuffer whereClause = new StringBuffer();
whereClause +=" AND TRUNC(ValidTo) <= "+ DB.TO_DATE(p_ValidTo);
whereClause.append(MPPProductBOMLine.COLUMNNAME_M_Product_ID+"=?");
params.add(p_M_Product_ID);
if (p_ValidTo != null)
{
whereClause.append(" AND TRUNC("+MPPProductBOMLine.COLUMNNAME_ValidTo+") <= ?");
params.add(p_ValidTo);
}
if (p_ValidFrom != null) if (p_ValidFrom != null)
whereClause +=" AND TRUNC(ValidFrom) >= "+DB.TO_DATE(p_ValidFrom); {
whereClause.append(" AND TRUNC("+MPPProductBOMLine.COLUMNNAME_ValidFrom+") >= ?");
List<MPPProductBOMLine> components = new Query(getCtx(), MPPProductBOMLine.Table_Name, params.add(p_ValidFrom);
whereClause,get_TrxName()).setParameters(new Object[]{p_M_Product_ID}).list(); }
List<MPPProductBOMLine> components = new Query(getCtx(), MPPProductBOMLine.Table_Name, whereClause.toString(), get_TrxName())
.setParameters(params)
.list();
for(MPPProductBOMLine bomline : components) for(MPPProductBOMLine bomline : components)
{ {
if (p_Action.equals("A")) if (p_Action.equals(ACTION_Add))
{ {
MPPProductBOMLine newbomline = new MPPProductBOMLine(Env.getCtx(), 0 ,get_TrxName()); actionAdd(bomline, 0);
newbomline.setAssay(bomline.getAssay()); }
newbomline.setBackflushGroup(bomline.getBackflushGroup()); else if (p_Action.equals(ACTION_Deactivate))
newbomline.setQtyBatch(bomline.getQtyBatch()); {
newbomline.setC_UOM_ID(bomline.getC_UOM_ID()); actionDeactivate(bomline);
newbomline.setDescription(bomline.getDescription()); }
newbomline.setHelp(bomline.getHelp()); else if (p_Action.equals(ACTION_Expire))
newbomline.setM_ChangeNotice_ID(bomline.getM_ChangeNotice_ID()); {
newbomline.setForecast(bomline.getForecast()); actionExpire(bomline);
newbomline.setQtyBOM(p_Qty); }
newbomline.setComponentType(bomline.getComponentType()); else if (p_Action.equals(ACTION_Replace))
newbomline.setIsQtyPercentage(bomline.isQtyPercentage()); {
newbomline.setIsCritical(bomline.isCritical()); actionAdd(bomline, bomline.getLine() + 1);
newbomline.setIssueMethod(bomline.getIssueMethod()); actionDeactivate(bomline);
newbomline.setLine(25); }
newbomline.setLeadTimeOffset(bomline.getLeadTimeOffset()); else if (p_Action.equals(ACTION_ReplaceAndExpire))
newbomline.setM_AttributeSetInstance_ID(bomline.getM_AttributeSetInstance_ID()); {
newbomline.setM_Product_ID(p_New_M_Product_ID); actionAdd(bomline, bomline.getLine() + 1);
newbomline.setPP_Product_BOM_ID(bomline.getPP_Product_BOM_ID()); actionExpire(bomline);
newbomline.setScrap(bomline.getScrap()); }
newbomline.setValidFrom(newbomline.getUpdated()); else
newbomline.setM_ChangeNotice_ID(p_M_ChangeNotice_ID); {
newbomline.saveEx(); throw new AdempiereException("Action not supported - "+p_Action);
addLog("Component add"); }
} addLog(MRefList.getListName(getCtx(), ACTION_AD_Reference_ID, p_Action));
else if (p_Action.equals("D")) }
{ return "@OK@";
bomline.setM_ChangeNotice_ID(p_M_ChangeNotice_ID); } // doIt
bomline.setIsActive(false);
bomline.saveEx(); protected void actionAdd(MPPProductBOMLine bomline, int line)
addLog("Deactivate "); {
} MPPProductBOMLine newbomline = new MPPProductBOMLine(getCtx(), 0, get_TrxName());
else if (p_Action.equals("E")) MPPProductBOMLine.copyValues(bomline, newbomline);
{ newbomline.setIsActive(true);
bomline.setM_ChangeNotice_ID(p_M_ChangeNotice_ID); newbomline.setLine(line);
bomline.setValidTo(bomline.getUpdated()); newbomline.setM_ChangeNotice_ID(p_M_ChangeNotice_ID);
bomline.saveEx(); //
addLog("Expire"); newbomline.setM_Product_ID(p_New_M_Product_ID);
} if (p_Qty.signum() != 0)
else if (p_Action.equals("R")) {
{ newbomline.setQtyBOM(p_Qty);
MPPProductBOMLine newbomline = new MPPProductBOMLine(getCtx(), 0 , get_TrxName()); }
newbomline.setAssay(bomline.getAssay()); newbomline.setValidFrom(newbomline.getUpdated());
newbomline.setBackflushGroup(bomline.getBackflushGroup()); newbomline.saveEx();
newbomline.setQtyBatch(bomline.getQtyBatch()); }
newbomline.setComponentType(bomline.getComponentType());
newbomline.setC_UOM_ID(bomline.getC_UOM_ID()); protected void actionDeactivate(MPPProductBOMLine bomline)
newbomline.setDescription(bomline.getDescription()); {
newbomline.setM_ChangeNotice_ID(bomline.getM_ChangeNotice_ID()); bomline.setIsActive(false);
newbomline.setHelp(bomline.getHelp()); bomline.setM_ChangeNotice_ID(p_M_ChangeNotice_ID);
newbomline.setForecast(bomline.getForecast()); bomline.saveEx();
newbomline.setQtyBOM(p_Qty); }
newbomline.setIsQtyPercentage(bomline.isQtyPercentage());
newbomline.setIsCritical(bomline.isCritical()); protected void actionExpire(MPPProductBOMLine bomline)
newbomline.setIssueMethod(bomline.getIssueMethod()); {
newbomline.setLine(25); bomline.setIsActive(true);
newbomline.setLeadTimeOffset(bomline.getLeadTimeOffset()); bomline.setValidTo(bomline.getUpdated());
newbomline.setM_AttributeSetInstance_ID(bomline.getM_AttributeSetInstance_ID()); bomline.setM_ChangeNotice_ID(p_M_ChangeNotice_ID);
newbomline.setM_Product_ID(p_New_M_Product_ID); bomline.saveEx();
newbomline.setPP_Product_BOM_ID(bomline.getPP_Product_BOM_ID()); }
newbomline.setScrap(bomline.getScrap());
newbomline.setValidFrom(newbomline.getUpdated());
newbomline.setM_ChangeNotice_ID(p_M_ChangeNotice_ID);
newbomline.save();
bomline.setIsActive(false);
bomline.setM_ChangeNotice_ID(p_M_ChangeNotice_ID);
bomline.saveEx();
addLog("Replace");
}
else if (p_Action.equals("RE"))
{
MPPProductBOMLine newbomline = new MPPProductBOMLine(getCtx(), 0 , get_TrxName());
newbomline.setAssay(bomline.getAssay());
newbomline.setBackflushGroup(bomline.getBackflushGroup());
newbomline.setQtyBatch(bomline.getQtyBatch());
newbomline.setComponentType(bomline.getComponentType());
newbomline.setC_UOM_ID(bomline.getC_UOM_ID());
newbomline.setDescription(bomline.getDescription());
newbomline.setHelp(bomline.getHelp());
newbomline.setM_ChangeNotice_ID(bomline.getM_ChangeNotice_ID());
newbomline.setHelp(bomline.getHelp());
newbomline.setForecast(bomline.getForecast());
newbomline.setQtyBOM(p_Qty);
newbomline.setIsQtyPercentage(bomline.isQtyPercentage());
newbomline.setIsCritical(bomline.isCritical());
newbomline.setIssueMethod(bomline.getIssueMethod());
newbomline.setLine(25);
newbomline.setLeadTimeOffset(bomline.getLeadTimeOffset());
newbomline.setM_AttributeSetInstance_ID(bomline.getM_AttributeSetInstance_ID());
newbomline.setM_Product_ID(p_New_M_Product_ID);
newbomline.setPP_Product_BOM_ID(bomline.getPP_Product_BOM_ID());
newbomline.setScrap(bomline.getScrap());
newbomline.setValidFrom(newbomline.getUpdated());
newbomline.saveEx();
bomline.setValidTo(bomline.getUpdated());
bomline.setM_ChangeNotice_ID(p_M_ChangeNotice_ID);
bomline.saveEx();
addLog("Replace & Expire");
}
}
return "@OK@";
} // doIt
} // Component Change } // Component Change