zoukankan      html  css  js  c++  java
  • activiti实例

    Java代码

    package com;  
      
    import org.activiti.bpmn.converter.BpmnXMLConverter;  
    import org.activiti.bpmn.model.BpmnModel;  
    import org.activiti.editor.constants.ModelDataJsonConstants;  
    import org.activiti.editor.language.json.converter.BpmnJsonConverter;  
    import org.activiti.engine.*;  
    import org.activiti.engine.form.StartFormData;  
    import org.activiti.engine.form.TaskFormData;  
    import org.activiti.engine.history.HistoricProcessInstance;  
    import org.activiti.engine.repository.Deployment;  
    import org.activiti.engine.repository.Model;  
    import org.activiti.engine.repository.ProcessDefinition;  
    import org.activiti.engine.runtime.ProcessInstance;  
    import org.activiti.engine.task.Task;  
    import org.codehaus.jackson.map.ObjectMapper;  
    import org.codehaus.jackson.node.ObjectNode;  
    import org.springframework.stereotype.Controller;  
    import org.springframework.web.bind.annotation.RequestMapping;  
    import org.springframework.web.bind.annotation.RequestParam;  
    import org.springframework.web.bind.annotation.ResponseBody;  
    import org.springframework.web.servlet.ModelAndView;  
      
    import javax.annotation.Resource;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    import java.util.Date;  
    import java.util.HashMap;  
    import java.util.List;  
    import java.util.Map;  
      
    /** 
     * Created with IntelliJ IDEA. 
     * User: pandy 
     * Date: 13-9-16 
     * Time: 上午9:36 
     * To change this template use File | Settings | File Templates. 
     */  
    @Controller  
    @RequestMapping("/a")  
    public class TestController {  
        @Resource(name = "taskService")  
        private TaskService taskService;  
      
        @Resource(name = "runtimeService")  
        private RuntimeService runtimeService;  
      
        @Resource(name = "repositoryService")  
        private RepositoryService repositoryService;  
      
        @Resource(name = "formService")  
        private FormService formService;  
      
        @Resource(name = "historyService")  
        private HistoryService historyService;  
      
      
        /** 
         * 获得一些查询列表 
         * 
         * @return 
         */  
        @RequestMapping("/list")  
        public ModelAndView list() {  
            ModelAndView view = new ModelAndView("list");  
            List<Task> tasks = taskService.createTaskQuery().list();  
            view.addObject("tasks", tasks);  
            view.addObject("message", "This is a message.");  
      
            List<Model> modelList = repositoryService.createModelQuery().list();  
            view.addObject("modelList", modelList);  
      
            List<Deployment> deploymentList = repositoryService.createDeploymentQuery().list();  
            view.addObject("deploymentList", deploymentList);  
      
            List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery().list();  
            view.addObject("processDefinitionList", processDefinitionList);  
      
            return view;  
        }  
      
        /** 
         * 创建一个modeler 
         * 
         * @return 
         */  
        @RequestMapping("/create")  
        public ModelAndView create() {  
      
            Date d = new Date();  
            String str = "Pandy_" + d.getTime() + "";  
      
            try {  
                ObjectMapper objectMapper = new ObjectMapper();  
                ObjectNode editorNode = objectMapper.createObjectNode();  
                editorNode.put("id", "canvas");  
                editorNode.put("resourceId", "canvas");  
                ObjectNode stencilSetNode = objectMapper.createObjectNode();  
                stencilSetNode.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#");  
                editorNode.put("stencilset", stencilSetNode);  
                Model modelData = repositoryService.newModel();  
      
                ObjectNode modelObjectNode = objectMapper.createObjectNode();  
                modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, str);  
                modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);  
                modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, str);  
                modelData.setMetaInfo(modelObjectNode.toString());  
                modelData.setName(str);  
                modelData.setKey(str);  
      
                repositoryService.saveModel(modelData);  
                repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));  
      
      
      
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
      
      
            String url = "redirect:/a/list.do";  
            return new ModelAndView(url);  
        }  
      
        /** 
         * 删除一个已经部署的实例 
         * 
         * @param id 
         * @return 
         */  
        @RequestMapping("/deleteModeler")  
        public ModelAndView deleteModeler(@RequestParam("id") String id) {  
            System.out.println("id=" + id);  
            try {  
                repositoryService.deleteModel(id);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
      
            String url = "redirect:/a/list.do";  
            return new ModelAndView(url);  
        }  
      
      
        /** 
         * 部署一个Modeler 
         * 
         * @param id 
         * @return 
         */  
        @RequestMapping("/deployModel")  
        public ModelAndView deployModel(@RequestParam("id") String id) {  
            System.out.println("id=" + id);  
            Deployment deployment = null;  
            try {  
                Model modelData = repositoryService.getModel(id);  
                ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(  
                        repositoryService.getModelEditorSource(modelData.getId())  
                );  
      
                byte[] bpmnBytes = null;  
      
                BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);  
                bpmnBytes = new BpmnXMLConverter().convertToXML(model);  
      
                String processName = modelData.getName() + ".bpmn20.xml";  
      
                deployment = repositoryService.createDeployment()  
                        .name(modelData.getName())  
                        .addString(processName, new String(bpmnBytes))  
                        .deploy();  
      
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
      
      
            String url = "redirect:/a/list.do";  
            return new ModelAndView(url);  
        }  
      
      
      
        /** 
         * 删除一个已经部署的实例 
         * 
         * @param id 
         * @return 
         */  
        @RequestMapping("/deleteDeploy")  
        public ModelAndView deleteDeploy(@RequestParam("id") String id) {  
            System.out.println("id=" + id);  
            try {  
                repositoryService.deleteDeployment(id,false);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
      
            String url = "redirect:/a/list.do";  
            return new ModelAndView(url);  
        }  
      
        /** 
         * 启动已经部署工作流 
         * 
         * @return 
         */  
        @RequestMapping("/start")  
        public ModelAndView start(@RequestParam("id") String defId) {  
            Map<String, Object> variables = new HashMap<String, Object>();  
           /* variables.put("employeeName", "Kermit"); 
            variables.put("numberOfDays", new Integer(4)); 
            variables.put("vacationMotivation", "I'm really tired!");*/  
            //启动  
            System.out.println("启动");  
      
            //ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess", variables);  
            ProcessInstance processInstance = runtimeService.startProcessInstanceById(defId, variables);  
            //StartFormData formData = formService.getStartFormData(processInstance.getProcessDefinitionId());  
      
            String url = "redirect:/a/list.do";  
            return new ModelAndView(url);  
        }  
      
        /** 
         * 完成一个节点 
         * 
         * @param id 
         * @return 
         */  
        @RequestMapping("/complete")  
        public ModelAndView complete(@RequestParam("id") String id) {  
            //List<Task> list = taskService.createTaskQuery().taskDefinitionKey(id).list();  
            List<Task> list = taskService.createTaskQuery().taskId(id).list();  
            if (list != null && !list.isEmpty()) {  
                for (Task task : list) {  
      
      
                    TaskFormData formData = formService.getTaskFormData(task.getId());  
                    taskService.complete(task.getId());  
                }  
            }  
      
            String url = "redirect:/a/list.do";  
            return new ModelAndView(url);  
        }  
      
        /** 
         * 导出流程 
         * 
         * @param fileName 
         * @param bpmnBytes 
         */  
        public void exportFile(String fileName, byte[] bpmnBytes) {  
            fileName = "/mnt/D/work_documents/workspace/ActivitiTemp/src/main/resources/diagrams/" + fileName;  
            System.out.println(fileName);  
            FileOutputStream fos = null;  
            try {  
                fos = new FileOutputStream(fileName, true);  
                fos.write(bpmnBytes);  
                fos.flush();  
            } catch (Exception e) {  
                e.printStackTrace();  
            } finally {  
                try {  
                    fos.close();  
                } catch (IOException iex) {  
                }  
            }  
      
      
        }  
      
      
      
        /** 
         * 启动已经部署工作流 
         * 
         * @return 
         */  
        @RequestMapping("/start1")  
        public ModelAndView start1() {  
            Map<String, Object> variables = new HashMap<String, Object>();  
           /* variables.put("employeeName", "Kermit"); 
            variables.put("numberOfDays", new Integer(4)); 
            variables.put("vacationMotivation", "I'm really tired!");*/  
            //启动  
            System.out.println("启动");  
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess", variables);  
            StartFormData formData = formService.getStartFormData(processInstance.getProcessDefinitionId());  
      
            String url = "redirect:/a/list.do";  
            return new ModelAndView(url);  
        }  
      
        /** 
         * 启动已经部署工作流 
         * 
         * @return 
         */  
        @RequestMapping("/start2")  
        public ModelAndView start2() {  
            Map<String, Object> variables = new HashMap<String, Object>();  
           /* variables.put("employeeName", "Kermit"); 
            variables.put("numberOfDays", new Integer(4)); 
            variables.put("vacationMotivation", "I'm really tired!");*/  
            //启动  
            System.out.println("启动");  
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess", variables);  
      
            String url = "redirect:/a/list.do";  
            return new ModelAndView(url);  
        }  
      
        /** 
         * 启动已经部署Form工作流 
         * 
         * @return 
         */  
        @RequestMapping("/start3")  
        public ModelAndView start3() {  
            Map<String, Object> variables = new HashMap<String, Object>();  
           /* variables.put("employeeName", "Kermit"); 
            variables.put("numberOfDays", new Integer(4)); 
            variables.put("vacationMotivation", "I'm really tired!");*/  
            //启动  
            System.out.println("启动");  
            Map<String, String> properties = new HashMap<String, String>();  
            ProcessInstance processInstance = formService.submitStartFormData("xxxxx", properties);  
      
            String url = "redirect:/a/list.do";  
            return new ModelAndView(url);  
        }  
      
        /** 
         * 这只是一个测试的方法 
         * 
         * @return 
         */  
        @RequestMapping("/mytest")  
        public ModelAndView mytest() {  
            System.out.println("开始执行工作流的方法");  
      
            //发布  
            System.out.println("发布");  
            repositoryService.createDeployment()  
                    .addClasspathResource("bpmn/MyProcess.bpmn")  
                    .deploy();  
      
            Map<String, Object> variables = new HashMap<String, Object>();  
           /* variables.put("employeeName", "Kermit"); 
            variables.put("numberOfDays", new Integer(4)); 
            variables.put("vacationMotivation", "I'm really tired!");*/  
      
      
            //启动  
            System.out.println("启动");  
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess", variables);  
      
            //遍历任务  
            System.out.println("遍历所有");  
            //List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("management").list();  
            //List<Task> tasks = taskService.createTaskQuery().taskCandidateUser("test").list();  
            List<Task> tasks = taskService.createTaskQuery().taskAssignee("test").list();  
            for (Task task : tasks) {  
                System.out.println("#Task available: " + task.getId() + ":" + task.getName());  
      
      
                Map<String, Object> taskVariables = new HashMap<String, Object>();  
                //taskService.complete(task.getId(), taskVariables);  
                System.out.println("complete#Task: " + task.getId() + ":" + task.getName());  
            }  
      
            //查看任务  
            System.out.println("遍历所有");  
            tasks = taskService.createTaskQuery().list();  
            for (Task task : tasks) {  
                System.out.println("@Task available: " + task.getId() + ":" + task.getName());  
            }  
      
            String url = "redirect:/a/list.do";  
            return new ModelAndView(url);  
        }  
    }  

      Html代码

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>  
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>  
    <%  
        String path=request.getContextPath();  
    %>  
    <html>  
    <head>  
        <title></title>  
        <style>  
            table,tr,th,td{  
                border: 1px solid #000000;  
            }  
        </style>  
    </head>  
    <body>  
    <br>模型列表: 创建模型: <a href="<%=path%>/a/create.do">Create</a>  
    <table border="0">  
        <tr>  
            <th>ID</th>  
            <th>Key</th>  
            <th>Name</th>  
            <th>编辑</th>  
            <th>部署</th>  
            <th>删除</th>  
        </tr>  
        <c:forEach items="${modelList}" var="t">  
            <tr>  
                <td>${t.id}</td>  
                <td>${t.key}</td>  
                <td>${t.name}</td>  
                <td><a target="_blank" href="<%=path%>/modeler/service/editor?id=${t.id}">编辑</a></td>  
                <td><a target="_self" href="<%=path%>/a/deployModel.do?id=${t.id}">部署</a></td>  
                <td><a target="_self" href="<%=path%>/a/deleteModeler.do?id=${t.id}">删除</a></td>  
            </tr>  
        </c:forEach>  
    </table>  
      
    <br>已经部署列表: 启动任务: <a href="<%=path%>/a/start1.do">Start1</a>  <a href="<%=path%>/a/start2.do">Start2</a>  
    <table border="0">  
        <tr>  
            <th>定义ID</th>  
            <th>部署ID</th>  
            <th>Key</th>  
            <th>Name</th>  
            <th>启动</th>  
            <th>删除</th>  
        </tr>  
        <c:forEach items="${processDefinitionList}" var="d">  
            <tr>  
                <td>${d.id}</td>  
                <td>${d.deploymentId}</td>  
                <td>${d.key}</td>  
                <td>${d.name}</td>  
                <td><a href="<%=path%>/a/start.do?id=${d.id}">启动</a></td>  
                <td><a href="<%=path%>/a/deleteDeploy.do?id=${d.deploymentId}">删除</a></td>  
            </tr>  
        </c:forEach>  
    </table>  
      
      
    <br>任务列表:  
    <table border="0">  
        <tr>  
            <th>ID</th>  
            <th>Name</th>  
            <th>完成</th>  
        </tr>  
        <c:forEach items="${tasks}" var="t">  
            <tr>  
                <td>${t.id}</td>  
                <td>${t.name}</td>  
                <td><a href="<%=path%>/a/complete.do?id=${t.id}">批准</a></td>  
            </tr>  
        </c:forEach>  
    </table>  
      
    </body>  
    </html>  

    xml

    <?xml version="1.0" encoding="UTF-8"?>  
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
           xmlns:context="http://www.springframework.org/schema/context"  
           xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"  
           xmlns:util="http://www.springframework.org/schema/util"  
           xsi:schemaLocation="  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd  
            http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">  
        <context:component-scan base-package="com">  
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
        </context:component-scan>  
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
            <property name="url" value="jdbc:mysql://192.168.0.196:3306/activiti_demo"/>  
            <property name="username" value="root"/>  
            <property name="password" value=""/>  
        </bean>  
        <!--<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource">  
            <property name="driverClass" value="com.mysql.jdbc.Driver" />  
            <property name="jdbcUrl" value="jdbc:mysql://192.168.0.196:3306/activiti_demo" />  
            <property name="username" value="root" />  
            <property name="password" value="" />  
        </bean>-->  
      
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
            <property name="dataSource" ref="dataSource"/>  
        </bean>  
        <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">  
            <property name="dataSource" ref="dataSource"/>  
            <property name="deploymentResources" value="classpath*:/bpmn/*.bpmn"/>  
            <property name="transactionManager" ref="transactionManager"/>  
            <property name="databaseSchemaUpdate" value="true"/>  
            <property name="jobExecutorActivate" value="false"/>  
            <property name="processDefinitionCacheLimit" value="10"/>  
            <property name="activityFontName" value="${diagram.activityFontName}"/>  
            <property name="labelFontName" value="${diagram.labelFontName}"/>  
        </bean>  
        <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">  
            <property name="processEngineConfiguration" ref="processEngineConfiguration"/>  
        </bean>  
        <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>  
        <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>  
        <bean id="formService" factory-bean="processEngine" factory-method="getFormService"/>  
        <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService"/>  
        <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>  
        <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>  
        <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService"/>  
        <!-- Activiti end -->  
    </beans> 
  • 相关阅读:
    Redis基本数据类型与持久化
    Jpa创建筛选器自动忽略nul值进行搜索
    layui单元格换行
    form多个name相同表单处理
    layui合并单元格
    关于jpa example使用
    mui下拉菜单
    img在div中居中
    高德地图定位api使用
    【剑指offer】20.表示数值的字符串
  • 原文地址:https://www.cnblogs.com/qqyong123/p/9788848.html
Copyright © 2011-2022 走看看