JBPM4 中实现会签

在jbpm3中实现会签时,好像大部分的方案都是通过 动态创建任务来实现的。

那么 在 jbpm4.0 中如何实现对应的功能呢?

其实在 jbpm4 中要实现这个功能 可能要比 在 jbpm3 中实现要来的简单。

在jbpm4 的 taskService 中提供了 newTask(String parentTaskId) 和 saveTask(Task task) 两个接口,通过这两个接口基本可以满足 会签的需要。

在subtask的创建上,刚搜到 小开ye 兄已经提供了一个思路, 就是在 提交到会签后,获取到会签的task,然后针对这个task创建 subtask

重新参考了jbpm4的文档,终于借助于 custome 类型 activity 来 模拟 task 的行为 来实现会签。

下面从头开始描述。

  • 首先定义会签信息的描述对象
public interface CountersignInfo extends Serializable{

    /**
     * 获取会签人员列表
     *
     * @return
     */
    public String[] getUsers();

    /**
     * 获取会签接收人
     *
     * @return
     */
    public String getReceiver();

    /**
     * 获取会签会议结论
     *
     * @return
     */
    public Conclusion getConclusion();

    /**
     * 是否全部人员都已签完
     *
     * @return
     */
    public boolean isAllSign();

    /**
     * 获取特定用户的会签结论
     * @param userId
     * @return
     */
    public Conclusion getUserConclusion(String userId);

    /**
     * 获取所有用户的会签结论
     * @return
     */
    public Map<String, Conclusion> getConclusions();

    /**
     * 设置会签综合结论计算方式
     * @param model
     */
    public void setConclusionCalculator(CountersignCalculator model);

    /**
     * 获取会签结论计算方式
     * @return
     */
    public CountersignCalculator getConclusionCalculator();

    /**
     * 会议结论定义
     *
     * @author phoenix
     */
    public enum Conclusion implements Serializable {
        // 通过
        AGREE,
        // 否决
        DENED,
        // 弃权
        ABSTAIN
    }
}
 


这里定义为接口是为了以后扩展方便。 这里包含了 会签人员、会签结论计算方式、会签接收人(汇总人),如果如要还可以设置会议组织者等信息。


2.在各种会签模式上(一票否决、半数通过、按比例通过等)的描述上,通过定义会签结论计算器 接口及起不同的实现来处理。

先看接口:

public interface CountersignCalculator extends Serializable {
    /**
     *
     * @param info
     * @return
     */
    public Conclusion calculate(CountersignInfo info);
}
 

一票否决的实现:

public class AllAgreeSign implements CountersignCalculator {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private static final Logger LOG = LoggerFactory.getLogger(AllAgreeSign.class);

    @Override
    public Conclusion calculate(CountersignInfo info) {
        // Map<String, Conclusion> conclusions = info.getConclusions();
        for (Map.Entry<String, Conclusion> entry : info.getConclusions().entrySet()) {
            if (entry.getValue() != Conclusion.AGREE) {
                LOG.debug("用户 [{}] 否决,会签未通过", entry.getKey());
                return Conclusion.DENED;
            }
        }
        return Conclusion.AGREE;
    }

}
 


按比例通过的实现:

public class PercentAgreeSign implements CountersignCalculator {
    private static final Logger LOG = LoggerFactory.getLogger(PercentAgreeSign.class);

    public PercentAgreeSign() {
    }

    public PercentAgreeSign(int minAgreeSize) {
        this.minAgreeSize = minAgreeSize;
    }

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    public int minAgreeSize = 0;

    /**
     * @return the minAgreeSize
     */
    public int getMinAgreeSize() {
        return minAgreeSize;
    }

    /**
     * @param minAgreeSize
     *            the minAgreeSize to set
     */
    public void setMinAgreeSize(int minAgreeSize) {
        this.minAgreeSize = minAgreeSize;
    }

    @Override
    public Conclusion calculate(CountersignInfo info) {
        Map<String, Conclusion> conclusions = info.getConclusions();
        int agreeSize = 0;
        for (Conclusion conclusion : conclusions.values()) {
            if (conclusion == Conclusion.AGREE) {
                agreeSize++;
            }
        }
        LOG.debug("同意人数为 [{}] 最少同意人数[{}] 总意见数[{}]", new Object[] { agreeSize, minAgreeSize, conclusions.size() });
        return (agreeSize >= minAgreeSize) ? Conclusion.AGREE : Conclusion.DENED;
    }

}
 


半数通过只是比例的一种,因此不再提供额外的实现,只需要将总人数和最小通过人数设置好就可以了(会签发起前,参会人员应该都已经确定).


会签结论以枚举的形式定义在了会签信息接口中

3.定义custom类型activity节点的处理类。

 

先把代码贴上再予以说明

public class CountersignActivity extends JpdlExternalActivity {
    private static final Logger LOG = LoggerFactory.getLogger(CountersignActivity.class);

    /**
     * 默认的会签信息在上下文中的名字
     */
    public static final String DEFAULT_COUNTERSIGN_VAR_NAME = "COUNTERSIGN";

    /**
     * 
     */
    private String counterignVarName = DEFAULT_COUNTERSIGN_VAR_NAME;

    /**
     * Task的名字,可以不设置,不设置时将采用Activity的名字
     */
    private String name;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 
     */
    private static final long serialVersionUID = 5125499454447181794L;

    /**
     * (non-Javadoc)
     * 
     * @see org.jbpm.jpdl.internal.activity.TaskActivity#execute(org.jbpm.api.activity.ActivityExecution)
     */
    @Override
    public void execute(ActivityExecution activityExecution) {
        ExecutionImpl execution = (ExecutionImpl) activityExecution;
        String taskName = StringUtils.isEmpty(name) ? execution.getActivityName() : name;

        // LOG.debug("执行 execution[{}]", execution);
        // LOG.debug("是否是 ActivityBehaviour 的实例 [{}]" + (this instanceof ActivityBehaviour));
        // LOG.debug("name 为 [{}]", taskName);
        // LOG.debug("从 execution 中取到的 Activity [{}] 的名字为 [{}]", execution.getActivity(), execution.getActivity()
        // .getName());

        CountersignInfo countersignInfo = getCountersignInfo(execution);

        // 创建主任务
        DbSession dbSession = Environment.getFromCurrent(DbSession.class);
        TaskImpl task = (TaskImpl) dbSession.createTask();
        task.setExecution(execution);
        task.setProcessInstance((ExecutionImpl) execution.getProcessInstance());
        task.setSignalling(true);
        task.setName(taskName);
        dbSession.save(task);
        HistoryEvent.fire(new TaskActivityStart(task), (ExecutionImpl) execution);

        // 创建子任务
        for (String countersigner : countersignInfo.getUsers()) {
            TaskImpl subtask = (TaskImpl) dbSession.createTask();
            LOG.debug("父任务ID为[{}]", task.getDbid());
            // subTask.setSuperTaskDbid(task.getDbid());
            task.addSubTask(subtask);
            subtask.setAssignee(countersigner);
            subtask.setName(taskName + "." + countersigner);
            subtask.setProcessInstance((ExecutionImpl) execution.getProcessInstance());
            // 这里一定设置为false,设置为true时,completeTask 会触发 signal
            // 参见 TaskImpl.complete() 方法
            // subTask.setSignalling(false);
            dbSession.save(subtask);
            // 触发事件
            HistoryEvent.fire(new TaskActivityStart(subtask), (ExecutionImpl) execution);
        }
        execution.waitForSignal();
    }

    /**
     * (non-Javadoc)
     * 
     * @see org.jbpm.jpdl.internal.activity.TaskActivity#signal(org.jbpm.api.activity.ActivityExecution,
     *      java.lang.String, java.util.Map)
     */
    @Override
    public void signal(ActivityExecution activityExecution, String signalName, Map<String, ?> parameters)
            throws Exception {
        ExecutionImpl execution = (ExecutionImpl) activityExecution;
        // LOG.debug("根据会签结论进行跳转");
        CountersignInfo countersignInfo = getCountersignInfo(execution);
        LOG.debug("会签结论为 [{}] 接收人为 [{}]", countersignInfo.getConclusion(), countersignInfo.getReceiver());

        if (parameters != null) {
            execution.setVariables(parameters);
        }
        Activity activity = execution.getActivity();
        execution.fire(signalName, activity);

        Transition transition = null;
        LOG.debug("signalName [{}], outGoingSize [{}]", signalName, activity.getOutgoingTransitions().size());
        if ((signalName == null)
                || ((Task.STATE_COMPLETED.equals(signalName)) && (activity.getOutgoingTransitions() != null) && (activity
                        .getOutgoingTransitions().size() == 1))) {
            LOG.debug("use default transaction for[{}] execution[{}]", activityExecution.getActivityName(), execution);
            transition = activity.getOutgoingTransitions().get(0);
        } else {
            transition = activity.findOutgoingTransition(signalName);
        }
        LOG.debug("执行跳转 [{}]", transition);
        if (transition != null) {
            execution.historyActivityEnd(signalName);
            execution.take(transition);
        } else {
            LOG.error("transition 为null,execution[{}] 继续等待", execution);
            execution.waitForSignal();
        }
    }

    /**
     * 
     * @param execution
     * @return
     */
    protected CountersignInfo getCountersignInfo(ActivityExecution execution) {
        return (CountersignInfo) execution.getVariable(counterignVarName);
    }
}
 

 

明天补充代码说明及如何使用
(待续)

 

你可能感兴趣的:(qq,jbpm,Gmail)