JBPM工具类 - JBPMUtil

dystyle

贡献于2014-10-14

字数:26914 关键词: jBPM 工作流引擎

package com.rdkl.safepub.util; import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.zip.ZipInputStream; import javax.annotation.Resource; import org.jbpm.api.Execution; import org.jbpm.api.ExecutionService; import org.jbpm.api.HistoryService; import org.jbpm.api.IdentityService; import org.jbpm.api.ManagementService; import org.jbpm.api.ProcessDefinition; import org.jbpm.api.ProcessEngine; import org.jbpm.api.ProcessInstance; import org.jbpm.api.RepositoryService; import org.jbpm.api.TaskQuery; import org.jbpm.api.TaskService; import org.jbpm.api.history.HistoryActivityInstance; import org.jbpm.api.history.HistoryProcessInstance; import org.jbpm.api.history.HistoryTask; import org.jbpm.api.job.Job; import org.jbpm.api.model.Activity; import org.jbpm.api.model.ActivityCoordinates; import org.jbpm.api.model.Transition; import org.jbpm.api.task.Task; import org.jbpm.pvm.internal.env.EnvironmentFactory; import org.jbpm.pvm.internal.env.EnvironmentImpl; import org.jbpm.pvm.internal.model.ActivityImpl; import org.jbpm.pvm.internal.model.ExecutionImpl; import org.jbpm.pvm.internal.model.ProcessDefinitionImpl; import org.jbpm.pvm.internal.model.TransitionImpl; import org.jbpm.pvm.internal.repository.RepositoryServiceImpl; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; import com.rdkl.safepub.jbpm.handler.RollbackCommand; import com.rdkl.safepub.jbpm.handler.Sign; /** * @ClassName: JBPMUtil * @Description: JBPM工具类 * @author pjj * @date 2014-6-10 上午11:09:46 */ @Component("jBPMUtil") public class JBPMUtil { @Resource(name="processEngine") private ProcessEngine processEngine; private RepositoryService repositoryService = null; private ExecutionService executionService = null; private TaskService taskService = null; private HistoryService historyService = null; private ManagementService managementService = null; private IdentityService identityService = null; public JBPMUtil() { } public JBPMUtil(ProcessEngine processEngine) { this.processEngine = processEngine; repositoryService = processEngine.getRepositoryService(); executionService = processEngine.getExecutionService(); taskService = processEngine.getTaskService(); historyService = processEngine.getHistoryService(); managementService = processEngine.getManagementService(); identityService = processEngine.getIdentityService(); } public ProcessEngine getProcessEngine() { return processEngine; } public void setProcessEngine(ProcessEngine processEngine) { this.processEngine = processEngine; System.out.println("processEngine=" + processEngine); repositoryService = processEngine.getRepositoryService(); executionService = processEngine.getExecutionService(); taskService = processEngine.getTaskService(); historyService = processEngine.getHistoryService(); managementService = processEngine.getManagementService(); identityService = processEngine.getIdentityService(); } /** * @Description: 部署新流程(xml) * @createDate: 2014-6-10 上午11:34:29 * @param resourceName xml的名字 * @return 返回流程定义id */ public String deployNew(String resourceName) { String path = "com/rdkl/safepub/jbpm/jpdl/"; return repositoryService.createDeployment().addResourceFromClasspath(path+resourceName+".jpdl.xml").addResourceFromClasspath(path+resourceName+".png").deploy(); } /** * @Description: 部署新流程(zip) * @createDate: 2014-6-10 上午11:35:05 * @param resourceZipName zip名字 * @return 返回流程定义id */ public String deployZipNew(String resourceZipName) { ZipInputStream zis = new ZipInputStream(this.getClass() .getResourceAsStream(resourceZipName)); return repositoryService.createDeployment() .addResourcesFromZipInputStream(zis).deploy(); } /** * @Description: 开始一个流程实例 * @createDate: 2014-6-10 上午11:36:07 * @param id 流程定义id * @param map 键是String类型,值是Object类型,用来存放变量 * @return ProcessInstance */ public ProcessInstance startPIById(String id, Map map) { return executionService.startProcessInstanceById(id, map); } public ProcessInstance startPIByKey(String id){ return executionService.startProcessInstanceByKey(id); } public ProcessInstance startPIByKey(String id, Map map){ return executionService.startProcessInstanceByKey(id, map); } /** * @Description: 完成任务 :根据任务id和变量完成单个任务 * @param taskId 任务id * @param map * @return void */ public void completeTask(String taskId, Map map) { taskService.completeTask(taskId, map); } /** * @Description: 完成任务 :根据id完成单个任务 * @param taskId 任务id * @return void */ public void completeTask(String taskId) { taskService.completeTask(taskId); } /** * @Description: 完成任务:根据任务id,并将任务流转到指定名字的流程outcome中去 * @param taskId 任务id * @param outcome 流转路径 * @return void */ public void completeTask(String taskId, String outcome) { taskService.completeTask(taskId, outcome); } /** * @Description: 完成任务:根据任务id和变量,并将任务流转到指定名字的流程outcome中去 * @createDate: 2014-6-10 上午11:40:20 * @param taskId * @param outcome * @param variables */ public void completeTask(String taskId, String outcome, Map variables) { taskService.completeTask(taskId, outcome, variables); } /** * @Description: 获得所有发布了的流程 * @createDate: 2014-6-10 上午11:41:49 * @return 返回流程集合 */ public List getAllPdList() { return repositoryService.createProcessDefinitionQuery().list(); } /** * @Description: 获取指定的流程定义:根据key * @createDate: 2014-6-10 上午11:42:35 * @param key * @return */ public ProcessDefinition getProcessDefinition(String key) { return repositoryService.createProcessDefinitionQuery().processDefinitionName(key).list().get(0); } /** * @Description: 获得所有流程实例 * @createDate: 2014-6-10 上午11:45:39 * @return 流程实例集合 */ public List getAllPiList() { return executionService.createProcessInstanceQuery().list(); } /** * @Description: 获得XX流程实例:根据流程定义id * @createDate: 2014-6-10 上午11:46:14 * @param processDefinitionId 流程定义id * @return 流程实例集合 */ public List getPiList(String processDefinitionId) { return executionService.createProcessInstanceQuery().processDefinitionId(processDefinitionId).list(); } /** * @Description: 获得历史实例 :根据处理人id,表jbpm4_hist_task * @createDate: 2014-6-10 上午11:47:25 * @param employeeId 处理人id * @return 历史任务实例集合 */ public List getHistoryList(String employeeId) { return historyService.createHistoryTaskQuery().assignee(employeeId).list(); } /** * @Description: 获得多条历史活动实例 :表jbpm4_hist_actinst * @createDate: 2014-6-10 上午11:50:49 * @param executionId 实例id * @return 节点实例集合 */ public List getHistActinstListByExeId(String executionId){ return historyService.createHistoryActivityInstanceQuery().executionId(executionId).list(); } /** * @Description: 获取下一个节点的名字: 当前节点complete之前 * @createDate: 2014-6-10 上午11:54:02 * @param ExecutionId 当前实例id * @return 接下来一个或多个节点的名字集合 */ public List getTransitions(String ExecutionId) { // 这里不会影响事物 EnvironmentImpl envImpl = ((EnvironmentFactory) processEngine).openEnvironment(); List listadd = new ArrayList(); try { ExecutionImpl e = (ExecutionImpl) executionService.findExecutionById(ExecutionId); ActivityImpl clerkOpinionActivityImpl = e.getActivity(); List list = clerkOpinionActivityImpl.getOutgoingTransitions(); Iterator iter=list.iterator(); while (iter.hasNext()) { Transition transition = (Transition)iter.next(); Activity destination = transition.getDestination(); if(destination!=null){ String nextActivityName = destination.getName(); listadd.add(nextActivityName); } } } catch (Exception e) { e.printStackTrace(); } finally { envImpl.close(); } return listadd; } /** * @Description: 获取某个变量值: 根据流程实例Id和变量名 * @createDate: 2014-6-10 上午11:55:35 * @param executionId 实例id * @param variableName 变量名 * @return */ public Object getVariableByexecutionId(String executionId, String variableName) { return executionService.getVariable(executionId, variableName); } /** * @Description: 获取某个变量值: 根据任务id和变量名 * @createDate: 2014-6-10 上午11:57:18 * @param taskId 任务id * @param variableName 变量名 * @return 某个变量值 */ public Object getVariableByTaskId(String taskId, String variableName) { return taskService.getVariable(taskId, variableName); } /** * @Description: 获取多个变量值:根据任务id和变量名集合 * @createDate: 2014-6-10 上午11:58:12 * @param taskId 任务id * @param variableNames 变量名集合 * @return 多个变量值 */ public Map getVariablesByTaskId(String taskId, Set variableNames) { return taskService.getVariables(taskId, variableNames); } /** * @Description: 获取多个变量值:实例id和变量名集合 * @createDate: 2014-6-10 上午11:59:32 * @param executionId 实例id * @param variableNames 变量名集合 * @return 多个变量值 */ public Map getVariablesByExecutionId(String executionId, Set variableNames) { return executionService.getVariables(executionId, variableNames); } /** * @Description: 查找某个用户的任务列表 * @createDate: 2014-6-10 下午12:02:02 * @param userName 用户名 * @return 任务集合 */ public List findPersonalTasks(String userName) { return taskService.findPersonalTasks(userName); } /** * @Description: 获取指定用户名字的组任务 * @createDate: 2014-6-10 下午12:05:42 * @param userName 用户名 * @return 任务集合 */ public List findGroupTasks(String userName) { // return taskService.findGroupTasks(userName); return taskService.createTaskQuery().candidate(userName).orderDesc(TaskQuery.PROPERTY_CREATEDATE).list(); } /** * @Description: 获取任务列表 :根据流程定义id * @createDate: 2014-6-10 下午12:06:49 * @param processDifinitionId 流程定义id * @return 任务集合 */ public List findByProcessDefinitionId(String processDifinitionId) { return taskService.createTaskQuery().processDefinitionId(processDifinitionId).list(); } /** * @Description: 根据任务id获取任务 * @createDate: 2014-6-10 下午12:07:52 * @param taskId 任务id * @return */ public Task getTask(String taskId) { return taskService.getTask(taskId); } /** * @Description: 级联删除流程定义:包括删除该流程定义下的所有实例 * @createDate: 2014-6-10 下午12:08:19 * @param deploymentId 流程定义id */ public void deleteDeploymentCascade(String deploymentId) { repositoryService.deleteDeploymentCascade(deploymentId); } public ActivityCoordinates findActivityCoordinates(String id) { // 通过id查到流程实例 ProcessInstance processInstance = executionService.findProcessInstanceById(id); Set activityNames = processInstance.findActiveActivityNames(); // Coordinates为相依衣物 return repositoryService.getActivityCoordinates(processInstance.getProcessDefinitionId(), activityNames.iterator().next()); } public InputStream findPicInputStream(String id,String resourceName) { // 通过流程实例id查找到流程实例 ProcessInstance processInstance = executionService.findProcessInstanceById(id); // 通过流程实例查找流程定义id String processDefinitionId = processInstance.getProcessDefinitionId(); // 通过流程id查找流程 ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).uniqueResult(); return repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), "com/rdkl/safepub/jbpm/jpdl/"+resourceName+".png"); } //=========================================【中国特色工作流的jbpm实现】=============================================== /** * @Description: 子流程【原理】是在一个节点上面建子流程。当子流程完成后,父流程的流向就会继续 * @createDate: 2014-6-10 下午12:56:21 * @param document (document是parameter-in的变量)父流程传递变量到子流程中 * @param ProcessDocument 父流程的name值或者key值 * @param subResult 子流程变量(以后要被父流程读取的变量) * @param accept 变量。给子流程subResult赋的任意一个值 */ public void subprocess1(String document,String ProcessDocument,String sometext,String subResult,String accept){ Map variables = new HashMap(); //我们启动父流程,并赋予一个文档变量。(document是parameter-in的变量)父流程传递变量到子流程中 variables.put(document, "sometext"); ProcessInstance processInstance = executionService .startProcessInstanceByKey(ProcessDocument, variables); //ProcessDocument //显示节点是在[子流程变量]处 System.out.println(processInstance.findActiveActivityNames()); /* 然后父流程达到sub-process 节点,触发子流程启动,并且子流程达到get approval 任务节 点,将 subvar 设置的 subDocument作为分配人 赋值给该任务,因此我们可以使用 sometext用户来获得其任务列表。*/ List taskList = taskService.findPersonalTasks(sometext); Task task = taskList.get(0); //System.out.println("任务名称:"+task.getActivityName()+'\n'+"任务处理人:"+task.getAssignee()); //而后我们将变量subResult 设置到子流程的实例中 Map map = new HashMap(); map.put(subResult, accept); //给子流程变量赋值为accept taskService.setVariables(task.getId(), map); //完成这个子流程的任务,整个子流程结束 taskService.completeTask(task.getId()); processInstance = executionService.findProcessInstanceById(processInstance.getId()); /*当子流程结束时,父流程会被signal 标记(不是notify 提醒)。首先subResult 变量会从子 流程赋值到父流程的result 变量中,然后上级流程继续并离开[子流程变量]节点。*/ //System.out.println("此时节点名字:"+processInstance.findActiveActivityNames());//显示节点是wait String result = (String) executionService.getVariable(processInstance.getId(), "result"); //显示result是否为accept //System.out.println("此时父流程变量var为:"+result); /*System.out.println("===========【第二个子流程测试子流程转向不同的父流程节点】==================================="); String executionid= processInstance.findActiveExecutionIn("wait").getId(); executionService.signalExecutionById(executionid,"to 子流程转向父流程不同节点"); System.out.println("当前任务所在节点名称:"+processInstance.findActiveActivityNames()); List taskList2 = taskService.findPersonalTasks("johndoe"); Task task2 = taskList2.get(0); System.out.println("task name:"+task2.getActivityName()+'\n'+"task assignee:"+task2.getAssignee()); taskService.completeTask(task2.getId(),"ok"); ProcessInstance processInstance2 = executionService.findProcessInstanceById(processInstance.getId()); //显示节点是否为next step System.out.println("此时节点名字:"+processInstance2.findActiveActivityNames()); */ } /** * @Description: 子流程转向不同的父流程节点 * @createDate: 2014-6-10 下午12:26:25 * @param document (document是parameter-in的变量)父流程传递变量到子流程中 * @param ProcessDocument 父流程的name值或者key值 * @param userName 子流程任务的处理人 * @param outGoing 子流程走向的结束路径 */ public void subprocess2(String document,String ProcessDocument,String userName,String outGoing){ Map variables = new HashMap(); //我们启动父流程,并赋予一个文档变量。document将父流程传递变量到子流程中 variables.put(document, "sometext"); ProcessInstance processInstance = executionService .startProcessInstanceByKey(ProcessDocument, variables); //System.out.println("当前节点名称:"+processInstance.findActiveActivityNames()); List taskList2 = taskService.findPersonalTasks(userName); Task task2 = taskList2.get(0); taskService.completeTask(task2.getId(),outGoing); ProcessInstance processInstance2 = executionService.findProcessInstanceById(processInstance.getId()); //显示节点是否为next step //System.out.println("当前节点名称:"+processInstance2.findActiveActivityNames()); } /** * @Description: 删除新建的自由流扭转路线(这个一般都会删除一下,防止影响原本流程定义 ) * @createDate: 2014-6-10 下午1:14:39 * @param pd 流程定义 * @param sourceName 当前任务节点 * @param destName 目标节点 */ public void removeOutTransition(ProcessDefinitionImpl pd,String sourceName,String destName){ ActivityImpl sourceActivity = pd.findActivity(sourceName); List trans=(List) sourceActivity.getOutgoingTransitions(); for(Transition tran:trans){ if(destName.equals(tran.getDestination().getName())){ trans.remove(tran); break; } } } /** * @Description: 创建一个OutgoingTransition然后流转 * @createDate: 2014-6-10 下午1:16:27 * @param pd 流程定义 * @param sourceName 当前任务节点 * @param destName 目标节点 */ public void addOutTransition(ProcessDefinitionImpl pd,String sourceName,String destName){ //获取到当前任务节点 ActivityImpl sourceActivity = pd.findActivity(sourceName); //获得目标节点 ActivityImpl destActivity=pd.findActivity(destName); //创建TransitionImpl TransitionImpl transition = sourceActivity.createOutgoingTransition(); transition.setName("to " +destName); transition.setDestination(destActivity); sourceActivity.addOutgoingTransition(transition); } /** * 【需求】比如有这么一个流程定义科员-->科长-->主任。三个环节,现在 * 科员看完任务发现没有必要提交给科长,可以直接提交给主任,或者直接提交到END节点。 * 【原理】自由流的原理就是在代码中创建一条Transition,在任意两个节点中建立流转 * 【使用】使用的是createOutgoingTransition方法,这个方法可以创建一个然后流转。 * * @Description: 自由流 * @createDate: 2014-6-10 下午1:37:08 * @param assignee 目的地任务处理人 * @param key 流程实例定义key * @param fromTask 跳转起始地 * @param toTask 跳转目的地 */ public void testTaskAssignee(String fromTask,String toTask,String assignee) { ProcessInstance processInstance3 = executionService.startProcessInstanceByKey("task_15"); ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().processDefinitionId(processInstance3.getProcessDefinitionId()).uniqueResult(); addOutTransition((ProcessDefinitionImpl)pd,fromTask,toTask); Execution executionInA = processInstance3.findActiveExecutionIn("state1"); processInstance3 = executionService.signalExecutionById(executionInA.getId(),"to task1"); //删除自由路径 removeOutTransition((ProcessDefinitionImpl)pd,"task1","state1"); Task task=taskService.findPersonalTasks(assignee).get(0); //System.out.println("======"+task.getActivityName()); taskService.completeTask(task.getId()); } /** * 【需求】回退:针对当前“待办任务”的操作,即当前用户主动把“待办”任务,退回到上一级 * 【原理】——》利用监听动态的创建一条转移路径,到想退回到的节点——》清除历史记录(删除那条动态路径) * 【使用】RollbackCommand命令 * * @Description: 回退 * @createDate: 2014-6-10 下午3:14:21 * @param Jack 第一个任务(初审)的处理人 * @param Alex 第二个任务(复审)的处理人 * @param activityName1 第一个任务的名字 * @param activityName2 第二个任务的名字 */ public void testRowback(String Jack,String Alex ,String activityName1,String activityName2){ ProcessInstance processInstance=executionService.startProcessInstanceByKey("Rollback"); final String pid = processInstance.getId(); final Task taskOutlayApply = taskService.findPersonalTasks(Jack).get(0); //首先用户jack正常完成任务activityName1 taskService.completeTask(taskOutlayApply.getId()); Task taskLeaderAudit = taskService.findPersonalTasks(Alex).get(0); System.out.println("确定是不是已经到了第二个任务节点:"+processInstance.isActive(activityName2)); //然后他发现不对这个任务不是他的,需要回退任务。这里模拟来自客户端历史任务列表中的操作:"取回任务"这个操作应该是由用户jack发起的 Task task = taskService.getTask(taskLeaderAudit.getId()); //客户端请求退回。 参数1第二个任务的taskid,参数二:退回的流转路径 //让他流向那个新建的transition,路径是当流程进入第二个task的时候监听器动态创建好的——》初审 to 复审 taskService.completeTask(taskLeaderAudit.getId(), task.getActivityName() + " to " + activityName1); //取回任务,清除历史记录 RollbackCommand command = new RollbackCommand(pid); processEngine.execute(command); //回退之后判断当前任务所在的节点,是不是回到了第一个任务 System.out.println("确定是不是已经返回到了第一个任务节点:"+processInstance.isActive(activityName1)); /* //结束任务即取回任务 Task taskOutlayApply2 = taskService.findPersonalTasks(Jack).get(0); taskService.completeTask(taskOutlayApply2.getId()); Task taskLeaderAudit2 = taskService.findPersonalTasks(Alex).get(0); taskService.completeTask(taskLeaderAudit2.getId(),"to end1"); processInstance = executionService.findProcessInstanceById(pid); HistoryProcessInstance hProcInst = historyService.createHistoryProcessInstanceQuery().processInstanceId(pid).uniqueResult(); */ } /** * @Description: 分配任务的候选人:通过任务分配处理器 * @createDate: 2014-6-10 下午12:13:32 * @param userName 任务候选人 */ public void testAssignmentHandler(String userName){ List list = taskService.findPersonalTasks(userName); for(int i=0;i>list.size();i++){ System.out.println(userName+"的任务名称:"+list.get(i).getActivityName()); } Task tasks = list.get(0); taskService.completeTask(tasks.getId()); //System.out.println("任务完成情况------>"+tasks.STATE_COMPLETED); } /** * * @Description: 测试超时的情况 * 如果流程没有在开始以后 1分钟内执行 singal, * 事件 timeout 就会被触发 事件监听器timer.event.Escalate 将被触发 * 即在流程变量表中会插入监听类的一条记录 * @Auther: pjj */ public void testTimerEventTimerFires() { ProcessInstance processInstance = executionService.startProcessInstanceByKey("TimerEvent"); String executionId = processInstance.findActiveExecutionIn("state1").getId(); executionService.signalExecutionById(executionId, "to guardedWait"); /*//获得定时器 Job job = managementService.createJobQuery() .processInstanceId(processInstance.getId()) .uniqueResult(); //假设定时器被触发了,通过编码执行定时器进行模拟 managementService.executeJob(job.getId()); processInstance = executionService.findProcessInstanceById(processInstance.getId()); Set expectedActivityNames = Collections.singleton("guardedWait"); System.out.println(">>>>>>>>>>>>>>>>>>"+executionService.getVariable(processInstance.getId(), "mag")); */ } /** * * @Description: 发邮件 * @createDate: 2014-6-12 上午9:53:42 * @param address 收件人地址 * @param newspaper 标题 * @param details 信件内容 * @param name 任务名 */ public void testMail(String address,String newspaper,String details,String name) { ClassPathXmlApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml"); applicationContext.start(); Date date=new Date(); Map variables =new HashMap(); variables.put("addressee", address); variables.put("newspaper", newspaper); variables.put("date", date); variables.put("name", name); variables.put("description", details); //启动一个流程实例 ProcessInstance processInstance = executionService.startProcessInstanceByKey("TemplateMail",variables); } /** * 【需求】分配任务:通过泳道 * 【原理】一个流程中的多个任务可能会被分配给同一个用户或候选人,当泳道执行过一次后,接下来的任务将会同样被这个泳道的用户或候选人所接收。 * 【使用】虽然用到也是groups但是可以添加N个用户却只可以指定给一个用户,并且接下来的都只会只给这一个用户。不需要N个用户都完成才算完成。 * 同样的用户A拿到take了任务,用户B是take不到的。简单的说泳道的概念就是把所有assingee=同一个用户。 * * @Description: 分配任务:通过泳道 * @createDate: 2014-6-12 上午9:47:46 * @param hr_dept 泳道名 * @param assignee 待办人 * @param givenName 名字 * @param familyName 姓氏 */ public void testSwimlane (String hr_dept,String assignee,String givenName,String familyName){ if(identityService.findGroupById(hr_dept)!=null && identityService.findUserById(assignee)!=null){ identityService.deleteGroup(hr_dept); identityService.deleteUser(assignee); } identityService.createGroup(hr_dept); identityService.createUser(assignee, givenName, familyName); identityService.createMembership(assignee, hr_dept); List taskList = taskService.findGroupTasks(assignee); Task task = taskList.get(0); String taskId = task.getId(); //assignee 来领任务 taskService.takeTask(task.getId(),assignee); taskList = taskService.findPersonalTasks(assignee); taskService.completeTask(taskId); //查看下一任务属于谁 taskList = taskService.findPersonalTasks(assignee); task = taskList.get(0); //System.out.println("下一任务名称:"+task.getName()+"代办人:"+task.getAssignee()); } //====================================【下面的几种情况属于还未封装的方法】============================================ /** * 【需求】会签:一个处理任务需要多个人同时处理,然后汇总多个人的意见,决定流程下一步该如何执行 * 【原理】用一个任务来代表会签任务,而在流程运行至该任务节点的时候,根据当前有多少人参与会签,动态生成该任务下的会签子任务 * 【使用】 */ public void testSign() { Map variables=new HashMap(); List pariticipants=new ArrayList(); pariticipants.add("千颂伊"); pariticipants.add("都敏俊"); variables.put("participants", pariticipants); ProcessInstance processInstance=executionService.startProcessInstanceByKey("sign",variables); processInstance.getProcessDefinitionId(); Task task=taskService.createTaskQuery() .processInstanceId(processInstance.getId()) .activityName(processInstance.findActiveActivityNames().iterator().next()).uniqueResult(); if(pariticipants.size()>0){ for(int i=0;i v=new HashMap(); v.put(Sign.VAR_SIGN, "不同意"); taskService.setVariables(joinTask.getId(), v); sign=new Sign(task.getId(),joinTask,"to 执行","to End"); processEngine.execute(sign); List hProcessInstances = historyService.createHistoryProcessInstanceQuery().processInstanceId(processInstance.getId()) .list(); for(HistoryProcessInstance h:hProcessInstances){ System.out.println("流程"+processInstance.getId()+"状态:"+h.getState()); } */ } /** * @Description: 会签一票否决 * @createDate: 2014-6-10 下午4:20:50 */ /*public void testSign2() { //一下这段代码可能是放在其他地方。 Map variables=new HashMap(); //这里list的泛型应该是一个对象,里面存的有用户以及用户对应的意见。 List pariticipants=new ArrayList(); pariticipants.add("千颂伊"); pariticipants.add("都敏俊"); variables.put("participants", pariticipants); ProcessInstance processInstance=executionService.startProcessInstanceByKey("sign",variables); Task task=taskService.createTaskQuery() .processInstanceId(processInstance.getId()) .activityName(processInstance.findActiveActivityNames().iterator().next()) .uniqueResult(); if(pariticipants.size()>0){ for(int i=0;i v=new HashMap(); v.put(Sign.VAR_SIGN, "不同意"); taskService.setVariables(joinTask.getId(), v); } Sign sign=new Sign(task.getId(),joinTask,"to 执行","to End"); //返回会签结果,并执行了输出流向。返回true表示全体通过 processEngine.execute(sign); } } List hProcessInstances = historyService.createHistoryProcessInstanceQuery().processInstanceId(processInstance.getId()) .list(); for(HistoryProcessInstance h:hProcessInstances){ System.out.println("流程"+processInstance.getId()+"状态:"+h.getState()); } }*/ /** * 【原理】把组任务分配给组,那么这个组下面的所有成员都可以拿到并完成任务。但是,如果用户A完成了任务,那么用户B是么办法再拿到任务的。 * 同样,如果用户A还没有完成任务,他只是拿到了take了任务,那么用户B同样是没办法拿到任务的。还有组下面的用户都有查询任务的权利是 * 【用法】findGroupTasks而不是findPersonalTasks,findPersonalTasks只适用于用户已经take了任务,是针对assigne="Tom20"。 */ public void testCandidateGroups(String sales,List assignees,List givenNames,List familyNames,String username){ if(identityService.findGroupById(sales)!=null ){ identityService.deleteGroup(sales); } for(int i=0;i list = taskService.findGroupTasks(username); Task task1 = list.get(0); //拿到任务 taskService.takeTask(task1.getId(), username); //完成任务 Task personTask = list.get(0); String taskid = personTask.getId(); taskService.completeTask(taskid); } /** * 【需求】撤销: * 【原理】将已办任务,从新置回当前待办状态。由A完成的由A撤销 * 【使用】 */ public void testWithDraw(){ } //======================================================================== public void setVariables(String taskId,Map map){ taskService.setVariables(taskId, map); } public void endProcessInstance(String executionId){ executionService.endProcessInstance(executionId, ProcessInstance.STATE_ENDED); } public ProcessInstance getProcessInstanceByExecutionId(String executionId) { Execution execution = executionService.findExecutionById(executionId); String piId = execution.getIsProcessInstance() ? execution.getId() : execution.getProcessInstance().getId(); return getProcessInstanceByPiId(piId); } public ProcessDefinition getProcessDefinitionByProcessInstance(String processInstanceId){ Execution execution = executionService.findExecutionById(processInstanceId); String processDefinitionId = execution.getProcessDefinitionId(); ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).uniqueResult(); return processDefinition; } public String getEid(String executionId){ Execution execution = executionService.findExecutionById(executionId); return execution.getId(); } public ProcessInstance getProcessInstanceByPiId(String piId){ return executionService.findProcessInstanceById(piId); } public IdentityService getIdentityService() { return identityService; } public void setIdentityService(IdentityService identityService) { this.identityService = identityService; } public RepositoryService getRepositoryService() { return repositoryService; } public void setRepositoryService(RepositoryService repositoryService) { this.repositoryService = repositoryService; } public ExecutionService getExecutionService() { return executionService; } public void setExecutionService(ExecutionService executionService) { this.executionService = executionService; } public TaskService getTaskService() { return taskService; } public void setTaskService(TaskService taskService) { this.taskService = taskService; } public HistoryService getHistoryService() { return historyService; } public void setHistoryService(HistoryService historyService) { this.historyService = historyService; } public ManagementService getManagementService() { return managementService; } public void setManagementService(ManagementService managementService) { this.managementService = managementService; } }

下载文档,方便阅读与编辑

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享文档获得金币 ]
1 人已下载

下载文档

相关文档