Search in sources :

Example 6 with DocAction

use of org.compiere.process.DocAction in project adempiere by adempiere.

the class LiberoValidator method modelChange.

//	initialize
public String modelChange(PO po, int type) throws Exception {
    log.info(po.get_TableName() + " Type: " + type);
    boolean isChange = (TYPE_AFTER_NEW == type || (TYPE_AFTER_CHANGE == type && MPPMRP.isChanged(po)));
    boolean isDelete = (TYPE_BEFORE_DELETE == type);
    boolean isReleased = false;
    boolean isVoided = false;
    //Update MRP Change Net 
    if (MPPMRP.isChanged(po) && (TYPE_AFTER_CHANGE == type || TYPE_AFTER_NEW == type)) {
        MPPMRP.setIsRequired(po, MPPProductPlanning.COLUMNNAME_IsRequiredMRP, true, po.get_TrxName());
    }
    DocAction doc = null;
    if (po instanceof DocAction) {
        doc = (DocAction) po;
    } else if (po instanceof MOrderLine) {
        doc = ((MOrderLine) po).getParent();
    }
    if (doc != null) {
        String docStatus = doc.getDocStatus();
        isReleased = DocAction.STATUS_InProgress.equals(docStatus) || DocAction.STATUS_Completed.equals(docStatus);
        isVoided = DocAction.STATUS_Voided.equals(docStatus);
    }
    // Can we change M_Product.C_UOM_ID ?
    if (po instanceof MProduct && TYPE_BEFORE_CHANGE == type && po.is_ValueChanged(MProduct.COLUMNNAME_C_UOM_ID) && MPPMRP.hasProductRecords((MProduct) po)) {
        throw new AdempiereException("@SaveUomError@");
    }
    //
    if (isDelete || isVoided || !po.isActive()) {
        if (MOrder.Table_Name.equals(po.get_TableName()) || MOrderLine.Table_Name.equals(po.get_TableName()) || MPPOrder.Table_Name.equals(po.get_TableName()) || MPPOrderBOMLine.Table_Name.equals(po.get_TableName()) || MDDOrder.Table_Name.equals(po.get_TableName()) || MDDOrderLine.Table_Name.equals(po.get_TableName()) || MRequisition.Table_Name.equals(po.get_TableName()) || MRequisitionLine.Table_Name.equals(po.get_TableName()) || MForecast.Table_Name.equals(po.get_TableName()) || MForecastLine.Table_Name.equals(po.get_TableName()))
            MPPMRP.deleteMRP(po);
    } else if (po instanceof MOrder) {
        MOrder order = (MOrder) po;
        // or when you change DatePromised or DocStatus and is Purchase Order
        if (isChange && !order.isSOTrx()) {
            MPPMRP.C_Order(order);
        } else // or you change DatePromised
        if (type == TYPE_AFTER_CHANGE && order.isSOTrx()) {
            if (isReleased || MPPMRP.isChanged(order)) {
                MPPMRP.C_Order(order);
            }
        }
    } else // 
    if (po instanceof MOrderLine && isChange) {
        MOrderLine ol = (MOrderLine) po;
        MOrder order = ol.getParent();
        // Create/Update a planning supply when isPurchase Order or you change relevant fields
        if (!order.isSOTrx()) {
            MPPMRP.C_OrderLine(ol);
        } else // you change relevant fields
        if (order.isSOTrx() && isReleased) {
            MPPMRP.C_OrderLine(ol);
        }
    } else //
    if (po instanceof MRequisition && isChange) {
        MRequisition r = (MRequisition) po;
        MPPMRP.M_Requisition(r);
    } else //
    if (po instanceof MRequisitionLine && isChange) {
        MRequisitionLine rl = (MRequisitionLine) po;
        MPPMRP.M_RequisitionLine(rl);
    } else //
    if (po instanceof MForecast && isChange) {
        MForecast fl = (MForecast) po;
        MPPMRP.M_Forecast(fl);
    } else //
    if (po instanceof MForecastLine && isChange) {
        MForecastLine fl = (MForecastLine) po;
        MPPMRP.M_ForecastLine(fl);
    } else if (po instanceof MDDOrder && isChange) {
        MDDOrder order = (MDDOrder) po;
        MPPMRP.DD_Order(order);
    } else //
    if (po instanceof MDDOrderLine && isChange) {
        MDDOrderLine ol = (MDDOrderLine) po;
        MPPMRP.DD_OrderLine(ol);
    } else //
    if (po instanceof MPPOrder && isChange) {
        MPPOrder order = (MPPOrder) po;
        MPPMRP.PP_Order(order);
    } else //
    if (po instanceof MPPOrderBOMLine && isChange) {
        MPPOrderBOMLine obl = (MPPOrderBOMLine) po;
        MPPMRP.PP_Order_BOMLine(obl);
    }
    //
    return null;
}
Also used : MProduct(org.compiere.model.MProduct) DocAction(org.compiere.process.DocAction) MForecast(org.compiere.model.MForecast) MOrder(org.compiere.model.MOrder) MForecastLine(org.compiere.model.MForecastLine) AdempiereException(org.adempiere.exceptions.AdempiereException) MOrderLine(org.compiere.model.MOrderLine) MRequisitionLine(org.compiere.model.MRequisitionLine) MRequisition(org.compiere.model.MRequisition)

Example 7 with DocAction

use of org.compiere.process.DocAction in project adempiere by adempiere.

the class MWFNodeNext method isValidFor.

//	isUnconditional
/**
	 * 	Is this a Valid Transition For ..
	 * 	@param activity activity
	 *	@return true if valid
	 */
public boolean isValidFor(MWFActivity activity) {
    if (isStdUserWorkflow()) {
        PO po = activity.getPO();
        if (po instanceof DocAction) {
            DocAction da = (DocAction) po;
            String docStatus = da.getDocStatus();
            String docAction = da.getDocAction();
            if (!DocAction.ACTION_Complete.equals(docAction) || DocAction.STATUS_Completed.equals(docStatus) || DocAction.STATUS_WaitingConfirmation.equals(docStatus) || DocAction.STATUS_WaitingPayment.equals(docStatus) || DocAction.STATUS_Voided.equals(docStatus) || DocAction.STATUS_Closed.equals(docStatus) || DocAction.STATUS_Reversed.equals(docStatus)) /*
					|| DocAction.ACTION_Complete.equals(docAction)	
					|| DocAction.ACTION_ReActivate.equals(docAction)	
					|| DocAction.ACTION_None.equals(docAction)
					|| DocAction.ACTION_Post.equals(docAction)
					|| DocAction.ACTION_Unlock.equals(docAction)
					|| DocAction.ACTION_Invalidate.equals(docAction)	) */
            {
                log.fine("isValidFor =NO= StdUserWF - Status=" + docStatus + " - Action=" + docAction);
                return false;
            }
        }
    }
    //	No Conditions
    if (getConditions(false).length == 0) {
        log.fine("#0 " + toString());
        return true;
    }
    //	First condition always AND
    boolean ok = m_conditions[0].evaluate(activity);
    for (int i = 1; i < m_conditions.length; i++) {
        if (m_conditions[i].isOr())
            ok = ok || m_conditions[i].evaluate(activity);
        else
            ok = ok && m_conditions[i].evaluate(activity);
    }
    //	for all conditions
    log.fine("isValidFor (" + ok + ") " + toString());
    return ok;
}
Also used : DocAction(org.compiere.process.DocAction) PO(org.compiere.model.PO)

Example 8 with DocAction

use of org.compiere.process.DocAction in project adempiere by adempiere.

the class MWFActivity method performWork.

//	run
/**
	 * 	Perform Work.
	 * 	Set Text Msg.
	 * 	@param trx transaction
	 *	@return true if completed, false otherwise
	 *	@throws Exception if error
	 */
private boolean performWork(Trx trx) throws Exception {
    log.info(m_node + " [" + trx.getTrxName() + "]");
    m_docStatus = null;
    if (//	overwrite priority if defined
    m_node.getPriority() != 0)
        setPriority(m_node.getPriority());
    String action = m_node.getAction();
    /******	Sleep (Start/End)			******/
    if (MWFNode.ACTION_WaitSleep.equals(action)) {
        log.fine("Sleep:WaitTime=" + m_node.getWaitTime());
        if (m_node.getWaitingTime() == 0)
            //	done
            return true;
        Calendar cal = Calendar.getInstance();
        cal.add(m_node.getDurationCalendarField(), m_node.getWaitTime());
        setEndWaitTime(new Timestamp(cal.getTimeInMillis()));
        //	not done
        return false;
    } else /******	Document Action				******/
    if (MWFNode.ACTION_DocumentAction.equals(action)) {
        log.fine("DocumentAction=" + m_node.getDocAction());
        getPO(trx);
        if (m_po == null)
            throw new Exception("Persistent Object not found - AD_Table_ID=" + getAD_Table_ID() + ", Record_ID=" + getRecord_ID());
        boolean success = false;
        String processMsg = null;
        DocAction doc = null;
        if (m_po instanceof DocAction) {
            doc = (DocAction) m_po;
            //
            try {
                //	** Do the work
                success = doc.processIt(m_node.getDocAction());
                setTextMsg(doc.getSummary());
                processMsg = doc.getProcessMsg();
                // the rest of methods return boolean, so doc status must not be taken into account when not successful
                if (DocAction.ACTION_Prepare.equals(m_node.getDocAction()) || DocAction.ACTION_Complete.equals(m_node.getDocAction()) || success)
                    m_docStatus = doc.getDocStatus();
            } catch (Exception e) {
                if (m_process != null)
                    m_process.setProcessMsg(e.getLocalizedMessage());
                throw e;
            }
            if (m_process != null)
                m_process.setProcessMsg(processMsg);
        } else
            throw new IllegalStateException("Persistent Object not DocAction - " + m_po.getClass().getName() + " - AD_Table_ID=" + getAD_Table_ID() + ", Record_ID=" + getRecord_ID());
        //
        if (!m_po.save()) {
            success = false;
            processMsg = "SaveError";
        }
        if (!success) {
            if (processMsg == null || processMsg.length() == 0) {
                processMsg = "PerformWork Error - " + m_node.toStringX();
                if (//	problem: status will be rolled back
                doc != null)
                    processMsg += " - DocStatus=" + doc.getDocStatus();
            }
            throw new Exception(processMsg);
        }
        return success;
    } else /******	Report						******/
    if (MWFNode.ACTION_AppsReport.equals(action)) {
        log.fine("Report:AD_Process_ID=" + m_node.getAD_Process_ID());
        //	Process
        MProcess process = MProcess.get(getCtx(), m_node.getAD_Process_ID());
        process.set_TrxName(trx != null ? trx.getTrxName() : null);
        if (!process.isReport() || process.getAD_ReportView_ID() == 0)
            throw new IllegalStateException("Not a Report AD_Process_ID=" + m_node.getAD_Process_ID());
        //
        ProcessInfo pi = new ProcessInfo(m_node.getName(true), m_node.getAD_Process_ID(), getAD_Table_ID(), getRecord_ID());
        pi.setAD_User_ID(getAD_User_ID());
        pi.setAD_Client_ID(getAD_Client_ID());
        MPInstance pInstance = new MPInstance(process, getRecord_ID());
        pInstance.set_TrxName(trx != null ? trx.getTrxName() : null);
        fillParameter(pInstance, trx);
        pi.setAD_PInstance_ID(pInstance.getAD_PInstance_ID());
        //	Report
        ReportEngine re = ReportEngine.get(getCtx(), pi);
        if (re == null)
            throw new IllegalStateException("Cannot create Report AD_Process_ID=" + m_node.getAD_Process_ID());
        File report = re.getPDF();
        //	Notice
        //	HARDCODED WorkflowResult
        int AD_Message_ID = 753;
        MNote note = new MNote(getCtx(), AD_Message_ID, getAD_User_ID(), trx.getTrxName());
        note.setTextMsg(m_node.getName(true));
        note.setDescription(m_node.getDescription(true));
        note.setRecord(getAD_Table_ID(), getRecord_ID());
        note.saveEx();
        //	Attachment
        MAttachment attachment = new MAttachment(getCtx(), MNote.Table_ID, note.getAD_Note_ID(), get_TrxName());
        attachment.addEntry(report);
        attachment.setTextMsg(m_node.getName(true));
        attachment.saveEx();
        return true;
    } else /******	Process						******/
    if (MWFNode.ACTION_AppsProcess.equals(action)) {
        log.fine("Process:AD_Process_ID=" + m_node.getAD_Process_ID());
        //	Process
        MProcess process = MProcess.get(getCtx(), m_node.getAD_Process_ID());
        MPInstance pInstance = new MPInstance(process, getRecord_ID());
        fillParameter(pInstance, trx);
        //
        ProcessInfo pi = new ProcessInfo(m_node.getName(true), m_node.getAD_Process_ID(), getAD_Table_ID(), getRecord_ID());
        pi.setAD_User_ID(getAD_User_ID());
        pi.setAD_Client_ID(getAD_Client_ID());
        pi.setAD_PInstance_ID(pInstance.getAD_PInstance_ID());
        return process.processItWithoutTrxClose(pi, trx);
    } else /******	EMail						******/
    if (MWFNode.ACTION_EMail.equals(action)) {
        log.fine("EMail:EMailRecipient=" + m_node.getEMailRecipient());
        getPO(trx);
        if (m_po == null)
            throw new Exception("Persistent Object not found - AD_Table_ID=" + getAD_Table_ID() + ", Record_ID=" + getRecord_ID());
        if (m_po instanceof DocAction) {
            m_emails = new ArrayList<String>();
            sendEMail();
            setTextMsg(m_emails.toString());
        } else {
            MClient client = MClient.get(getCtx(), getAD_Client_ID());
            MMailText mailtext = new MMailText(getCtx(), getNode().getR_MailText_ID(), null);
            String subject = getNode().getDescription() + ": " + mailtext.getMailHeader();
            String message = mailtext.getMailText(true) + "\n-----\n" + getNodeHelp();
            String to = getNode().getEMail();
            client.sendEMail(to, subject, message, null);
        }
        //	done
        return true;
    } else /******	Set Variable				******/
    if (MWFNode.ACTION_SetVariable.equals(action)) {
        String value = m_node.getAttributeValue();
        log.fine("SetVariable:AD_Column_ID=" + m_node.getAD_Column_ID() + " to " + value);
        MColumn column = m_node.getColumn();
        int dt = column.getAD_Reference_ID();
        return setVariable(value, dt, null, trx);
    } else /******	TODO Start WF Instance		******/
    if (MWFNode.ACTION_SubWorkflow.equals(action)) {
        log.warning("Workflow:AD_Workflow_ID=" + m_node.getAD_Workflow_ID());
        log.warning("Start WF Instance is not implemented yet");
    } else /******	User Choice					******/
    if (MWFNode.ACTION_UserChoice.equals(action)) {
        log.fine("UserChoice:AD_Column_ID=" + m_node.getAD_Column_ID());
        //	Approval
        if (m_node.isUserApproval() && getPO(trx) instanceof DocAction) {
            DocAction doc = (DocAction) m_po;
            boolean autoApproval = false;
            //	Approval Hierarchy
            if (isInvoker()) {
                //	Set Approver
                int startAD_User_ID = Env.getAD_User_ID(getCtx());
                if (startAD_User_ID == 0)
                    startAD_User_ID = doc.getDoc_User_ID();
                int nextAD_User_ID = getApprovalUser(startAD_User_ID, doc.getC_Currency_ID(), doc.getApprovalAmt(), doc.getAD_Org_ID(), //	own doc
                startAD_User_ID == doc.getDoc_User_ID());
                //	same user = approved
                autoApproval = startAD_User_ID == nextAD_User_ID;
                if (!autoApproval)
                    setAD_User_ID(nextAD_User_ID);
            } else //	fixed Approver
            {
                MWFResponsible resp = getResponsible();
                // [ 1742751 ] Workflow: User Choice is not working
                if (resp.isHuman()) {
                    autoApproval = resp.getAD_User_ID() == Env.getAD_User_ID(getCtx());
                    if (!autoApproval && resp.getAD_User_ID() != 0)
                        setAD_User_ID(resp.getAD_User_ID());
                } else if (resp.isRole()) {
                    MUserRoles[] urs = MUserRoles.getOfRole(getCtx(), resp.getAD_Role_ID());
                    for (int i = 0; i < urs.length; i++) {
                        if (urs[i].getAD_User_ID() == Env.getAD_User_ID(getCtx())) {
                            autoApproval = true;
                            break;
                        }
                    }
                } else if (resp.isOrganization()) {
                    throw new AdempiereException("Support not implemented for " + resp);
                } else {
                    throw new AdempiereException("@NotSupported@ " + resp);
                }
            // end MZ
            }
            if (autoApproval && doc.processIt(DocAction.ACTION_Approve) && doc.save())
                //	done
                return true;
        }
        //	wait for user
        return false;
    } else /******	User Form					******/
    if (MWFNode.ACTION_UserForm.equals(action)) {
        log.fine("Form:AD_Form_ID=" + m_node.getAD_Form_ID());
        return false;
    } else if (MWFNode.ACTION_SmartBrowse.equals(action)) {
        log.fine("Form:AD_Browse_ID=" + m_node.getAD_Browse_ID());
        return false;
    } else /******	User Window					******/
    if (MWFNode.ACTION_UserWindow.equals(action)) {
        log.fine("Window:AD_Window_ID=" + m_node.getAD_Window_ID());
        return false;
    }
    //
    throw new IllegalArgumentException("Invalid Action (Not Implemented) =" + action);
}
Also used : MColumn(org.compiere.model.MColumn) MProcess(org.compiere.model.MProcess) MAttachment(org.compiere.model.MAttachment) DocAction(org.compiere.process.DocAction) MMailText(org.compiere.model.MMailText) Calendar(java.util.Calendar) MUserRoles(org.compiere.model.MUserRoles) ProcessInfo(org.compiere.process.ProcessInfo) Timestamp(java.sql.Timestamp) SQLException(java.sql.SQLException) AdempiereException(org.adempiere.exceptions.AdempiereException) Savepoint(java.sql.Savepoint) MClient(org.compiere.model.MClient) MPInstance(org.compiere.model.MPInstance) ReportEngine(org.compiere.print.ReportEngine) AdempiereException(org.adempiere.exceptions.AdempiereException) File(java.io.File) MNote(org.compiere.model.MNote)

Example 9 with DocAction

use of org.compiere.process.DocAction in project adempiere by adempiere.

the class MWFActivity method run.

//	getApproval
/**************************************************************************
	 * 	Execute Work.
	 * 	Called from MWFProcess.startNext
	 * 	Feedback to Process via setWFState -> checkActivities
	 */
public void run() {
    log.info("Node=" + getNode());
    m_newValue = null;
    //m_trx = Trx.get(, true);
    Trx trx = null;
    boolean localTrx = false;
    if (get_TrxName() == null) {
        this.set_TrxName(Trx.createTrxName("WFA"));
        localTrx = true;
    }
    trx = Trx.get(get_TrxName(), true);
    Savepoint savepoint = null;
    //
    try {
        if (!localTrx)
            savepoint = trx.setSavepoint(null);
        if (!m_state.isValidAction(StateEngine.ACTION_Start)) {
            setTextMsg("State=" + getWFState() + " - cannot start");
            addTextMsg(new Exception(""));
            setWFState(StateEngine.STATE_Terminated);
            return;
        }
        //
        setWFState(StateEngine.STATE_Running);
        if (getNode().get_ID() == 0) {
            setTextMsg("Node not found - AD_WF_Node_ID=" + getAD_WF_Node_ID());
            setWFState(StateEngine.STATE_Aborted);
            return;
        }
        //	Do Work
        /****	Trx Start	****/
        boolean done = performWork(Trx.get(get_TrxName(), false));
        // Reason: if the commit fails the document should be put in Invalid state
        if (localTrx) {
            try {
                trx.commit(true);
            } catch (Exception e) {
                // If we have a DocStatus, change it to Invalid, and throw the exception to the next level
                if (m_docStatus != null)
                    m_docStatus = DocAction.STATUS_Invalid;
                throw e;
            }
        }
        setWFState(done ? StateEngine.STATE_Completed : StateEngine.STATE_Suspended);
    } catch (Exception e) {
        log.log(Level.WARNING, "" + getNode(), e);
        /****	Trx Rollback	****/
        if (localTrx) {
            trx.rollback();
        } else if (savepoint != null) {
            try {
                trx.rollback(savepoint);
            } catch (SQLException e1) {
            }
        }
        //
        if (e.getCause() != null)
            log.log(Level.WARNING, "Cause", e.getCause());
        String processMsg = e.getLocalizedMessage();
        if (processMsg == null || processMsg.length() == 0)
            processMsg = e.getMessage();
        setTextMsg(processMsg);
        addTextMsg(e);
        //	unlocks
        setWFState(StateEngine.STATE_Terminated);
        //	Set Document Status 
        if (m_po != null && m_po instanceof DocAction && m_docStatus != null) {
            m_po.load(get_TrxName());
            DocAction doc = (DocAction) m_po;
            doc.setDocStatus(m_docStatus);
            m_po.saveEx();
        }
    } finally {
        if (localTrx && trx != null) {
            trx.close();
        }
    }
}
Also used : DocAction(org.compiere.process.DocAction) SQLException(java.sql.SQLException) Savepoint(java.sql.Savepoint) Trx(org.compiere.util.Trx) SQLException(java.sql.SQLException) AdempiereException(org.adempiere.exceptions.AdempiereException)

Aggregations

DocAction (org.compiere.process.DocAction)9 AdempiereException (org.adempiere.exceptions.AdempiereException)5 SQLException (java.sql.SQLException)4 MClient (org.compiere.model.MClient)4 File (java.io.File)3 Savepoint (java.sql.Savepoint)3 PO (org.compiere.model.PO)3 MMailText (org.compiere.model.MMailText)2 MNote (org.compiere.model.MNote)2 Trx (org.compiere.util.Trx)2 Timestamp (java.sql.Timestamp)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 ExportHelper (org.adempiere.process.rpl.exp.ExportHelper)1 MAttachment (org.compiere.model.MAttachment)1 MColumn (org.compiere.model.MColumn)1 MEXPFormat (org.compiere.model.MEXPFormat)1 MForecast (org.compiere.model.MForecast)1