这里是接着上篇进行往下写的,相关配置的话这里就不重复了,可以参照前面的文章
获取配置连接
private ProcessEngine processEngine;
@Before
public void initProcessEngine(){
processEngine = ProcessEngines.getDefaultProcessEngine();
}
流程的定义和部署
@Test
public void testProcessEngine(){
/*Deployment deployment = processEngine.getRepositoryService()
.createDeployment()
.addClasspathResource("diagram/leave.bpmn")
.addClasspathResource("diagram/leave.png")
.name("请假流程")
.deploy();
System.out.println("流程部署ID:"+deployment.getId());
System.out.println("流程部署名称:"+deployment.getName());*/
//以zip的形式进行加载流程文件,存到数据表中的时候还是会已解压后的文件进行存储,通过类的加载器,将压缩的文件以流的形式进行上传
InputStream inputStream = this.getClass()
.getClassLoader()
.getResourceAsStream("diagram/leave.zip");
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
Deployment deployment = processEngine.getRepositoryService()
.createDeployment()
.addZipInputStream(zipInputStream)
.name("请假流程")
.deploy();
System.out.println("流程部署ID:"+deployment.getId());
System.out.println("流程部署名称:"+deployment.getName());
}
流程图
leave.bpmn代码
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="leave" name="leaveProcess" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="请假申请" activiti:assignee="张三"></userTask>
<userTask id="usertask2" name="领导审批" activiti:assignee="李四"></userTask>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
<sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_leave">
<bpmndi:BPMNPlane bpmnElement="leave" id="BPMNPlane_leave">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="375.0" y="22.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="340.0" y="122.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="340.0" y="242.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="375.0" y="352.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="392.0" y="57.0"></omgdi:waypoint>
<omgdi:waypoint x="392.0" y="122.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="392.0" y="177.0"></omgdi:waypoint>
<omgdi:waypoint x="392.0" y="242.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="392.0" y="297.0"></omgdi:waypoint>
<omgdi:waypoint x="392.0" y="352.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
流程定义信息的查询
可以加入一些定义的条件进行条件筛选查询
@Test
public void testQueryProcessDefinition(){
List<ProcessDefinition> processDefinitions = processEngine.getRepositoryService()
.createProcessDefinitionQuery()
.processDefinitionId("leave:5:32504")
.processDefinitionVersion(5)
.list();
if (processDefinitions != null && processDefinitions.size() > 0) {
for (ProcessDefinition processDefinition : processDefinitions) {
System.out.println("流程定义的ID:" +processDefinition.getId());
System.out.println("流程定义的名称:" +processDefinition.getName());
System.out.println("流程定义的key:" +processDefinition.getKey());
System.out.println("流程定义的版本号:" +processDefinition.getVersion());
System.out.println("流程定义的图片:" +processDefinition.getDiagramResourceName());
System.out.println("流程定义的配置:" +processDefinition.getResourceName());
System.out.println("流程定义的描述:" +processDefinition.getDescription());
System.out.println("流程定义的种类:" +processDefinition.getCategory());
System.out.println("流程定义的部署:" +processDefinition.getDeploymentId());
System.out.println("=================================================");
}
}
}
流程定义的删除
删除的方式有两种;一种是普通删除;一种是强制删除;
注意的是:
当流程定义在运行的时候,如果使用的是普通删除,则会抛出异常;如果使用的是强制删除的话,会强行终止流程,强制删除
@Test
public void testDeleteProcessDefinition(){
//根据deploymentID删除定义的流程,普通删除
processEngine.getRepositoryService().deleteDeployment("1");
System.out.println("普通删除--流程定义删除成功");
//强制删除
processEngine.getRepositoryService().deleteDeployment("1", true);
System.out.println("强制删除--流程定义删除成功");
}
根据查询条件获取流程定义信息获取到部署流程的id,根据id去删除流程定义
@Test
public void testDeleteAllProcessDefinition(){
//将流程定义信息全部查询出来
List<ProcessDefinition> list = processEngine.getRepositoryService()
.createProcessDefinitionQuery()
.deploymentId("32501")
.list();
if (list != null && list.size() > 0) {
for (ProcessDefinition processDefinition : list) {
String deploymentId = processDefinition.getDeploymentId();
System.out.println("===="+deploymentId);
//根据deploymentId进行删除
processEngine.getRepositoryService().deleteDeployment(deploymentId);
}
}
}
获取图片
@Test
public void testViewImage(){
String deploymentId = "10001";
List<String> deploymentResourceNames = processEngine.getRepositoryService()
.getDeploymentResourceNames(deploymentId);
String imageName = "";
if (deploymentResourceNames != null && deploymentResourceNames.size() > 0) {
for (String string : deploymentResourceNames) {
if (string.indexOf(".png") > 0) {
imageName = string;
}
}
}
InputStream resourceAsStream = processEngine.getRepositoryService()
.getResourceAsStream(deploymentId, imageName);
File file = new File("F:/Java/activiti/image/"+imageName);
try {
FileUtils.copyInputStreamToFile(resourceAsStream, file);
System.out.println("获取图片成功");
} catch (IOException e) {
e.printStackTrace();
}
}