BPM流程有办法让它后台自动启动吗?

如图,审批人我已经通过代码设置好了,就是后台死活调不起啊啊啊!

流程

image

image

实例还在new状态时,activiti 就会去找流程实例(数据库里还没有),然后去加载它的审批者信息,找不到我就去找默认配置里的,找到了,给它了但也没啥用。可能还有其他地方也需要,但没报错!

image

image

这个没看懂,TaskEdit.java、 task-edit.xml 这两个文件自己创建?在哪里?

image

问题还没解决,只是我把它启动起来了!实在没招这也是一个方案。

搞定 :sob:

参考:
https://doc.cuba-platform.cn/bpm-7.1-chs/task_execution_sample.html

我是在提交关闭编辑界面时触发的

getApprovers方法是自己添加的,用于获取配置的默认审批者。

image

   @Subscribe
    private void onBeforeShow(BeforeShowEvent event) {
        assetRequisitionDl.load();
        initProcActionsFragment();
    }

    @Subscribe("commitAndCloseBtn")
    public void onCommitAndCloseBtnClick(Button.ClickEvent event) {
        commitChanges().then(() -> {
            BpmEntitiesService.ProcInstanceDetails procInstanceDetails = new BpmEntitiesService.ProcInstanceDetails(PROCESS_CODE)
                    .setEntity(getEditedEntity());
            List<Approver> approvers = myProcService.getApprovers(PROCESS_CODE);
            for (Approver approver : approvers) {
                procInstanceDetails.addProcActor(approver.getRole().getCode(), approver.getUser());
            }

            ProcInstance procInstance = bpmEntitiesService.createProcInstance(procInstanceDetails);

            HashMap<String, Object> processVariables = new HashMap<>();
            // processVariables.put("acceptanceRequired", getEditedEntity().getAcceptanceRequired());

            processRuntimeService.startProcess(procInstance, "Process started programmatically", processVariables);

            initProcActionsFragment();
        });
    }

    private void initProcActionsFragment() {
        procActionsFragment.initializer()
                .standard()
                .setBeforeStartProcessPredicate(() -> {
                    if (commitChanges().getStatus() == OperationResult.Status.SUCCESS) {
                        // 设置审批者
                        ProcInstance procInstance = procActionsFragment.getProcInstance();
                        Set<ProcActor> procActors = myProcService.getProcActors(PROCESS_CODE);
                        procInstance.setProcActors(procActors);
                        return true;
                    }
                    return false;
                })
                .setStartProcessActionProcessVariablesSupplier(() -> {
                    /*the supplier returns a map with process variables that will be used by the Activiti process*/
                    Map<String, Object> processVariables = new HashMap<>();
                    // processVariables.put("acceptanceRequired", getEditedEntity().g());
                    return processVariables;
                })
//                .setAfterStartProcessListener(this::initProcActionsFragment)
//                .setAfterCompleteTaskListener(this::initProcActionsFragment)
                .init(PROCESS_CODE, getEditedEntity());
    }

MyProcServiceBean.java

package com.haulmont.bpm.service;

import com.haulmont.bpm.entity.Approver;
import com.haulmont.bpm.entity.ProcActor;
import com.haulmont.bpm.entity.ProcInstance;
import com.haulmont.bpm.entity.ProcRole;
import com.haulmont.cuba.core.Persistence;
import com.haulmont.cuba.core.Transaction;
import com.haulmont.cuba.core.global.Metadata;
import com.haulmont.cuba.security.entity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 流程服务
 *
 * @author Rubin
 * @version v1 2021/4/12 12:31
 */
@Service(MyProcService.NAME)
public class MyProcServiceBean implements MyProcService {

    @Resource
    private Metadata metadata;

    @Resource
    private Persistence persistence;

    @Resource
    private ProcessRuntimeService processRuntimeService;

    @Override
    public Set<ProcActor> getProcActors(String processCode) {
        Set<ProcActor> actors = new HashSet<>();
        List<Approver> approvers = getApprovers(processCode);
        for (Approver approver : approvers) {
            actors.add(createProcActor(approver.getRole(), approver.getUser()));
        }
        return actors;
    }

    @Override
    public List<Approver> getApprovers(String processCode) {
        List<Approver> approvers;
        try (Transaction tx = persistence.getTransaction()) {
            approvers = new ArrayList<>(persistence.getEntityManager()
                    .createQuery("select e from bpm_Approver e where e.processCode = :processCode", Approver.class)
                    .setParameter("processCode", processCode)
                    .addViewName("approver-view")
                    .getResultList());
            tx.commit();
        }
        return approvers;
    }

    @Override
    public void start(ProcInstance procInstance) {
        processRuntimeService.startProcess(procInstance, "后台启动", null);
    }

    /**
     * 设置角色审批人
     *
     * @param role         角色
     * @param user         审批人
     */
    private ProcActor createProcActor(ProcRole role, User user) {
        ProcActor procActor = metadata.create(ProcActor.class);
        procActor.setProcRole(role);
        procActor.setUser(user);
        procActor.setOrder(1);
        return procActor;
    }

}
1赞