其中,总的请假次数的计算逻辑交给drools处理
新建maven项目,目录结构如下:
一:加入maven依赖:
UTF-8
5.17.0
5.6.0.Final
org.drools
drools-core
${drools.version}
org.drools
drools-compiler
${drools.version}
org.activiti
activiti-engine
${activiti.version}
provided
org.activiti
activiti-bpmn-layout
${activiti.version}
provided
org.activiti
activiti-spring
${activiti.version}
provided
org.postgresql
postgresql
9.4-1201-jdbc41
spring配置文件:
= 10}]]>
流程图如下:
package com.product;
import com.lala.bean.Leave;
rule "leave1"
when
u : Leave(day < 3);
then
u.setTotal(u.getDay() + 2);
end
rule "leave2"
when
u : Leave(day >= 3);
then
u.setTotal(u.getDay() + 5);
end
package com.lala.bean;
import java.io.Serializable;
public class Leave implements Serializable
{
private static final long serialVersionUID = 1L;
private String name;
private Integer day; //当前请假天数
private Integer total = 0; //总共请假天数
public Leave(String name, Integer day)
{
this.name = name;
this.day = day;
}
public Integer getDay()
{
return day;
}
public void setDay(Integer day)
{
this.day = day;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public String toString()
{
return "[name="+name+",day="+day+",total="+total+"]";
}
}
package com.lala.service;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
public class DroolsService implements JavaDelegate
{
public void execute(DelegateExecution execution) throws Exception
{
System.out.println("++++++++++++++++++++++++++++++++++++++");
System.out.println(execution.getVariable("reason"));
System.out.println("++++++++++++++++++++++++++++++++++++++");
}
}
package com.lala.spring;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lala.bean.Leave;
public class Main
{
static void run(ApplicationContext context) throws Exception
{
RepositoryService repositoryService = (RepositoryService)context.getBean("repositoryService");
/**
* 注意这里:必须要把drl文件一起deploy
*/
DeploymentBuilder deploy = repositoryService.createDeployment();
deploy.addClasspathResource("rule/product.drl");
deploy.addClasspathResource("bpmn/ppleave.bpmn");
deploy.deploy();
RuntimeService runtimeService = (RuntimeService)context.getBean("runtimeService");
ProcessInstance pi = runtimeService.startProcessInstanceByKey("leave");
TaskService taskService = (TaskService)context.getBean("taskService");
Map vars = new HashMap();
vars.put("leave", new Leave("白展堂", 12));
/**
* 当前任务
*/
List tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).list();
for(Task task : tasks)
{
System.out.println(task.getId() + " , " + task.getName());
taskService.complete(task.getId(), vars);
}
/**
* 下一步任务
*/
tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).list();
for(Task task : tasks)
{
System.out.println(task.getId() + " , " + task.getName());
}
}
public static void main( String[] args )throws Exception
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
run(context);
context.close();
}
}
请假天数小于3天时,输出结果:
5009 , 部门经理审批
++++++++++++++++++++++++++++++++++++++
[[name=白展堂,day=2,total=4]]
++++++++++++++++++++++++++++++++++++++
5019 , HR审批
否则,输出结果:
7509 , 部门经理审批
++++++++++++++++++++++++++++++++++++++
[[name=白展堂,day=22,total=27]]
++++++++++++++++++++++++++++++++++++++
7519 , 总经理审批