package com.weaver.seconddev.interfaces.eipoa;

import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.seconddev.interfaces.eipoa.SI_HTPortalPlan_OUTServiceStub.*;
import com.weaver.seconddev.interfaces.prop.ConfigProp;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.general.Util;
import org.apache.axis2.kernel.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties;
import org.apache.axis2.transport.http.impl.httpclient4.HttpTransportPropertiesImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class PortalPlanService extends BaseBean {

	private static final Logger logger_c9c5c87a = LoggerFactory.getLogger(PortalPlanService.class);
	@Autowired
	private ConfigProp configProp;
	public String[] insertPlan(InsertPlan insertData) {
		String[] insertReturn = new String[2];
		try {
			InsertPlanResponse insertRes = getStub().cREATE(insertData);
			insertReturn[0] = "S";
			insertReturn[1] = insertRes.getInsertPlanReturn();
		} catch (Exception e) {
			logger_c9c5c87a.error(String.valueOf("往门户推送待办出错：" + e.toString()));
			insertReturn[0] = "E";
			insertReturn[1] = e.toString();
		}
		return insertReturn;
	}

	public String[] updatePlan(UpdatePlan updateData) {
		String[] updateReturn = new String[2];
		try {
			UpdatePlanResponse updateRes = getStub().uPDATE(updateData);
			updateReturn[0] = "S";
			updateReturn[1] = updateRes.getUpdatePlanReturn();
		} catch (Exception e) {
			logger_c9c5c87a.error(String.valueOf("往门户更新待办状态出错：" + e.toString()));
			updateReturn[0] = "E";
			updateReturn[1] = e.toString();
		}
		return updateReturn;
	}

	public String[] editPlan(EditPlan editData) {
		String[] editreturn = new String[2];
		try {
			EditPlanResponse editRes = getStub().eDIT(editData);
			editreturn[0] = "S";
			editreturn[1] = editRes.getEditPlanReturn();
		} catch (Exception e) {
			logger_c9c5c87a.error(String.valueOf("往门户更新待办内容出错：" + e.toString()));
			editreturn[0] = "E";
			editreturn[1] = e.toString();
		}
		return editreturn;
	}

	public SI_HTPortalPlan_OUTServiceStub getStub() throws Exception {
//		String url = Util.null2String(I18nContextUtilcus.getBean(Environment.class).getProperty("htportaloaUrl.plan_url")).trim();
		String url = configProp.getPlan_url();
		SI_HTPortalPlan_OUTServiceStub stub = new SI_HTPortalPlan_OUTServiceStub(url);
		stub._getServiceClient().getOptions().setProperty("__CHUNKED__", Boolean.FALSE);
		stub._getServiceClient().getOptions().setTimeOutInMilliSeconds(300000);
		stub._getServiceClient().getOptions().setProperty(HTTPConstants.SO_TIMEOUT,300000);
		stub._getServiceClient().getOptions().setProperty(HTTPConstants.CONNECTION_TIMEOUT,300000);
//		HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
//		auth.setUsername(Util.null2String(I18nContextUtilcus.getBean(Environment.class).getProperty("htportaloaUrl.po_user")).trim());
//		auth.setPassword(Util.null2String(I18nContextUtilcus.getBean(Environment.class).getProperty("htportaloaUrl.po_password")).trim());
		HttpTransportPropertiesImpl.Authenticator auth = new HttpTransportPropertiesImpl.Authenticator();
		auth.setUsername(configProp.getPo_user());

		auth.setPassword(configProp.getPo_password());
		stub._getServiceClient().getOptions().setProperty(org.apache.axis2.kernel.http.HTTPConstants.AUTHENTICATE,auth);
		return stub;
	}
}
