【整合篇】Activiti業務與流程整合之查詢(二)

來源:互聯網
上載者:User

標籤:

    

繼上篇部落格:【整合篇】JBPM4.4業務與流程整合之查詢 應用第二種方式:申請實體中加入需要的相應的工作流程的屬性

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;   /* @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")    private Date realityStartTime;    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")    private Date realityEndTime;*/    private Date applyTime;    private String leaveType;    private String reason;    //-- 臨時屬性 --//    // 流程任務    private Task task;    private Map<String, Object> variables;    // 運行中的流程執行個體    private ProcessInstance processInstance;    // 曆史的流程執行個體    private HistoricProcessInstance historicProcessInstance;    // 流程定義    private ProcessDefinition processDefinition;    @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;    }       /** * 學生基礎資訊 */@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = StudentCourseInfo.class)@JoinColumn(name = "studentCourseInfoId", nullable = true)private StudentCourseInfo studentCourseInfo;    public StudentCourseInfo getStudentCourseInfo() {return studentCourseInfo;}public void setStudentCourseInfo(StudentCourseInfo studentCourseInfo) {this.studentCourseInfo = studentCourseInfo;}@Transient    public Task getTask() {        return task;    }    public void setTask(Task task) {        this.task = task;    }    @Transient    public Map<String, Object> getVariables() {        return variables;    }    public void setVariables(Map<String, Object> variables) {        this.variables = variables;    }    @Transient    public ProcessInstance getProcessInstance() {        return processInstance;    }    public void setProcessInstance(ProcessInstance processInstance) {        this.processInstance = processInstance;    }    @Transient    public HistoricProcessInstance getHistoricProcessInstance() {        return historicProcessInstance;    }    public void setHistoricProcessInstance(HistoricProcessInstance historicProcessInstance) {        this.historicProcessInstance = historicProcessInstance;    }    @Transient    public ProcessDefinition getProcessDefinition() {        return processDefinition;    }    public void setProcessDefinition(ProcessDefinition processDefinition) {        this.processDefinition = processDefinition;    }}


在真正應用之前我們先來簡單介紹一下在Activiti中比JBPM4.4的一處最佳化。

在activiti中,它為我們提供了一個businessKey欄位。

 

在啟動流程時,流程執行個體中會添加一條記錄,而且流程執行個體表中會有一個businessKey欄位,String businessKey = leave.getId().toString();這樣我們的流程表中就始終擁有業務的id,當我們再次需要的時候可以重新查詢。




下面舉例說明:

 /**     * 工作清單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<Leave> results = new ArrayList<Leave>();    String userId = UserUtil.getUserFromSession(session).getId();    results=abstractTaskList(userId);        return new ModelAndView("/oa/leave/taskList","results",results);                                         }

/**     * 抽象出來的查看工作清單,與基本業務無關     *     * @param userId 使用者id     * @return     */public List<Leave> abstractTaskList(String userId){ List<Leave> results = new ArrayList<Leave>();     // 根據當前人的ID查詢     TaskQuery taskQuery = taskService.createTaskQuery().taskCandidateOrAssigned(userId);             List<Task> tasks = taskQuery.list();          int i=0;          // 根據流程的業務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;         }          Leave leave=updateEntity(processInstance,task,businessKey);         results.add(leave);          i=i+1;     }return results;}

//更新實體    public Leave updateEntity(ProcessInstance processInstance,Task task,String businessKey){   Leave leave = leaveBean.findEntityById(businessKey);   leave.setProcessInstance(processInstance);leave.setProcessDefinition(getProcessDefinition(processInstance.getProcessDefinitionId()));                leave.setTask(task);return leave;   }


對比:

    之前的JBPM的操作方式,啟動流程時將業務資料存放區到流程變數中,在需要查詢時,將資料從流程變數中取出,然後將取出的值賦給我們的組合實體即可。這樣做的弊端在於資料可能會出現不同步的情況,但是應該並不多見。

 

    其實使用這種方式呢,即使在實體層添加了工作流程的屬性,但是並不需要在真正的資料庫表中添加相應的欄位,那麼這樣做的便利在哪裡呢?其實這樣做主要是使得我們在返回前台資料時更加方便,我們只需要返回一個實體即可。但是弊端是什麼呢,就是業務的實體中需要知道一些工作流程的屬性。

    而且弊端不僅如此,在大型系統中,相當於實體層需要加入所有的依賴工作流程的jar包,如果這個實體還需要被其他系統所依賴,那麼所有的系統都會有工作流程的依賴,我們曾遇到的問題就是,當我們在當前系統的實體項目中加入工作流程的依賴時,由於其他系統也依賴我們本實體,導致所有的系統均癱瘓jboss不能正常啟動,報不識別工作流程的類。這種影響就是深遠的,所以我們要實現的還是解耦。


下篇繼續




【整合篇】Activiti業務與流程整合之查詢(二)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.