写一个java的工作流使用案例
时间: 2023-07-20 18:33:57 浏览: 104
假设有一个请假申请的工作流,流程如下:
- 申请人提交请假申请单。
- 直属上级审批申请单,如果通过则流程结束;如果拒绝则返回给申请人修改。
- 部门经理审批申请单,如果通过则流程结束;如果拒绝则返回给申请人修改。
- 人事部门审批申请单,如果通过则流程结束;如果拒绝则返回给申请人修改。
下面是一个简单的Java工作流使用案例:
1. 定义请假申请单类
```java
public class LeaveApplication {
private String applicant; // 申请人
private int days; // 请假天数
private String reason; // 请假原因
private String comment; // 审批意见
private int status; // 审批状态
// 省略 getter 和 setter 方法
}
```
2. 定义审批人角色枚举类
```java
public enum Role {
APPLICANT, // 申请人
DIRECT_SUPERIOR, // 直属上级
DEPARTMENT_MANAGER, // 部门经理
HR_DEPARTMENT // 人事部门
}
```
3. 定义工作流类
```java
public class Workflow {
private Map<Role, User> users; // 角色与用户的映射
private Map<Role, Role> approvalMap; // 审批流转图
public Workflow(User applicant, User directSuperior, User departmentManager, User hrDepartment) {
users = new HashMap<>();
users.put(Role.APPLICANT, applicant);
users.put(Role.DIRECT_SUPERIOR, directSuperior);
users.put(Role.DEPARTMENT_MANAGER, departmentManager);
users.put(Role.HR_DEPARTMENT, hrDepartment);
approvalMap = new HashMap<>();
approvalMap.put(Role.APPLICANT, Role.DIRECT_SUPERIOR);
approvalMap.put(Role.DIRECT_SUPERIOR, Role.DEPARTMENT_MANAGER);
approvalMap.put(Role.DEPARTMENT_MANAGER, Role.HR_DEPARTMENT);
}
public LeaveApplication startWorkflow(LeaveApplication application) {
application.setStatus(0); // 设置审批状态为0,即待审批
application.setComment(""); // 设置审批意见为空
// 设置当前审批人为申请人
Role currentRole = Role.APPLICANT;
User currentUser = users.get(currentRole);
while (currentRole != null && currentUser != null) {
// 获取下一个审批人角色
Role nextRole = approvalMap.get(currentRole);
if (nextRole != null) {
// 获取下一个审批人
User nextUser = users.get(nextRole);
// 向下一个审批人发送审批申请
LeaveApplication approvalResult = nextUser.approve(application);
// 更新申请单审批状态
application.setStatus(approvalResult.getStatus());
application.setComment(approvalResult.getComment());
}
// 设置当前审批人为下一个审批人
currentRole = nextRole;
currentUser = users.get(currentRole);
}
return application;
}
}
```
4. 定义用户类
```java
public class User {
private String name;
private Role role;
public User(String name, Role role) {
this.name = name;
this.role = role;
}
public LeaveApplication approve(LeaveApplication application) {
// 根据角色进行审批
switch (role) {
case DIRECT_SUPERIOR:
// TODO: 审批逻辑
break;
case DEPARTMENT_MANAGER:
// TODO: 审批逻辑
break;
case HR_DEPARTMENT:
// TODO: 审批逻辑
break;
}
return application;
}
}
```
5. 使用工作流处理请假申请
```java
public class Main {
public static void main(String[] args) {
// 创建用户
User applicant = new User("申请人", Role.APPLICANT);
User directSuperior = new User("直属上级", Role.DIRECT_SUPERIOR);
User departmentManager = new User("部门经理", Role.DEPARTMENT_MANAGER);
User hrDepartment = new User("人事部门", Role.HR_DEPARTMENT);
// 创建工作流
Workflow workflow = new Workflow(applicant, directSuperior, departmentManager, hrDepartment);
// 创建请假申请单
LeaveApplication application = new LeaveApplication();
application.setApplicant("申请人");
application.setDays(3);
application.setReason("家中有事");
// 开始工作流
LeaveApplication result = workflow.startWorkflow(application);
// 输出结果
System.out.println("申请状态:" + result.getStatus());
System.out.println("审批意见:" + result.getComment());
}
}
```
以上是一个简单的Java工作流使用案例,实际的工作流可能会更加复杂,需要根据实际情况进行设计。
阅读全文