From: Jorg J. <jj...@us...> - 2004-03-22 07:25:19
|
Update of /cvsroot/compiere/base/src/org/compiere/wf In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4778/src/org/compiere/wf Modified Files: MWFActivity.java MWFNode.java MWFNodeNext.java MWFProcess.java MWorkflow.java WorkflowServer.java Log Message: . Index: MWFActivity.java =================================================================== RCS file: /cvsroot/compiere/base/src/org/compiere/wf/MWFActivity.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** MWFActivity.java 21 Mar 2004 05:29:34 -0000 1.3 --- MWFActivity.java 22 Mar 2004 07:15:04 -0000 1.4 *************** *** 22,31 **** /** ! * Workflow Activity Model * * @author Jorg Janke * @version $Id$ */ ! public class MWFActivity extends X_AD_WF_Activity { /** --- 22,33 ---- /** ! * Workflow Activity Model. ! * Controlled by WF Process: ! * set Node - startWork * * @author Jorg Janke * @version $Id$ */ ! public class MWFActivity extends X_AD_WF_Activity implements Runnable { /** *************** *** 55,61 **** /** * Parent Contructor ! * @param process */ ! public MWFActivity (MWFProcess process) { super (process.getCtx(), 0); --- 57,64 ---- /** * Parent Contructor ! * @param process process ! * @param AD_WF_Node_ID start node */ ! public MWFActivity (MWFProcess process, int AD_WF_Node_ID) { super (process.getCtx(), 0); *************** *** 63,81 **** setAD_WF_Responsible_ID (process.getAD_WF_Responsible_ID()); setAD_User_ID(process.getAD_User_ID()); ! setWFState (WFSTATE_NotStarted); ! setProcessed (false); ! // Get First Workflow Node ! m_AD_Workflow_ID = process.getAD_Workflow_ID(); ! m_workflow = MWorkflow.get (getCtx(), m_AD_Workflow_ID); ! setAD_WF_Node_ID (m_workflow.getAD_WF_Node_ID()); m_state = new StateEngine (getWFState()); } // MWFActivity /** State Machine */ private StateEngine m_state = null; ! /** Workflow ID */ ! private int m_AD_Workflow_ID = 0; ! /** Workflow */ ! private MWorkflow m_workflow = null; /** --- 66,85 ---- setAD_WF_Responsible_ID (process.getAD_WF_Responsible_ID()); setAD_User_ID(process.getAD_User_ID()); ! // ! setAD_Table_ID(process.getAD_Table_ID()); ! setRecord_ID(process.getRecord_ID()); ! // ! super.setWFState(WFSTATE_NotStarted); m_state = new StateEngine (getWFState()); + setProcessed (false); + // Set First Workflow Node + setAD_Workflow_ID (process.getAD_Workflow_ID()); + setAD_WF_Node_ID (AD_WF_Node_ID); } // MWFActivity /** State Machine */ private StateEngine m_state = null; ! /** Workflow Node */ ! private MWFNode m_node = null; /** *************** *** 94,97 **** --- 98,103 ---- public void setWFState (String WFState) { + if (m_state == null) + m_state = new StateEngine (getWFState()); if (m_state.isClosed()) return; *************** *** 99,109 **** { String oldState = getWFState(); ! log.debug("setWFState - " + WFState); super.setWFState (WFState); m_state = new StateEngine (getWFState()); if (m_state.isClosed()) setProcessed(true); - if (WFSTATE_Running.equals(getState())) - performRun(); save(); // Inform Process --- 105,113 ---- { String oldState = getWFState(); ! log.debug("setWFState - " + WFState + " - " + getTextMsg()); super.setWFState (WFState); m_state = new StateEngine (getWFState()); if (m_state.isClosed()) setProcessed(true); save(); // Inform Process *************** *** 115,156 **** } else ! log.error("setWFState - Ignored Invalid Transformation - New=" + WFState + ", Current=" + getWFState()); } // setWFState /** ! * Perform Action ! * @param action State.ACTION_* ! * @return true if valid */ ! public boolean perform (String action) { ! if (!m_state.isValidAction(action)) { ! log.error("perform - Ignored Invalid Transformation - Action=" + action ! + ", CurrentState=" + getWFState()); ! return false; } ! log.debug("perform - " + action); ! // Action is Valid ! setWFState (m_state.getNewStateIfAction(action)); ! return true; ! } // perform /** ! * Perform Run. ! * Execute WF Step */ ! private void performRun() { ! MWFNode node = null; ! if (m_workflow != null) ! node = m_workflow.getNode(getAD_WF_Node_ID()); ! if (node == null) ! node = MWFNode.get (getCtx(), getAD_WF_Node_ID()); // ! log.info ("performRun - " + node); ! } // performRun } // MWFActivity --- 119,202 ---- } else ! log.warn("setWFState - Ignored Invalid Transformation - New=" + WFState + ", Current=" + getWFState()); } // setWFState + /** ! * Set AD_WF_Node_ID. ! * (Re)Set to Not Started ! * @param AD_WF_Node_ID now node */ ! public void setAD_WF_Node_ID (int AD_WF_Node_ID) { ! if (AD_WF_Node_ID == 0) ! throw new IllegalArgumentException("Workflow Node is not defined"); ! super.setAD_WF_Node_ID (AD_WF_Node_ID); ! // ! if (!WFSTATE_NotStarted.equals(getWFState())) { ! super.setWFState(WFSTATE_NotStarted); ! m_state = new StateEngine (getWFState()); } ! if (isProcessed()) ! setProcessed (false); ! save(); ! } // setAD_WF_Node_ID ! ! ! public MWFNode getNode() ! { ! if (m_node == null) ! m_node = MWFNode.get (getCtx(), getAD_WF_Node_ID()); ! return m_node; ! } // getNode /** ! * Execute Work. ! * Feedback to Process via setWFState -> checkActivities */ ! public void run() { ! log.info ("run - " + getAD_WF_Node_ID()); ! if (!m_state.isValidAction(StateEngine.ACTION_Start)) ! { ! setTextMsg("State=" + getWFState() + " - cannot start"); ! setWFState(StateEngine.STATE_Terminated); ! return; ! } // ! setWFState(StateEngine.STATE_Running); ! // ! try ! { ! if (getNode().getID() == 0) ! { ! setTextMsg("Node not found - AD_WF_Node_ID=" + getAD_WF_Node_ID()); ! setWFState(StateEngine.STATE_Aborted); ! return; ! } ! // ! boolean done = performWork(); ! setWFState(done ? StateEngine.STATE_Completed : StateEngine.STATE_Suspended); ! } ! catch (Exception e) ! { ! log.error("run", e); ! setTextMsg(e.toString()); ! setWFState(StateEngine.STATE_Terminated); ! } ! } // run + /** + * Perform Work. + * @return true if completed, false otherwise + */ + private boolean performWork() + { + log.info ("performWork - " + m_node); + return true; + } // performWork; + } // MWFActivity Index: MWFNode.java =================================================================== RCS file: /cvsroot/compiere/base/src/org/compiere/wf/MWFNode.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MWFNode.java 17 Mar 2004 06:46:35 -0000 1.2 --- MWFNode.java 22 Mar 2004 07:15:04 -0000 1.3 *************** *** 91,95 **** private void loadNext() { ! String sql = "SELECT * FROM AD_WF_NodeNext WHERE AD_WF_Node_ID=? AND IsActive='Y'"; try { --- 91,95 ---- private void loadNext() { ! String sql = "SELECT * FROM AD_WF_NodeNext WHERE AD_WF_Node_ID=? AND IsActive='Y' ORDER BY SeqNo"; try { *************** *** 150,157 **** /** ! * Get the next nodes * @return array of next nodes */ ! public MWFNodeNext[] getNextNodes() { MWFNodeNext[] retValue = new MWFNodeNext [m_next.size()]; --- 150,157 ---- /** ! * Get the transitions * @return array of next nodes */ ! public MWFNodeNext[] getNodeNexts() { MWFNodeNext[] retValue = new MWFNodeNext [m_next.size()]; *************** *** 217,219 **** --- 217,232 ---- } // getPosition + /** + * String Representation + * @return info + */ + public String toString () + { + StringBuffer sb = new StringBuffer ("MWFNode["); + sb.append(getID()).append("-").append(getName()) + .append(",Action=").append(getAction()) + .append ("]"); + return sb.toString (); + } // toString + } // M_WFNext Index: MWFNodeNext.java =================================================================== RCS file: /cvsroot/compiere/base/src/org/compiere/wf/MWFNodeNext.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MWFNodeNext.java 3 Jan 2004 01:56:08 -0000 1.1 --- MWFNodeNext.java 22 Mar 2004 07:15:04 -0000 1.2 *************** *** 53,56 **** --- 53,70 ---- } // MWFNodeNext + /** + * String Representation + * @return info + */ + public String toString () + { + StringBuffer sb = new StringBuffer ("MWFNodeNext["); + sb.append(getSeqNo()) + .append(":Node=").append(getAD_WF_Node_ID()).append("->Next=").append(getAD_WF_Next_ID()) + .append("-").append(getDescription()) + .append ("]"); + return sb.toString (); + } // toString + /*************************************************************************/ Index: MWFProcess.java =================================================================== RCS file: /cvsroot/compiere/base/src/org/compiere/wf/MWFProcess.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MWFProcess.java 21 Mar 2004 05:29:34 -0000 1.2 --- MWFProcess.java 22 Mar 2004 07:15:04 -0000 1.3 *************** *** 57,72 **** * New Constructor * @param wf workflow */ ! public MWFProcess (MWorkflow wf) { super (wf.getCtx(), 0); if (!TimeUtil.isValid(wf.getValidFrom(), wf.getValidTo())) throw new IllegalStateException("Workflow not valid"); setAD_Workflow_ID (wf.getAD_Workflow_ID()); - setAD_WF_Responsible_ID (wf.getAD_WF_Responsible_ID()); setAD_User_ID(Env.getAD_User_ID(wf.getCtx())); super.setWFState (WFSTATE_NotStarted); - setProcessed (false); m_state = new StateEngine (getWFState()); } // MWFProcess --- 57,81 ---- * New Constructor * @param wf workflow + * @param Record_ID record */ ! public MWFProcess (MWorkflow wf, int Record_ID) { super (wf.getCtx(), 0); if (!TimeUtil.isValid(wf.getValidFrom(), wf.getValidTo())) throw new IllegalStateException("Workflow not valid"); + m_wf = wf; setAD_Workflow_ID (wf.getAD_Workflow_ID()); setAD_User_ID(Env.getAD_User_ID(wf.getCtx())); + if (wf.getAD_WF_Responsible_ID() == 0) + setAD_WF_Responsible_ID(); + else + setAD_WF_Responsible_ID(wf.getAD_WF_Responsible_ID()); + // + setAD_Table_ID(wf.getAD_Table_ID()); + setRecord_ID(Record_ID); + // super.setWFState (WFSTATE_NotStarted); m_state = new StateEngine (getWFState()); + setProcessed (false); } // MWFProcess *************** *** 74,78 **** private StateEngine m_state = null; /** Activities */ ! private MWFActivity[] m_activities = null; /** --- 83,89 ---- private StateEngine m_state = null; /** Activities */ ! private MWFActivity[] m_activities = null; ! /** Workflow */ ! private MWorkflow m_wf = null; /** *************** *** 143,146 **** --- 154,161 ---- public void setWFState (String WFState) { + if (m_state == null) + m_state = new StateEngine (getWFState()); + if (m_state.isClosed()) + return; if (m_state.isValidNewState(WFState)) { *************** *** 167,172 **** --- 182,189 ---- public void checkActivities() { + log.info("checkActivities (" + getAD_Workflow_ID() + ") - " + getWFState()); if (m_state.isClosed()) return; + // MWFActivity[] activities = getActivities(true); String closedState = null; *************** *** 174,177 **** --- 191,200 ---- { String activityState = activities[i].getWFState(); + // Completed - Start Next + if (activities[i].getState().isCompleted()) + { + if (!startNext (activities[i])) + continue; // nothing to start + } if (!activities[i].getState().isClosed()) closedState = null; // all need to be closed *************** *** 191,196 **** --- 214,248 ---- setWFState(closedState); } // checkActivities + /** + * Set Workflow Responsible. + * Searches for a Invoker. + */ + public void setAD_WF_Responsible_ID () + { + int AD_WF_Responsible_ID = DB.getSQLValue( + MRole.getDefault(getCtx(), false).addAccessSQL( + "SELECT AD_WF_Responsible_ID FROM AD_WF_Responsible " + + "WHERE ResponsibleType='H' AND COALESCE(AD_User_ID,0)=0 " + + "ORDER BY AD_Client_ID DESC", + "AD_WF_Responsible", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO)); + setAD_WF_Responsible_ID (AD_WF_Responsible_ID); + } // setAD_WF_Responsible_ID + + /** + * Get Workflow + * @return workflow + */ + private MWorkflow getWorkflow() + { + if (m_wf == null) + m_wf = new MWorkflow (getCtx(), getAD_Workflow_ID()); + if (m_wf.getID() == 0) + throw new IllegalStateException("Workflow not found - AD_Workflow_ID=" + getAD_Workflow_ID()); + return m_wf; + } // getWorkflow + + /************************************************************************** * Perform Action * @param action State.ACTION_* *************** *** 208,212 **** // Action is Valid if (StateEngine.ACTION_Start.equals(action)) ! return performStart(); // Set new State setWFState (m_state.getNewStateIfAction(action)); --- 260,264 ---- // Action is Valid if (StateEngine.ACTION_Start.equals(action)) ! return startWork(); // Set new State setWFState (m_state.getNewStateIfAction(action)); *************** *** 218,233 **** * @return true if success */ ! public boolean performStart () { if (!m_state.isValidAction(StateEngine.ACTION_Start)) return false; log.debug("performStart"); - if (getID() == 0) // needs to be saved - save(); - MWFActivity activity = new MWFActivity (this); - activity.save(); setWFState(WFSTATE_Running); return true; } // performStart } // MWFProcess --- 270,331 ---- * @return true if success */ ! public boolean startWork() { if (!m_state.isValidAction(StateEngine.ACTION_Start)) + { + log.warn("startWork - State=" + getWFState() + " - cannot start"); return false; + } log.debug("performStart"); setWFState(WFSTATE_Running); + try + { + // Start first Activity with first Node + MWFActivity activity = new MWFActivity (this, getWorkflow().getAD_WF_Node_ID()); + new Thread(activity).start(); + } + catch (Exception e) + { + log.error("startWork", e); + setTextMsg(e.toString()); + setWFState(StateEngine.STATE_Terminated); + return false; + } return true; } // performStart + /** + * Start Next Activity + * @param activity activity + * @return true if there is a next activity + */ + private boolean startNext (MWFActivity activity) + { + MWFNodeNext[] transitions = getWorkflow().getNodeNexts(activity.getAD_WF_Node_ID()); + if (transitions == null || transitions.length == 0) + return false; // done + // + String split = activity.getNode().getSplitElement(); + boolean first = true; + for (int i = 0; i < transitions.length; i++) + { + // Is this a valid transition? + + // first Activity + if (first) + { + activity.setAD_WF_Node_ID(transitions[i].getAD_WF_Next_ID()); + new Thread(activity).start(); + first = false; + if (MWFNode.SPLITELEMENT_XOR.equals(split)) + return true; + } + else // New Activity + { + + } + } // for all transitions + return true; + } // startNext + } // MWFProcess Index: MWorkflow.java =================================================================== RCS file: /cvsroot/compiere/base/src/org/compiere/wf/MWorkflow.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** MWorkflow.java 21 Mar 2004 05:29:34 -0000 1.3 --- MWorkflow.java 22 Mar 2004 07:15:04 -0000 1.4 *************** *** 195,199 **** return null; // ! MWFNodeNext[] nexts = node.getNextNodes(); ArrayList list = new ArrayList(); for (int i = 0; i < nexts.length; i++) --- 195,199 ---- return null; // ! MWFNodeNext[] nexts = node.getNodeNexts(); ArrayList list = new ArrayList(); for (int i = 0; i < nexts.length; i++) *************** *** 240,244 **** { list.add(node); ! MWFNodeNext[] nexts = node.getNextNodes(); for (int i = 0; i < nexts.length; i++) addNode (list, nexts[i].getAD_WF_Next_ID()); --- 240,244 ---- { list.add(node); ! MWFNodeNext[] nexts = node.getNodeNexts(); for (int i = 0; i < nexts.length; i++) addNode (list, nexts[i].getAD_WF_Next_ID()); *************** *** 249,253 **** /** ! * Get (first) Next Node of ID * @param AD_WF_Node_ID id * @return next AD_WF_Node_ID or 0 --- 249,253 ---- /** ! * Get first transition (Next Node) of ID * @param AD_WF_Node_ID id * @return next AD_WF_Node_ID or 0 *************** *** 260,264 **** if (nodes[i].getAD_WF_Node_ID() == AD_WF_Node_ID) { ! MWFNodeNext[] nexts = nodes[i].getNextNodes(); if (nexts.length > 0) return nexts[0].getAD_WF_Next_ID(); --- 260,264 ---- if (nodes[i].getAD_WF_Node_ID() == AD_WF_Node_ID) { ! MWFNodeNext[] nexts = nodes[i].getNodeNexts(); if (nexts.length > 0) return nexts[0].getAD_WF_Next_ID(); *************** *** 270,273 **** --- 270,291 ---- /** + * Get Transitions (NodeNext) of ID + * @param AD_WF_Node_ID id + * @return array of next nodes + */ + public MWFNodeNext[] getNodeNexts (int AD_WF_Node_ID) + { + MWFNode[] nodes = getNodesInOrder(); + for (int i = 0; i < nodes.length; i++) + { + if (nodes[i].getAD_WF_Node_ID() == AD_WF_Node_ID) + { + return nodes[i].getNodeNexts(); + } + } + return null; + } // getNext + + /** * Get (first) Previous Node of ID * @param AD_WF_Node_ID id *************** *** 379,396 **** /** ! * Start Workflow * @return process */ ! public MWFProcess start() { MWFProcess retValue = null; try { ! retValue = new MWFProcess (this); retValue.save(); } catch (Exception e) { ! log.error("start - " + e.getMessage()); } return retValue; --- 397,416 ---- /** ! * Start Workflow. ! * @param Record_ID record for the workflow * @return process */ ! public MWFProcess start (int Record_ID) { MWFProcess retValue = null; try { ! retValue = new MWFProcess (this, Record_ID); retValue.save(); + retValue.startWork(); } catch (Exception e) { ! log.error("start - " + e.getLocalizedMessage(), e); } return retValue; *************** *** 404,409 **** { org.compiere.Compiere.startupClient(); ! MWorkflow wf = new MWorkflow (Env.getCtx(), 0); ! } // main --- 424,429 ---- { org.compiere.Compiere.startupClient(); ! MWorkflow wf = new MWorkflow (Env.getCtx(), 115); // Requisition WF ! wf.start(100); // M_Requsition_ID } // main Index: WorkflowServer.java =================================================================== RCS file: /cvsroot/compiere/base/src/org/compiere/wf/WorkflowServer.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** WorkflowServer.java 17 Mar 2004 06:46:35 -0000 1.1 --- WorkflowServer.java 22 Mar 2004 07:15:04 -0000 1.2 *************** *** 99,102 **** --- 99,105 ---- public static void main (String[] args) { + System.out.println (new java.awt.Rectangle(1,2,3,4)); + + org.compiere.Compiere.startupClient(); MWorkflowProcessor processor = new MWorkflowProcessor (Env.getCtx(), 100); |