public interface WorkflowEngine{ /** 核心是:拿到定义,部署定义 *inputStream the content of process definition id of the new deployed process remark */ public long deployProcessDefinition(InputStream inputStream,String remark); /* *启动流程之后,第一个节点随之结束,执行点在第二个节点 pdname 流程定义的名称 starter 流程的启动者 version 流程定义的版本,如果不为空,则启动指定版本的流程 long 返回刚启动的流程实例的id varMap 变量列表,在工作流启动的时候,可以设置些变量进去 */ public String startProcess(Strin pname,String starter,String version,Map varMap); /** * 获取参与者指定状态的任务列表 actorId 参与者id status 任务的状态 PagedObject 分页后的结果集 */ public PagedObject getWorkList(String actorId,WorkitemCriteria wc); /** * 办理任务,主要实现任务的签收 actorId 任务参与者id wid 工作项id */ public String takeWorkitem(String actorId,String wid); /** *完成任务 */ public void completeWorkitem(String currentWorkItemId ,NextStep nextStep); } public class JbpmImpl implements WorkflowEngine{ public String deployProcessDefinition(InputStream inputStream,String remark){ JbpmContext jbpmContext = JbpmConfiguration.getInstance().getCurrentJbpmContext(); ProcessDefinition pd = ProcessDefinition.parseParZipInputStream(new ZipInputStream(inputStream)); jbpmContext.deployProcessDefinition(pd); //可以将remark存入我们自己扩充的表里 return Long.toString(pd.getId()); } public String startProcess(String pdname,String starter,String version,Map varMap){ ProcessDefinition pd = null; JbpmContext jbpmContext = JbpmConfiguration.getInstance().getCurrentJbpmContext(); if(version != null){ //如果version为空,则获得最新版本的流程定义 pd = jbpmContext.getGraphSession().findProcessDefinition(pdname,Integer.parseInt(version)); }else{ pd = jbpmContext.getGraphSession().findLatestProcessDefinition(pdname); } ProcessInstance pi = new ProcessInstance(pd); if(varMap != null){ for(Iterator it =varMap.keySet().iterator();it.hasNext()){ String varName = (String)it.next(); pi.getContextInstance().setVariable(varName,varMap.get(varName)); } } pi.getInstance().setVariable("starter",starter); //为了以后的节点可以拿到它 jbpmContext.setActorId(starter); //设置参与者 //如果开始节点有Task和没有Task的不同处理,如果没有Task,那么endTime当然可以不用设置 if(pi.getTaskMgmtInstance().getTaskMgmtDefinition().getStartTask() != null){ TaskInstance ti = pi.getTaskMgmtInstance().createStartTaskInstance(); ti.start(); ti.end() }else{ pi.signal(); } jbpmContext.save(pi); return Long.toString(pi.getId()); } public PagedObject getWorkList(String actorId,WorkitemCriteria wc){ PagedObject po = new PagedObject(); JbpmContext jbpmContext = JbpmConfiguration.getInstance().getCurrentJbpmContext(); Criteria rootCriteria = jbpmContext.getSession().createCriteria(TaskInstance.class); if(wc.getPdname() != null && !"".equals(wc.getPdname())) rootCriteria.createAlias("processInstance","pi") .createAlias("pi.processDefinition","pd") .add(Expression.like("pd.name"),wc.getPdname()); if(wc.getActorIdea() != null && !"".equals(wc.getActorIdea())) rootCriteria.add(Expression.eq("actorId",actorId)); if(wc.getTname()!= null && !"".equals(wc.getTname())) rootCriteria.add(Expression.like("name",wc.getTname())); if(wc.getStatus() != null){ if("0".equals(wc.getStatus())) //刚创建 rootCriteria.add(Expression.and(Expression.isNull("start"),Expression.isNull("end"))); if("1".equals(wc.getStatus())) //任务已经启动 rootCriteria.add(Expression.and(Expression.isNotNull("start"),Expression.isNull("end"))); if("2".equals(wc.getStatus())) //任务完成 rootCriteria.add(Expression.and(Expression.isNotNull("start"),Expression.isNotNull("end"))); } int total = (Integer)(rootCriteria.setProjection(org.hibernate.criterion.Projections.countDistinct("id")).uniqueResult()).intValue(); rootCriteria.setProjection(null); rootCriteria.setMaxResults(wc.getNeveryPage()); rootCriteria.setFirstResult(wc.getStart()); List list = rootCriteria.setResultTransformer(Criteria.DISINCT_ROOT_ENTITY).list(); po.setResult(list); po.setTotal(total); po.setNpages((total+wc.getNereryPage()-1)/wc.getNereryPage()); return po; } public String takeWorkitem(String actorId,String wid){ JbpmContext jbpmContext = JbmpConfiguration.getInstance().getCurrentJbpmContext(); TaskInstance ti = jbpmContext.loadTaskInstance(Long.parseLong(wid)); if(ti.getStart() != null){ ti.start(); if(actorId != null)ti.start(actorId); } String pdname = ti.getProcessInstance().getProcessDefinition().getName(); String view = ProcessViewMap.getView(pdname); return view } //完成任务的核心是 ti.end(),但是是需要end到下 public void completeWorkitem(String currentWorkItemId ,NextStep nextStep){ JbpmContext jbpmContext = JbpmConfiguration.getInstance().getCurrentJbpmContext(); TaskInstance ti = jbpmContext.loadTaskInstance(Long.parseLong(currentWorkItemId)); if(nextStep != null){ if(nextStep.getActorId() != null&&!"".equals(nextStep.getActorId())) //下一步可能有多个执行者,把这个设置进去 ti.getProcessInstance().getContextInstance().setVariable("actorId",nextStep.getActorId()); if(nextStep.getNextStepName() != null && !"".equals(nextStep.getNextStepName())){ ti.end(nextStep.getNextStepName()); } }else{ ti.end(); } } } public class ServiceLocator{ public static WorkflowEngine getWorkflowEngine(){ return new JbpmImpl(); } } public class PagedObject{ private int ncurrentPage; private static int N_EVERY_PAGE = 10; private List result; private int total; public int getNpages(){ return (total+N_EVERY_PAGE -1)/N_EVERY_PAGE; } } public class WorkitemCriteria{ private String pdname; private String tname; private String status; private String actorId; private int start; private int maxResult; } public class NextStep{ private String nextStepName; private String actorId; }