package com.blueland.bpm.engine.core.model;

import java.io.Serializable;
import java.util.Date;

import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.blueland.bpm.common.base.BaseEntity;
import com.blueland.bpm.engine.config.model.ProcessInfoConfig;
import com.blueland.bpm.engine.util.WorkFlowException;
/**
 * 流程定义模型
 * 
 * @version: 1.0
 * @author lw
 */
public class ProcessDefinitionModel extends BaseEntity implements Serializable {
	private static final long serialVersionUID = 1L;
	/**
	 * 流程定义ID-随机生成
	 */
	String procDefId;
	/**
	 * 流程定义KEY(建模模型填写的KEY)
	 */
	String procDefKey;
	/**
	 * 流程名称
	 */
	String procDefName;
	/**
	 * 流程分组ID--待定
	 */
	String category;
	/**
	 * 流程描述--待定
	 */
	String description;
	/**
	 * 流程版本
	 */
	int version;
	/**
	 * 流程资源名称--待定
	 *//*
	String resourceName;
	*//**
	 * 流程图形资源名称--待定
	 *//*
	String diagramResourceName;
	*//**
	 * 是否被挂起
	 *//*
	boolean isSuspended;*/
	
	/**
	 * 流程定义创建时间
	 */
	Date pdCreateTime;
	/**
	 * 流程部署ID
	 */
	String deploymentId;
	/**
	 *  部署名称
	 *//*
	String deploymentName;
	*//**
	 * 部署分类--待定
	 *//*
	String deploymentCategory;*/
	/**
	 * 租户ID
	 */
	String tenantId;
	/**
	 * 流程配置对象
	 */
	private ProcessInfoConfig processInfoConfig;
	
	public ProcessDefinitionModel build(ProcessDefinitionModel processDefinitionModel) {
		if (processDefinitionModel == null)
			return this;
		return processDefinitionModel;
	}
	
	/**
	 * 构建ProcessDefinitionModel对象属性
	 * @param processDefinition
	 * @return
	 */
	public static ProcessDefinitionModel build(ProcessDefinition processDefinition){
		ProcessDefinitionModel processDefinitionModel=new ProcessDefinitionModel();
		if(processDefinition==null){
			throw new WorkFlowException(ExceptionErrorCode.B2002,"processDefinition is null");
		}if(StringUtils.isEmpty(processDefinition.getId())){
			throw new WorkFlowException(ExceptionErrorCode.B2002,"processDefinition id is null");
		}
		processDefinitionModel.procDefId=processDefinition.getId();
		processDefinitionModel.procDefKey=processDefinition.getKey();
		processDefinitionModel.procDefName=processDefinition.getName();
		processDefinitionModel.version=processDefinition.getVersion();
		processDefinitionModel.tenantId=processDefinition.getTenantId();
		processDefinitionModel.description=processDefinition.getDescription();
		processDefinitionModel.deploymentId=processDefinition.getDeploymentId();
		//this.category=processDefinition.getCategory();
	/*	this.isSuspended=processDefinition.isSuspended();
		this.diagramResourceName=processDefinition.getDiagramResourceName();
		this.resourceName=processDefinition.getResourceName();
		this.deploymentId=processDefinition.getDeploymentId();*/
		return processDefinitionModel;
	}
	
	
	
	/**
	 * 构建ProcessDefinitionModel对象属性
	 * @param processDefinition
	 * @return
	 */
	public static ProcessDefinitionModel build(HistoricProcessInstance processInstance){
		ProcessDefinitionModel processDefinitionModel=new ProcessDefinitionModel();
		if(processInstance==null){
			throw new WorkFlowException(ExceptionErrorCode.B2002,"processInstance is null");
		}if(StringUtils.isEmpty(processInstance.getProcessDefinitionId())){
			throw new WorkFlowException(ExceptionErrorCode.B2002,"processDefinition id is null");
		}
		processDefinitionModel.procDefId=processInstance.getProcessDefinitionId();
		processDefinitionModel.procDefKey=processInstance.getProcessDefinitionName();
		
		/*this.processDefinitionKey=processDefinition.getKey();
		this.processDefinitionName=processDefinition.getName();
		this.hasStartFormKey=processDefinition.hasStartFormKey();
		this.isSuspended=processDefinition.isSuspended();
		this.version=processDefinition.getVersion();
		this.tenantId=processDefinition.getTenantId();
		this.diagramResourceName=processDefinition.getDiagramResourceName();
		this.resourceName=processDefinition.getResourceName();
		this.description=processDefinition.getDescription();
		this.category=processDefinition.getCategory();
		this.deploymentId=processDefinition.getDeploymentId();*/
		return processDefinitionModel;
	}
	
	/**
	 * 构建ProcessDefinitionModel对象属性
	 * @param processDefinition
	 * @return
	 */
	public static ProcessDefinitionModel build(ProcessDefinition processDefinition,Deployment deployment ){
		ProcessDefinitionModel processDefinitionModel=new ProcessDefinitionModel();
		if(processDefinition==null||deployment==null)
			return processDefinitionModel;
		processDefinitionModel.procDefId=processDefinition.getId();
		processDefinitionModel.procDefKey=processDefinition.getKey();
		processDefinitionModel.procDefName=processDefinition.getName();
		processDefinitionModel.version=processDefinition.getVersion();
		processDefinitionModel.tenantId=processDefinition.getTenantId();
		processDefinitionModel.description=processDefinition.getDescription();
		processDefinitionModel.deploymentId=processDefinition.getDeploymentId();
		processDefinitionModel.pdCreateTime=deployment.getDeploymentTime();
		
		//this.category=processDefinition.getCategory();
	/*	this.isSuspended=processDefinition.isSuspended();
		this.diagramResourceName=processDefinition.getDiagramResourceName();
		this.resourceName=processDefinition.getResourceName();
		this.deploymentId=processDefinition.getDeploymentId();
		//deployment property
		this.deploymentName=deployment.getName();
		this.deploymentCategory=deployment.getCategory();*/
		return processDefinitionModel;
	}

	
	public String getProcDefId() {
		return procDefId;
	}

	public void setProcDefId(String procDefId) {
		this.procDefId = procDefId;
	}

	public String getProcDefKey() {
		return procDefKey;
	}

	public void setProcDefKey(String procDefKey) {
		this.procDefKey = procDefKey;
	}

	public String getProcDefName() {
		return procDefName;
	}

	public void setProcDefName(String procDefName) {
		this.procDefName = procDefName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public Date getPdCreateTime() {
		return pdCreateTime;
	}

	public void setPdCreateTime(Date pdCreateTime) {
		this.pdCreateTime = pdCreateTime;
	}

	public String getTenantId() {
		return tenantId;
	}

	public void setTenantId(String tenantId) {
		this.tenantId = tenantId;
	}

	public ProcessInfoConfig getProcessInfoConfig() {
		return processInfoConfig;
	}

	public void setProcessInfoConfig(ProcessInfoConfig processInfoConfig) {
		this.processInfoConfig = processInfoConfig;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getDeploymentId() {
		return deploymentId;
	}

	public void setDeploymentId(String deploymentId) {
		this.deploymentId = deploymentId;
	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this,ToStringStyle.MULTI_LINE_STYLE);
	}
}
