基于jeecgboot的flowable流程增加节点表单的支持(一)

       1、流程保存的时候检测开始节点是否有form设置,有就是直接进行流程表单的关联处理,默认就是开始节点的form作为流程关联form。

    

@SuppressWarnings("null")
	@ApiOperation(value = "保存流程设计器内的xml文件")
    @PostMapping("/save")
    public Result save(@RequestBody FlowSaveXmlVo vo) {
        InputStream in = null;
        BpmnModel bpmnModel = FlowableUtils.getBpmnModel(vo.getXml());
        if (ObjectUtil.isEmpty(bpmnModel)) {
            throw new RuntimeException("获取模型设计失败!");
        }
        String processName = bpmnModel.getMainProcess().getName();
        // 获取开始节点
        StartEvent startEvent = FlowableUtils.getStartEvent(bpmnModel);
        if (ObjectUtil.isNull(startEvent)) {
            throw new RuntimeException("开始节点不存在,请检查流程设计是否有误!");
        }
        try {
            in = new ByteArrayInputStream(vo.getXml().getBytes(StandardCharsets.UTF_8));
            String deployId = flowDefinitionService.importFile(vo.getName(), vo.getCategory(), in);
            // 导入成功,保存流程实例节点的表单关联信息
            flowDefinitionService.saveDeployForm(deployId, bpmnModel);
            
        } catch (Exception e) {
            log.error("导入失败:", e);
            return Result.error(e.getMessage());
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                log.error("关闭输入流出错", e);
            }
        }
        return Result.OK("导入成功");
    }

/**
     * 新增流程实例关联表单
     *
     * @param deployId,bpmnModel 流程实例关联表单
     * @return boolean
     */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveDeployForm(String deployId, BpmnModel bpmnModel) {
		List deployFormList = new ArrayList<>();
        // 获取开始节点
        StartEvent startEvent = FlowableUtils.getStartEvent(bpmnModel);
        if (ObjectUtil.isNull(startEvent)) {
            throw new RuntimeException("开始节点不存在,请检查流程设计是否有误!");
        }
        // 保存开始节点表单信息
        SysDeployForm startDeployForm = buildDeployForm(deployId, startEvent);
        if (ObjectUtil.isNotNull(startDeployForm)) {
            deployFormList.add(startDeployForm);
        }
        // 保存用户节点表单信息
        Collection userTasks = FlowableUtils.getAllUserTaskEvent(bpmnModel);
        if (CollUtil.isNotEmpty(userTasks)) {
            for (UserTask userTask : userTasks) {
            	SysDeployForm userTaskDeployForm = buildDeployForm(deployId, userTask);
                if (ObjectUtil.isNotNull(userTaskDeployForm)) {
                    deployFormList.add(userTaskDeployForm);
                }
            }
        }
        // 批量新增部署流程和表单关联信息
        return sysDeployFormService.insertBatch(deployFormList);
	}

/**
     * 构建发布表单关联信息对象
     * @param deployId 部署ID
     * @param node 节点信息
     * @return 发布表单关联对象。若无表单信息(formKey),则返回null
     */
    private SysDeployForm buildDeployForm(String deployId, FlowNode node) {
        String formKey = null;
        SysDeployForm deployForm = new SysDeployForm();
        if (node instanceof StartEvent) {
            formKey = ((StartEvent) node).getFormKey();
            deployForm.setFormFlag("1"); //作为开始form表单标志    
        } else if (node instanceof UserTask) {
            formKey = ((UserTask) node).getFormKey();
        }
        if (StringUtils.isEmpty(formKey)) {
            return null;
        }
        SysForm sysForm = sysFormMapper.selectSysFormById(formKey);
        if (ObjectUtil.isNull(sysForm)) {
            throw new RuntimeException("表单信息查询错误");
        }
        deployForm.setDeployId(deployId);
        deployForm.setNodeKey(node.getId());
        deployForm.setNodeName(node.getName());
        deployForm.setFormId(formKey);
        return deployForm;
    }

       由于考虑到要支持其它各种类型的流程,包括钉钉等等,所以也容许用户进行表单自己手工绑定,不限制只能在流程里进行表单的绑定,目前还只支持在线formdesigner的表单绑定,还不支持online在线表单的绑定,后续考虑集成。

你可能感兴趣的:(java开发,servlet,java,服务器)