📄 mworkflow.java
字号:
} // getNext
/**
* Get Transitions (NodeNext) of ID
* @param AD_WF_Node_ID id
* @param AD_Client_ID for client
* @return array of next nodes
*/
public MWFNodeNext[] getNodeNexts (int AD_WF_Node_ID, int AD_Client_ID)
{
MWFNode[] nodes = getNodesInOrder(AD_Client_ID);
for (int i = 0; i < nodes.length; i++)
{
if (nodes[i].getAD_WF_Node_ID() == AD_WF_Node_ID)
{
return nodes[i].getTransitions(AD_Client_ID);
}
}
return null;
} // getNext
/**
* Get (first) Previous Node of ID
* @param AD_WF_Node_ID id
* @param AD_Client_ID for client
* @return next AD_WF_Node_ID or 0
*/
public int getPrevious (int AD_WF_Node_ID, int AD_Client_ID)
{
MWFNode[] nodes = getNodesInOrder(AD_Client_ID);
for (int i = 0; i < nodes.length; i++)
{
if (nodes[i].getAD_WF_Node_ID() == AD_WF_Node_ID)
{
if (i > 0)
return nodes[i-1].getAD_WF_Node_ID();
return 0;
}
}
return 0;
} // getPrevious
/**
* Get very Last Node
* @param AD_WF_Node_ID ignored
* @param AD_Client_ID for client
* @return next AD_WF_Node_ID or 0
*/
public int getLast (int AD_WF_Node_ID, int AD_Client_ID)
{
MWFNode[] nodes = getNodesInOrder(AD_Client_ID);
if (nodes.length > 0)
return nodes[nodes.length-1].getAD_WF_Node_ID();
return 0;
} // getLast
/**
* Is this the first Node
* @param AD_WF_Node_ID id
* @param AD_Client_ID for client
* @return true if first node
*/
public boolean isFirst (int AD_WF_Node_ID, int AD_Client_ID)
{
return AD_WF_Node_ID == getAD_WF_Node_ID();
} // isFirst
/**
* Is this the last Node
* @param AD_WF_Node_ID id
* @param AD_Client_ID for client
* @return true if last node
*/
public boolean isLast (int AD_WF_Node_ID, int AD_Client_ID)
{
MWFNode[] nodes = getNodesInOrder(AD_Client_ID);
return AD_WF_Node_ID == nodes[nodes.length-1].getAD_WF_Node_ID();
} // isLast
/**************************************************************************
* Get Name
* @param translated translated
* @return Name
*/
public String getName(boolean translated)
{
if (translated && m_translated)
return m_name_trl;
return getName();
} // getName
/**
* Get Description
* @param translated translated
* @return Description
*/
public String getDescription (boolean translated)
{
if (translated && m_translated)
return m_description_trl;
return getDescription();
} // getDescription
/**
* Get Help
* @param translated translated
* @return Name
*/
public String getHelp (boolean translated)
{
if (translated && m_translated)
return m_help_trl;
return getHelp();
} // getHelp
/**
* String Representation
* @return info
*/
public String toString ()
{
StringBuffer sb = new StringBuffer ("MWorkflow[");
sb.append(get_ID()).append("-").append(getName())
.append ("]");
return sb.toString ();
} // toString
/**************************************************************************
* Before Save
* @param newRecord new
* @return true
*/
protected boolean beforeSave (boolean newRecord)
{
validate();
return true;
} // beforeSave
/**
* After Save.
* @param newRecord new record
* @param success success
* @return true if save complete (if not overwritten true)
*/
protected boolean afterSave (boolean newRecord, boolean success)
{
log.fine("Success=" + success);
if (success && newRecord)
{
// save all nodes -- Creating new Workflow
MWFNode[] nodes = getNodesInOrder(0);
for (int i = 0; i < nodes.length; i++)
nodes[i].save(get_TrxName());
}
if (newRecord)
{
int AD_Role_ID = Env.getAD_Role_ID(getCtx());
MWorkflowAccess wa = new MWorkflowAccess(this, AD_Role_ID);
wa.save();
}
// Menu/Workflow
else if (is_ValueChanged("IsActive") || is_ValueChanged("Name")
|| is_ValueChanged("Description") || is_ValueChanged("Help"))
{
MMenu[] menues = MMenu.get(getCtx(), "AD_Workflow_ID=" + getAD_Workflow_ID());
for (int i = 0; i < menues.length; i++)
{
menues[i].setIsActive(isActive());
menues[i].setName(getName());
menues[i].setDescription(getDescription());
menues[i].save();
}
X_AD_WF_Node[] nodes = M_Window.getWFNodes(getCtx(), "AD_Workflow_ID=" + getAD_Workflow_ID());
for (int i = 0; i < nodes.length; i++)
{
boolean changed = false;
if (nodes[i].isActive() != isActive())
{
nodes[i].setIsActive(isActive());
changed = true;
}
if (nodes[i].isCentrallyMaintained())
{
nodes[i].setName(getName());
nodes[i].setDescription(getDescription());
nodes[i].setHelp(getHelp());
changed = true;
}
if (changed)
nodes[i].save();
}
}
return success;
} // afterSave
/**************************************************************************
* Start Workflow.
* @param pi Process Info (Record_ID)
* @return process
*/
public MWFProcess start (ProcessInfo pi)
{
MWFProcess retValue = null;
try
{
retValue = new MWFProcess (this, pi);
retValue.save();
retValue.startWork();
}
catch (Exception e)
{
log.log(Level.SEVERE, e.getLocalizedMessage(), e);
pi.setSummary(e.getMessage(), true);
retValue = null;
}
return retValue;
} // MWFProcess
/**
* Start Workflow and Wait for completion.
* @param pi process info with Record_ID record for the workflow
* @return process
*/
public MWFProcess startWait (ProcessInfo pi)
{
final int SLEEP = 500; // 1/2 sec
final int MAXLOOPS = 30; // 15 sec
//
MWFProcess process = start(pi);
if (process == null)
return null;
Thread.yield();
StateEngine state = process.getState();
int loops = 0;
while (!state.isClosed() && !state.isSuspended())
{
if (loops > MAXLOOPS)
{
log.warning("Timeout after sec " + ((SLEEP*MAXLOOPS)/1000));
pi.setSummary("Started ... still running (requery later)");
return process;
}
// System.out.println("--------------- " + loops + ": " + state);
try
{
Thread.sleep(SLEEP);
loops++;
}
catch (InterruptedException e)
{
log.log(Level.SEVERE, "startWait interrupted", e);
pi.setSummary("Interrupted");
return process;
}
Thread.yield();
state = process.getState();
}
String summary = process.getProcessMsg();
if (summary == null || summary.trim().length() == 0)
summary = state.toString();
pi.setSummary(summary, state.isTerminated() || state.isAborted());
log.fine(summary);
return process;
} // startWait
/**
* Get Duration Base in Seconds
* @return duration unit in seconds
*/
public long getDurationBaseSec ()
{
if (getDurationUnit() == null)
return 0;
else if (DURATIONUNIT_Second.equals(getDurationUnit()))
return 1;
else if (DURATIONUNIT_Minute.equals(getDurationUnit()))
return 60;
else if (DURATIONUNIT_Hour.equals(getDurationUnit()))
return 3600;
else if (DURATIONUNIT_Day.equals(getDurationUnit()))
return 86400;
else if (DURATIONUNIT_Month.equals(getDurationUnit()))
return 2592000;
else if (DURATIONUNIT_Year.equals(getDurationUnit()))
return 31536000;
return 0;
} // getDurationBaseSec
/**
* Get Duration CalendarField
* @return Calendar.MINUTE, etc.
*/
public int getDurationCalendarField()
{
if (getDurationUnit() == null)
return Calendar.MINUTE;
else if (DURATIONUNIT_Second.equals(getDurationUnit()))
return Calendar.SECOND;
else if (DURATIONUNIT_Minute.equals(getDurationUnit()))
return Calendar.MINUTE;
else if (DURATIONUNIT_Hour.equals(getDurationUnit()))
return Calendar.HOUR;
else if (DURATIONUNIT_Day.equals(getDurationUnit()))
return Calendar.DAY_OF_YEAR;
else if (DURATIONUNIT_Month.equals(getDurationUnit()))
return Calendar.MONTH;
else if (DURATIONUNIT_Year.equals(getDurationUnit()))
return Calendar.YEAR;
return Calendar.MINUTE;
} // getDurationCalendarField
/**************************************************************************
* Validate workflow.
* Sets Valid flag
* @return errors or ""
*/
public String validate()
{
StringBuffer errors = new StringBuffer();
//
if (getAD_WF_Node_ID() == 0)
errors.append(" - No Start Node");
//
if (WORKFLOWTYPE_DocumentValue.equals(getWorkflowType())
&& (getDocValueLogic() == null || getDocValueLogic().length() == 0))
errors.append(" - No Document Value Logic");
//
// final
boolean valid = errors.length() == 0;
setIsValid(valid);
if (!valid)
log.info("validate: " + errors);
return errors.toString();
} // validate
/**************************************************************************
* main
* @param args
*/
public static void main (String[] args)
{
org.compiere.Compiere.startup(true);
// Create Standard Document Process
MWorkflow wf = new MWorkflow(Env.getCtx(), 0, null);
wf.setValue ("Process_xx");
wf.setName (wf.getValue());
wf.setDescription("(Standard " + wf.getValue());
wf.setEntityType (ENTITYTYPE_Dictionary);
wf.save();
//
MWFNode node10 = new MWFNode (wf, "10", "(Start)");
node10.setDescription("(Standard Node)");
node10.setEntityType (ENTITYTYPE_Dictionary);
node10.setAction(MWFNode.ACTION_WaitSleep);
node10.setWaitTime(0);
node10.setPosition(5, 5);
node10.save();
wf.setAD_WF_Node_ID(node10.getAD_WF_Node_ID());
wf.save();
MWFNode node20 = new MWFNode (wf, "20", "(DocAuto)");
node20.setDescription("(Standard Node)");
node20.setEntityType (ENTITYTYPE_Dictionary);
node20.setAction(MWFNode.ACTION_DocumentAction);
node20.setDocAction(MWFNode.DOCACTION_None);
node20.setPosition(5, 120);
node20.save();
MWFNodeNext tr10_20 = new MWFNodeNext(node10, node20.getAD_WF_Node_ID());
tr10_20.setEntityType (ENTITYTYPE_Dictionary);
tr10_20.setDescription("(Standard Transition)");
tr10_20.setSeqNo(100);
tr10_20.save();
MWFNode node100 = new MWFNode (wf, "100", "(DocPrepare)");
node100.setDescription("(Standard Node)");
node100.setEntityType (ENTITYTYPE_Dictionary);
node100.setAction(MWFNode.ACTION_DocumentAction);
node100.setDocAction(MWFNode.DOCACTION_Prepare);
node100.setPosition(170, 5);
node100.save();
MWFNodeNext tr10_100 = new MWFNodeNext(node10, node100.getAD_WF_Node_ID());
tr10_100.setEntityType (ENTITYTYPE_Dictionary);
tr10_100.setDescription("(Standard Approval)");
tr10_100.setIsStdUserWorkflow(true);
tr10_100.setSeqNo(10);
tr10_100.save();
MWFNode node200 = new MWFNode (wf, "200", "(DocComplete)");
node200.setDescription("(Standard Node)");
node200.setEntityType (ENTITYTYPE_Dictionary);
node200.setAction(MWFNode.ACTION_DocumentAction);
node200.setDocAction(MWFNode.DOCACTION_Complete);
node200.setPosition(170, 120);
node200.save();
MWFNodeNext tr100_200 = new MWFNodeNext(node100, node200.getAD_WF_Node_ID());
tr100_200.setEntityType (ENTITYTYPE_Dictionary);
tr100_200.setDescription("(Standard Transition)");
tr100_200.setSeqNo(100);
tr100_200.save();
/**
Env.setContext(Env.getCtx(), "#AD_Client_ID ", "11");
Env.setContext(Env.getCtx(), "#AD_Org_ID ", "11");
Env.setContext(Env.getCtx(), "#AD_User_ID ", "100");
//
int AD_Workflow_ID = 115; // Requisition WF
int M_Requsition_ID = 100;
MRequisition req = new MRequisition (Env.getCtx(), M_Requsition_ID);
req.setDocStatus(DocAction.DOCSTATUS_Drafted);
req.save();
Log.setTraceLevel(8);
System.out.println("---------------------------------------------------");
MWorkflow wf = MWorkflow.get (Env.getCtx(), AD_Workflow_ID);
**/
// wf.start(M_Requsition_ID);
} // main
} // MWorkflow_ID
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -