package cn.com.origin.autocode.jfom.util;

import java.util.List;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCAccessControlService;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCComponentUser;
import com.teamcenter.rac.kernel.TCComponentUserType;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;

import net.sf.json.JSONArray;

public class SoaServerUtil {

	public static TCPreferenceService preferenceService;
	public static String priverUserID;
	public static String webServiceURL;

	static {
		try {

			if (preferenceService == null) {
				preferenceService = ((TCSession) AIFUtility.getCurrentApplication().getSession())
						.getPreferenceService();
			}

			if (priverUserID == null) {
				priverUserID = preferenceService.getString(TCPreferenceService.TC_preference_site,
						"Autocode_PriverUser_ID");
			}

			if (webServiceURL == null) {
				webServiceURL = preferenceService.getString(TCPreferenceService.TC_preference_site,
						"Autocode_SendClassServer_URL");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void sendToClass(String wsoPUID, String classID, String json) {
		try {
			ServiceClient sc = new ServiceClient();
			Options opts = new Options();
			String url = webServiceURL;// "http://10.201.5.203:19090/ErpWebService.asmx?WSDL";
			EndpointReference end = new EndpointReference(url);
			opts.setTo(end);
			opts.setAction("SendToClass");
			sc.setOptions(opts);
			OMFactory fac = OMAbstractFactory.getOMFactory();
			OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", "");
			OMElement method = fac.createOMElement("SendToClass", omNs);
			OMElement value = fac.createOMElement("wsoPuiid", omNs);
			value.setText(wsoPUID);
			OMElement value1 = fac.createOMElement("classID", omNs);
			value1.setText(classID);
			OMElement value2 = fac.createOMElement("JsonContainer", omNs);
			// "[{\"value\":\"1\",\"propertyID\":-500003},{\"value\":\"1\",\"propertyID\":-500011}]"
			value2.setText(json);

			method.addChild(value);
			method.addChild(value1);
			method.addChild(value2);

			OMElement res = sc.sendReceive(method);
			res.getFirstElement().getText();
			System.out.println(res.getFirstElement().getText());
		} catch (AxisFault e) {
			e.printStackTrace();
		}

	}

	public static String objectToJson(List<ClassProperty> props) {
		String str = null;
		try {
			JSONArray json = JSONArray.fromObject(props);// 将java对象转换为json对象
			str = json.toString();// 将json对象转换为字符串
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("JSON =>" + str);
		return str;
	}

	public static void grantReadPrivilege(TCSession tcSession, TCComponent tccomponent) {
		try {
			TCAccessControlService accessControlService = tcSession.getTCAccessControlService();
			TCComponentUserType type = (TCComponentUserType) tcSession.getTypeComponent("User");
			TCComponentUser user = type.find(priverUserID);// 2135

			if (user == null) {
				return;
			}
			if (tcSession.getUser().equals(user)) {
				return;
			}

			String[] as2 = new String[] { "READ" };

			if (tccomponent instanceof TCComponentItemRevision) {
				TCComponentItem tempItem = ((TCComponentItemRevision) tccomponent).getItem();
				openByPass(tcSession);
				accessControlService.grantPrivilege(tempItem, user, as2);
				closeByPass(tcSession);

				openByPass(tcSession);
				accessControlService.grantPrivilege(tccomponent, user, as2);
				closeByPass(tcSession);
			} else {
				openByPass(tcSession);
				accessControlService.grantPrivilege(tccomponent, user, as2);
				closeByPass(tcSession);
			}
		} catch (TCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void grantWritePrivilege(TCSession tcSession, TCComponent tccomponent) {
		try {
			TCAccessControlService accessControlService = tcSession.getTCAccessControlService();
			TCComponentUserType type = (TCComponentUserType) tcSession.getTypeComponent("User");

			TCComponentUser user = type.find(priverUserID);// 2135
			if (user == null) {
				return;
			}
			if (tcSession.getUser().equals(user)) {
				return;
			}

			String[] as2 = new String[] { "READ", "WRITE", "DELETE" };

			if (tccomponent instanceof TCComponentItemRevision) {
				TCComponentItem tempItem = ((TCComponentItemRevision) tccomponent).getItem();
				openByPass(tcSession);
				accessControlService.grantPrivilege(tempItem, user, as2);
				closeByPass(tcSession);

				openByPass(tcSession);
				accessControlService.grantPrivilege(tccomponent, user, as2);
				closeByPass(tcSession);
			} else {
				openByPass(tcSession);
				accessControlService.grantPrivilege(tccomponent, user, as2);
				closeByPass(tcSession);
			}
		} catch (TCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void openByPass(TCSession session) throws TCException {
//		TCUserService userservice = session.getUserService();
//		Object[] obj = new Object[1];
//		obj[0] = "origin";
//		userservice.call("ORIGIN_set_bypass", obj);
		session.getUserService().call("setbypass", new Object[] { "true" });

	}

	public static void closeByPass(TCSession session) throws TCException {
//		TCUserService userservice = session.getUserService();
//		Object[] obj = new Object[1];
//		obj[0] = "origin";
//		userservice.call("ORIGIN_close_bypass", obj);
		session.getUserService().call("setbypass", new Object[] { "false" });
	}

}
