Search in sources :

Example 16 with MPInstancePara

use of org.compiere.model.MPInstancePara in project adempiere by adempiere.

the class DistributionRunOrders method executeDistribution.

/**
      * Execute Distribution Run
      * @return
      * @throws Exception
      */
public boolean executeDistribution() throws Exception {
    int M_DocType_ID = 0;
    MDocType[] doc = MDocType.getOfDocBaseType(getCtx(), MDocType.DOCBASETYPE_DistributionOrder);
    if (doc == null || doc.length == 0) {
        log.severe("Not found default document type for docbasetype " + MDocType.DOCBASETYPE_DistributionOrder);
        throw new Exception(Msg.getMsg(getCtx(), "SequenceDocNotFound"), CLogger.retrieveException());
    } else
        M_DocType_ID = doc[0].getC_DocType_ID();
    String trxName = Trx.createTrxName("Run Distribution to DRP");
    //trx needs to be committed too
    Trx trx = Trx.get(trxName, true);
    //Prepare Process
    int AD_Process_ID = 271;
    AD_Process_ID = MProcess.getProcess_ID("M_DistributionRun Create", get_TrxName());
    MPInstance instance = new MPInstance(Env.getCtx(), AD_Process_ID, 0);
    if (!instance.save()) {
        throw new Exception(Msg.getMsg(getCtx(), "ProcessNoInstance"), CLogger.retrieveException());
    }
    //call process
    ProcessInfo pi = new ProcessInfo("M_DistributionRun Orders", AD_Process_ID);
    pi.setAD_PInstance_ID(instance.getAD_PInstance_ID());
    pi.setRecord_ID(m_run.getM_DistributionRun_ID());
    //	Add Parameter - Selection=Y
    MPInstancePara ip = new MPInstancePara(instance, 10);
    ip.setParameter("C_DocType_ID", M_DocType_ID);
    if (!ip.save()) {
        //  not translated
        String msg = "No Parameter added";
        throw new Exception(msg, CLogger.retrieveException());
    }
    //	Add Parameter - DatePromised
    ip = new MPInstancePara(instance, 20);
    ip.setParameter("DatePromised", "");
    ip.setP_Date(p_DatePromised);
    //ip.setP_Date_To(p_DatePromised_To);
    if (!ip.save()) {
        //  not translated
        String msg = "No Parameter added";
        throw new Exception(msg, CLogger.retrieveException());
    }
    //	Add Parameter - M_Warehouse_ID
    ip = new MPInstancePara(instance, 30);
    ip.setParameter("M_Warehouse_ID", p_M_Warehouse_ID);
    if (!ip.save()) {
        //  not translated
        String msg = "No Parameter added";
        throw new Exception(msg, CLogger.retrieveException());
    }
    //	Add Parameter - CreateDO
    ip = new MPInstancePara(instance, 40);
    ip.setParameter("ConsolidateDocument", p_ConsolidateDocument);
    if (!ip.save()) {
        //  not translated
        String msg = "No Parameter added";
        throw new Exception(msg, CLogger.retrieveException());
    }
    //	Add Parameter - IsTest=Y
    ip = new MPInstancePara(instance, 50);
    ip.setParameter("IsTest", p_IsTest);
    if (!ip.save()) {
        //  not translated
        String msg = "No Parameter added";
        throw new Exception(msg, CLogger.retrieveException());
    }
    //Distribution List
    ip = new MPInstancePara(instance, 60);
    ip.setParameter("M_DistributionList_ID", p_M_DistributionList_ID);
    if (!ip.save()) {
        //  not translated
        String msg = "No Parameter added";
        throw new Exception(msg, CLogger.retrieveException());
    }
    //Based in DRP Demand
    ip = new MPInstancePara(instance, 70);
    ip.setParameter("IsRequiredDRP", p_BasedInDamnd);
    if (!ip.save()) {
        //  not translated
        String msg = "No Parameter added";
        throw new Exception(msg, CLogger.retrieveException());
    }
    //	Execute Process
    MProcess worker = new MProcess(getCtx(), AD_Process_ID, get_TrxName());
    worker.processIt(pi, Trx.get(get_TrxName(), true));
    m_run.delete(true);
    return true;
}
Also used : MPInstancePara(org.compiere.model.MPInstancePara) MDocType(org.compiere.model.MDocType) MPInstance(org.compiere.model.MPInstance) MProcess(org.compiere.model.MProcess) Trx(org.compiere.util.Trx) ProcessInfo(org.compiere.process.ProcessInfo)

Example 17 with MPInstancePara

use of org.compiere.model.MPInstancePara in project adempiere by adempiere.

the class ProcessBuilder method withParameter.

/**
     * Define parameter and sequence
     * @param name
     * @param value
     * @param sequence
     * @return
     */
public ProcessBuilder withParameter(String name, Object value, Integer sequence) {
    if (name == null || name.length() == 0)
        return this;
    if (instance == null)
        generateProcessInstance();
    seqNo = sequence;
    MPInstancePara parameter = new MPInstancePara(instance, sequence);
    if (value instanceof String)
        parameter.setParameter(name, (String) value);
    if (value instanceof Integer)
        parameter.setParameter(name, (Integer) value);
    if (value instanceof Timestamp)
        parameter.setParameter(name, (Timestamp) value);
    if (value instanceof Boolean)
        parameter.setParameter(name, (java.lang.Boolean) value);
    if (value instanceof BigDecimal)
        parameter.setParameter(name, (BigDecimal) value);
    parameter.saveEx();
    return this;
}
Also used : MPInstancePara(org.compiere.model.MPInstancePara) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal)

Example 18 with MPInstancePara

use of org.compiere.model.MPInstancePara in project adempiere by adempiere.

the class Scheduler method fillParameter.

/**
	 * 	Fill Parameter
	 *	@param pInstance process instance
	 */
private void fillParameter(MPInstance pInstance) {
    MSchedulerPara[] sParams = m_model.getParameters(false);
    MPInstancePara[] iParams = pInstance.getParameters();
    for (int pi = 0; pi < iParams.length; pi++) {
        MPInstancePara iPara = iParams[pi];
        for (int np = 0; np < sParams.length; np++) {
            MSchedulerPara sPara = sParams[np];
            if (iPara.getParameterName().equals(sPara.getColumnName())) {
                String paraDesc = sPara.getDescription();
                if (paraDesc != null && paraDesc.trim().length() > 0)
                    iPara.setInfo(sPara.getDescription());
                String variable = sPara.getParameterDefault();
                log.fine(sPara.getColumnName() + " = " + variable);
                //	Value - Constant/Variable
                Object value = variable;
                if (variable == null || (variable != null && variable.length() == 0))
                    value = null;
                else if (variable.indexOf('@') != -1 && //	we have a variable / BF [1926032]
                variable.indexOf('@') != variable.lastIndexOf('@')) {
                    //	Strip
                    int index = variable.indexOf('@');
                    String columnName = variable.substring(index + 1);
                    index = columnName.indexOf('@');
                    if (index == -1) {
                        log.warning(sPara.getColumnName() + " - cannot evaluate=" + variable);
                        break;
                    }
                    columnName = columnName.substring(0, index);
                    //	try Env
                    String env = Env.getContext(m_schedulerctx, columnName);
                    if (env == null || env.length() == 0)
                        env = Env.getContext(getCtx(), columnName);
                    if (env.length() == 0) {
                        log.warning(sPara.getColumnName() + " - not in environment =" + columnName + "(" + variable + ")");
                        break;
                    } else
                        value = env;
                }
                //	No Value
                if (value == null) {
                    log.fine(sPara.getColumnName() + " - empty");
                    break;
                }
                //	Convert to Type
                try {
                    if (DisplayType.isNumeric(sPara.getDisplayType()) || DisplayType.isID(sPara.getDisplayType())) {
                        BigDecimal bd = null;
                        if (value instanceof BigDecimal)
                            bd = (BigDecimal) value;
                        else if (value instanceof Integer)
                            bd = new BigDecimal(((Integer) value).intValue());
                        else
                            bd = new BigDecimal(value.toString());
                        iPara.setP_Number(bd);
                        log.fine(sPara.getColumnName() + " = " + variable + " (=" + bd + "=)");
                    } else if (DisplayType.isDate(sPara.getDisplayType())) {
                        Timestamp ts = null;
                        if (value instanceof Timestamp)
                            ts = (Timestamp) value;
                        else
                            ts = Timestamp.valueOf(value.toString());
                        iPara.setP_Date(ts);
                        log.fine(sPara.getColumnName() + " = " + variable + " (=" + ts + "=)");
                    } else {
                        iPara.setP_String(value.toString());
                        log.fine(sPara.getColumnName() + " = " + variable + " (=" + value + "=) " + value.getClass().getName());
                    }
                    if (!iPara.save())
                        log.warning("Not Saved - " + sPara.getColumnName());
                } catch (Exception e) {
                    log.warning(sPara.getColumnName() + " = " + variable + " (" + value + ") " + value.getClass().getName() + " - " + e.getLocalizedMessage());
                }
                break;
            }
        //	parameter match
        }
    //	scheduler parameter loop
    }
//	instance parameter loop
}
Also used : MPInstancePara(org.compiere.model.MPInstancePara) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) MSchedulerPara(org.compiere.model.MSchedulerPara)

Example 19 with MPInstancePara

use of org.compiere.model.MPInstancePara in project adempiere by adempiere.

the class ProcessController method loadParameters.

/**
	 * Load values from saved parameters
	 * @param instance
	 * @return
	 */
public boolean loadParameters(MPInstance instance) {
    log.config("");
    //	
    MPInstancePara[] params = instance.getParameters();
    for (int j = 0; j < getFieldSize(); j++) {
        //	Get Values
        GridField field = (GridField) getField(j);
        GridField fieldTo = (GridField) getFieldTo(j);
        //	Set Values
        setValue(j, null);
        if (fieldTo != null)
            setValue_To(j, null);
        for (int i = 0; i < params.length; i++) {
            MPInstancePara para = params[i];
            para.getParameterName();
            if (field.getColumnName().equals(para.getParameterName())) {
                if (para.getP_Date() != null || para.getP_Date_To() != null) {
                    setValue(j, para.getP_Date());
                    if (fieldTo != null)
                        setValue_To(j, para.getP_Date_To());
                } else //	String
                if (para.getP_String() != null || para.getP_String_To() != null) {
                    setValue(j, para.getP_String());
                    if (fieldTo != null)
                        setValue_To(j, para.getP_String_To());
                } else if (!Env.ZERO.equals(para.getP_Number()) || !Env.ZERO.equals(para.getP_Number_To())) {
                    setValue(j, para.getP_Number());
                    if (fieldTo != null)
                        setValue_To(j, para.getP_Number_To());
                }
                log.fine(para.toString());
                break;
            }
        }
    // for every saved parameter
    }
    //	for every field
    return true;
}
Also used : MPInstancePara(org.compiere.model.MPInstancePara) GridField(org.compiere.model.GridField)

Example 20 with MPInstancePara

use of org.compiere.model.MPInstancePara in project adempiere by adempiere.

the class ProcessController method saveParameters.

//	validateParameters
/**
	 * Validate and save parameters if not happened error
	 * @return null if nothing happened
	 */
public String saveParameters() {
    log.config("");
    setIsProcessed(false);
    //	Valid parameters
    String validError = validateParameters();
    if (validError != null)
        return validError;
    //	FR [ 295 ]
    if (processInfo.getAD_PInstance_ID() <= 0) {
        MPInstance instance = null;
        //	Set to null for reload
        //	BR [ 380 ]
        processInfo.setParameter(null);
        try {
            instance = new MPInstance(Env.getCtx(), processInfo.getAD_Process_ID(), processInfo.getRecord_ID());
            instance.saveEx();
            //	Set Instance
            processInfo.setAD_PInstance_ID(instance.getAD_PInstance_ID());
        } catch (Exception e) {
            processInfo.setSummary(e.getLocalizedMessage());
            processInfo.setError(true);
            log.warning(processInfo.toString());
            return processInfo.getSummary();
        }
    }
    /**********************************************************************
		 *	Save Now
		 */
    for (int i = 0; i < getFieldSize(); i++) {
        //	Get Values
        GridField field = (GridField) getField(i);
        GridField fieldTo = (GridField) getFieldTo(i);
        //	FR [ 566 ] Only Information
        if (field.isInfoOnly())
            continue;
        //	Validate
        field.validateValue();
        Object result = getValue(i);
        Object result2 = null;
        if (fieldTo != null) {
            result2 = getValue_To(i);
        }
        //	Create Parameter
        MPInstancePara para = new MPInstancePara(Env.getCtx(), processInfo.getAD_PInstance_ID(), i);
        para.setParameterName(field.getColumnName());
        //	
        if (result instanceof Timestamp || result2 instanceof Timestamp) {
            //	Date
            para.setP_Date((Timestamp) result);
            if (fieldTo != null && result2 != null)
                para.setP_Date_To((Timestamp) result2);
        } else if (result instanceof Integer || result2 instanceof Integer) {
            //	Integer
            if (result != null) {
                Integer ii = (Integer) result;
                para.setP_Number(ii.intValue());
            }
            if (fieldTo != null && result2 != null) {
                Integer ii = (Integer) result2;
                para.setP_Number_To(ii.intValue());
            }
        } else if (result instanceof BigDecimal || result2 instanceof BigDecimal) {
            //	BigDecimal
            para.setP_Number((BigDecimal) result);
            if (fieldTo != null && result2 != null)
                para.setP_Number_To((BigDecimal) result2);
        } else if (result instanceof Boolean) {
            //	Boolean
            Boolean bb = (Boolean) result;
            String value = bb.booleanValue() ? "Y" : "N";
            para.setP_String(value);
        //	to does not make sense
        } else {
            //	String
            if (result != null)
                para.setP_String(result.toString());
            if (fieldTo != null && result2 != null)
                para.setP_String_To(result2.toString());
        }
        //  Info
        para.setInfo(getDisplay(i));
        if (fieldTo != null)
            para.setInfo_To(getDisplay_To(i));
        //
        para.saveEx();
        log.fine(para.toString());
    }
    //	for every parameter
    setIsProcessed(true);
    return null;
}
Also used : MPInstancePara(org.compiere.model.MPInstancePara) MPInstance(org.compiere.model.MPInstance) GridField(org.compiere.model.GridField) Timestamp(java.sql.Timestamp) AdempiereException(org.adempiere.exceptions.AdempiereException) BigDecimal(java.math.BigDecimal)

Aggregations

MPInstancePara (org.compiere.model.MPInstancePara)22 MPInstance (org.compiere.model.MPInstance)17 ProcessInfo (org.compiere.process.ProcessInfo)10 Timestamp (java.sql.Timestamp)8 BigDecimal (java.math.BigDecimal)7 Trx (org.compiere.util.Trx)7 SQLException (java.sql.SQLException)4 IOException (java.io.IOException)3 AdempiereException (org.adempiere.exceptions.AdempiereException)3 MProcessPara (org.compiere.model.MProcessPara)3 File (java.io.File)2 ServletException (javax.servlet.ServletException)2 JasperPrint (net.sf.jasperreports.engine.JasperPrint)2 ProcessCtl (org.compiere.apps.ProcessCtl)2 GridField (org.compiere.model.GridField)2 MProcess (org.compiere.model.MProcess)2 KeyNamePair (org.compiere.util.KeyNamePair)2 Savepoint (java.sql.Savepoint)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1