diff --git a/build.gradle b/build.gradle index 97291bd..819114b 100644 --- a/build.gradle +++ b/build.gradle @@ -22,6 +22,8 @@ dependencies { implementation 'org.activiti:activiti-spring-boot-starter:7.1.0.M3.1' implementation 'org.activiti:activiti-image-generator:7.1.0.M3.1' + compile group: 'org.activiti', name: 'activiti-json-converter', version: '7.1.0.M3.1' + // compile group: 'org.activiti.dependencies', name: 'activiti-dependencies', version: '7.1.0.M3.1', ext: 'pom' diff --git a/src/main/java/com/example/demo/controller/HolidayController.java b/src/main/java/com/example/demo/controller/HolidayController.java index 3d19858..5fdcb66 100644 --- a/src/main/java/com/example/demo/controller/HolidayController.java +++ b/src/main/java/com/example/demo/controller/HolidayController.java @@ -16,6 +16,7 @@ import org.activiti.engine.task.Task; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; @@ -47,7 +48,7 @@ public class HolidayController { @GetMapping("deploy") public void deploy() { RepositoryService repositoryService = processEngine.getRepositoryService(); - Deployment deploy = repositoryService.createDeployment().addClasspathResource("bpmn/holidayEx.bpmn").name("请假申请流程").key("holidayex").deploy(); + Deployment deploy = repositoryService.createDeployment().addClasspathResource("bpmn/test.bpmn").name("请假申请流程").key("holidayex").deploy(); System.out.println(deploy.getId()); System.out.println(deploy.getName()); System.out.println(deploy.getKey()); @@ -55,14 +56,15 @@ public class HolidayController { @ApiOperation(value = "启动工作流") @GetMapping("start") - public void start() { - processRuntime.start(ProcessPayloadBuilder.start().withProcessDefinitionKey("").build()); + public void start(@RequestParam Integer num) { +// processRuntime.start(ProcessPayloadBuilder.start().withProcessDefinitionKey("").build()); RuntimeService runtimeService = processEngine.getRuntimeService(); Holiday holiday = new Holiday(); - holiday.setNum(7f); +// holiday.setNum(num); Map map = new HashMap<>(); - map.put("holiday",holiday); + map.put("num",num); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holidayex",map); +// ProcessInstance processInstance = runtimeService.startProcessInstanceById("holidayex:3:7347e871-225c-11eb-bd7d-225bd818af7d",map); System.out.println(processInstance.getProcessDefinitionId()); System.out.println(processInstance.getId()); } @@ -79,7 +81,6 @@ public class HolidayController { System.out.println(x.getProcessDefinitionId()); System.out.println(x.getId()); System.out.println(x.getName()); - System.out.println(x.getAssignee()); }); return true; } diff --git a/src/main/java/com/example/demo/controller/ProcessController.java b/src/main/java/com/example/demo/controller/ProcessController.java new file mode 100644 index 0000000..b364193 --- /dev/null +++ b/src/main/java/com/example/demo/controller/ProcessController.java @@ -0,0 +1,61 @@ +package com.example.demo.controller; + +import com.example.demo.entity.Holiday; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.activiti.api.process.model.builders.ProcessPayloadBuilder; +import org.activiti.api.process.runtime.ProcessRuntime; +import org.activiti.api.runtime.shared.query.Page; +import org.activiti.api.runtime.shared.query.Pageable; +import org.activiti.api.task.runtime.TaskRuntime; +import org.activiti.engine.ProcessEngine; +import org.activiti.engine.RepositoryService; +import org.activiti.engine.RuntimeService; +import org.activiti.engine.TaskService; +import org.activiti.engine.repository.Deployment; +import org.activiti.engine.repository.ProcessDefinition; +import org.activiti.engine.repository.ProcessDefinitionQuery; +import org.activiti.engine.runtime.ProcessInstance; +import org.activiti.engine.task.Task; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author yanpeng + * @version 1.0 + * @desc TODO + * @company 北京中经网软件有限公司 + * @date 2020/10/27 14:34 + */ +@RestController +@RequestMapping("process") +@Api(tags = "工作流流程接口") +public class ProcessController { + + @Autowired + private RepositoryService repositoryService; + + @ApiOperation(value = "查询工作流") + @GetMapping("page") + public void page() { + ProcessDefinitionQuery processDefinitionQuery = repositoryService + .createProcessDefinitionQuery(); + List list = processDefinitionQuery.processDefinitionKey("holidayex").orderByProcessDefinitionVersion().asc().list(); + + for(ProcessDefinition t :list){ + if(t.isSuspended()){ +// repositoryService.suspendProcessDefinitionByKey("holidayex"); +// repositoryService.suspendProcessDefinitionById(t.getId(),true,null); + repositoryService.activateProcessDefinitionById(t.getId(),true,null); + }else{ + repositoryService.suspendProcessDefinitionById(t.getId(),true,null); + } + } + } +} diff --git a/src/main/java/com/example/demo/controller/WorkFlowController.java b/src/main/java/com/example/demo/controller/WorkFlowController.java index 725b111..ca29c32 100644 --- a/src/main/java/com/example/demo/controller/WorkFlowController.java +++ b/src/main/java/com/example/demo/controller/WorkFlowController.java @@ -1,20 +1,36 @@ package com.example.demo.controller; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import org.activiti.bpmn.converter.BpmnXMLConverter; +import org.activiti.bpmn.converter.CallActivityXMLConverter; +import org.activiti.bpmn.converter.UserTaskXMLConverter; +import org.activiti.bpmn.model.*; +import org.activiti.bpmn.model.Process; +import org.activiti.editor.language.json.converter.BpmnJsonConverter; import org.activiti.engine.ProcessEngine; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.repository.Deployment; +import org.activiti.engine.repository.DeploymentBuilder; +import org.activiti.engine.repository.Model; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ClassPathResource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.List; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; +import java.io.UnsupportedEncodingException; +import java.util.*; +import java.util.stream.Collectors; /** * @author yanpeng @@ -31,21 +47,113 @@ public class WorkFlowController { @Autowired private ProcessEngine processEngine; + @ApiOperation(value = "保存工作流") + @GetMapping("model") + public void model() { + RepositoryService repositoryService = processEngine.getRepositoryService(); + Model model = repositoryService.newModel(); + model.setName("请假工作流"); + model.setKey("holidayex"); + repositoryService.saveModel(model); + System.out.println(model.getId()); + } + + + @ApiOperation(value = "保存xml") + @GetMapping("modelxml") + public void modelXml(String modeid) throws UnsupportedEncodingException { + RepositoryService repositoryService = processEngine.getRepositoryService(); + Model model = repositoryService.getModel(modeid); +// Deployment deploy = repositoryService.createDeployment().addClasspathResource("bpmn/testex1.bpmn").name("请假申请流程").deploy(); + // 创建转换对象 + BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter(); + // XMLStreamReader读取XML资源 + XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); + XMLStreamReader xmlStreamReader = null; + try { + ClassPathResource resource = new ClassPathResource("bpmn/testex1.xml"); + xmlStreamReader = xmlInputFactory.createXMLStreamReader(resource.getInputStream()); + } catch (Exception e) { + e.printStackTrace(); + } + BpmnXMLConverter.addConverter(new UserTaskXMLConverter()); + BpmnXMLConverter.addConverter(new CallActivityXMLConverter()); + // 把xml转换成BpmnModel对象 + BpmnModel bpmnModel = bpmnXMLConverter.convertToBpmnModel(xmlStreamReader); + // 创建转换对象 + BpmnJsonConverter bpmnJsonConverter = new BpmnJsonConverter(); + // 把BpmnModel对象转换成json + String bpmnXml = bpmnJsonConverter.convertToJson(bpmnModel).toString(); + repositoryService.addModelEditorSource(model.getId(), bpmnXml.getBytes("utf-8")); + } + + @Autowired + private RepositoryService repositoryService; + @ApiOperation(value = "发布工作流") @GetMapping("deploy") - public void deploy() { - RepositoryService repositoryService = processEngine.getRepositoryService(); - Deployment deploy = repositoryService.createDeployment().addClasspathResource("bpmn/testbpmn.bpmn").addClasspathResource("bpmn/testbpmn.png").name("请假申请流程").key("testbpmn").deploy(); + public void deploy(String modeid) throws Exception { + Model modelData = repositoryService.getModel(modeid); + byte[] bytes = repositoryService.getModelEditorSource(modelData.getId()); + JsonNode modelNode = new ObjectMapper().readTree(bytes); + BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode); + model.getProcesses().get(0).setName(modelData.getName()); + //校验有没有开始结束节点- + List endEventList = model.getProcesses().get(0).findFlowElementsOfType(EndEvent.class); + List startEventList = model.getProcesses().get(0).findFlowElementsOfType(StartEvent.class); + List gateways = model.getProcesses().get(0).findFlowElementsOfType(ExclusiveGateway.class); + if (gateways != null) { + for (ExclusiveGateway gateway : gateways) { + System.out.println(gateway.getId()); + Collection flowElements = model.getProcesses().get(0).getFlowElements(); + List collect = model.getProcesses().get(0).findFlowElementsOfType(SequenceFlow.class).stream().filter(x -> { + flowElements.remove(x); + return x.getSourceRef().equals(gateway.getId()); + }).collect(Collectors.toList()); + Collections.sort(collect, (x1, x2) -> { + if (x1.getConditionExpression() == null && x2.getConditionExpression() != null) { + return 1; + } + if (x1.getConditionExpression() != null && x2.getConditionExpression() == null) { + return -1; + } + return 0; + }); + flowElements.addAll(collect); + System.out.println(collect); + } + } + if (startEventList.size() == 0) { + System.out.println("没开始节点"); + } + if (endEventList.size() == 0) { + System.out.println("没有结束节点"); + } + + byte[] bpmnByates = new BpmnXMLConverter().convertToXML(model); + //发布流程 + String processName = modelData.getName() + ".bpmn20.xml"; + String convertToXML = new String(bpmnByates); + System.out.println(convertToXML); + DeploymentBuilder deployment = repositoryService.createDeployment(); + deployment.name(modelData.getName()); + deployment.addString(processName, convertToXML); +// deployment.key(modelData.getKey()); + deployment.key("holidayex"); + Deployment deploy = deployment.deploy(); + modelData.setDeploymentId(deploy.getId()); System.out.println(deploy.getId()); - System.out.println(deploy.getName()); System.out.println(deploy.getKey()); + repositoryService.saveModel(modelData); } @ApiOperation(value = "启动工作流") @GetMapping("start") - public void start() { + public void start(Integer num) { RuntimeService runtimeService = processEngine.getRuntimeService(); - ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("testbpmn"); + Map map = new HashMap<>(); + map.put("num",num); + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holidayex",map); System.out.println(processInstance.getProcessDefinitionId()); System.out.println(processInstance.getId()); } @@ -57,8 +165,8 @@ public class WorkFlowController { @GetMapping("query") public Boolean query() { // TaskService taskService = processEngine.getTaskService(); - List testbpmn = taskService.createTaskQuery().processDefinitionKey("testbpmn").list(); - testbpmn.stream().forEach(x->{ + List holidayex = taskService.createTaskQuery().processDefinitionKey("holidayex").list(); + holidayex.stream().forEach(x -> { System.out.println(x.getProcessDefinitionId()); System.out.println(x.getId()); System.out.println(x.getName()); diff --git a/src/main/resources/bpmn/holidayEx.bpmn b/src/main/resources/bpmn/holidayEx.bpmn index e118291..25d8cc4 100644 --- a/src/main/resources/bpmn/holidayEx.bpmn +++ b/src/main/resources/bpmn/holidayEx.bpmn @@ -4,25 +4,28 @@ - - + - 5}]]> + =7}]]> - + + - =1}]]> + =2 && holiday.num<7}]]> - - + + + + + @@ -44,56 +47,62 @@ - - - - - - - + - + - + - - - + + + + + + + + + + + + + + + - - - + + + - - - + + + - - - + + + - + @@ -110,9 +119,23 @@ - - - + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/bpmn/holidayEx.xml b/src/main/resources/bpmn/holidayEx.xml new file mode 100644 index 0000000..15d87f4 --- /dev/null +++ b/src/main/resources/bpmn/holidayEx.xml @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + =7}]]> + + + + + + =2 && holiday.num<7}]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/bpmn/test.bpmn b/src/main/resources/bpmn/test.bpmn new file mode 100644 index 0000000..45605c0 --- /dev/null +++ b/src/main/resources/bpmn/test.bpmn @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + + + =7}]]> + + + + + + =3 && num<7}]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/bpmn/test.xml b/src/main/resources/bpmn/test.xml new file mode 100644 index 0000000..45605c0 --- /dev/null +++ b/src/main/resources/bpmn/test.xml @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + + + =7}]]> + + + + + + =3 && num<7}]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/bpmn/testbpmn.bpmn b/src/main/resources/bpmn/testbpmn.bpmn deleted file mode 100644 index 3835328..0000000 --- a/src/main/resources/bpmn/testbpmn.bpmn +++ /dev/null @@ -1,61 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/resources/bpmn/testbpmn.png b/src/main/resources/bpmn/testbpmn.png deleted file mode 100644 index 437ec2d..0000000 Binary files a/src/main/resources/bpmn/testbpmn.png and /dev/null differ diff --git a/src/main/resources/bpmn/testbpmn.xml b/src/main/resources/bpmn/testbpmn.xml deleted file mode 100644 index 8220832..0000000 --- a/src/main/resources/bpmn/testbpmn.xml +++ /dev/null @@ -1,61 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/resources/bpmn/testex.bpmn b/src/main/resources/bpmn/testex.bpmn new file mode 100644 index 0000000..bebbc24 --- /dev/null +++ b/src/main/resources/bpmn/testex.bpmn @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + + =7}]]> + + + + + =3 && num<7}]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/bpmn/testex.xml b/src/main/resources/bpmn/testex.xml new file mode 100644 index 0000000..bebbc24 --- /dev/null +++ b/src/main/resources/bpmn/testex.xml @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + + =7}]]> + + + + + =3 && num<7}]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/bpmn/testex1.bpmn b/src/main/resources/bpmn/testex1.bpmn new file mode 100644 index 0000000..cc0f504 --- /dev/null +++ b/src/main/resources/bpmn/testex1.bpmn @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + + + =7}]]> + + + + + + =3 && num<7}]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/bpmn/testex1.xml b/src/main/resources/bpmn/testex1.xml new file mode 100644 index 0000000..99eec9c --- /dev/null +++ b/src/main/resources/bpmn/testex1.xml @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + + + =7}]]> + + + + + + =3 && num<7}]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/bpmn/testt1.bpmn b/src/main/resources/bpmn/testt1.bpmn new file mode 100644 index 0000000..8097c58 --- /dev/null +++ b/src/main/resources/bpmn/testt1.bpmn @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + + + =7}]]> + + + + + =3}]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/bpmn/testt1.xml b/src/main/resources/bpmn/testt1.xml new file mode 100644 index 0000000..d5c85b6 --- /dev/null +++ b/src/main/resources/bpmn/testt1.xml @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + + + =7}]]> + + + + + =3 && num<7}]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +