Search in sources :

Example 1 with RunProcessResponseDocument

use of org.idempiere.adInterface.x10.RunProcessResponseDocument 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 RunProcessResponseDocument

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

the class CompositeServiceImpl method performOperations.

/**
 * Perform operation
 * @param trx
 * @param ops
 * @param modelADService
 * @param compResp
 * @param respAggregator
 * @param reqlogin
 * @return isError
 */
private boolean performOperations(Trx trx, Operation[] ops, ModelADServiceImpl modelADService, CompositeResponse compResp, ArrayList<StandardResponse> respAggregator, ADLoginRequest reqlogin) {
    for (Operation operation : ops) {
        if (operation.getPreCommit()) {
            if (!commitTrx(trx, compResp, respAggregator, "Cannot commit before Operation", true)) {
                return false;
            }
        }
        modelADService.setManageTrx(false);
        TargetPort.Enum portEnum = operation.getTargetPort();
        StandardResponseDocument respDoc = null;
        if (portEnum == TargetPort.CREATE_DATA) {
            ModelCRUDRequestDocument wrapperDoc = getWrappedCRUD(operation, reqlogin);
            if (wrapperDoc == null) {
                rollbackAndSetError(trx, compResp, respAggregator, "Operation createData must required ModelCRUD");
                return false;
            } else {
                respDoc = modelADService.createData(wrapperDoc);
            }
        } else if (portEnum == TargetPort.DELETE_DATA) {
            ModelCRUDRequestDocument wrapperDoc = getWrappedCRUD(operation, reqlogin);
            if (wrapperDoc == null) {
                rollbackAndSetError(trx, compResp, respAggregator, "Operation deleteData must required ModelCRUD");
                return false;
            } else {
                respDoc = modelADService.deleteData(wrapperDoc);
            }
        } else if (portEnum == TargetPort.CREATE_UPDATE_DATA) {
            ModelCRUDRequestDocument wrapperDoc = getWrappedCRUD(operation, reqlogin);
            if (wrapperDoc == null) {
                rollbackAndSetError(trx, compResp, respAggregator, "Operation createUpdateData must required ModelCRUD");
                return false;
            } else {
                respDoc = modelADService.createUpdateData(wrapperDoc);
            }
        } else if (portEnum == TargetPort.READ_DATA) {
            ModelCRUDRequestDocument wrapperDoc = getWrappedCRUD(operation, reqlogin);
            if (wrapperDoc == null) {
                // Do not rollback, Continue with consecutive operations
                respDoc = StandardResponseDocument.Factory.newInstance();
                StandardResponse resp = respDoc.addNewStandardResponse();
                resp.setIsError(false);
                resp.setError("Operation readData must required ModelCRUD");
            } else {
                WindowTabDataDocument dataResponse = modelADService.readData(wrapperDoc);
                if (dataResponse != null) {
                    respDoc = StandardResponseDocument.Factory.newInstance();
                    StandardResponse resp = respDoc.addNewStandardResponse();
                    // Do not fail though read is failed
                    resp.setIsError(false);
                    resp.setWindowTabData(dataResponse.getWindowTabData());
                }
            }
        } else if (portEnum == TargetPort.RUN_PROCESS) {
            if (operation.getModelRunProcess() == null) {
                rollbackAndSetError(trx, compResp, respAggregator, "Operation runProcess must required ModelRunProcess");
                return false;
            }
            ModelRunProcessRequestDocument wrapperDoc = ModelRunProcessRequestDocument.Factory.newInstance();
            ModelRunProcessRequest runProcessReq = wrapperDoc.addNewModelRunProcessRequest();
            runProcessReq.setADLoginRequest(reqlogin);
            runProcessReq.setModelRunProcess(operation.getModelRunProcess());
            RunProcessResponseDocument runResponse = modelADService.runProcess(wrapperDoc);
            if (runResponse != null) {
                respDoc = StandardResponseDocument.Factory.newInstance();
                StandardResponse resp = respDoc.addNewStandardResponse();
                resp.setIsError(runResponse.getRunProcessResponse().getIsError());
                resp.setRunProcessResponse(runResponse.getRunProcessResponse());
            }
        } else if (portEnum == TargetPort.SET_DOC_ACTION) {
            if (operation.getModelSetDocAction() == null) {
                rollbackAndSetError(trx, compResp, respAggregator, "Operation updateData must required ModelSetDocAction");
                return false;
            }
            ModelSetDocActionRequestDocument wrapperDoc = ModelSetDocActionRequestDocument.Factory.newInstance();
            ModelSetDocActionRequest actionReq = wrapperDoc.addNewModelSetDocActionRequest();
            actionReq.setADLoginRequest(reqlogin);
            actionReq.setModelSetDocAction(operation.getModelSetDocAction());
            respDoc = modelADService.setDocAction(wrapperDoc);
        } else if (portEnum == TargetPort.UPDATE_DATA) {
            ModelCRUDRequestDocument wrapperDoc = getWrappedCRUD(operation, reqlogin);
            if (wrapperDoc == null) {
                rollbackAndSetError(trx, compResp, respAggregator, "Operation updateData must required ModelCRUD");
                return false;
            } else {
                respDoc = modelADService.updateData(wrapperDoc);
            }
        }
        if (respDoc != null)
            respAggregator.add(respDoc.getStandardResponse());
        if (respDoc != null && respDoc.getStandardResponse().getIsError()) {
            rollbackAndSetError(trx, compResp, respAggregator, null);
            return false;
        }
        if (operation.getPostCommit()) {
            if (!commitTrx(trx, compResp, respAggregator, "Cannot commit after operation", false)) {
                return false;
            }
        }
    }
    return true;
}
Also used : ModelRunProcessRequestDocument(org.idempiere.adInterface.x10.ModelRunProcessRequestDocument) TargetPort(org.idempiere.adInterface.x10.Operation.TargetPort) WindowTabDataDocument(org.idempiere.adInterface.x10.WindowTabDataDocument) StandardResponse(org.idempiere.adInterface.x10.StandardResponse) ModelSetDocActionRequest(org.idempiere.adInterface.x10.ModelSetDocActionRequest) ModelSetDocActionRequestDocument(org.idempiere.adInterface.x10.ModelSetDocActionRequestDocument) ModelRunProcessRequest(org.idempiere.adInterface.x10.ModelRunProcessRequest) Operation(org.idempiere.adInterface.x10.Operation) ModelCRUDRequestDocument(org.idempiere.adInterface.x10.ModelCRUDRequestDocument) StandardResponseDocument(org.idempiere.adInterface.x10.StandardResponseDocument) RunProcessResponseDocument(org.idempiere.adInterface.x10.RunProcessResponseDocument)

Example 3 with RunProcessResponseDocument

use of org.idempiere.adInterface.x10.RunProcessResponseDocument 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

RunProcessResponseDocument (org.idempiere.adInterface.x10.RunProcessResponseDocument)3 RunProcess (org.idempiere.adInterface.x10.RunProcess)2 RunProcessResponse (org.idempiere.adInterface.x10.RunProcessResponse)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 ModelCRUDRequestDocument (org.idempiere.adInterface.x10.ModelCRUDRequestDocument)1