JBPM handler

1、 AssignmentHandler用于设置任务的接受者:

/**
 * 按照角色查询得到角色下面的所有用户,然后将任务实例分配给这些用户
 * @author Lee
 *
 */
public class RoleAssignmentHandler implements AssignmentHandler {

    //这个值将会由JBPM自动注入,因此在定义AssignmentHandler的时候,需要定义它
    private String roleName;
    
    public void assign(Assignable assignable, ExecutionContext exeContext) throws Exception {
        if (roleName == null) {
            throw new RuntimeException("你必须设角色置名称!");
        }
        
        Session session = executionContext.getJbpmContext().getSession();
        String hql = "select dm.empId from DepartmentMember dm where dm.positionId=(select sys.encode from Syscode sys where sys.codeValue=?)";
        List usernames = session.createQuery(hql).setParameter(0, roleName).list();
        if (usernames.isEmpty()) {
            throw new RuntimeException("角色【" + roleName + "】下无任何用户,无法完成任务!");
        }
        String[] pooledActors = (String[]) usernames
                .toArray(new String[usernames.size()]);
        assignable.setPooledActors(pooledActors);
        System.out.println(usernames.get(0));

}

2、DecisionHandler 在decision节点,决定流向的逻辑

public class FlowDecisionHandler implements DecisionHandler {

    @Override
    public String decide(ExecutionContext executionContext) throws Exception {
        // TODO Auto-generated method stub
        BeanFactory factory = BeanFactoryHelper.getFactory();
        DepartmentService departmentService = (DepartmentService)factory.getBean("departmentService");
        DeptMemberService  deptMemberService = (DeptMemberService)factory.getBean("deptMemberService");
        SyscodeService syscodeService = (SyscodeService)factory.getBean("syscodeService");
        String empId =(String)executionContext.getContextInstance().getVariable("empId");
        String position = "";//职位
        String department= "";//部门
        position = syscodeService.queryByEncode(deptMemberService.findDeptMemberByEmpId(empId).getPositionId()).getCodeValue();
        department = departmentService.queryByDeptId(deptMemberService.findDeptMemberByEmpId(empId).getDeptId()).getDeptName();
        if(position.equals("普通员工")) {
            return "提交给部门经理审批";
        }
        if(position.equals("部门经理") && department.equals("综合部")) {
            return "提交给总经理审批";
        }
        if(position.equals("部门经理") && (!department.equals("综合部"))) {
            return "提交给技术总监审批";
        }
        if(position.equals("技术总监")) {
            return     "提交给总经理审批";    
        }
        

        return null;
    }

}

PS:其中BeanFactory factory = BeanFactoryHelper.getFactory();是为了能在jbpm的handler中能够使用由spring管理的bean对象

public class BeanFactoryHelper implements BeanFactoryAware{

    private static BeanFactory factory;  
         
        public static  BeanFactory getFactory(){  
            return factory;  
        }

        @Override
        public void setBeanFactory(BeanFactory f) throws BeansException {
            // TODO Auto-generated method stub
             factory = f;  
        }  

}

 

 

你可能感兴趣的:(handler)