1. 程式人生 > 其它 >Activiti7開發(二)-流程定義

Activiti7開發(二)-流程定義

目錄

1.部署流程模型為流程定義

@PostMapping(value = "/addDeploymentByString")
public AjaxResult addDeploymentByString(@RequestParam("stringBPMN") String stringBPMN) {
        processDefinitionService.addDeploymentByString(stringBPMN);
        return AjaxResult.success();
}

public void addDeploymentByString(String stringBPMN) {
	repositoryService.createDeployment()
			.addString("CreateWithBPMNJS.bpmn", stringBPMN)
			.deploy();
}

說明:stringBPMN該字串其實就是一個xml,但與Modeler生成的內容不同;此處沒有建立模型,直接進行部署。如果後期需求變更,可以基於該模型進行修改後再次部署,區別在於版本號+1,同時最新建立的流程例項是根據最新版本的流程定義建立的。

2.掛起/啟用流程定義

@PostMapping("/suspendOrActiveApply")
@ResponseBody
public AjaxResult suspendOrActiveApply(@RequestBody ProcessDefinitionDTO processDefinition) {
	processDefinitionService.suspendOrActiveApply(processDefinition.getId(), processDefinition.getSuspendState());
	return AjaxResult.success();
}

public void suspendOrActiveApply(String id, Integer suspendState) {
	if (1==suspendState) {
		// 當流程定義被掛起時,已經發起的該流程定義的流程例項不受影響(如果選擇級聯掛起則流程例項也會被掛起)。
		// 當流程定義被掛起時,無法發起新的該流程定義的流程例項。
		// 直觀變化:act_re_procdef 的 SUSPENSION_STATE_ 為 2
		repositoryService.suspendProcessDefinitionById(id);
	} else if (2==suspendState) {
		repositoryService.activateProcessDefinitionById(id);
	}
}

3.刪除流程定義

@DeleteMapping(value = "/remove/{deploymentId}")
public AjaxResult delDefinition(@PathVariable("deploymentId") String deploymentId) {
	return toAjax(processDefinitionService.deleteProcessDefinitionById(deploymentId));
}

public int deleteProcessDefinitionById(String id) {
	repositoryService.deleteDeployment(id, false);
	return 1;
}

4.查詢流程定義

@GetMapping(value = "/list")
public TableDataInfo list(ProcessDefinitionDTO processDefinition) {
	PageDomain pageDomain = TableSupport.buildPageRequest();
	return getDataTable(processDefinitionService.selectProcessDefinitionList(processDefinition, pageDomain));
}

public Page<ProcessDefinitionDTO> selectProcessDefinitionList(ProcessDefinitionDTO processDefinition, PageDomain pageDomain) {
	Page<ProcessDefinitionDTO> list = new Page<>();
	ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery().orderByProcessDefinitionId().orderByProcessDefinitionVersion().desc();
	if (StringUtils.isNotBlank(processDefinition.getName())) {
		processDefinitionQuery.processDefinitionNameLike("%" + processDefinition.getName() + "%");
	}
	if (StringUtils.isNotBlank(processDefinition.getKey())) {
		processDefinitionQuery.processDefinitionKeyLike("%" + processDefinition.getKey() + "%");
	}
	List<ProcessDefinition> processDefinitions = processDefinitionQuery.listPage((pageDomain.getPageNum() - 1) * pageDomain.getPageSize(), pageDomain.getPageSize());
	long count = processDefinitionQuery.count();
	list.setTotal(count);
	if (count!=0) {
		//把流程id拿到(去重)
		Set<String> ids = processDefinitions.parallelStream().map(pdl -> pdl.getDeploymentId()).collect(Collectors.toSet());
		//id deployTime
		List<ActReDeploymentVO> actReDeploymentVOS = actReDeploymentMapper.selectActReDeploymentByIds(ids);
		// id name key version deploymentId resourceName
		List<ProcessDefinitionDTO> processDefinitionDTOS = processDefinitions.stream()
				.map(pd -> new ProcessDefinitionDTO((ProcessDefinitionEntityImpl) pd, actReDeploymentVOS.parallelStream().filter(ard -> pd.getDeploymentId().equals(ard.getId())).findAny().orElse(new ActReDeploymentVO())))
				.collect(Collectors.toList());
		list.addAll(processDefinitionDTOS);
	}
	return list;
}

5.上傳並部署流程定義

public AjaxResult uploadStreamAndDeployment(@RequestParam("file") MultipartFile file) throws IOException {
	processDefinitionService.uploadStreamAndDeployment(file);
	return AjaxResult.success();
}

public void uploadStreamAndDeployment(MultipartFile file) throws IOException {
	// 獲取上傳的檔名
	String fileName = file.getOriginalFilename();
	// 得到輸入流(位元組流)物件
	InputStream fileInputStream = file.getInputStream();
	// 檔案的副檔名
	String extension = FilenameUtils.getExtension(fileName);

	if (extension.equals("zip")) {
		ZipInputStream zip = new ZipInputStream(fileInputStream);
		repositoryService.createDeployment()//初始化流程
			.addZipInputStream(zip)
			.deploy();
	} else {
		repositoryService.createDeployment()//初始化流程
			.addInputStream(fileName, fileInputStream)
			.deploy();
	}
}

6.檢視流程模型

public void getProcessDefineXML(HttpServletResponse response,@RequestParam("deploymentId") String deploymentId,@RequestParam("resourceName") String resourceName) throws IOException {
	processDefinitionService.getProcessDefineXML(response, deploymentId, resourceName);
}
	
public void getProcessDefineXML(HttpServletResponse response, String deploymentId, String resourceName) throws IOException {
	InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, resourceName);
	int count = inputStream.available();
	byte[] bytes = new byte[count];
	response.setContentType("text/xml");
	OutputStream outputStream = response.getOutputStream();
	while (inputStream.read(bytes) != -1) {
		outputStream.write(bytes);
	}
	inputStream.close();
}

返回是xml

<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:activiti="http://activiti.org/bpmn" id="sample-diagram" targetNamespace="http://activiti.org/bpmn" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd">
  <bpmn2:process id="material_apply" name="物料申請1.2" isExecutable="true" activiti:versionTag="1.2.0">
    <bpmn2:startEvent id="StartEvent_1" name="Start" activiti:initiator="applyUserId">
      <bpmn2:outgoing>Flow_0w19svd</bpmn2:outgoing>
    </bpmn2:startEvent>
    <bpmn2:endEvent id="Event_15f0qdt" name="End">
      <bpmn2:incoming>Flow_1mzijq0</bpmn2:incoming>
      <bpmn2:incoming>Flow_0eqii1k</bpmn2:incoming>
    </bpmn2:endEvent>
    <bpmn2:userTask id="saleSupportVerify" name="銷售支援部審批" activiti:formKey="saleSupportVerify" activiti:candidateUsers="${sale_support_member}">
      <bpmn2:extensionElements>
        <activiti:formProperty id="FormProperty_27mbu4f--__!!radio--__!!審批意見--__!!i--__!!同意--__--不同意" type="string" label="單選按鈕" />
        <activiti:formProperty id="FormProperty_2am58qs--__!!textarea--__!!批註--__!!f__!!null" type="string" />
      </bpmn2:extensionElements>
      <bpmn2:incoming>Flow_0w19svd</bpmn2:incoming>
      <bpmn2:outgoing>Flow_156vjgs</bpmn2:outgoing>
    </bpmn2:userTask>
    <bpmn2:exclusiveGateway id="Gateway_1wob96l" name="閘道器3">
      <bpmn2:incoming>Flow_156vjgs</bpmn2:incoming>
      <bpmn2:outgoing>Flow_1mzijq0</bpmn2:outgoing>
      <bpmn2:outgoing>Flow_0eqii1k</bpmn2:outgoing>
    </bpmn2:exclusiveGateway>
    <bpmn2:sequenceFlow id="Flow_156vjgs" sourceRef="saleSupportVerify" targetRef="Gateway_1wob96l" />
    <bpmn2:sequenceFlow id="Flow_1mzijq0" name="同意" sourceRef="Gateway_1wob96l" targetRef="Event_15f0qdt">
      <bpmn2:extensionElements>
        <activiti:executionListener class="com.chosenmed.workflow.oa.listener.MaterialListener" event="take">
          <activiti:field name="state">
            <activiti:string>3</activiti:string>
          </activiti:field>
        </activiti:executionListener>
      </bpmn2:extensionElements>
      <bpmn2:conditionExpression xsi:type="bpmn2:tFormalExpression">${FormProperty_27mbu4f==0}</bpmn2:conditionExpression>
    </bpmn2:sequenceFlow>
    <bpmn2:sequenceFlow id="Flow_0w19svd" sourceRef="StartEvent_1" targetRef="saleSupportVerify" />
    <bpmn2:sequenceFlow id="Flow_0eqii1k" name="駁回" sourceRef="Gateway_1wob96l" targetRef="Event_15f0qdt">
      <bpmn2:extensionElements>
        <activiti:executionListener class="com.chosenmed.workflow.oa.listener.MaterialListener" event="take">
          <activiti:field name="state">
            <activiti:string>2</activiti:string>
          </activiti:field>
        </activiti:executionListener>
      </bpmn2:extensionElements>
    </bpmn2:sequenceFlow>
  </bpmn2:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="material_apply">
      <bpmndi:BPMNEdge id="Flow_0eqii1k_di" bpmnElement="Flow_0eqii1k">
        <di:waypoint x="820" y="105" />
        <di:waypoint x="820" y="292" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="825" y="196" width="21" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_0w19svd_di" bpmnElement="Flow_0w19svd">
        <di:waypoint x="208" y="80" />
        <di:waypoint x="530" y="80" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_1mzijq0_di" bpmnElement="Flow_1mzijq0">
        <di:waypoint x="845" y="80" />
        <di:waypoint x="1090" y="80" />
        <di:waypoint x="1090" y="310" />
        <di:waypoint x="838" y="310" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1094" y="193" width="23" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_156vjgs_di" bpmnElement="Flow_156vjgs">
        <di:waypoint x="630" y="80" />
        <di:waypoint x="795" y="80" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
        <dc:Bounds x="172" y="62" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="181" y="105" width="25" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Event_15f0qdt_di" bpmnElement="Event_15f0qdt">
        <dc:Bounds x="802" y="292" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="811" y="335" width="20" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_0i9t2gt_di" bpmnElement="saleSupportVerify">
        <dc:Bounds x="530" y="40" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Gateway_1wob96l_di" bpmnElement="Gateway_1wob96l" isMarkerVisible="true">
        <dc:Bounds x="795" y="55" width="50" height="50" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="805" y="31" width="29" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn2:definitions>