Activiti moduler + spring web环境构建(二)

阅读更多

Activiti moduler + spring web环境构建(二)

项目结构:基于activiti 5.21版本,可下载附件demo项目


Activiti moduler + spring web环境构建(二)_第1张图片
 

maven 配置:


	4.0.0
	org.activiti.examples
	activiti-examples
	1.0-SNAPSHOT
	jar
	BPMN 2.0 with Activiti - Examples

	
		5.21.0
		4.3.3.RELEASE
	

	
		
			org.activiti
			activiti-engine
			${activiti-version}
		
		
			org.activiti
			activiti-modeler
			
				
					xalan
					xalan
				
			
			${activiti-version}
		
		
		
			org.activiti
			activiti-diagram-rest
			${activiti-version}
		
		
			org.activiti
			activiti-explorer
			
				
					vaadin
					com.vaadin
				
				
					dcharts-widget
					org.vaadin.addons
				
				
					activiti-simple-workflow
					org.activiti
				
			
			${activiti-version}
		
		
			org.activiti
			activiti-spring
			${activiti-version}
		

		
			org.springframework
			spring-context
			${spring-version}
		
		
			org.springframework
			spring-jdbc
			${spring-version}
		
		
			org.springframework
			spring-tx
			${spring-version}
		
		
			org.springframework
			spring-core
			${spring-version}
		
		
			org.springframework
			spring-beans
			${spring-version}
		
		
			org.springframework
			spring-test
			${spring-version}
		

		
			javax.servlet
			javax.servlet-api
			3.1.0
			provided
		
		
			javax.servlet
			jstl
			1.2
		

		
			mysql
			mysql-connector-java
			5.1.39
		

		
			org.slf4j
			slf4j-api
			1.7.6
		
		
			org.slf4j
			slf4j-jdk14
			1.7.6
		

		
			junit
			junit
			4.12
		
	
	
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				2.3.2
				
					1.6
					1.6
				
			
			
				org.apache.maven.plugins
				maven-eclipse-plugin
				true
				
					
						org.eclipse.jdt.USER_LIBRARY/Activiti Designer
							Extensions
					
				
			
		
	


 

web配置



    

    
    
        org.springframework.web.context.ContextLoaderListener
    

    
        contextConfigLocation
        classpath:applicationContext.xml
    

    
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    
    
        encodingFilter
        /*
    

    
    
        springMVC
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springMVC.xml
        
        1
    
    
        springMVC
        /
    

    
    
        480
    


 

spring基础配置




    

    
        
        
        
        
        
    

    
        
        
        
        
        
    

    
        
    

    
    
    
    
    
    
    

    
    

    
        
    

 

spring MVC配置




    
    

    
    

    
    
        
            
                text/html;charset=UTF-8
            
        
    

    
    
        
            
                
            
        
    

    
        
        
        
    

    
    

 

基础操作

@RequestMapping(value = "/create", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public String create(HttpServletRequest request, HttpServletResponse response) {
        String id = "";
        try {
            String name = request.getParameter("name"),
                    key = request.getParameter("key"),
                    description = request.getParameter("category");
            // 元数据信息
            ObjectNode modelObjectNode = objectMapper.createObjectNode();
            modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, name);
            modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
            modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION,
                    org.apache.commons.lang3.StringUtils
                            .defaultString(description));
            Model newModel = repositoryService.newModel();
            newModel.setMetaInfo(modelObjectNode.toString());
            newModel.setName(name);
            newModel.setKey(key);
            newModel.setCategory(description);
            repositoryService.saveModel(newModel);
            // 创建所必须的JSON数据
            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);
            repositoryService.addModelEditorSource(newModel.getId(), editorNode.toString().getBytes("utf-8"));
            id = newModel.getId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/demo/editor?modelId=" + id;
    }

    @RequestMapping("/deploy/{modelId}")
    @ResponseBody
    public Result deploy(@PathVariable String modelId) throws IOException {
        Result result = new Result();
        try {
            // 将创建的模型转为json
            JsonNode editorNode = new ObjectMapper().readTree(repositoryService.getModelEditorSource(modelId));
            BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
            BpmnModel modelCt = jsonConverter.convertToBpmnModel(editorNode);

            // 获取流程模型
            Model model = repositoryService.createModelQuery().modelId(modelId).singleResult();
            // 发布流程模型
            String processName = model.getName() + ".bpmn20.xml";
            repositoryService.createDeployment().name(model.getName()).addBpmnModel(processName, modelCt).deploy();
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/process/start/{processDefinitionId}")
    @ResponseBody
    public Map startProcess(@PathVariable String processDefinitionId, ModelMap mav, HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map result = new HashMap();
        result.put("success", true);
        try {
            Object obj = request.getSession().getAttribute("user");

            // 查询是否有表单,有的话弹出表单,否则的话直接提交任务
            FormDataImpl formData = (FormDataImpl) formService.getStartFormData(processDefinitionId);
            List propertyList = formData.getFormProperties();
            result.put("hasForm", propertyList.isEmpty() ? false : true);
            if (propertyList.isEmpty()) {
                // 定义map用于往工作流数据库中传值。
                Map variables = new HashMap();
                variables.put("applyUserName", ((User) obj).getFirstName());
                identityService.setAuthenticatedUserId(((User) obj).getId());
                // 启动实例
                runtimeService.startProcessInstanceById(processDefinitionId, variables);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    /**
     * 获取表单用于展示
     *
     * @param type     start 表示启动时的表单;task 表示任务中的表单
     * @param id       类型为start时,为流程定义ID;类型为task时,为任务ID
     * @param modelMap 返回前台数据
     * @return String 页面
     * @author DS
     */
    @RequestMapping(value = "/process/getForm/{type}/{id}")
    public String getForm(@PathVariable String type, @PathVariable String id, ModelMap modelMap) {

        FormDataImpl formData = null;

        if ("start".equals(type)) {
            formData = (FormDataImpl) formService.getStartFormData(id);
        } else if ("task".equals(type)) {
            formData = (FormDataImpl) formService.getTaskFormData(id);
        }
        // 获取属性设置
        List formProperties = formData.getFormProperties();
        Object values;
        Map attributes = new HashMap();
        for (FormProperty formProperty : formProperties) {
            if("date".equals(formProperty.getType().getName()) || "enum".equals(formProperty.getType().getName())){
                values = formProperty.getType().getInformation("values");
                if (values != null) {
                    attributes.put(formProperty.getId(), values);
                }else {
                    values = formProperty.getType().getInformation("datePattern");
                    if (values != null){
                        attributes.put(formProperty.getId(),values);
                    }
                }
            }
        }
        modelMap.put("attributes",attributes);
        modelMap.put("formList", formProperties);
        modelMap.put("type",type);
        modelMap.put("id",id);

        return "apply";
    }

    /**
     * 保存表单数据
     *
     * @param type     start 表示启动时的表单;task 表示任务中的表单
     * @param id       类型为start时,为流程定义ID;类型为task时,为任务ID
     * @param request  请求
     * @param response 响应
     * @return Result 保存结果
     * @author DS
     */
    @RequestMapping(value = "/process/saveForm/{type}/{id}")
    @ResponseBody
    public Result submitStartFormAndStartProcessInstance(@PathVariable String type, @PathVariable("id") String id,
                                                         HttpServletRequest request, HttpServletResponse response) throws IOException {
        Result result = new Result();
        Map formProperties = new HashMap();

        // 从request中读取参数然后转换
        Map parameterMap = request.getParameterMap();
        Set keySet = parameterMap.keySet();
        for (String key : keySet) {
            formProperties.put(key, parameterMap.get(key)[0]);
        }

        // 用户未登录不能操作,实际应用使用权限框架实现,例如Spring Security、Shiro等
        Object obj = request.getSession().getAttribute("user");
        try {
            if("start".equals(type)){
                formProperties.put("applyUserName",((User)obj).getFirstName());
                formProperties.put("applyUserId",((User)obj).getId());
                identityService.setAuthenticatedUserId(((User)obj).getId());
                ProcessInstance processInstance = formService.submitStartFormData(id, formProperties);

                logger.debug("流程启动成功: {}", processInstance.getId());
            }else if ("task".equals(type)){
                formService.submitTaskFormData(id,formProperties);
                logger.debug("流程进入下一步");
            }
        }catch (Exception e){
            result.setSuccess(false);
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }finally {
            identityService.setAuthenticatedUserId(null);
        }
        result.setResult(id);

        return result;
    }

 

  • Activiti moduler + spring web环境构建(二)_第2张图片
  • 大小: 40.3 KB
  • ActivitiDemo.rar (1.5 MB)
  • 下载次数: 10
  • 查看图片附件

你可能感兴趣的:(springMVC,activiti,moduler,maven,acticiti,5.21,demo)