標籤:
繼前兩篇部落格:應用第三種方式
實體中不需要引入任何與工作流程相關的任何屬性,單純的做好自己即可!例如如下申請實體(Leave):
package com.tgb.itoo.basic.entity;import java.util.Date;import java.util.HashSet;import java.util.Map;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.OneToMany;import javax.persistence.Table;import javax.persistence.Temporal;import javax.persistence.TemporalType;import javax.persistence.Transient;import org.activiti.engine.history.HistoricProcessInstance;import org.activiti.engine.repository.ProcessDefinition;import org.activiti.engine.runtime.ProcessInstance;import org.activiti.engine.task.Task;import org.codehaus.jackson.annotate.JsonIgnore;import org.springframework.format.annotation.DateTimeFormat;/** * Entity: Leave * * @author hejingyuan IdEntity implements Serializable */@Entity@Table(name = "OA_LEAVE")public class Leave extends IdEntity { /** * */private static final long serialVersionUID = 1L;private String processInstanceId; private String userId; private String testId; private String oldCourse; private String applyCourse; @Column public String getApplyCourse() {return applyCourse;}public void setApplyCourse(String applyCourse) {this.applyCourse = applyCourse;}@Column public String getOldCourse() {return oldCourse;}public void setOldCourse(String oldCourse) {this.oldCourse = oldCourse;}@Columnpublic String getNewCourse() {return newCourse;}public void setNewCourse(String newCourse) {this.newCourse = newCourse;}private String newCourse; @Column public String getTestId() {return testId;}public void setTestId(String testId) {this.testId = testId;} @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") private Date startTime; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") private Date endTime; private Date applyTime; private String leaveType; private String reason; //-- 臨時屬性 --// private Map<String, Object> variables; @Column public String getProcessInstanceId() { return processInstanceId; } public void setProcessInstanceId(String processInstanceId) { this.processInstanceId = processInstanceId; } @Column public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "START_TIME") public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "END_TIME") public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } @Column @Temporal(TemporalType.TIMESTAMP) public Date getApplyTime() { return applyTime; } public void setApplyTime(Date applyTime) { this.applyTime = applyTime; } @Column public String getLeaveType() { return leaveType; } public void setLeaveType(String leaveType) { this.leaveType = leaveType; } @Column public String getReason() { return reason; } public void setReason(String reason) { this.reason = reason; } @Transient public Map<String, Object> getVariables() { return variables; } public void setVariables(Map<String, Object> variables) { this.variables = variables; }}
那麼查詢時要如何與工作流程進行整合?
我們來看待辦任務結點:
/** * 工作清單ERROR [stderr] (http-localhost/127.0.0.1:8080-3) ScriptEngineManager providers.next(): javax.script.ScriptEngineFactory: Provider com.sun.script.javascript.RhinoScriptEngineFactory not found * * @param leave */ @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); } /** * 抽象出來的查看工作清單,與基本業務無關 * * @param userId 使用者id * @return */public List<Map<String, Object>> abstractTaskList(String userId){ List<Leave> results = new ArrayList<Leave>(); // 根據當前人的ID查詢 TaskQuery taskQuery = taskService.createTaskQuery().taskCandidateOrAssigned(userId); List<Task> tasks = taskQuery.list(); int i=0; List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>(); // 根據流程的業務ID查詢實體並關聯 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); /*Leave leave=updateEntity(processInstance,task,businessKey); results.add(leave); */ i=i+1; }return mapList;}
此時我們採用的方式是利用businessKey,並不使用流程變數中存放的業務資料,而是直接去業務表中進行查詢,避免了不同步的情況,而且返回值採用map的形式,並不需要業務實體去添加額外的屬性,離我們所說的AOP更近了一步。
以上的這些方式也都是在實踐中的積累,做個總結記錄一下!
【整合篇】Activiti業務與流程整合之查詢(三)