对于不管是Activtit还是jbpm来说,业务与流程的整合均类似。启动流程是绑定业务。流程与业务的整合放到动态代理中
-
-
-
-
-
-
@RequestMapping(value = "start", method = RequestMethod.POST)
-
public String startWorkflow(Leave leave,RedirectAttributes redirectAttributes, HttpSession session) {
-
try {
-
-
User user = UserUtil.getUserFromSession(session);
-
-
if (user == null || StringUtils.isBlank(user.getId())) {
-
return "redirect:/login?timeout=true";
-
}
-
leave.setUserId(user.getId());
-
Map<String, Object> variables = new HashMap<String, Object>();
-
variables.put("leave", leave);
-
-
leave.setTestId(new Date().toString());
-
leaveBean.saveBeforeEntity(leave);
-
-
Leave Leavetest=null;
-
Leavetest=leaveBean.queryByTestid(leave.getTestId());
-
leave=Leavetest;
-
logger.debug("save entity: {}", leave);
-
-
-
-
-
String businessKey = leave.getId().toString();
-
ProcessInstance processInstance = null;
-
-
-
-
-
startNode.common(businessKey, variables,runtimeService,identityService);
-
LogHandler1 logHandler = startNode.new LogHandler1();
-
-
-
-
LeaveBean leaveBeanProxy=(LeaveBean)logHandler.newProxyInstanceStart(leaveBean);
-
leaveBeanProxy.updeatChangeApply(leave);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
redirectAttributes.addFlashAttribute("message", "流程已启动" );
-
} catch (ActivitiException e) {
-
if (e.getMessage().indexOf("no processes deployed with key") != -1) {
-
logger.warn("没有部署流程!", e);
-
redirectAttributes.addFlashAttribute("error", "没有部署流程,请在[工作流]->[流程管理]页面点击<又一次部署流程>");
-
} else {
-
logger.error("启动请假流程失败:", e);
-
redirectAttributes.addFlashAttribute("error", "系统内部错误!
"
); -
}
-
} catch (Exception e) {
-
logger.error("启动请假流程失败:", e);
-
redirectAttributes.addFlashAttribute("error", "系统内部错误!
"
); -
}
-
return "redirect:/oa/leave/apply";
-
}
动态代理:
-
package com.tgb.itoo.activiti.controller;
-
-
import java.lang.reflect.InvocationHandler;
-
import java.lang.reflect.Method;
-
import java.lang.reflect.Proxy;
-
import java.util.Map;
-
-
import org.activiti.engine.IdentityService;
-
import org.activiti.engine.RuntimeService;
-
import org.activiti.engine.runtime.ProcessInstance;
-
import org.slf4j.Logger;
-
import org.slf4j.LoggerFactory;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Component;
-
import org.springframework.transaction.annotation.Transactional;
-
import com.tgb.itoo.basic.entity.Leave;
-
-
@Component
-
@Transactional
-
public class StartNode{
-
-
private Logger logger = LoggerFactory.getLogger(getClass());
-
-
private Map<String, Object> variables;
-
private String businessKey;
-
-
-
protected IdentityService identityService;
-
@Autowired
-
public void setIdentifyService(IdentityService identityService) {
-
this.identityService = identityService;
-
}
-
protected RuntimeService runtimeService;
-
@Autowired
-
public void setRuntimeService(RuntimeService runtimeService) {
-
this.runtimeService = runtimeService;
-
}
-
-
@Autowired
-
RuntimeService runtimeService1;
-
-
public void common(String businessKey,Map<String, Object> variables,RuntimeService runtimeService,IdentityService identityService){
-
this.variables=variables;
-
this.businessKey=businessKey;
-
this.runtimeService=runtimeService;
-
this.identityService=identityService;
-
}
-
-
-
-
-
-
-
-
-
-
public class LogHandler1 implements InvocationHandler{
-
-
private Object targetObject;
-
-
public Object newProxyInstanceStart(Object targetObject){
-
this.targetObject=targetObject;
-
-
-
-
-
-
return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),
-
targetObject.getClass().getInterfaces(),this);
-
}
-
-
@Override
-
-
-
public Object invoke(Object proxy, Method method, Object[] args)
-
throws Throwable {
-
System.out.println("start-->>");
-
for(int i=0;i<args.length;i++){
-
System.out.println(args[i]);
-
}
-
Object ret=null;
-
try{
-
-
System.out.println("satrt-->>");
-
-
-
-
Leave leave=(Leave)args[0];
-
-
try {
-
identityService.setAuthenticatedUserId(leave.getUserId());
-
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("ChangeCourse", businessKey, variables);
-
String processInstanceId = processInstance.getId();
-
-
leave.setProcessInstanceId(processInstanceId);
-
logger.debug("start process of {key={}, bkey={}, pid={}, variables={}}", new Object[]{"ChangeCourse", processInstanceId, variables});
-
} finally {
-
identityService.setAuthenticatedUserId(null);
-
}
-
args[0]=leave;
-
ret=method.invoke(targetObject, args);
-
-
-
-
-
-
-
System.out.println("success-->>");
-
}catch(Exception e){
-
e.printStackTrace();
-
System.out.println("error-->>");
-
throw e;
-
}
-
return ret;
-
}
-
}
-
-
}
-
-
-
-
-
-
@RequestMapping(value = "list/task")
-
public ModelAndView taskList(HttpSession session, HttpServletRequest request) {
-
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
-
String userId = UserUtil.getUserFromSession(session).getId();
-
results=abstractTaskList(userId);
-
return new ModelAndView("/oa/leave/taskList","results",results);
-
-
}
-
-
-
-
-
-
-
public List<Map<String, Object>> abstractTaskList(String userId){
-
List<Leave> results = new ArrayList<Leave>();
-
-
TaskQuery taskQuery = taskService.createTaskQuery().taskCandidateOrAssigned(userId);
-
List<Task> tasks = taskQuery.list();
-
int i=0;
-
-
List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
-
-
for (Task task : tasks) {
-
String processInstanceId = task.getProcessInstanceId();
-
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).active().singleResult();
-
String businessKey = processInstance.getBusinessKey();
-
if (businessKey == null) {
-
continue;
-
}
-
-
Map<String, Object> map = new HashMap<String, Object>();
-
-
Leave leave = leaveBean.findEntityById(businessKey);
-
-
-
-
-
-
map.put("leave", leave);
-
map.put("task", task);
-
map.put("processDefinition", getProcessDefinition(processInstance.getProcessDefinitionId()));
-
map.put("processInstance", processInstance);
-
mapList.add(map);
-
-
-
i=i+1;
-
}
-
return mapList;
-
}
-
-
-
-
-
-
@RequestMapping(value = "list/running")
-
public ModelAndView runningList(HttpSession session,HttpServletRequest request) {
-
String userId = UserUtil.getUserFromSession(session).getId();
-
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
-
results=abstractRuningList(userId);
-
-
return new ModelAndView ("/oa/leave/running","results",results);
-
}
-
-
-
-
-
-
-
public List<Map<String, Object>> abstractRuningList(String userId){
-
List<Leave> results = new ArrayList<Leave>();
-
ProcessInstanceQuery query = runtimeService.createProcessInstanceQuery().processDefinitionKey("ChangeCourse").involvedUser(userId).active().orderByProcessInstanceId().desc();
-
List<ProcessInstance> list = query.list();
-
List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
-
-
for (ProcessInstance processInstance : list) {
-
String businessKey = processInstance.getBusinessKey();
-
if (businessKey == null) {
-
continue;
-
}
-
-
List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstance.getId()).active().orderByTaskCreateTime().desc().listPage(0, 1);
-
Map<String, Object> map = new HashMap<String, Object>();
-
-
Leave leave = leaveBean.findEntityById(businessKey);
-
-
-
-
-
-
map.put("leave", leave);
-
map.put("task", tasks.get(0));
-
map.put("processDefinition", getProcessDefinition(processInstance.getProcessDefinitionId()));
-
map.put("processInstance", processInstance);
-
mapList.add(map);
-
-
-
-
-
-
-
-
}
-
return mapList;
-
}
-
-
-
-
-
-
@RequestMapping(value = "list/finished")
-
public ModelAndView finishedList(HttpSession session,HttpServletRequest request) {
-
String userId = UserUtil.getUserFromSession(session).getId();
-
List<Leave> results = new ArrayList<Leave>();
-
HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery().processDefinitionKey("ChangeCourse").involvedUser(userId).finished().orderByProcessInstanceEndTime().desc();
-
List<HistoricProcessInstance> list = query.list();
-
List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
-
-
for (HistoricProcessInstance historicProcessInstance : list) {
-
Map<String, Object> map = new HashMap<String, Object>();
-
String businessKey = historicProcessInstance.getBusinessKey();
-
Leave leave = leaveBean.findEntityById(businessKey);
-
-
-
-
map.put("leave", leave);
-
map.put("processDefinition", getProcessDefinition(historicProcessInstance.getProcessDefinitionId()));
-
map.put("historicProcessInstance", historicProcessInstance);
-
mapList.add(map);
-
-
}
-
-
return new ModelAndView("/oa/leave/finished","results",mapList);
-
}
-
-
-
-
-
-
-
@RequestMapping(value = "/complete/{id}", method = {RequestMethod.POST, RequestMethod.GET})
-
@ResponseBody
-
public String complete(@PathVariable("id") String taskId, Variable var) {
-
try {
-
-
Map<String, Object> variables = var.getVariableMap();
-
-
-
-
-
taskService.complete(taskId, variables);
-
-
-
-
-
-
return "success";
-
} catch (Exception e) {
-
logger.error("error on complete task {}", new Object[]{taskId, e});
-
return "error";
-
}
-
}
总结:
对于在流程与业务的整合中应用动态代理也不知道是否符合AOP的理念,类似其它方面的流程操作还未抽取出来(交互太多),在这里记录一下学习Activiti的一个过程,在之后的尝试中能够换个方式。换个思路,直接将整个工作流应用抽取出来(请假,改动课程等)。