package com.csc.library.process;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Vector;

import org.jdom.Element;

import com.csc.library.database.ConnectionPools;
import com.csc.library.database.StaticProperties;
import com.csc.library.service.GenId;
import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialEnvironment;
import com.csc.library.session.InitialRecord;
import com.csc.library.utilities.CscCalendar;
import com.csc.library.utilities.MyLog;
import com.csc.library.utilities.UProfile;
import com.csc.library.utilities.WriteXml;

/**
 * <PRE>
 * �� Base Cls �����㹡�� Process
 * �� Method ��ѡ� ��� ����� ���¡�� ��� 
 *      - public void initData(HashMap data) throws Exception
 *      - public Vector getProcessList() throws Exception
 *      - public void process() throws Exception
 * </PRE>
 */
public class MyProcess  implements DbProcess {
	protected UProfile			upf=null;
	private String				msg				= "";
	private String				full_msg				= "";
	private InitialEnvironment	env;
	protected HashMap			data;
	protected Vector			processList		= new Vector();
	protected String			xmlName			= "", processId = "", subIndex = "", pName = "",parentId;
	protected WriteXml			wx;
	private String				processStatus	= "C";// default is Complete
	HashMap 			param = new HashMap();
	private String path = new InitialEnvironment("GLOBAL").getValue("temp-dir") + "/process/condition/";
	private String fileType="";
	private StringBuffer remark=new StringBuffer();
	private String expName ="";
	private String subFolder="";
	
	protected String referKey="";
	
	protected String processCode="";	
	
	public MyProcess() {
	}
	
	public void setProcessCode(String code){
		this.processCode=code;
	}
	
	public String getReferKey() {
		return referKey;
	}

	public void setReferKey(String referKey) {
		this.referKey = referKey;
	}

	/**
	 * ��֧���ʶҹ�㹡�� process
	 * @return String ʶҹ�㹡�� process C ��� Complete F ��� Fail
	 */
	public String getProcessStatus() {
		return processStatus;
	}
	public void setProcessId(String processId){
		this.processId=processId;
	}
	/**
	 * ���絤��ʶҹ�㹡�� process
	 * @param processStatus ��� ʶҹ�㹡�� process �ռ��㹡�� ��ʶҹ�� Lprocess_sub
	 *  ��� C �� write ����� complete state ��ǹ F �� write ����� error state 
	 */
	public void setProcessStatus(String processStatus) {
		this.processStatus = processStatus;
	}
	/**
	 * ��㹡�ô֧��Ң�������ͤӹǹ �������� Process ��ͧ�ӡ��ᵡ Thread �͡�繡�� Thread
	 * ������������ ������� ����������� Vector �� this.processList.add("process");
	 * @return Vector ���й��ᵡ Thred ��͹��� ProcessService �� invoke Method Process ��� 
	 */
	public Vector getProcessList() {
		return this.processList;
	}
	public void setProcessList(Vector processList) throws Exception {
		this.processList = processList;
	}
	/**
	 * ���ʹ��������¡��� Process �ӧҹ ������зӹ�����
	 */
	public void process() throws Exception {
	}
	public void setParam(HashMap data) {
		try{
			if(data.containsKey("UProfile"))
				this.upf = (UProfile) data.get("UProfile");
			else 
				this.upf = (UProfile) data.get("uprofile");
		}catch(Exception ex){}
		this.data = data;
	}
	/**
	 * ��㹨ѧ��з�� Process ��ͧ��ùӤ�� Condition �ҡ˹�Ҩ� ������
	 * Initial ��Һҧ���ҧ
	 * @param data ��� Condition �ҡ˹�Ҩ�
	 */
	public void initData(HashMap data) throws Exception {
	}
	public String getMessage() {
		return this.msg;
	}
	public String getFullMessage() {
		return this.full_msg;
	}
	public void setXmlLog(String processId, String pName, String subIndex) {
		this.processId = processId;
		this.pName = pName;
		this.subIndex = subIndex;
		this.xmlName = this.processId + "_" + subIndex;
		//	this.createXmlLog();
	}
	/**
	 * Sets the msg.
	 * @param msg The msg to set
	 */
	public void setMessage(String msg) {
		this.msg = msg;
	}
	public void setFullMessage(String msg) {
		this.full_msg = msg;
	}
	public void setXmlLog(String processId, String subIndex) {
		this.processId = processId;
		this.subIndex = subIndex;
		this.xmlName = this.processId + "_" + subIndex;
		this.createXmlLog();
	}
	private void createXmlLog() {
		Element root = new Element("PROCESS_LOG");
		root.addAttribute("id", this.processId);
		root.addAttribute("name", this.pName);
		root.addAttribute("subindex", this.subIndex);
		root.addAttribute("start_time", new CscCalendar().getDDMMYYYYHHMMSS());
		root.addAttribute("end_time", "");
		root.addAttribute("status", "R");
		String path = new InitialEnvironment("GLOBAL").getValue("temp-dir") + "/logxml/" + this.xmlName;
		wx = new WriteXml(root, path);
		wx.output();
	}
	/**
	 * Returns the upf.
	 * @return UProfile
	 */
	public UProfile getUProfile() {
		return upf;
	}
	/**
	 * Sets the upf.
	 * @param upf The upf to set
	 */
	public void setUProfile(UProfile upf) {
		this.upf = upf;
	}
	
	public void alive(String result) {
		StaticProperties.serverManager.aliveProcess(this.processId, result);
	}
	
	public boolean isCancle() {
		boolean result = false;
		if (StaticProperties.serverManager.isCancle(this.processId)) {
			result = true;
		}
		return result;
	}
	
	public String saveCondition() {
		String process_id = new GenId(this.upf).getId(this.path)+ ".ser";
		String FileName = this.path + process_id ;
		try {
			FileOutputStream fu = new FileOutputStream(FileName);
			ObjectOutputStream ou = new ObjectOutputStream(fu);
			ou.writeObject(this.param);
			ou.close();
			return process_id;
		} catch (IOException ex) {
			MyLog.error(this, this.upf, ex);
		}
		return "";
	}

	public int createQuque(UProfile up, HashMap condition) throws RemoteException {
		DbRecord db = new InitialRecord(upf).getDbRecord("com.csc.entity.CSC_Quque");
		this.param =  condition;
		this.param.put("UProfile", up);
		CscCalendar d  = new CscCalendar();
		db.clearField();
		db.set("ename","Send Email");
		db.set("tname","��������ùԤ����");
		db.set("class_name","MailProcess");
		db.set("process_status","W");
		db.set("process_type","N");
		db.set("process_group","P");
        db.set("owner", (String) upf.get("appname"));
        db.set("remote_ip", (String) upf.get("remote_ip"));
        db.set("employeeid", (String) upf.getEmployee());
        db.set("companyid", (String) upf.getComCode());
        db.set("user_name", (String) upf.getUserName());
//        db.set("screen_name", screenName);
        db.set("server_name", "NONE");
        db.set("next_time", d.getYYYYMMDDHHMMSS());
        db.set("start_time", "waiting");
        db.set("end_time", "waiting");
//		db.set("remark",remark);
 //       db.set("process_priority", priority);
		db.set("condition",this.saveCondition());
		db.set("parentid",db.getString("condition").substring(0,db.getString("condition").indexOf(".ser")));
		db.set("ptype", "PR0");
		db.save();
		StaticProperties.serverManager.inviteServerCheckQuque();
		return 1; 

		
	}
	
	public String getFileType() {
		return fileType;
	}

	public void setFileType(String fileType) {
		this.fileType = fileType;
	}

	public void postProcessTrigger() {
	}

	public String getRemark() {
		return remark.toString();
	}

	public void appendRemark(String remark) {
		this.remark.append(remark);
	}

	public void setRemark(StringBuffer remark) {
		this.remark=remark;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getProcessId() {
		return processId;
	}

	public String getExportFileName() {
		return expName;
	}

	public void setExportFileName(String expName) {
		this.expName = expName;
	}

	public String getSubFolder() {
		return subFolder;
	}

	public void setSubFolder(String folder) {
		this.subFolder = folder;
	}	

	public void closeConnection(){
	try{
		if(this.processId!=null &&this.processId.length()>0){
			ConnectionPools cp = (ConnectionPools) StaticProperties.connectionMap.get(this.getUProfile().getDbName().toLowerCase());
			cp.removeConnectionProcess(this.processId);
			this.processId="";
		}
	}catch(Exception ex){
		MyLog.error(this, ex);
	}
		
	}
	
	
}