【整合篇】Activiti业务与流程整合之查询(三)


继前两篇博客:应用第三种方式

 

实体中不需要引入任何与工作流相关的任何属性,单纯的做好自己即可!例如如下申请实体(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;
	}
	@Column
	public 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业务与流程整合之查询(三))