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; } }
作者:Alexia(minmin)
如果您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】
如果您希望与我交流互动,欢迎微博互粉
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。