pom.xml
<!-- activit jar 包 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.19.0.2</version>
</dependency> <dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>5.19.0.2</version>
</dependency> <dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-model</artifactId>
<version>5.19.0.2</version>
</dependency>
<!-- spring整合activiti插件 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>5.19.0.3</version>
</dependency>
----------
spring 管理activiti
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> <!-- 连接数据库的配置 -->
<property name="jdbcDriver" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="jdbcUrl" value="jdbc:oracle:thin:@10.2.13.100:1521:orcl"></property>
<property name="jdbcUsername" value="cyqq"></property>
<property name="jdbcPassword" value="1"></property> <!-- 没有表创建表 -->
<property name="databaseSchemaUpdate" value="true"></property>
</bean> <!-- 创建流程引擎对象 -->
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean> <!-- 由流程引擎对象,提供的方法,创建项目中使用的Activiti工作流的Service -->
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
<bean id="formService" factory-bean="processEngine" factory-method="getFormService" /> <!-- 将Activiti工作流的Service注入WrokflowService中 -->
<bean id="changeWorkflowService" class="com.chen.workflowservice.impl.ChangeWorkFlowService">
<property name="repositoryService" ref="repositoryService"></property>
<property name="runtimeService" ref="runtimeService"></property>
<property name="taskService" ref="taskService"></property>
<property name="formService" ref="formService"></property>
<property name="historyService" ref="historyService"></property>
</bean>
</beans>
ChangeWorkFlowService
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream; import javax.servlet.http.HttpServletResponse; import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmActivity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.task.TaskDefinition;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task; import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.tcinc.cyqq.model.other.TP_BUSINESS;
import com.tcinc.cyqq.workflowservice.IWorkflowService; public class ChangeWorkFlowService implements IWorkflowService {
/** 创建工作流Service */
public RepositoryService repositoryService;// 流程定义
public RuntimeService runtimeService;// 运行时
public TaskService taskService;// 任务
public FormService formService;// 任务表单
public HistoryService historyService;// 历史 public void setRepositoryService(RepositoryService repositoryService) {
this.repositoryService = repositoryService;
} public void setRuntimeService(RuntimeService runtimeService) {
this.runtimeService = runtimeService;
} public void setTaskService(TaskService taskService) {
this.taskService = taskService;
} public void setFormService(FormService formService) {
this.formService = formService;
} public void setHistoryService(HistoryService historyService) {
this.historyService = historyService;
} /*-------------------------------------方法实现------------------------------------*/ /*----------------------------------------- 接口实现 ----------------------------------------*/ @Override
public void deploy(String deployBpmn, String deployPng, String deployName) {
try {
repositoryService.createDeployment()
// 创建部署对象
.name(deployName).addClasspathResource(deployBpmn)
.addClasspathResource(deployPng).deploy();// 完成部署
} catch (Exception ex) {
ex.printStackTrace();
}
} @Override
public void deploy(File file, String deployName) {
try {
ZipInputStream zipInputStream = new ZipInputStream(
new FileInputStream(file));
repositoryService.createDeployment().name(deployName)
.addZipInputStream(zipInputStream).deploy();
} catch (Exception ex) {
ex.printStackTrace();
}
} @Override
public void delDeployGeneral(String deployId) {
try {
// 普通删除,如果当前规则下有正在执行的流程,则抛异常
repositoryService.deleteDeployment(deployId);
} catch (Exception ex) {
ex.printStackTrace();
}
} @Override
public void delDeployCascade(String deployId) {
try {
// 级联删除,会删除和当前规则相关的所有信息,包括历史
repositoryService.deleteDeployment(deployId, true);
} catch (Exception ex) {
ex.printStackTrace();
}
} @Override
public List<Deployment> findDeploymentList() {
List<Deployment> list = null;
try {
list = repositoryService.createDeploymentQuery()
.orderByDeploymenTime().asc().list();
} catch (Exception ex) {
ex.printStackTrace();
}
return list;
} @Override
public List<Deployment> findDeploymentList(String deployName) {
List<Deployment> list = null;
try {
list = repositoryService.createDeploymentQuery()
.deploymentName(deployName).orderByDeploymenTime().asc()
.list();
} catch (Exception ex) {
ex.printStackTrace();
}
return list;
} @Override
public Deployment findDeployment(String deployName) {
Deployment deployment = null;
try {
deployment = repositoryService.createDeploymentQuery()
.deploymentName(deployName).orderByDeploymenTime().desc()
.singleResult();
} catch (Exception ex) {
ex.printStackTrace();
}
return deployment;
} @Override
public List<ProcessDefinition> findProcessDefinitionList() {
List<ProcessDefinition> list = null;
try {
list = repositoryService.createProcessDefinitionQuery()
.orderByProcessDefinitionVersion().asc().list();
} catch (Exception ex) {
ex.printStackTrace();
}
return list;
} @Override
public ProcessDefinition findProcessDefinition(String deployId) {
ProcessDefinition processDefinition = null;
try {
processDefinition = repositoryService
.createProcessDefinitionQuery().deploymentId(deployId)
.orderByProcessDefinitionVersion().desc().singleResult();
} catch (Exception ex) {
ex.printStackTrace();
}
return processDefinition;
} @Override
public String findProcessKey(String deployName) {
Deployment deployment = findDeployment(deployName);
ProcessDefinition pd = findProcessDefinition(deployment.getId()); return pd.getKey();
} @Override
public ActivityImpl findTaskNode(String processInstanceId) {
ExecutionEntity execution = null;
ProcessDefinitionEntity processDefinitionEntity = null;
ActivityImpl activity = null;
try {
// 获取流程发布Id信息
String definitionId = getDefinitionId(processInstanceId); processDefinitionEntity = getProcessDefinitionEntity(definitionId); execution = getExecutionEntity(processInstanceId); // 获取流程所有节点信息
List<ActivityImpl> activitiList = processDefinitionEntity
.getActivities(); // 遍历所有节点信息
for (ActivityImpl activityImpl : activitiList) {
// 找到当前节点信息
if (execution.getActivityId().equals(activityImpl.getId())) {
activity = activityImpl;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return activity;
} @Override
public ProcessDefinition findProcessDefinitionByPid(String processInstanceId) {
ProcessDefinitionEntity processDefinitionEntity = null;
ProcessDefinition processDefinition = null;
try {
// 获取流程发布Id信息
String definitionId = getDefinitionId(processInstanceId); processDefinitionEntity = getProcessDefinitionEntity(definitionId); String deploymentId = processDefinitionEntity.getDeploymentId(); processDefinition = getProcessDefinition(deploymentId); } catch (Exception ex) {
ex.printStackTrace();
}
return processDefinition;
} @Override
public ProcessDefinition findProcessDefinitionByHistory(
String processInstanceId) {
ProcessDefinitionEntity processDefinitionEntity = null;
ProcessDefinition processDefinition = null;
try {
// 获取流程发布Id信息
String definitionId = getDefinitionIdByHistory(processInstanceId); processDefinitionEntity = getProcessDefinitionEntity(definitionId); String deploymentId = processDefinitionEntity.getDeploymentId(); processDefinition = getProcessDefinition(deploymentId); } catch (Exception ex) {
ex.printStackTrace();
}
return processDefinition;
} @Override
public List<Task> findTaskListByRole(String roleId) {
List<Task> list = taskService.createTaskQuery()
.taskCandidateUser(roleId).orderByTaskCreateTime().asc().list(); return list;
} @Override
public String findCompleteTaskByUser(String userId, String chartId) {
List<HistoricTaskInstance> list = historyService
.createHistoricTaskInstanceQuery().taskAssignee(userId).list();
HistoricProcessInstance hi = null;
ProcessDefinition pd = null; StringBuilder businessIds = new StringBuilder();
String str = "";
for (HistoricTaskInstance ti : list) {
hi = getHistoricProcessInstance(ti.getProcessInstanceId().trim());
pd = getProcessDefinitionByDe(hi.getProcessDefinitionId().trim()); if (!pd.getKey().trim().equals(chartId))
continue; str = "'"
+ hi.getBusinessKey().trim()
.replace(pd.getKey().trim() + ".", "") + "',";
if (!businessIds.toString().contains(str)) {
businessIds.append(str);
}
} str = businessIds.toString().trim();
if (!"".equals(str) && str.endsWith(",")) {
str = str.substring(0, str.length() - 1);
} return str;
} @Override
public InputStream findImageInputStream(String deploymentId,
String imageName) {
return repositoryService.getResourceAsStream(deploymentId, imageName);
} @Override
public Page<TP_BUSINESS> findBusiness(int pageNumber, int pageSize,
String roleId) {
return null;
} @Override
public ProcessInstance startProcess(String key) {
System.out.println("key...................................");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key);
System.out.println(processInstance);
// 获取任务对象
return processInstance;
} @Override
public Map<String, Object> findCoording(String processInstanceId) {
// 存放坐标
Map<String, Object> map = new HashMap<String, Object>();
// 使用任务ID,查询任务对象
Task task = getTask(processInstanceId); // 获取流程定义的ID
String processDefinitionId = task.getProcessDefinitionId();
// 获取流程定义的实体对象,(对应.bpmn文件中的数据)
ProcessDefinitionEntity processDefinitionEntity = getProcessDefinitionEntity(processDefinitionId); // 使用流程实例ID,查询正在执行的对象表,获取当前活动对应的流程实例对象
ProcessInstance pi = getProcessInstance(processInstanceId); // 获取当前活动的ID
String activityId = pi.getActivityId();
// 获取当前活动对象
ActivityImpl activityImpl = processDefinitionEntity
.findActivity(activityId);
// 获取坐标
map.put("x", activityImpl.getX());
map.put("y", activityImpl.getY());
map.put("width", activityImpl.getWidth());
map.put("height", activityImpl.getHeight());
return map;
} @Override
public List<Map<String, Object>> findCoordings(String deploymentId) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = null;
ProcessDefinition pd = getProcessDefinition(deploymentId);
ProcessDefinitionEntity processDefinition = getProcessDefinitionEntity(pd
.getId());
List<ActivityImpl> activitiList = processDefinition.getActivities(); for (ActivityImpl activity : activitiList) {
map = new HashMap<String, Object>();
map.put("x", activity.getX());
map.put("y", activity.getY());
map.put("width", activity.getWidth());
map.put("height", activity.getHeight());
map.put("taskId", activity.getId());
map.put("taskName", activity.getProperty("name"));
map.put("processKey", pd.getKey()); list.add(map);
} return list;
} @Override
public boolean finishTask(String processInstanceId, String lineName,
String userId) {
String taskId = "";
Map<String, Object> variables = new HashMap<String, Object>(); // 使用任务ID,查询任务对象
Task task = getTask(processInstanceId); taskId = task.getId().trim(); // 拾取办理人
taskService.claim(taskId, userId); // 设置连线名称
variables.put("msg", lineName); // 完成任务
taskService.complete(taskId, variables); // 判断流程是否结束
ProcessInstance pi = getProcessInstance(processInstanceId); if (pi == null) {
return true;
} return false;
} @Override
public TaskDefinition getNextTaskDefinition(String processInstanceId,
String msg) { String activityId = ""; String definitionId = getDefinitionId(processInstanceId); ProcessDefinitionEntity processDefinitionEntity = getProcessDefinitionEntity(definitionId); ExecutionEntity execution = getExecutionEntity(processInstanceId); activityId = execution.getActivityId(); ActivityImpl activityImpl = processDefinitionEntity
.findActivity(activityId);// 当前节点 // 获取当前节点连线所有集合
List<PvmTransition> pvmTransitions = activityImpl
.getOutgoingTransitions(); for (PvmTransition pvmTransition : pvmTransitions) {
PvmActivity pa = pvmTransition.getDestination();// 获取所有的终点节点 if ("userTask".equals(pa.getProperty("type"))) {
return ((UserTaskActivityBehavior) ((ActivityImpl) pa)
.getActivityBehavior()).getTaskDefinition();
}
} return null;
} @Override
public Map<String, String> isEndTask(String processInstanceId, String msg) { Map<String, String> map = new HashMap<String, String>();
map.put("isEnd", "false"); String activityId = ""; String definitionId = getDefinitionId(processInstanceId); ProcessDefinitionEntity processDefinitionEntity = getProcessDefinitionEntity(definitionId); ExecutionEntity execution = getExecutionEntity(processInstanceId); activityId = execution.getActivityId(); ActivityImpl activityImpl = processDefinitionEntity
.findActivity(activityId);// 当前节点 // 获取当前节点连线所有集合
List<PvmTransition> pvmTransitions = activityImpl
.getOutgoingTransitions(); for (PvmTransition pvmTransition : pvmTransitions) {
PvmActivity pa = pvmTransition.getDestination();// 获取所有的终点节点
// 获取连线名
String condition = pvmTransition.getProperty("conditionText")
.toString().trim().replace("${msg==", "").replace("}", "")
.replace("'", ""); if (msg.equals(condition)) {
if ("endEvent".equals(pa.getProperty("type"))) {
map.put("isEnd", "true");
map.put("node", pa.getId().trim());
return map;
}
}
} return map;
} /*---------------------------------------------------常用方法-----------------------------------------------*/ /**
* 根据流程实例 ID,获取流程定义ID
*
* @param processInstanceId
* 流程实例ID
* @return
*/
public String getDefinitionId(String processInstanceId) {
String definitionId = "";
ProcessInstance pi = runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
definitionId = pi.getProcessDefinitionId().trim(); return definitionId;
} /**
* 根据流程实例ID,获取流程定义ID(历史表)
*
* @param processInstanceId
* @return
*/
public String getDefinitionIdByHistory(String processInstanceId) {
String definitionId = "";
HistoricProcessInstance pi = historyService
.createHistoricProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
definitionId = pi.getProcessDefinitionId().trim();
return definitionId;
} /**
* 根据流程定义ID,获取流程定义对象实体
*
* @param definitionId
* 流程定义ID
* @return
*/
public ProcessDefinitionEntity getProcessDefinitionEntity(
String definitionId) {
ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
.getDeployedProcessDefinition(definitionId); return processDefinitionEntity;
} /**
* 根据流程部署ID,获取流程定义对象实体
*
* @param deploymentId
* 流程部署ID
* @return
*/
public ProcessDefinition getProcessDefinition(String deploymentId) {
ProcessDefinition processDefinition = repositoryService
.createProcessDefinitionQuery().deploymentId(deploymentId)
.singleResult(); return processDefinition;
} /**
* 根据流程定义ID,获取流程定义对象实体
*
* @param deploymentId
* 流程定义ID
* @return
*/
public ProcessDefinition getProcessDefinitionByDe(String processDefinitionId) {
ProcessDefinition processDefinition = repositoryService
.createProcessDefinitionQuery()
.processDefinitionId(processDefinitionId).singleResult(); return processDefinition;
} /**
* 根据流程实例ID,获取任务
*
* @param processInstanceId
* 流程实例ID
* @return
*/
public Task getTask(String processInstanceId) {
Task task = taskService.createTaskQuery()
.processInstanceId(processInstanceId).singleResult(); return task;
} /**
* 根据流程实例ID,获取运行时对象实体
*
* @param processInstanceId
* 流程实例ID
* @return
*/
public ExecutionEntity getExecutionEntity(String processInstanceId) {
ExecutionEntity execution = (ExecutionEntity) runtimeService
.createProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult(); return execution;
} /**
* 使用流程实例ID,查询正在执行的对象表,获取当前活动对应的流程实例对象
*
* @param processInstanceId
* 流程实例ID
* @return
*/
public ProcessInstance getProcessInstance(String processInstanceId) {
ProcessInstance processInstance = runtimeService
.createProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult(); return processInstance;
} /**
* 使用流程实例ID,查询历史流程实例
*
* @param processInstanceId
* @return
*/
public HistoricProcessInstance getHistoricProcessInstance(
String processInstanceId) {
HistoricProcessInstance historicProcessInstance = historyService
.createHistoricProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
return historicProcessInstance;
} /**
* 通过流程定义的key,级联删除流程
*
* @param processKey
* 流程定义key
*/
public void deleteDeploy(String processKey) {
List<ProcessDefinition> pdList = repositoryService// 获取Service
.createProcessDefinitionQuery()// 创建流程定义查询
.processDefinitionKey(processKey)// 通过key查询
.list();// 返回一个集合
for (ProcessDefinition pd : pdList) {
repositoryService// 获取Service
.deleteDeployment(pd.getDeploymentId(), true);
} } /**
* 根据流程实例id,得到当前任务
* @param processInstanceId 流程实例id
* @return
*/
public Task findTask(String processInstanceId) {
return taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
} /**
* 根据流程实例id,得到上一个已完成节点
* @param processInstanceId
* @return
*/
public HistoricActivityInstance findLastTask(String processInstanceId) {
//得到以完成的上一任务节点
List<HistoricActivityInstance> haList=historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstanceId).finished().orderByHistoricActivityInstanceEndTime().desc().list();
//已完成的上一任务
HistoricActivityInstance ha = haList.get(0);
return ha;
} @Override
public Execution getExecution(String processInstanceId) {
return runtimeService.createExecutionQuery()
.processInstanceId(processInstanceId)
.singleResult();
} @Override
public HistoricTaskInstance findLastHistTask(String processInstanceId) {
List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
.processInstanceId(processInstanceId)
.orderByHistoricTaskInstanceEndTime()
.desc()
.list();
return list.get(0);
}
/**
* 通过流程定义key,得到所有节点数量和节点字符串
* @param processKey 流程定义key
* @return
* @throws Exception
*/
public Object[] getNodeId(String processKey){
// 得到流程部署实例(得到部署最新的那一个流程)
Deployment deployment =repositoryService.createDeploymentQuery().processDefinitionKey(processKey).orderByDeploymenTime().desc().list().get(0);
// 得到流程定义实例
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
//得到流程定义实体
ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
.getDeployedProcessDefinition(processDefinition.getId());
List<ActivityImpl> activitiList = processDefinitionEntity.getActivities();
// 初始化节点集合
List<String> nodeList = new ArrayList<String>();
for (ActivityImpl activity : activitiList) {
nodeList.add(activity.getId());
}
StringBuffer nodeId = new StringBuffer();
for (String node : nodeList) {
nodeId.append("'").append(node).append("',");
}
Object[] obj = new Object[]{nodeList.size(),nodeId.substring(0, nodeId.length() - 1)};
return obj;
} @Override
public List<TP_BUSINESS> getApplyList(List<TP_BUSINESS> list, String currentUserId, String currentUserName) {
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
// 获取流程实例Id
String processInstanceId = list.get(i).get("PI_ID");
// 设置办理人为当前登录用户
list.get(i).set("CURRENTUSER", currentUserName);
// 判断流程是否结束,如果结束则标志为完成
// 通过流程实例id查找正在执行的对象
Execution execution = runtimeService.createExecutionQuery().processInstanceId(processInstanceId)
.singleResult();
String status = "";
if (execution != null) {
status = "在办";
} else {
status = "完成";
}
list.get(i).set("AUDIT_STATUS", status);
}
}
return list;
} @Override
public String getActId(String processInstanceId) {
String actId = "";
// 通过流程实例id获取流程实例对象
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
if (processInstance != null) {
actId = processInstance.getActivityId();
}
return actId;
} @Override
public List<TP_BUSINESS> getWaitList(List<TP_BUSINESS> list, String currentUserId, String currentUserName) {
if(list != null && list.size() > 0) {
for(int i=0; i<list.size(); i++) {
//流程实例id
String pi_id = list.get(i).get("pi_id");
//设置当前办理人名称
list.get(i).set("CURRENTUSER", currentUserName);
//查询当前流程的历史任务
List<HistoricTaskInstance> hisList = historyService.createHistoricTaskInstanceQuery()
.processInstanceId(pi_id)
.orderByHistoricTaskInstanceEndTime()
.desc()
.list();
if(hisList != null && hisList.size() > 0) {
//设置上一个环节名称
list.get(i).set("LAST_LINK", hisList.get(0).getName());
//设置上个环节提交时间
list.get(i).set("LAST_LINK_DATE", hisList.get(0).getEndTime());
//得到上个办理人id
String lastAsseId = hisList.get(0).getAssignee();
List<Record> userList = Db.find("SELECT REAL_NAME FROM SYS_USER WHERE ID='"+lastAsseId+"'");
if(userList != null && userList.size() > 0) {
//设置上个办理人名称
list.get(i).set("LAST_LINK_APPLICANT", userList.get(0).get("real_name"));
} }
if(list.get(i).get("AUDIT_STATUS").equals("1")) {
list.get(i).set("AUDIT_STATUS", "在办");
}else {
list.get(i).set("AUDIT_STATUS", "完结");
}
}
}
return list;
} @Override
public InputStream viewImg(String processInstanceId) {
InputStream in = null;
// 获取流程实例对象
HistoricProcessInstance processinstance = historyService.createHistoricProcessInstanceQuery()
.processInstanceId(processInstanceId)
.singleResult();
if (processinstance != null) {
// 获取流程定义id
String processDefinitionId = processinstance.getProcessDefinitionId();
// 获取流程定义对象
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.processDefinitionId(processDefinitionId).singleResult();
// 部署id
String deployId = processDefinition.getDeploymentId();
// 文件名称
String imgName = processDefinition.getDiagramResourceName();
// 获取输入流
in = repositoryService.getResourceAsStream(deployId, imgName);
}
return in;
} @Override
public Execution getCurrentExecution(String processInstanceId) {
Execution execution = runtimeService.createExecutionQuery()
.processInstanceId(processInstanceId)
.singleResult();
return execution;
}
@Override
public boolean completeProcess(String processInstanceId,String status,String currentUserId) {
boolean result = false;
String elString="${message=='完成'}";
Map<String,Object> variables = new HashMap<String, Object>();
Task task = taskService.createTaskQuery()
.processInstanceId(processInstanceId)
.singleResult();
if(task != null) {
//获取任务id
String taskId = task.getId();
//拾取任务
if(task.getAssignee() == null) {
//采用当前要用户拾取任务
taskService.claim(taskId, currentUserId);
}
//得到下一个节点id
String nextActId = this.viewNextAct(processInstanceId, elString);
//如果下一个节点id是endevent1(结束节点),则根据status判断是同意还是拒绝用于最后是否保存数据
if(nextActId.toString().trim().equals("endevent1")) {
variables.put("message", "完成");
}else {
variables.put("message", status);
}
taskService.complete(taskId,variables);
result = true;
}
return result;
} @Override
public String viewNextAct(String processInstanceId, String elString) {
String nextActId = "";
//获取流程实例对象
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId)
.singleResult(); if(processInstance != null) {
//获取当前节点id
String activityId = processInstance.getActivityId();
//获取流程定义id
String processDefinitionId = processInstance.getProcessDefinitionId();
//获取流程定义实体对象
ProcessDefinitionEntity processDefinitionEntity= (ProcessDefinitionEntity)repositoryService.getProcessDefinition(processDefinitionId);
if(processDefinitionEntity != null) {
//获取当前节点对象
ActivityImpl activityImpl = processDefinitionEntity.findActivity(activityId);
if(activityImpl != null) {
//得到当前节点所有出线信息
List<PvmTransition> pvmList = activityImpl.getOutgoingTransitions();
if(pvmList != null && pvmList.size() > 1) {
for(PvmTransition pvm:pvmList) {
//得到连线的表达式
String conditionText = (String)pvm.getProperty("conditionText");
//看得到的连线名称和传递过来的名称是否匹配
if(elString.equals(conditionText)) {
ActivityImpl a = (ActivityImpl)pvm.getDestination();
//获取下一个节点的id
nextActId = a.getId();
}
}
}
}
}
}
return nextActId;
} @Override
public boolean revoke(String processInstanceId) {
// 根据流程实例id获取任务对象
Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
String taskId = task.getId();
// 结束流程时设置流程变量
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("message", "撤销");
taskService.complete(taskId, variables);
// 查看流程是否结束(execution为空则结束)
Execution execution = runtimeService.createExecutionQuery().processInstanceId(processInstanceId).singleResult();
if (execution == null) {
return true;
} else {
return false;
}
}
/**
* 查看流程图的当前活动节点
* @param taskId 任务id
* @return [0]x、[1]y、[2]width、[3]height、[4]流程部署id、[5]资源图片路径
* @throws Exception
*/
public Object[] showCurrentView(String processInstanceId) throws Exception{
//无论流程是否走完,得到的都是最后一条记录(ACT_HI_TASKINST)
HistoricTaskInstance h = historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId).list().get(0);
String processDefinitionId = h.getProcessDefinitionId();//得到流程定义id
//流程定义实体
ProcessDefinitionEntity pdm = (ProcessDefinitionEntity) repositoryService.getProcessDefinition(processDefinitionId);
ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult(); /*因为activiti框架的缓存会话机制,所以我在这个位置将“流程部署id和支援图片路径取出来”*/
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId.trim()).singleResult();
String deploymentId = pd.getDeploymentId(); //流程部署id
String diagramResourceName = pd.getDiagramResourceName(); //流程资源图片路径
/*因为activiti框架的缓存会话机制,所以我在这个位置将“流程部署id和支援图片路径取出来”*/ Object[] view = new Object[6];
view[4] = deploymentId;
view[5] = diagramResourceName;
//因为有可能流程已经走完,所有还是判断哈,免得空指针异常
if(pi != null){
ActivityImpl ai = pdm.findActivity(pi.getActivityId());
//根据活动ID 获取活动实例
ActivityImpl activityImpl = pdm.findActivity(pi.getActivityId());
view[0] = activityImpl.getX();
view[1] = activityImpl.getY();
view[2] = activityImpl.getWidth(); //宽度
view[3] = activityImpl.getHeight(); //高度
} return view;
}
/**
* 查看流程图
* @param deploymentId 流程部署id
* @param diagramResourceName 资源图片名称
* @param response 将数据向页面输出
* @throws IOException
*/
public void showView(String deploymentId,String diagramResourceName,HttpServletResponse response) throws Exception{
//调用接口得到流程图
InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, diagramResourceName);
OutputStream out = response.getOutputStream();
for(int b = -1;(b=inputStream.read())!=-1;){
out.write(b);
}
out.close();
inputStream.close();
}
}
web.xml
<!-- 加载Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 设置Spring容器加载所有的配置文件的路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:activiti-context.xml</param-value>
</context-param>