taskService 流程任務組件

act_ru_task:任務表
act_ru_identitylink:權限表(流程定義和用戶組(用戶)之間的權限數據)
act_ru_variable:參數表
act_hi_attachment:任務附件(能夠以流的方式存儲到act_ge_bytearray表中(父表),外鍵CONTENT_ID_(沒有強制物理關係))
act_hi_comment:任務評論和事件記錄表(由type決定,"event":事件的記錄,"comment":流程的評論數據)
1.建立
//獲取任務服務組件
TaskService taskService = engine.getTaskService();
//保存第一個Task,不設置ID
Task task1 = taskService.newTask();
taskService.saveTask(task1);
//保存第二個Task,設置ID
Task task2 = taskService.newTask("審覈任務");
taskService.saveTask(task2);
2.刪除
// 刪除task(不包括歷史數據和子任務)
taskService.deleteTask("1");
// 刪除task(包括歷史數據和子任務)
taskService.deleteTask("2", true);
// 刪除多個task(不包括歷史數據和子任務)
List<String> ids = new ArrayList<String>();
ids.add("3");
ids.add("4");
taskService.deleteTasks(ids);
//刪除多個task(包括歷史數據和子任務)
ids = new ArrayList<String>();
ids.add("5");
ids.add("6");
taskService.deleteTasks(ids, true);
// 再刪除ID爲3的task,此時3的歷史數據也會被刪除(若是有的話)
taskService.deleteTask("3", true);web

3.設置任務持有人
taskService.setOwner(task1.getId(), user.getId());
//該用戶持有的任務數量
taskService.createTaskQuery().taskOwner(user.getId()).count()ide

4. 設置任務受理人
taskService.setAssignee(task1.getId(), user.getId());
//該用戶受理的任務數量
taskService.createTaskQuery().taskAssignee(user.getId()).count()this

5. 任務候選組(綁定權限) act_ru_identitylink
taskService.addCandidateGroup("task1", groupA.getId());url

6.任務候選人(綁定權限) act_ru_identitylink
taskService.addCandidateUser("task1", user.getId());spa

7. 查詢
//根據用戶組查詢任務
List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup(groupA.getId()).list();.net

//根據用戶查詢任務
tasks = taskService.createTaskQuery().taskCandidateUser(user.getId()).list();
查詢的SQL語句: Preparing: select distinct RES.* from ACT_RU_TASK RES inner join ACT_RU_IDENTITYLINK I on I.TASK_ID_ = RES.ID_ WHERE RES.ASSIGNEE_ is null and I.TYPE_ = 'candidate' and ( I.USER_ID_ = ? ) order by RES.ID_ asc LIMIT ? OFFSET ?
Parameters: djl(String), 2147483647(Integer), 0(Integer)對象

//查詢權限數據(任務-->權限)
List<IdentityLink> links = taskService.getIdentityLinksForTask(task.getId());
//調用taskCandidateGroupIn
List<String> groupIds = new ArrayList<String>();
groupIds.add(groupA.getId());
groupIds.add(groupB.getId());
tasks = taskService.createTaskQuery().taskCandidateGroupIn(groupIds).list();blog

8.添加任務權限數據
//調用addGroupIdentityLink方法
taskService.addGroupIdentityLink(task1.getId(), groupA.getId(), IdentityLinkType.CANDIDATE); -->addCandidateGroup(); 任務候選組
//調用addUserIdentityLink方法
taskService.addUserIdentityLink(task1.getId(), user.getId(), IdentityLinkType.CANDIDATE); --> setOwner(); 任務持有人
taskService.addUserIdentityLink(task1.getId(), user.getId(), IdentityLinkType.OWNER); -->addCandidateUser() 任務候選人
taskService.addUserIdentityLink(task1.getId(), user.getId(), IdentityLinkType.ASSIGNEE); --> setAssignee(); 任務受理人事件

9.刪除
//刪除用戶組權限
taskService.deleteGroupIdentityLink(task1.getId(), groupA.getId(),
IdentityLinkType.CANDIDATE);
taskService.deleteCandidateGroup(task1.getId(), groupA.getId());
//刪除用戶權限
taskService.deleteUserIdentityLink(task1.getId(), user.getId(), IdentityLinkType.OWNER);
taskService.deleteCandidateUser(task1.getId(), user.getId());圖片

10.任務參數
參數做用域
setVariable:整個流程中均可以使用
setVariableLocal:當前任務中使用
兩個做用域不交集 肯定
taskService.setVariableLocal(task.getId(), "target", "歐洲");
Object data3 = taskService.getVariableLocal(task.getId(), "target");
//初始化參數
Map<String,Object> vars = new HashMap<String, Object>();
vars.put("days", 10);
vars.put("target", "歐洲");
taskService.setVariables(task1.getId(), vars);
Date d = new Date();
short s = 3;
//設置各類基本類型參數
taskService.setVariable(task1.getId(), "arg0", false);
Object arg0 = taskService.getVariable(task1.getId(), "arg0");
taskService.setVariable(task1.getId(), "arg1", d);
taskService.setVariable(task1.getId(), "arg2", 1.5D);
taskService.setVariable(task1.getId(), "arg3", 2);
taskService.setVariable(task1.getId(), "arg4", 10L);
taskService.setVariable(task1.getId(), "arg5", null);
taskService.setVariable(task1.getId(), "arg6", s);
taskService.setVariable(task1.getId(), "arg7", "test");
taskService.setVariable(task1.getId(), "target", new TestVO("北京"));
(TestVO要實現序列化
/**
* 序列化對象
* @author yangenxiong
*
*/
public class TestVO implements Serializable {

private String name;
public TestVO(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
//獲取參數


11.附件
// 部署流程描述文件
Deployment dep = repositoryService.createDeployment()
.addClasspathResource("bpmn/vacation.bpmn").deploy();
// 查找流程定義
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
.deploymentId(dep.getId()).singleResult();
// 啓動流程
ProcessInstance pi = runtimeService
.startProcessInstanceById(pd.getId());
// 查找任務
Task task = taskService.createTaskQuery().processInstanceId(pi.getId())
.singleResult();
// 設置任務附件
Attachment att1 = taskService.createAttachment("web url", task.getId(), pi.getId(), "Attachement1",
"163 web page", "http://www.163.com");
// 建立圖片輸入流
InputStream is = new FileInputStream(new File("resource/artifact/result.png"));
// 設置輸入流爲任務附件
Attachment att2 = taskService.createAttachment("web url", task.getId(), pi.getId(), "Attachement2",
"Image InputStream", is);
// 根據流程實例ID查詢附件
List<Attachment> attas1 = taskService.getProcessInstanceAttachments(pi.getId());
System.out.println("流程附件數量:" + attas1.size());
// 根據任務ID查詢附件
List<Attachment> attas2 = taskService.getTaskAttachments(task.getId());
System.out.println("任務附件數量:" + attas2.size());
// 根據附件ID查詢附件
Attachment attResult = taskService.getAttachment(att1.getId());
System.out.println("附件1名稱:" + attResult.getName());
// 根據附件ID查詢附件內容
InputStream stream1 = taskService.getAttachmentContent(att1.getId());
System.out.println("附件1的輸入流:" + stream1);
InputStream stream2 = taskService.getAttachmentContent(att2.getId());
System.out.println("附件2的輸入流:" + stream2);


12.事件記錄(事件和評論)
// 部署流程描述文件
Deployment dep = repositoryService.createDeployment()
.addClasspathResource("bpmn/vacation.bpmn").deploy();
// 查找流程定義
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
.deploymentId(dep.getId()).singleResult();
// 啓動流程
ProcessInstance pi = runtimeService
.startProcessInstanceById(pd.getId());
// 查找任務
Task task = taskService.createTaskQuery().processInstanceId(pi.getId())
.singleResult();
// 調用各個記錄事件的方法
taskService.addComment(task.getId(), pi.getId(), "this is comment message");
taskService.addUserIdentityLink(task.getId(), "1", "user");
taskService.deleteUserIdentityLink(task.getId(), "1", "user");
taskService.addGroupIdentityLink(task.getId(), "1", "group");
taskService.deleteGroupIdentityLink(task.getId(), "1", "group");
Attachment atta = taskService.createAttachment("test", task.getId(), pi.getId(), "test", "test", "");
taskService.deleteAttachment(atta.getId());
// 查詢Comment和Event
List<Comment> comments = taskService.getTaskComments(task.getId());
System.out.println("總共的評論數量:" + comments.size());
List<Event> events = taskService.getTaskEvents(task.getId());
System.out.println("總共的事件數量:" + events.size());
// 查詢事件與評論
List<Comment> commonts1 = taskService.getProcessInstanceComments(pi.getId());
System.out.println("流程評論(事件)數量:" + commonts1.size());
commonts1 = taskService.getTaskComments(task.getId());
System.out.println("任務評論數量:" + commonts1.size());
List<Event> events = taskService.getTaskEvents(task.getId());
System.out.println("事件數量:" + events.size());

13.任務聲明(簽收)
taskService.claim(task.getId(), userID);
只能聲明給同一個用戶(可屢次),不能再次聲明,和setAssignee方法相似

14.任務完成
complete參數爲兩個時,效果同setVariable
只有一個參數taskID時,會將完成的相關Task數據從數據表中刪除,放到歷史數據表中,而後讓執行流繼續往下,若是發現這個任務爲流程中的最後一個任務,則會連同實例的數據也一併刪除,而且
按照歷史配置來記錄流程的歷史數據。

// 部署流程描述文件Deployment dep = repositoryService.createDeployment().addClasspathResource("bpmn/vacation2.bpmn").deploy();// 查找流程定義ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult();// 啓動流程ProcessInstance pi = runtimeService.startProcessInstanceById(pd.getId());// 查找任務Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();// 調用complete方法完成任務,傳入參數Map<String, Object> vars = new HashMap<String, Object>();vars.put("days", 2);taskService.complete(task.getId(), vars);// 再次查找任務task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();//獲得參數Integer days = (Integer)taskService.getVariable(task.getId(), "days");if (days > 5) {System.out.println("大於5天,不批");} else {System.out.println("小於5天,完成任務,流程結束");taskService.complete(task.getId());}

相關文章
相關標籤/搜索