Search in sources :

Example 1 with RunProcessResponse

use of org.idempiere.adInterface.x10.RunProcessResponse in project idempiere by idempiere.

the class Process method runProcess.

/**
 ************************************************************************
 * 	Run process
 *	@param m_cs
 *  @param req
 *  @param requestCtx
 *  @param trxName
 *	@return {@link RunProcessResponseDocument}
 */
public static RunProcessResponseDocument runProcess(CompiereService m_cs, RunProcessDocument req, Map<String, Object> requestCtx, String trxName) {
    RunProcessResponseDocument res = RunProcessResponseDocument.Factory.newInstance();
    RunProcessResponse r = res.addNewRunProcessResponse();
    RunProcess rp = req.getRunProcess();
    int AD_Menu_ID = rp.getADMenuID();
    int AD_Process_ID = rp.getADProcessID();
    int m_record_id = rp.getADRecordID();
    MProcess process = null;
    if (AD_Menu_ID <= 0 && AD_Process_ID > 0)
        process = MProcess.get(m_cs.getCtx(), AD_Process_ID);
    else if (AD_Menu_ID > 0 && AD_Process_ID <= 0)
        process = MProcess.getFromMenu(m_cs.getCtx(), AD_Menu_ID);
    if (process == null) {
        r.setError("Process not found");
        r.setIsError(true);
        return res;
    }
    // Evaluate DocAction, if call have DocAction parameter, then try to set DocAction before calling workflow process
    String docAction = rp.getDocAction();
    if (docAction != null && docAction.length() > 0) {
        // - the process must be a workflow document
        if (process.getAD_Workflow_ID() > 0) {
            MWorkflow wf = MWorkflow.get(m_cs.getCtx(), process.getAD_Workflow_ID());
            if (wf.getWorkflowType().equals(MWorkflow.WORKFLOWTYPE_DocumentProcess)) {
                // - get the table associated with the workflow document
                // - set DocAction in such table
                // get the PO for the tablename and record ID
                MTable table = MTable.get(m_cs.getCtx(), wf.getAD_Table_ID());
                if (table != null) {
                    PO po = table.getPO(m_record_id, null);
                    if (po != null) {
                        po.set_ValueOfColumn("DocAction", docAction);
                        po.saveEx();
                    }
                }
            }
        }
    }
    // Create Process Instance
    MPInstance pInstance = null;
    try {
        pInstance = fillParameter(m_cs, rp.getParamValues(), process, requestCtx);
    } catch (Exception ex) {
        r.setError(ex.getMessage());
        r.setIsError(true);
        return res;
    }
    DataField[] fields = rp.getParamValues().getFieldArray();
    for (DataField field : fields) {
        if ("AD_Record_ID".equals(field.getColumn())) {
            Object value = null;
            String s = field.getVal();
            if (requestCtx != null && !Util.isEmpty(s) && s.charAt(0) == '@') {
                value = ModelADServiceImpl.parseVariable(m_cs, requestCtx, field.getColumn(), s);
                if (value != null) {
                    if (value instanceof Number) {
                        m_record_id = ((Number) value).intValue();
                    } else {
                        try {
                            m_record_id = Integer.parseInt(value.toString());
                        } catch (Exception e) {
                        }
                    }
                }
            } else if (!Util.isEmpty(s)) {
                try {
                    m_record_id = Integer.parseInt(s);
                } catch (Exception e) {
                }
            }
        }
    }
    if (m_record_id > 0) {
        pInstance.setRecord_ID(m_record_id);
        pInstance.saveEx();
    }
    // 
    ProcessInfo pi = new ProcessInfo(process.getName(), process.getAD_Process_ID());
    pi.setAD_User_ID(Env.getAD_User_ID(m_cs.getCtx()));
    pi.setAD_Client_ID(Env.getAD_Client_ID(m_cs.getCtx()));
    pi.setAD_PInstance_ID(pInstance.getAD_PInstance_ID());
    if (m_record_id > 0)
        pi.setRecord_ID(m_record_id);
    ProcessInfoParameter[] parameters = pi.getParameter();
    if (parameters == null) {
        ProcessInfoUtil.setParameterFromDB(pi);
        parameters = pi.getParameter();
    }
    for (DataField field : fields) {
        if (isDataURI(field.getVal())) {
            for (ProcessInfoParameter param : parameters) {
                if (param.getParameterName().equals(field.getColumn())) {
                    String data = field.getVal().substring(field.getVal().indexOf(";base64,") + ";base64,".length());
                    param.setParameter(data);
                    break;
                }
            }
        }
    }
    boolean processOK = false;
    boolean jasperreport = (process != null && (process.getJasperReport() != null || (process.getClassname() != null && process.getClassname().indexOf(ProcessUtil.JASPER_STARTER_CLASS) >= 0)));
    if (jasperreport) {
        processOK = true;
    }
    // Start
    if (process.isWorkflow()) {
        try {
            int AD_Workflow_ID = process.getAD_Workflow_ID();
            MWorkflow wf = MWorkflow.get(Env.getCtx(), AD_Workflow_ID);
            // may return null
            MWFProcess wfProcess = wf.startWait(pi);
            if (wfProcess != null) {
                // wynik
                r.setSummary(pi.getSummary());
                r.setLogInfo(pi.getLogInfo(true));
                r.setIsError(false);
                return res;
            }
        } catch (Exception ex) {
            r.setError(ex.getMessage());
            r.setLogInfo(pi.getLogInfo(true));
            r.setIsError(true);
            return res;
        }
    }
    if (process.isJavaProcess() && !jasperreport) {
        Trx trx = trxName == null ? Trx.get(Trx.createTrxName("WebPrc"), true) : Trx.get(trxName, true);
        if (trxName == null)
            trx.setDisplayName(Process.class.getName() + "_runProcess");
        try {
            processOK = process.processIt(pi, trx, false);
            if (trxName == null && processOK)
                trx.commit();
            else if (trxName == null && !processOK)
                trx.rollback();
        } catch (Throwable t) {
            trx.rollback();
        } finally {
            if (trxName == null)
                trx.close();
        }
        if (!processOK || pi.isError()) {
            r.setSummary(pi.getSummary());
            r.setError(pi.getSummary());
            r.setLogInfo(pi.getLogInfo(true));
            r.setIsError(true);
            processOK = false;
        } else {
            try {
                if (pi.getExportFile() != null) {
                    r.setData(java.nio.file.Files.readAllBytes(pi.getExportFile().toPath()));
                    r.setReportFormat(pi.getExportFileExtension());
                }
                r.setSummary(pi.getSummary());
                r.setError(pi.getSummary());
                r.setLogInfo(pi.getLogInfo(true));
                r.setIsError(false);
            } catch (Exception e) {
                r.setError("Cannot get the export file:" + e.getMessage());
                r.setLogInfo(pi.getLogInfo(true));
                r.setIsError(true);
                return res;
            }
        }
    }
    // Report
    if ((process.isReport() || jasperreport)) {
        pi.setReportingProcess(true);
        r.setIsReport(true);
        ReportEngine re = null;
        if (!jasperreport)
            re = start(pi);
        if (re == null && !jasperreport) {
            ;
        } else {
            try {
                boolean ok = false;
                String file_type = "pdf";
                if (!jasperreport) {
                    MPrintFormat pf = re.getPrintFormat();
                    if (pf.isTableBased()) {
                        CharArrayWriter wr = new CharArrayWriter();
                        ok = ReportEngineEx.createEXCEL_HTML_wr(re, m_cs.getCtx(), wr, false, re.getPrintFormat().getLanguage());
                        file_type = "html";
                        String data = wr.toString();
                        if (data != null)
                            r.setData(data.getBytes());
                        r.setReportFormat(file_type);
                    } else {
                        byte[] dat = re.createPDFData();
                        file_type = "pdf";
                        r.setData(dat);
                        r.setReportFormat(file_type);
                    }
                    ok = true;
                } else {
                    Trx trx = trxName == null ? Trx.get(Trx.createTrxName("WebPrc"), true) : Trx.get(trxName, true);
                    pi.setPrintPreview(false);
                    pi.setIsBatch(true);
                    ProcessUtil.startJavaProcess(Env.getCtx(), pi, trx, true, null);
                    file_type = "pdf";
                    r.setData(java.nio.file.Files.readAllBytes(pi.getPDFReport().toPath()));
                    r.setReportFormat(file_type);
                    ok = true;
                }
                if (ok) {
                    // Marker that Process is OK
                    m_cs.getCtx().put("AD_PInstance_ID=" + pInstance.getAD_PInstance_ID(), "ok");
                } else {
                    r.setError("Cannot create report");
                    r.setLogInfo(pi.getLogInfo(true));
                    r.setIsError(true);
                    return res;
                }
            } catch (Exception e) {
                r.setError("Cannot create report:" + e.getMessage());
                r.setLogInfo(pi.getLogInfo(true));
                r.setIsError(true);
                return res;
            // ,
            }
        }
    }
    return res;
}
Also used : MWFProcess(org.compiere.wf.MWFProcess) RunProcess(org.idempiere.adInterface.x10.RunProcess) MWFProcess(org.compiere.wf.MWFProcess) MProcess(org.compiere.model.MProcess) RunProcessResponse(org.idempiere.adInterface.x10.RunProcessResponse) CharArrayWriter(java.io.CharArrayWriter) RunProcess(org.idempiere.adInterface.x10.RunProcess) Trx(org.compiere.util.Trx) MProcess(org.compiere.model.MProcess) MWorkflow(org.compiere.wf.MWorkflow) ProcessInfo(org.compiere.process.ProcessInfo) ParseException(java.text.ParseException) ProcessInfoParameter(org.compiere.process.ProcessInfoParameter) MPInstance(org.compiere.model.MPInstance) ReportEngine(org.compiere.print.ReportEngine) MPrintFormat(org.compiere.print.MPrintFormat) MTable(org.compiere.model.MTable) DataField(org.idempiere.adInterface.x10.DataField) RunProcessResponseDocument(org.idempiere.adInterface.x10.RunProcessResponseDocument) PO(org.compiere.model.PO)

Example 2 with RunProcessResponse

use of org.idempiere.adInterface.x10.RunProcessResponse in project idempiere by idempiere.

the class ModelADServiceImpl method runProcess.

public RunProcessResponseDocument runProcess(ModelRunProcessRequestDocument req) {
    try {
        getCompiereService().connect();
        RunProcessResponseDocument resbadlogin = RunProcessResponseDocument.Factory.newInstance();
        RunProcessResponse rbadlogin = resbadlogin.addNewRunProcessResponse();
        ModelRunProcess modelRunProcess = req.getModelRunProcessRequest().getModelRunProcess();
        String serviceType = modelRunProcess.getServiceType();
        ADLoginRequest reqlogin = req.getModelRunProcessRequest().getADLoginRequest();
        String err = login(reqlogin, webServiceName, "runProcess", serviceType);
        if (err != null && err.length() > 0) {
            rbadlogin.setError(err);
            rbadlogin.setIsError(true);
            return resbadlogin;
        }
        // Validate parameters
        try {
            modelRunProcess.setADMenuID(validateParameter("AD_Menu_ID", modelRunProcess.getADMenuID()));
        } catch (XmlValueOutOfRangeException e) {
            // Catch the exception when the Menu ID is not an Integer
            String menuUU = getUUIDValue(modelRunProcess.xgetADMenuID());
            if (menuUU == null) {
                throw e;
            }
            modelRunProcess.setADMenuID(validateParameter("AD_Menu_ID", 0, menuUU));
        }
        try {
            modelRunProcess.setADProcessID(validateParameter("AD_Process_ID", modelRunProcess.getADProcessID()));
        } catch (XmlValueOutOfRangeException e) {
            // Catch the exception when the Process ID is not an Integer
            String processUU = getUUIDValue(modelRunProcess.xgetADProcessID());
            if (processUU == null) {
                throw e;
            }
            modelRunProcess.setADProcessID(validateParameter("AD_Process_ID", 0, processUU));
        }
        modelRunProcess.setADRecordID(validateParameter("AD_Record_ID", modelRunProcess.getADRecordID()));
        modelRunProcess.setDocAction(validateParameter("DocAction", modelRunProcess.getDocAction()));
        RunProcessDocument docprocess = RunProcessDocument.Factory.newInstance();
        RunProcess reqprocess = docprocess.addNewRunProcess();
        reqprocess.setParamValues(modelRunProcess.getParamValues());
        reqprocess.setADProcessID(modelRunProcess.getADProcessID());
        reqprocess.setADMenuID(modelRunProcess.getADMenuID());
        reqprocess.setADRecordID(modelRunProcess.getADRecordID());
        reqprocess.setDocAction(modelRunProcess.getDocAction());
        RunProcessResponseDocument response = Process.runProcess(getCompiereService(), docprocess, getRequestCtx(), localTrxName);
        if (response != null && response.getRunProcessResponse() != null && response.getRunProcessResponse().getIsError())
            log.warning("Error running webservice " + serviceType + " -> " + response.getRunProcessResponse().getError());
        Map<String, Object> requestCtx = getRequestCtx();
        requestCtx.put(serviceType + "_Summary", response.getRunProcessResponse().getSummary());
        return response;
    } finally {
        getCompiereService().disconnect();
    }
}
Also used : ModelRunProcess(org.idempiere.adInterface.x10.ModelRunProcess) RunProcessDocument(org.idempiere.adInterface.x10.RunProcessDocument) ADLoginRequest(org.idempiere.adInterface.x10.ADLoginRequest) ModelRunProcess(org.idempiere.adInterface.x10.ModelRunProcess) RunProcess(org.idempiere.adInterface.x10.RunProcess) RunProcessResponse(org.idempiere.adInterface.x10.RunProcessResponse) XmlValueOutOfRangeException(org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException) RunProcessResponseDocument(org.idempiere.adInterface.x10.RunProcessResponseDocument)

Aggregations

RunProcess (org.idempiere.adInterface.x10.RunProcess)2 RunProcessResponse (org.idempiere.adInterface.x10.RunProcessResponse)2 RunProcessResponseDocument (org.idempiere.adInterface.x10.RunProcessResponseDocument)2 CharArrayWriter (java.io.CharArrayWriter)1 ParseException (java.text.ParseException)1 XmlValueOutOfRangeException (org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException)1 MPInstance (org.compiere.model.MPInstance)1 MProcess (org.compiere.model.MProcess)1 MTable (org.compiere.model.MTable)1 PO (org.compiere.model.PO)1 MPrintFormat (org.compiere.print.MPrintFormat)1 ReportEngine (org.compiere.print.ReportEngine)1 ProcessInfo (org.compiere.process.ProcessInfo)1 ProcessInfoParameter (org.compiere.process.ProcessInfoParameter)1 Trx (org.compiere.util.Trx)1 MWFProcess (org.compiere.wf.MWFProcess)1 MWorkflow (org.compiere.wf.MWorkflow)1 ADLoginRequest (org.idempiere.adInterface.x10.ADLoginRequest)1 DataField (org.idempiere.adInterface.x10.DataField)1 ModelRunProcess (org.idempiere.adInterface.x10.ModelRunProcess)1