/**
 * @author tyl
 * @Action  ini
 * 2011-4-5
 */
package cn.com.origin.autocode.common.operations;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import cn.com.origin.autocodemanager.common.tree.AbstractTreeData;

import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.kernel.TCUserService;
import com.teamcenter.schemas.soa._2006_03.exceptions.ServiceException;

public class ClassificationOperation {

	private TCUserService userService;
	private TCSession session = null;
	public String parentStr = "";

	public ClassificationOperation() {
		session = (TCSession) AIFUtility.getCurrentApplication().getSession();
		try {
			userService = (TCUserService) session.getUserService();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public TCComponent createNewClassificationNode(HashMap<?, ?> hashMap) throws ServiceException, TCException {
		String[] propNames = new String[hashMap.size()];
		String[] propValues = new String[hashMap.size()];
		Set<?> set = hashMap.keySet();
		Iterator<?> it = set.iterator();
		for (int i = 0; i < hashMap.size(); i++) {
			propNames[i] = (String) it.next();
			propValues[i] = (String) hashMap.get(propNames[i]);
		}
		Object[] params = { propNames, propValues };
		try {
			TCComponent newTCcomponent = (TCComponent) userService.call("createNewClassificationNode", params);
			return newTCcomponent;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public TCComponent updateClassificationNode(TCComponent component, HashMap<?, ?> hashMap) throws ServiceException, TCException {
		String[] propNames = new String[hashMap.size()];
		String[] propValues = new String[hashMap.size()];
		Set<?> set = hashMap.keySet();
		Iterator<?> it = set.iterator();
		for (int i = 0; i < hashMap.size(); i++) {
			propNames[i] = (String) it.next();
			propValues[i] = (String) hashMap.get(propNames[i]);
			if (propValues[i] == null) {
				propValues[i] = "";
			}
		}
		Object[] params = { component, propNames, propValues };
		try {
			TCComponent updateTCcomponent = (TCComponent) userService.call("updateClassificationNode", params);
			return updateTCcomponent;
		} catch (TCException e) {
			e.printStackTrace();
			return null;
		}
	}

	public TCComponent[] searchClassificationSegmentByUser(String itemType, TCComponent currentUser, TCComponent currentRole, TCComponent currentGroup) throws ServiceException, TCException {

		Object[] params = { itemType, currentUser, currentRole, currentGroup };
		try {
			TCComponent[] searchTCcomponents = (TCComponent[]) userService.call("searchClassificationSegmentByUser", params);
			return searchTCcomponents;
		} catch (TCException e) {
			e.printStackTrace();
			return null;
		}
	}

	public TCComponent[] searchClassificationUsers(HashMap<?, ?> hashMap) throws ServiceException, TCException {
		String[] propNames = new String[hashMap.size()];
		String[] propValues = new String[hashMap.size()];
		Set<?> set = hashMap.keySet();
		Iterator<?> it = set.iterator();
		for (int i = 0; i < hashMap.size(); i++) {
			propNames[i] = (String) it.next();
			propValues[i] = (String) hashMap.get(propNames[i]);
		}
		Object[] params = { propNames, propValues };
		try {
			TCComponent[] searchTCcomponents = (TCComponent[]) userService.call("searchClassificationUsers", params);
			return searchTCcomponents;
		} catch (TCException e) {
			e.printStackTrace();
			return null;
		}
	}

	public TCComponent[] searchClassificationSegments(HashMap<?, ?> hashMap) throws ServiceException, TCException {
		String[] propNames = new String[hashMap.size()];
		String[] propValues = new String[hashMap.size()];
		Set<?> set = hashMap.keySet();
		Iterator<?> it = set.iterator();
		for (int i = 0; i < hashMap.size(); i++) {
			propNames[i] = (String) it.next();
			propValues[i] = (String) hashMap.get(propNames[i]);
		}
		Object[] params = { propNames, propValues };
		try {
			TCComponent[] searchTCcomponents = (TCComponent[]) userService.call("searchClassificationSegments", params);
			return searchTCcomponents;
		} catch (TCException e) {
			e.printStackTrace();
			return null;
		}
	}

	public TCComponent[] searchClassificationNodes(HashMap<?, ?> hashMap) throws ServiceException, TCException {
		String[] propNames = new String[hashMap.size()];
		String[] propValues = new String[hashMap.size()];
		Set<?> set = hashMap.keySet();
		Iterator<?> it = set.iterator();
		for (int i = 0; i < hashMap.size(); i++) {
			propNames[i] = (String) it.next();
			propValues[i] = (String) hashMap.get(propNames[i]);
		}
		Object[] params = { propNames, propValues };
		try {
			TCComponent[] searchTCcomponents = (TCComponent[]) userService.call("searchClassificationNodes", params);
			return searchTCcomponents;
		} catch (TCException e) {
			e.printStackTrace();
			return null;
		}
	}

	public void deleteTreeData(AbstractTreeData treeData) {
		try {
			treeData.getPackCodeNodeInfo().getNodeComponent().delete();
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

	public void deleteTreeDatas(AbstractTreeData treeData) {
		if (treeData != null) {
			AbstractTreeData[] childrenDatas = treeData.getChildrenDatas();
			if ((childrenDatas != null) && (childrenDatas.length > 0)) {
				for (int i = 0; i < childrenDatas.length; i++) {
					deleteTreeDatas(childrenDatas[i]);
					deleteTreeData(childrenDatas[i]);
				}
			}
		}
	}

}
