Search in sources :

Example 1 with DataSpecificationNoRollbackException

use of com.rebuild.core.service.DataSpecificationNoRollbackException in project rebuild by getrebuild.

the class ApprovalStepService method txApprove.

/**
 * @param stepRecord
 * @param signMode
 * @param cc
 * @param nextApprovers [驳回时无需]
 * @param nextNode      [驳回时无需]
 * @param addedData     [驳回时无需]
 * @param checkUseGroup [驳回时无需]
 */
public void txApprove(Record stepRecord, String signMode, Set<ID> cc, Set<ID> nextApprovers, String nextNode, Record addedData, String checkUseGroup) {
    // 审批时更新主记录
    if (addedData != null) {
        GeneralEntityServiceContextHolder.setAllowForceUpdate(addedData.getPrimary());
        try {
            Application.getEntityService(addedData.getEntity().getEntityCode()).update(addedData);
        } finally {
            // 再清理一次,以防出错未清理
            GeneralEntityServiceContextHolder.isAllowForceUpdateOnce();
        }
        // 检查数据修改后的步骤对不对 GitHub#208
        if (checkUseGroup != null) {
            Object[] stepObject = Application.createQueryNoFilter("select recordId,approvalId from RobotApprovalStep where stepId = ?").setParameter(1, stepRecord.getPrimary()).unique();
            ApprovalProcessor approvalProcessor = new ApprovalProcessor((ID) stepObject[0], (ID) stepObject[1]);
            FlowNodeGroup nextNodes = approvalProcessor.getNextNodes();
            if (!nextNodes.getGroupId().equals(checkUseGroup)) {
                throw new DataSpecificationNoRollbackException(Language.L("由于更改数据导致流程变化,你需要重新审批"));
            }
        }
    }
    super.update(stepRecord);
    final ID stepRecordId = stepRecord.getPrimary();
    Object[] stepObject = Application.createQueryNoFilter("select recordId,approvalId,node from RobotApprovalStep where stepId = ?").setParameter(1, stepRecordId).unique();
    final ID submitter = getSubmitter((ID) stepObject[0], (ID) stepObject[1]);
    final ID recordId = (ID) stepObject[0];
    final ID approvalId = (ID) stepObject[1];
    final String currentNode = (String) stepObject[2];
    final ID approver = UserContextHolder.getUser();
    String entityLabel = EasyMetaFactory.getLabel(MetadataHelper.getEntity(recordId.getEntityCode()));
    ApprovalState state = (ApprovalState) ApprovalState.valueOf(stepRecord.getInt("state"));
    // 抄送人
    if (cc != null && !cc.isEmpty()) {
        String ccMsg = Language.L("用户 @%s 提交的 %s 审批已由 @%s %s,请知晓", submitter, entityLabel, approver, Language.L(state));
        for (ID c : cc) {
            Application.getNotifications().send(MessageBuilder.createApproval(c, ccMsg, recordId));
        }
    }
    // 拒绝了直接返回
    if (state == ApprovalState.REJECTED) {
        // 拒绝了,同一节点的其他审批人全部作废
        cancelAliveSteps(recordId, approvalId, currentNode, stepRecordId, true);
        // 更新主记录状态
        Record recordOfMain = EntityHelper.forUpdate(recordId, UserService.SYSTEM_USER, false);
        recordOfMain.setInt(EntityHelper.ApprovalState, ApprovalState.REJECTED.getState());
        if (recordOfMain.getEntity().containsField(EntityHelper.ApprovalLastUser)) {
            recordOfMain.setID(EntityHelper.ApprovalLastUser, approver);
        }
        super.update(recordOfMain);
        String rejectedMsg = Language.L("@%s 驳回了你的 %s 审批", approver, entityLabel);
        Application.getNotifications().send(MessageBuilder.createApproval(submitter, rejectedMsg, recordId));
        return;
    }
    // 或签/会签
    boolean goNextNode = true;
    String approvalMsg = Language.L("有一条 %s 记录请你审批", entityLabel);
    // 或签。一人通过其他作废
    if (FlowNode.SIGN_OR.equals(signMode)) {
        cancelAliveSteps(recordId, approvalId, currentNode, stepRecordId, false);
    } else // 会签。检查是否都签了
    {
        Object[][] currentNodeApprovers = Application.createQueryNoFilter("select state,isWaiting,stepId from RobotApprovalStep where recordId = ? and approvalId = ? and node = ? and isCanceled = 'F'").setParameter(1, recordId).setParameter(2, approvalId).setParameter(3, currentNode).array();
        for (Object[] o : currentNodeApprovers) {
            if ((Integer) o[0] == ApprovalState.DRAFT.getState()) {
                goNextNode = false;
                break;
            }
        }
        // 更新下一步审批人可以开始了(若有)
        if (goNextNode && nextNode != null) {
            Object[][] nextNodeApprovers = Application.createQueryNoFilter("select stepId,approver from RobotApprovalStep where recordId = ? and approvalId = ? and node = ? and isWaiting = 'T'").setParameter(1, recordId).setParameter(2, approvalId).setParameter(3, nextNode).array();
            for (Object[] o : nextNodeApprovers) {
                Record r = EntityHelper.forUpdate((ID) o[0], approver);
                r.setBoolean("isWaiting", false);
                super.update(r);
                Application.getNotifications().send(MessageBuilder.createApproval((ID) o[1], approvalMsg, recordId));
            }
        }
    }
    // 最终状态(审批通过)
    if (goNextNode && (nextApprovers == null || nextNode == null)) {
        Application.getEntityService(recordId.getEntityCode()).approve(recordId, ApprovalState.APPROVED, approver);
        return;
    }
    // 进入下一步
    if (goNextNode) {
        Record recordOfMain = EntityHelper.forUpdate(recordId, UserService.SYSTEM_USER, false);
        recordOfMain.setString(EntityHelper.ApprovalStepNode, nextNode);
        if (recordOfMain.getEntity().containsField(EntityHelper.ApprovalLastUser)) {
            recordOfMain.setID(EntityHelper.ApprovalLastUser, approver);
        }
        super.update(recordOfMain);
    }
    // 审批人
    if (nextApprovers != null) {
        for (ID to : nextApprovers) {
            ID created = createStepIfNeed(recordId, approvalId, nextNode, to, !goNextNode, currentNode);
            // 非会签通知审批
            if (goNextNode && created != null) {
                Application.getNotifications().send(MessageBuilder.createApproval(to, approvalMsg, recordId));
            }
        }
    }
}
Also used : DataSpecificationNoRollbackException(com.rebuild.core.service.DataSpecificationNoRollbackException) Record(cn.devezhao.persist4j.Record) ID(cn.devezhao.persist4j.engine.ID)

Example 2 with DataSpecificationNoRollbackException

use of com.rebuild.core.service.DataSpecificationNoRollbackException in project rebuild by getrebuild.

the class ApprovalController method doApprove.

@PostMapping("approve")
public RespBody doApprove(HttpServletRequest request, @IdParam(name = "record") ID recordId) {
    final ID approver = getRequestUser(request);
    final int state = getIntParameter(request, "state", ApprovalState.REJECTED.getState());
    JSONObject post = (JSONObject) ServletUtils.getRequestJson(request);
    JSONObject selectUsers = post.getJSONObject("selectUsers");
    String remark = post.getString("remark");
    String useGroup = post.getString("useGroup");
    // 可编辑字段
    JSONObject aformData = post.getJSONObject("aformData");
    Record addedRecord = null;
    // 没有或无更新
    if (aformData != null && aformData.size() > 1) {
        try {
            addedRecord = EntityHelper.parse(aformData, getRequestUser(request));
        } catch (DataSpecificationException known) {
            log.warn(">>>>> {}", known.getLocalizedMessage());
            return RespBody.error(known.getLocalizedMessage());
        }
    }
    try {
        new ApprovalProcessor(recordId).approve(approver, (ApprovalState) ApprovalState.valueOf(state), remark, selectUsers, addedRecord, useGroup);
        return RespBody.ok();
    } catch (DataSpecificationNoRollbackException ex) {
        return RespBody.error(ex.getLocalizedMessage(), DefinedException.CODE_APPROVE_WARN);
    } catch (ApprovalException ex) {
        return RespBody.error(ex.getLocalizedMessage());
    }
}
Also used : DataSpecificationNoRollbackException(com.rebuild.core.service.DataSpecificationNoRollbackException) JSONObject(com.alibaba.fastjson.JSONObject) Record(cn.devezhao.persist4j.Record) DataSpecificationException(com.rebuild.core.service.DataSpecificationException) ID(cn.devezhao.persist4j.engine.ID)

Aggregations

Record (cn.devezhao.persist4j.Record)2 ID (cn.devezhao.persist4j.engine.ID)2 DataSpecificationNoRollbackException (com.rebuild.core.service.DataSpecificationNoRollbackException)2 JSONObject (com.alibaba.fastjson.JSONObject)1 DataSpecificationException (com.rebuild.core.service.DataSpecificationException)1