package bin.feifei;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.zip.ZipInputStream;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jbpm.api.Configuration;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessDefinitionQuery;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskQuery;
import org.jbpm.api.TaskService;
import org.jbpm.api.model.Activity;
import org.jbpm.api.task.Task;
import org.jbpm.pvm.internal.model.ActivityImpl;
import org.jbpm.pvm.internal.model.ProcessDefinitionImpl;
import org.jbpm.pvm.internal.query.TaskQueryImpl;
import org.jbpm.pvm.internal.task.OpenTask;
import org.jbpm.pvm.internal.task.TaskImpl;
import bin.util.Env;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
public class JbpmServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String mothed = (String) request.getParameter("mothed");
try {
if ("deploy".equals(mothed)) { // 发布流程 no.1
doDeploy(request, response);
} else if ("listFlow".equals(mothed)) { // 查询所发布的流程 no.2
listFlow();
} else if ("apply".equals(mothed)) { // 提交请假申请单no.3
apply(request, response);
} else if ("subTask".equals(mothed)) { // 发布子任务
subTask(request, response);
} else if ("custom".equals(mothed)) { // 发布子任务
custom(request, response);
} else if ("findTask".equals(mothed)) { // 查询任务列表
findTask(request, response);
} else if ("complete".equals(mothed)) { // 查询任务列表
complete(request, response);
}else if ("deleteExecution".equals(mothed)) { // 提交请假申请单no.3
deleteExecution();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void custom(HttpServletRequest request, HttpServletResponse response) {
ProcessEngine processEngine = Configuration.getProcessEngine(); // 获取流程引擎
ExecutionService executionService = processEngine.getExecutionService();
executionService.startProcessInstanceByKey("custom", new Random().nextInt() + "");
}
private void complete(HttpServletRequest request, HttpServletResponse response) {
ProcessEngine processEngine = Configuration.getProcessEngine();
TaskService taskService = processEngine.getTaskService();
String taskId = request.getParameter("taskId");
taskService.completeTask(taskId);
}
private void findTask(HttpServletRequest request, HttpServletResponse response) throws Exception {
ProcessEngine processEngine = Configuration.getProcessEngine();
TaskService taskService = processEngine.getTaskService();
TaskQueryImpl taskQuery = (TaskQueryImpl)processEngine.getTaskService().createTaskQuery();
String id = request.getParameter("id");
TaskImpl taskImpl = (TaskImpl)taskQuery.processInstanceId(id).uniqueResult();
List<Task> taskList = taskService.getSubTasks(taskImpl.getDbid()+"");
request.setAttribute("taskList", taskList);
forward("/taskList.jsp");
}
private void subTask(HttpServletRequest request, HttpServletResponse response) {
ProcessEngine processEngine = Configuration.getProcessEngine(); // 获取流程引擎
ExecutionService executionService = processEngine.getExecutionService();
TaskService taskService = processEngine.getTaskService();
System.out.println("--------------findPersonalTasks-----------------------------");
ProcessInstance pi = executionService.startProcessInstanceByKey("subTask", new Random().nextInt() + "");
}
/**
* @description 发布流程 no.1
*/
private void doDeploy(HttpServletRequest request, HttpServletResponse response) throws IOException {
ServletOutputStream writer = response.getOutputStream();
try {
ProcessEngine processEngine = Configuration.getProcessEngine(); // 获取流程引擎
RepositoryService repositoryService = processEngine.getRepositoryService();
String filePath = getServletContext().getRealPath("/upload");
File file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
MultipartRequest multi = new MultipartRequest(request, filePath, 1024 * 1024, "UTF-8", new DefaultFileRenamePolicy());
FileInputStream fileIS = new FileInputStream(multi.getFile("flowFile"));
ZipInputStream zip = new ZipInputStream(fileIS);
// 创建流程实例 .setTimestamp(new Date().getTime()).setName("xiaoxu");
// 可以设置时间和发布人
System.out.println("--------------------------------------------");
repositoryService.createDeployment().addResourcesFromZipInputStream(zip).deploy();
} catch (Exception e) {
e.printStackTrace();
writer.print("发布失败");
}
}
/**
* @description 查询所发布的流程 no.2
*/
private void listFlow() throws Exception {
ProcessEngine processEngine = Configuration.getProcessEngine(); // 获取流程引擎
RepositoryService repositoryService = processEngine.getRepositoryService();
ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
/*--- 一些查询条件
query.processDefinitionNameLike(processName);
query.processDefinitionKey(processKey);
query.processDefinitionId(processId);
query.notSuspended(); //活动中
query.suspended(); //暂停
query.page(start, pager.getPageSize());
query.count();*/
Env.getEnv().getRequest().setAttribute("flowList", query.list());
forward("/flowList.jsp");
}
private void apply(HttpServletRequest request, HttpServletResponse response) {
ProcessEngine processEngine = Configuration.getProcessEngine(); // 获取流程引擎
ExecutionService executionService = processEngine.getExecutionService();
TaskService taskService = processEngine.getTaskService();
String user = request.getParameter("user");
Boolean isManager = Boolean.parseBoolean(request.getParameter("isManager"));
int day = Integer.parseInt(request.getParameter("day"));
Map<String, Object> param = new HashMap<String, Object>();
param.put("user", user);
param.put("isManager", isManager.booleanValue());
param.put("day", day);
System.out.println("--------------findPersonalTasks-----------------------------");
ProcessInstance pi = executionService.startProcessInstanceByKey("leave", param, new Random().nextInt() + "");
System.out.println("--------------findPersonalTasks-----------------------------");
List<Task> taskList = taskService.findPersonalTasks("feifeiJL");
}
private void forward(String url) throws Exception {
RequestDispatcher dispatcher = Env.getEnv().getRequest().getRequestDispatcher(url);
dispatcher.forward(Env.getEnv().getRequest(), Env.getEnv().getResponse());
}
private void deleteExecution(){
ProcessEngine processEngine = Configuration.getProcessEngine(); // 获取流程引擎
ExecutionService executionService = processEngine.getExecutionService();
System.out.println("--------------------------------------------");
//executionService.deleteProcessInstanceCascade("leave.-1700445529");
System.out.println("--------------------------------------------");
ProcessInstance pi = executionService.findProcessInstanceById("leave.-1700445529");
System.out.println(pi.getProcessInstance());
System.out.println(pi.getId());
RepositoryService repositoryService = processEngine.getRepositoryService();
ProcessDefinition definition = repositoryService.createProcessDefinitionQuery()
.processDefinitionId(pi.getProcessDefinitionId()).uniqueResult();
ProcessDefinitionImpl definitionimpl = (ProcessDefinitionImpl)definition;
List<? extends Activity> list = definitionimpl.getActivities();
Map<String, ActivityImpl> map = (Map<String, ActivityImpl>) definitionimpl.getActivitiesMap();
for (Activity activity : list) {
System.out.println(activity.getName());
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<process name="leave" key="leave" xmlns="http://jbpm.org/4.4/jpdl">
<start g="313,80,48,48" name="start1">
<transition g="-83,-17" name="是否经理" to="exclusive1"/>
</start>
<decision expr="#{isManager ? '老板' : '经理'}" g="314,159,48,48" name="exclusive1">
<transition g="227,184:-53,-17" name="经理" to="经理"/>
<transition g="460,180:-53,-17" name="老板" to="老板"/>
</decision>
<task assignee="feifeiJL" g="184,238,92,52" name="经理" >
<transition to="end1" g="237,506:-47,-17"/>
<transition name="需要老板审核" to="老板" g="-47,-17"/>
</task>
<task assignee="feifeiLB" g="418,360,92,52" name="老板" >
<transition to="end1" g="470,498:"/>
</task>
<end g="331,482,48,48" name="end1"/>
</process>
<field name="fei">
<object expr="#{fei}"/>
</field>