工作流的主要定义:
流程定义
流程实例
参与者
活动
活动实例
流转
工作列表
工作流引擎
processDefinition 定义了流程的业务逻辑,包括业务节点和流程。有向图结构
processinstance 执行期间的体现
包含流程定义被解释之后的一些信息,诸如开始时间,结束时间
其他相关联的信息
task taskinstance
task:
是processdefinition 的组成部分,定义了活动的具体时间
taskinstance
task 被解析执行时的信息,开始时间,结束时间,参与者id
create start end 三种状态
cator
活动的参与者
jbpm目前只需要actor的id,对actor的具体解释是由业务应用负责
任务创建时可以指定任务的参与者,也可以不指定
在一般的系统中不建议采用jbpm的identity组件
工作流系统研究切入点
如何编写流程定义
如何部署流程
如何加载流程
如何启动流程
如何执行任务
如何完成任务
public class JbpmFacadeImpl extends AbstractManager implements JbpmFacade { private JbpmConfiguration jbpmConfiguration; public long addProcessInstance(String processName, Document document) { JbpmContext context = getJbpmContext(); ProcessDefinition def = context.getGraphSession().findLatestProcessDefinition(processName); ProcessInstance instance = new ProcessInstance(def); instance.getContextInstance().setVariable("document", document.getId()); //将公文标题也提交到流程实例变量中,以便在E-Mail中能够提示这个公文的名称 instance.getContextInstance().setVariable("docTitle", document.getTitle()); //将公文的相关属性设置进入流程实例变量 Map props = document.getProperties(); if(props != null){ Set entries = props.entrySet(); for (Iterator iterator = entries.iterator(); iterator.hasNext();) { Map.Entry entry = (Map.Entry) iterator.next(); String propertyName = (String)entry.getKey(); Object obj = document.getProperty(propertyName); //将变量放入流程实例变量 instance.getContextInstance().setVariable(propertyName, obj); } } context.save(instance); return instance.getId(); } public void delProcessDefinition(String processName) { JbpmContext context = getJbpmContext(); List defs = context.getGraphSession().findAllProcessDefinitionVersions(processName); for (Iterator iterator = defs.iterator(); iterator.hasNext();) { ProcessDefinition def = (ProcessDefinition) iterator.next(); context.getGraphSession().deleteProcessDefinition(def); } } public void delProcessInstance(long processInstanceId) { JbpmContext context = getJbpmContext(); context.getGraphSession().deleteProcessInstance(processInstanceId); } public String deployProcessDefinition(byte[] processDef) { JbpmContext context = getJbpmContext(); ProcessDefinition def = ProcessDefinition.parseXmlInputStream( new ByteArrayInputStream(processDef) ); context.deployProcessDefinition(def); return def.getName(); } public String nextStep(long processInstanceId, String actorId, String transitionName) { JbpmContext context = getJbpmContext(); ProcessInstance instance = context.getProcessInstance(processInstanceId); //当前节点 String currentNodeName = instance.getRootToken().getNode().getName(); //起点的名称 String startNodeName = instance.getProcessDefinition().getStartState().getName(); //如果是在起点 if(startNodeName.equals(currentNodeName)){ if(transitionName == null){ instance.signal(); }else{ instance.signal(transitionName); } }else{ List taskInstances = context.getTaskMgmtSession().findTaskInstances(actorId); for (Iterator iterator = taskInstances.iterator(); iterator .hasNext();) { TaskInstance ti = (TaskInstance) iterator.next(); if(ti.getProcessInstance().getId() == processInstanceId){ if(transitionName == null){ ti.end(); }else{ ti.end(transitionName); } break; } } //查找所属组的任务实例 List pooledTaskInstances = context.getTaskMgmtSession().findPooledTaskInstances(actorId); for (Iterator iterator = pooledTaskInstances.iterator(); iterator .hasNext();) { TaskInstance ti = (TaskInstance) iterator.next(); if(ti.getProcessInstance().getId() == processInstanceId){ if(transitionName == null){ ti.end(); }else{ ti.end(transitionName); } break; } } } //返回转向之后的节点名称 return instance.getRootToken().getNode().getName(); } //回退 public Object[] backStep(long processInstanceId, String actorId) { JbpmContext context = getJbpmContext(); //根据流程实例标识查找流程实例 ProcessInstance instance = context.getProcessInstance(processInstanceId); Object[] os = new Object[2]; //搜索用户对应的所有的任务实例 List taskInstances = context.getTaskMgmtSession().findTaskInstances(actorId); for (Iterator iterator = taskInstances.iterator(); iterator .hasNext();) { TaskInstance ti = (TaskInstance) iterator.next(); if(ti.getProcessInstance().getId() == processInstanceId){ //先判断是否是要回退到起点 Set set = ti.getToken().getNode().getArrivingTransitions(); for (Iterator iterator2 = set.iterator(); iterator2.hasNext();) { Transition t = (Transition) iterator2.next(); //如果它需要回退到起点 if(t.getFrom().equals(ti.getProcessInstance().getProcessDefinition().getStartState())){ int docId = (Integer)ti.getProcessInstance().getContextInstance().getVariable("document"); //结束当前的流程实例 ti.getProcessInstance().end(); //结束当前任务实例 ti.end(); //重新创建流程实例对象 ProcessInstance pi = new ProcessInstance(ti.getProcessInstance().getProcessDefinition()); pi.getContextInstance().setVariable("document", docId); //将流程实例对象重新持久化到数据库 context.save(pi); os[0] = Document.STATUS_NEW; os[1] = pi.getId(); return os; } } //如果不需要回退到起点 ti.end(CreateTransitionAction.BACK_TRANSITION); break; } } os[0] = instance.getRootToken().getNode().getName(); os[1] = processInstanceId; return os; } public List searchMyTaskList(String actorId) { JbpmContext context = getJbpmContext(); List docIds = new ArrayList(); List taskInstances = context.getTaskMgmtSession().findTaskInstances(actorId); for (Iterator iterator = taskInstances.iterator(); iterator.hasNext();) { TaskInstance ti = (TaskInstance) iterator.next(); Integer docId = (Integer)ti.getProcessInstance().getContextInstance().getVariable("document"); docIds.add(docId); } //查找所属组的任务实例 List pooledTaskInstances = context.getTaskMgmtSession().findPooledTaskInstances(actorId); for (Iterator iterator = pooledTaskInstances.iterator(); iterator .hasNext();) { TaskInstance ti = (TaskInstance) iterator.next(); Integer docId = (Integer)ti.getProcessInstance().getContextInstance().getVariable("document"); docIds.add(docId); } return docIds; } public List searchNextTransitions(long processInstanceId, String actorId) { JbpmContext context = getJbpmContext(); ProcessInstance instance = context.getProcessInstance(processInstanceId); //当前节点 String currentNodeName = instance.getRootToken().getNode().getName(); //起点的名称 String startNodeName = instance.getProcessDefinition().getStartState().getName(); Collection transitions = null; //如果是在起点 if(startNodeName.equals(currentNodeName)){ transitions = instance.getRootToken().getAvailableTransitions(); }else{ List taskInstances = context.getTaskMgmtSession().findTaskInstances(actorId); for (Iterator iterator = taskInstances.iterator(); iterator .hasNext();) { TaskInstance ti = (TaskInstance) iterator.next(); if(ti.getProcessInstance().getId() == processInstanceId){ transitions = ti.getAvailableTransitions(); break; } } //查找所属组的任务实例 List pooledTaskInstances = context.getTaskMgmtSession().findPooledTaskInstances(actorId); for (Iterator iterator = pooledTaskInstances.iterator(); iterator .hasNext();) { TaskInstance ti = (TaskInstance) iterator.next(); if(ti.getProcessInstance().getId() == processInstanceId){ transitions = ti.getAvailableTransitions(); } } } List transitionNames = new ArrayList(); if(transitions != null){ //为了不把Transition对象直接暴露给OA系统,需要将其转换为名称列表 for (Iterator iterator = transitions.iterator(); iterator.hasNext();) { Transition transition = (Transition) iterator.next(); transitionNames.add(transition.getName()); } } return transitionNames; } private JbpmContext getJbpmContext(){ JbpmContext context = jbpmConfiguration.createJbpmContext(); context.setSession(getSession()); return context; } public void setJbpmConfiguration(JbpmConfiguration jbpmConfiguration) { this.jbpmConfiguration = jbpmConfiguration; } }