package cust.com.rac.integration;

import java.io.UnsupportedEncodingException;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;

import com.teamcenter.rac.aif.AbstractAIFUIApplication;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

public class ProjectPlanHandler extends AbstractHandler{

	private TCSession tcsession  = null;
	
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		// TODO Auto-generated method stub
		final AbstractAIFUIApplication app = AIFUtility.getCurrentApplication();
		InterfaceAIFComponent aifComponent = app.getTargetComponent();
		tcsession = (TCSession) app.getSession();
		
		if(aifComponent == null){
			MessageBox.post("请选择版本对象执行此功能！", "消息提示", MessageBox.INFORMATION);
			return null;
		}
		if( aifComponent instanceof TCComponentItemRevision){
			TCComponentItemRevision itemRevision = (TCComponentItemRevision)aifComponent;
			String itemRevType = itemRevision.getType();
			
			String url = tcsession.getPreferenceService().getString(TCPreferenceService.TC_preference_site, "Cust_PLM_To_BPM_URL");
				
			String msg = InterationOperation.sendProjectPlanToBPM(itemRevision, url);
			MessageBox.post(msg, "消息提示", MessageBox.INFORMATION);
			
		}else{
			MessageBox.post("请选择版本对象执行此功能！", "消息提示", MessageBox.INFORMATION);
			return null;
		}
		return null;
	}
	
	public Object execute_test(ExecutionEvent arg0) throws ExecutionException {
		// TODO Auto-generated method stub
		String url = "http://192.168.0.69:3378/YZSoft/BPM/Services/Post.ashx?method=Submit&from_system_id=PLM&process=%e9%a1%b9%e7%9b%ae%e8%bf%9b%e5%ba%a6";
		String name = "陈秀丽";

		try {
			String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"+
					"<FormData>"+
					  "<pm_project_schedule>"+
					    "<code></code>"+
					    "<app_user_name>"+java.net.URLEncoder.encode(name, "UTF-8")+"</app_user_name>"+
					    "<app_user_code>chenxiuli</app_user_code>"+
					    "<app_date>2017-09-22 00:00:00</app_date>"+
					    "<project_code>YSR-1111111</project_code>"+
					    "<attachment></attachment>"+
					 "</pm_project_schedule>"+
					"</FormData>";
			String paramsXML = java.net.URLEncoder.encode(xml, "UTF-8");
//			String rs = RequestServer.request(url, xml);
			String rs = RequestServer.sendFile(url, "C:\\Ⅱ类更改建议单.pdf");
			System.out.println("request="+rs);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
