package cust.com.rac.integration;

import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.teamcenter.rac.kernel.TCComponentForm;
import com.teamcenter.rac.kernel.TCComponentUser;
import com.teamcenter.rac.kernel.TCException;

public class XMLDocAPI {

	public static String createProjectPlanXML(TCComponentForm masterForm, String project_code){
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		StringBuffer errMsg = new StringBuffer("Error:");
		try {
			TCComponentUser appUer = (TCComponentUser)masterForm.getReferenceProperty("owning_user");
			
			Document document = DocumentHelper.createDocument();
			Element FormData = document.addElement("FormData"); 
			Element pm_project_schedule = FormData.addElement("pm_project_schedule");
			pm_project_schedule.addElement("code").addText("");
			pm_project_schedule.addElement("app_user_name").addText(URLEncoder.encode(appUer.getOSUserName(), "UTF-8"));
			pm_project_schedule.addElement("app_user_code").addText(appUer.getUserId());
			pm_project_schedule.addElement("app_date").addText(sdf.format(new Date()));
			pm_project_schedule.addElement("project_code").addText(project_code);
			pm_project_schedule.addElement("attachment").addText("");
			
	        XMLWriter xmlWriter = null;
	        StringWriter stringWrite = new StringWriter();
	        
	        OutputFormat format = OutputFormat.createPrettyPrint();  //转换字符串格式
	        format.setEncoding("UTF-8");
	        format.setNewlines(true);
	        
	      //输出字符串
	        xmlWriter = new XMLWriter(stringWrite, format);
	        xmlWriter.write(document);
	        xmlWriter.flush();
	        xmlWriter.close();
	        
	        return stringWrite.toString();
	        
		} catch (TCException e) {
			// TODO Auto-generated catch block
			errMsg.append(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			errMsg.append(e.getMessage());
			e.printStackTrace();
		}
		
		return errMsg.toString();
	}

	public static String createDeliveryApplicationXML(TCComponentForm masterForm){
		
		StringBuffer errMsg = new StringBuffer("Error:");
		try {
			Document document = DocumentHelper.createDocument();
			Element FormData = document.addElement("FormData"); 
			Element EF_YSR_ProjuctSendApp_M = FormData.addElement("EF_YSR_ProjuctSendApp_M");
			EF_YSR_ProjuctSendApp_M.addElement("ProjectCode").addText(URLEncoder.encode(masterForm.getProperty("ysr8_ProjectCode"), "UTF-8"));
			EF_YSR_ProjuctSendApp_M.addElement("code").addText("");
			EF_YSR_ProjuctSendApp_M.addElement("ApplicantName").addText(URLEncoder.encode(masterForm.getProperty("ysr8_Applicant"), "UTF-8"));
			EF_YSR_ProjuctSendApp_M.addElement("ApplicantCode").addText("");
			EF_YSR_ProjuctSendApp_M.addElement("ApplicantDate").addText(URLEncoder.encode(masterForm.getProperty("ysr8_ApplicantDate"), "UTF-8"));
			EF_YSR_ProjuctSendApp_M.addElement("ProjectName").addText(URLEncoder.encode(masterForm.getProperty("ysr8_ProjectName"), "UTF-8"));
			EF_YSR_ProjuctSendApp_M.addElement("CustomerName").addText(URLEncoder.encode(masterForm.getProperty("ysr8_CustomerName"), "UTF-8"));
			EF_YSR_ProjuctSendApp_M.addElement("TimeRequiremente").addText(URLEncoder.encode(masterForm.getProperty("ysr8_TimeRequiremente"), "UTF-8"));
			EF_YSR_ProjuctSendApp_M.addElement("DeliveryDate").addText(URLEncoder.encode(masterForm.getProperty("ysr8_DeliveryDate"), "UTF-8"));
			EF_YSR_ProjuctSendApp_M.addElement("Departure").addText(URLEncoder.encode(masterForm.getProperty("ysr8_Departure"), "UTF-8"));
			EF_YSR_ProjuctSendApp_M.addElement("Receiver").addText(URLEncoder.encode(masterForm.getProperty("ysr8_Receiver"), "UTF-8"));
			EF_YSR_ProjuctSendApp_M.addElement("ReceiverPhone").addText(URLEncoder.encode(masterForm.getProperty("ysr8_ReceiverPhone"), "UTF-8"));
			EF_YSR_ProjuctSendApp_M.addElement("ReceiptAddress").addText(URLEncoder.encode(masterForm.getProperty("ysr8_ReceiptAddress"), "UTF-8"));
			EF_YSR_ProjuctSendApp_M.addElement("SendInstall").addText(URLEncoder.encode(masterForm.getProperty("ysr8_SendInstall"), "UTF-8"));
			EF_YSR_ProjuctSendApp_M.addElement("DeliveryContent").addText(URLEncoder.encode(masterForm.getProperty("ysr8_DeliveryContent"), "UTF-8"));
			EF_YSR_ProjuctSendApp_M.addElement("ysr8_Fininshed").addText(URLEncoder.encode(masterForm.getProperty("ysr8_Fininshed"), "UTF-8"));
			
//			FileWriter fileWriter = null;
	        XMLWriter xmlWriter = null;
	        StringWriter stringWrite = new StringWriter();
	        
	        OutputFormat format = OutputFormat.createPrettyPrint();  //转换字符串格式
	        format.setEncoding("UTF-8");
	        format.setNewlines(true);
	        
	      //输出字符串
	        xmlWriter = new XMLWriter(stringWrite, format);
	        xmlWriter.write(document);
	        xmlWriter.flush();
	        xmlWriter.close();
	        
	        return stringWrite.toString();
	        
		} catch (TCException e) {
			// TODO Auto-generated catch block
			errMsg.append(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			errMsg.append(e.getMessage());
			e.printStackTrace();
		}
		
		return errMsg.toString();
	}
	
	public static String createAcceptComInfoXML(TCComponentForm masterForm, String item_id){
		
		StringBuffer errMsg = new StringBuffer("Error:");
		try {
			Document document = DocumentHelper.createDocument();
			Element FormData = document.addElement("FormData"); 
			Element EF_YSR_AcceptClient_M = FormData.addElement("EF_YSR_AcceptClient_M");
			EF_YSR_AcceptClient_M.addElement("code").addText("");
			EF_YSR_AcceptClient_M.addElement("PLMItemID").addText(URLEncoder.encode(item_id, "UTF-8"));
			EF_YSR_AcceptClient_M.addElement("ProjectCode").addText(URLEncoder.encode(masterForm.getProperty("ysr8_ProjectCode"), "UTF-8"));
			EF_YSR_AcceptClient_M.addElement("SendDate").addText(masterForm.getProperty("ysr8_SendDate"));
			EF_YSR_AcceptClient_M.addElement("HopeReceiveDate").addText(masterForm.getProperty("ysr8_HopeReceiveDate"));
			EF_YSR_AcceptClient_M.addElement("ProjectName").addText(URLEncoder.encode(masterForm.getProperty("ysr8_ProjectName"), "UTF-8"));
			EF_YSR_AcceptClient_M.addElement("CustomerName").addText(URLEncoder.encode(masterForm.getProperty("ysr8_CustomerName"), "UTF-8"));
			EF_YSR_AcceptClient_M.addElement("ProjectManager").addText(URLEncoder.encode(masterForm.getProperty("ysr8_ProjectManager"), "UTF-8"));
			EF_YSR_AcceptClient_M.addElement("Planner").addText(URLEncoder.encode(masterForm.getProperty("ysr8_Planner"), "UTF-8"));
			EF_YSR_AcceptClient_M.addElement("Sales").addText(URLEncoder.encode(masterForm.getProperty("ysr8_Sales"), "UTF-8"));
			EF_YSR_AcceptClient_M.addElement("ProductionState").addText(URLEncoder.encode(masterForm.getProperty("ysr8_ProductionState"), "UTF-8"));
			EF_YSR_AcceptClient_M.addElement("ProductionDate").addText(masterForm.getProperty("ysr8_ProductionDate"));
			EF_YSR_AcceptClient_M.addElement("ProjectState").addText(URLEncoder.encode(masterForm.getProperty("ysr8_ProjectState"), "UTF-8"));
			EF_YSR_AcceptClient_M.addElement("PlanFinishDate").addText(masterForm.getProperty("ysr8_PlanFinishDate"));
			EF_YSR_AcceptClient_M.addElement("DocumentState").addText(URLEncoder.encode(masterForm.getProperty("ysr8_DocumentState"), "UTF-8"));
			EF_YSR_AcceptClient_M.addElement("PlanSendtDate1").addText(masterForm.getProperty("ysr8_PlanSendtDate1"));
			EF_YSR_AcceptClient_M.addElement("SpareParts").addText(URLEncoder.encode(masterForm.getProperty("ysr8_SpareParts"), "UTF-8"));
			EF_YSR_AcceptClient_M.addElement("PlanSendtDate2").addText(masterForm.getProperty("ysr8_PlanSendtDate2"));
			EF_YSR_AcceptClient_M.addElement("CustomerState").addText(URLEncoder.encode(masterForm.getProperty("ysr8_CustomerState"), "UTF-8"));
			EF_YSR_AcceptClient_M.addElement("Reason1").addText(URLEncoder.encode(masterForm.getProperty("ysr8_Reason1"), "UTF-8"));
			EF_YSR_AcceptClient_M.addElement("ConfirmContent").addText(URLEncoder.encode(masterForm.getProperty("ysr8_ConfirmContent"), "UTF-8"));
			EF_YSR_AcceptClient_M.addElement("Others").addText(URLEncoder.encode(masterForm.getProperty("ysr8_Others"), "UTF-8"));
			EF_YSR_AcceptClient_M.addElement("OtherContent").addText(URLEncoder.encode(masterForm.getProperty("ysr8_OtherContent"), "UTF-8"));
			
//			FileWriter fileWriter = null;
	        XMLWriter xmlWriter = null;
	        StringWriter stringWrite = new StringWriter();
	        
	        OutputFormat format = OutputFormat.createPrettyPrint();  //转换字符串格式
	        format.setEncoding("UTF-8");
	        format.setNewlines(true);
	        
	      //输出字符串
	        xmlWriter = new XMLWriter(stringWrite, format);
	        xmlWriter.write(document);
	        xmlWriter.flush();
	        xmlWriter.close();
	        
	        return stringWrite.toString();
	        
		} catch (TCException e) {
			// TODO Auto-generated catch block
			errMsg.append(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			errMsg.append(e.getMessage());
			e.printStackTrace();
		}
		
		return errMsg.toString();
	}

	public static String readXmlErrorMsg(String xmlDoc) {
		
		String errMsg = "";
		try {
			Document doc = DocumentHelper.parseText(xmlDoc);
			Element rootElt = doc.getRootElement(); // 获取根节点
//			Iterator iter = rootElt.elementIterator("errmsg");
			errMsg = rootElt.elementTextTrim("errmsg");
			System.out.println("errMsg="+errMsg);
//			return URLEncoder.encode(errMsg, "UTF-8");
//			return URLEncoder.encode(errMsg, "GBK");
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}  
		return errMsg;
	}
	
	public static String readXmlTaskID(String xmlDoc) {
		
		String taskID = "";
		try {
			Document doc = DocumentHelper.parseText(xmlDoc);
			Element rootElt = doc.getRootElement(); // 获取根节点
			taskID = rootElt.elementTextTrim("taskid");
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return taskID;
	}
}
