/**
 * @author tyl
 * @Action  ini
 * 2011-3-15
 */
package cn.com.origin.autocodemanager.common.operations;

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

import cn.com.origin.autocodemanager.common.AutoCodeManagerPropertyName;
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.rac.util.MessageBox;
import com.teamcenter.schemas.soa._2006_03.exceptions.ServiceException;


public class TreeAndListCodeOperation {
	private TCUserService userService;
	private TCSession session = null;

	public static void main(String[] args) {
		
	}
	public TreeAndListCodeOperation() {
		session = (TCSession) AIFUtility.getCurrentApplication().getSession();
		try {
			userService = (TCUserService) session.getUserService();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int isDateExpire() {
		int isDateExpire = 0;
		Object[] params = {};
	//	try {
	//		isDateExpire = (Integer) userService.call("isExpire", params);
//			System.out.println("isDateExpire=-=========" + isDateExpire);
//		} catch (TCException e) {
//			e.printStackTrace();
//		}
		return isDateExpire;
	}

	public void addNodeReferenceComponents(TCComponent component, TCComponent referenceComponent) {
		if ((component == null) || (referenceComponent == null)) {
			return;
		}
		try {
			TCComponent[] components = getNodeReferenceComponents(component);
			if (components != null) {
				for (int i = 0; i < components.length; i++) {
					if (components[i].equals(referenceComponent)) {
						return;
					}
				}
			}
			component.add(AutoCodeManagerPropertyName.TreeAndListCodeNode_reference_nodes, referenceComponent);
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

	public void removeNodeReferenceComponents(TCComponent component, TCComponent referenceComponent) {
		if ((component == null) || (referenceComponent == null)) {
			return;
		}
		TCComponent[] components = getNodeReferenceComponents(component);
		try {
			if (components != null) {
				for (int i = 0; i < components.length; i++) {
					if (components[i].equals(referenceComponent)) {
						component.remove(AutoCodeManagerPropertyName.TreeAndListCodeNode_reference_nodes, referenceComponent);
					}
				}
			}
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

	public TCComponent[] getNodeReferenceComponents(TCComponent component) {
		if ((component == null)) {
			return null;
		}
		TCComponent[] components = null;
		try {
			components = component.getTCProperty(AutoCodeManagerPropertyName.TreeAndListCodeNode_reference_nodes).getReferenceValueArray();
		} catch (TCException e) {
			e.printStackTrace();
		}
		return components;
	}

	public TCComponent createNewTreeORListNode(TCComponent ruleNode, AbstractTreeData parentData, 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]);
			System.out.println("i am in createNewTreeORListNode propNames[i]="+propNames[i]+" propValues[i]="+propValues[i]);
		}
		Object[] params = { propNames, propValues };
		try {
			TCComponent newTCcomponent = (TCComponent) userService.call("createNewTreeOrListNode", params);
			if (newTCcomponent == null) {
				return null;
			}
			if (!newTCcomponent.getProperty(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_type).equals(AutoCodeManagerPropertyName.CodeGroupName)) {
				if (ruleNode == null) {
					newTCcomponent.setReferenceProperty(AutoCodeManagerPropertyName.TreeAndListCodeNode_rule_node, newTCcomponent);
				} else {
					newTCcomponent.setReferenceProperty(AutoCodeManagerPropertyName.TreeAndListCodeNode_rule_node, ruleNode);
					newTCcomponent.getTCProperty(AutoCodeManagerPropertyName.TreeAndListCodeNode_reference_nodes).setReferenceValueArray(new TCComponent[] { ruleNode });
				}
			}
			return newTCcomponent;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public TCComponent updateTreeORListNode(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]);
		}
		Object[] params = { component, propNames, propValues };
		try {
			TCComponent updateTCcomponent = (TCComponent) userService.call("updateTreeORListNode", params);
			return updateTCcomponent;
		} catch (TCException e) {
			e.printStackTrace();
			return null;
		}
	}

	public boolean deleteTreeORListNode(AbstractTreeData segmentTreeData, AbstractTreeData deleteTreeData) throws ServiceException, TCException {
		return deleteTreeRelations(segmentTreeData, deleteTreeData);
	}

	public boolean deleteTreeRelations(AbstractTreeData segmentTreeData, AbstractTreeData deleteTreeData) {
		TCComponent segmentComponent = null;
		TCComponent deleteComponent = null;
		boolean isContinueDelete = true;
		if ((deleteTreeData != null) && (deleteTreeData.getPackCodeNodeInfo().getNodeComponent() != null)) {
			deleteComponent = deleteTreeData.getPackCodeNodeInfo().getNodeComponent();
		} else {
			isContinueDelete = false;
			return isContinueDelete;
		}
		if ((segmentTreeData != null) && (segmentTreeData.getPackCodeNodeInfo().getNodeComponent() != null)) {
			segmentComponent = segmentTreeData.getPackCodeNodeInfo().getNodeComponent();
		}
		TCComponent[] referenceNode = getNodeReferenceComponents(deleteComponent);
		if (referenceNode != null) {
			if (referenceNode.length > 1) {
				MessageBox.post(deleteTreeData.getPackCodeNodeInfo().getNode_name() + "已经被使用不能删除", "INFO", MessageBox.WARNING);
				isContinueDelete = false;
				return isContinueDelete;
			}
		}
		String parentStr = "";
		isContinueDelete = deleteChildrenRelations(getParentStr(deleteTreeData, parentStr), deleteTreeData.getPackCodeNodeInfo().getNodeComponent().getUid(), null, isContinueDelete);
		if (isContinueDelete) {
			if (segmentComponent != null) {
				removeNodeReferenceComponents(deleteComponent, segmentComponent);
			}
			try {
				TCComponent ruleComponent = deleteComponent.getReferenceProperty(AutoCodeManagerPropertyName.TreeAndListCodeNode_rule_node);
				if ((deleteTreeData.getPackCodeNodeInfo().is_root) || (deleteTreeData.getPackCodeNodeInfo().getNode_type().equals(AutoCodeManagerPropertyName.CodeGroupName))) {
					updateClassificationAfterDeleteSegment(deleteComponent);
					updateTreeRelationAfterDelete(deleteComponent);
					deleteTreeData(deleteComponent);
				} else {
					if ((segmentComponent != null) && (ruleComponent != null) && (ruleComponent.equals(segmentComponent))) {
						updateTreeRelationAfterDelete(deleteComponent);
						deleteTreeData(deleteComponent);
					}
				}
			} catch (TCException e) {
				e.printStackTrace();
			}
		}
		if (isContinueDelete) {
			MessageBox.post("删除成功！", "INFO", MessageBox.WARNING);
		}
		return isContinueDelete;
	}

	public void removeTreeORListNode(AbstractTreeData segmentTreeData, AbstractTreeData deleteTreeData) throws ServiceException, TCException {
		removeTreeRelations(segmentTreeData, deleteTreeData);
		updateTreeRelation(deleteTreeData);
		removeNodeReferenceComponents(deleteTreeData.getPackCodeNodeInfo().getNodeComponent(), segmentTreeData.getPackCodeNodeInfo().getNodeComponent());
	}

	public void updateTreeRelation(AbstractTreeData deleteTreeData) {
		if ((deleteTreeData != null) && (deleteTreeData.getPackCodeNodeInfo().getNodeComponent() != null)) {
			String currentStr = "";
			String parentStr = "";
			parentStr = getParentStr(deleteTreeData.getParentTreeData(), parentStr);
			String childrenStr = deleteTreeData.getPackCodeNodeInfo().getNodeComponent().getUid();
			if ((deleteTreeData.getParentTreeData() != null) && (deleteTreeData.getParentTreeData().getPackCodeNodeInfo().getNodeComponent() != null)) {
				currentStr = deleteTreeData.getParentTreeData().getPackCodeNodeInfo().getNodeComponent().getUid();
			} else {
				currentStr = "";
			}
			Object[] params = { parentStr, currentStr, childrenStr };
			try {
				userService.call("removeTreeRelation", params);
			} catch (TCException e) {
				e.printStackTrace();
				return;
			}
		}
	}

	public void removeTreeRelations(AbstractTreeData segmentTreeData, AbstractTreeData deleteTreeData) {
		String parentStr = "";
		removeChildrenRelations(getParentStr(deleteTreeData, parentStr), deleteTreeData.getPackCodeNodeInfo().getNodeComponent().getUid(), segmentTreeData.getPackCodeNodeInfo().getNodeComponent());
	}

	private void removeChildrenRelations(String parentStr, String currentStr, TCComponent segmentComponent) {
		try {
			TCComponent[] tccomponents = null;
			HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
			hashMap.put(AutoCodeManagerPropertyName.CodeRelation_parent_node, parentStr.trim());
			hashMap.put(AutoCodeManagerPropertyName.CodeRelation_current_node, currentStr.trim());
			tccomponents = searchTreeORListNodes(hashMap);
			if (tccomponents == null) {
				return;
			}
			for (int i = 0; i < tccomponents.length; i++) {
				String parentStr2 = parentStr + "," + currentStr;
				if (parentStr2.trim().startsWith(",")) {
					parentStr2 = parentStr2.substring(1);
				}
				if (isRuleRootNode(tccomponents[i])) {
					segmentComponent = tccomponents[i];
					updateClassificationAfterDeleteSegment(segmentComponent);
				}
				removeChildrenRelations(parentStr2, tccomponents[i].getUid().trim(), segmentComponent);
				deleteCodeRelations(hashMap);
				removeNodeReferenceComponents(tccomponents[i], segmentComponent);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

	public void updateClassificationAfterDeleteSegment(TCComponent deleteComponent) {
		if (deleteComponent != null) {
			Object[] params = { deleteComponent.getUid() };
			try {
				userService.call("updateClassificationAfterDeleteSegment", params);
			} catch (TCException e) {
				e.printStackTrace();
				return;
			}
		}
	}

	public boolean isRuleRootNode(TCComponent component) {
		if (component == null) {
			return false;
		}
		try {
			if ((component.getLogicalProperty(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_root))) {
				return true;
			}
		} catch (TCException e) {
			e.printStackTrace();
		}
		return false;
	}

	public void deleteTreeData(TCComponent deleteComponent) {
		if (deleteComponent == null) {
			return;
		}
		try {
			deleteComponent.getTCProperty(AutoCodeManagerPropertyName.TreeAndListCodeNode_reference_nodes).setReferenceValueArray(null);
			deleteComponent.getTCProperty(AutoCodeManagerPropertyName.TreeAndListCodeNode_rule_node).setReferenceValue(null);
			deleteComponent.delete();
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

	private boolean deleteChildrenRelations(String parentStr, String currentStr, TCComponent segmentComponent, boolean isContinueDelete) {
		try {
			if (!isContinueDelete) {
				return false;
			}
			TCComponent[] tccomponents = null;
			HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
			hashMap.put(AutoCodeManagerPropertyName.CodeRelation_parent_node, parentStr.trim());
			hashMap.put(AutoCodeManagerPropertyName.CodeRelation_current_node, currentStr.trim());
			tccomponents = searchTreeORListNodes(hashMap);
			if (tccomponents == null) {
				return false;
			}
			for (int i = 0; i < tccomponents.length; i++) {
				TCComponent[] referenceNode = getNodeReferenceComponents(tccomponents[i]);
				if (referenceNode != null) {
					if (referenceNode.length > 1) {
						tccomponents[i].refresh();
						MessageBox.post(tccomponents[i].getProperty(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_name) + "已经被不能删除", "INFO", MessageBox.WARNING);
						return false;
					}
				}
			}
			for (int i = 0; i < tccomponents.length; i++) {
				String parentStr2 = parentStr + "," + currentStr;
				if (parentStr2.trim().startsWith(",")) {
					parentStr2 = parentStr2.substring(1);
				}
				if (isRuleRootNode(tccomponents[i])) {
					segmentComponent = tccomponents[i];
				}
				isContinueDelete = deleteChildrenRelations(parentStr2, tccomponents[i].getUid().trim(), segmentComponent, isContinueDelete);
				if (isContinueDelete) {
					removeNodeReferenceComponents(tccomponents[i], segmentComponent);
					try {
						TCComponent ruleComponent = tccomponents[i].getReferenceProperty(AutoCodeManagerPropertyName.TreeAndListCodeNode_rule_node);
						tccomponents[i].refresh();
						if ((isRuleRootNode(tccomponents[i])) || (tccomponents[i].getProperty(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_type).equals(AutoCodeManagerPropertyName.CodeGroupName))) {
							updateClassificationAfterDeleteSegment(tccomponents[i]);
							updateTreeRelationAfterDelete(tccomponents[i]);
							deleteTreeData(tccomponents[i]);
						} else {
							if ((segmentComponent != null) && (ruleComponent != null) && (ruleComponent.equals(segmentComponent))) {
								updateTreeRelationAfterDelete(tccomponents[i]);
								deleteTreeData(tccomponents[i]);
							}
						}
					} catch (TCException e) {
						e.printStackTrace();
					}
				}
			}
			if (isContinueDelete) {
				deleteCodeRelations(hashMap);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (TCException e) {
			e.printStackTrace();
		}
		return isContinueDelete;
	}

	private void updateTreeRelationAfterDelete(TCComponent deleteComponent) {
		Object[] params = { deleteComponent.getUid() };
		try {
			System.out.println("removeTreeRelationAfterDelete hashMap===  "+deleteComponent);
			userService.call("removeTreeRelationAfterDelete", params);
		} catch (TCException e) {
			e.printStackTrace();
			return;
		}
	}

	public void deleteCodeRelations(HashMap<?, ?> hashMap) throws ServiceException, TCException {
		String[] propNames = new String[hashMap.size()];
		String[] propValues = new String[hashMap.size()];
		System.out.println("deleteCodeRelations hashMap===  "+hashMap);
		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 {
			userService.call("deleteCodeRelations", params);
			return;
		} catch (TCException e) {
			e.printStackTrace();
			return;
		}
	}

	public TCComponent[] searchParentTreeOrListNodes(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("searchParentTreeOrListNodes", params);
			return searchTCcomponents;
		} catch (TCException e) {
			e.printStackTrace();
			return null;
		}
	}

	public boolean isContainsChildrenNodes(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 {
			String value = (String) userService.call("isContainsChildrenNodes", params);
			if (value.trim().equals("true")) {
				return true;
			}
		} catch (TCException e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}

	public TCComponent updateCodeRelation(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]);
		}
		Object[] params = { component, propNames, propValues };
		try {
			TCComponent updateTCcomponent = (TCComponent) userService.call("updateCodeRelation", params);
			return updateTCcomponent;
		} catch (TCException e) {
			e.printStackTrace();
			return null;
		}
	}

	public TCComponent updateChildrenCodeRelation(String parentStr, String currentStr, String childrenStr) {
		try {
			String[] propNames1 = new String[] { AutoCodeManagerPropertyName.CodeRelation_parent_node, AutoCodeManagerPropertyName.CodeRelation_current_node };
			String[] propValues1 = new String[] { parentStr, currentStr };
			Object[] params1 = { propNames1, propValues1 };

			TCComponent[] existComponent = (TCComponent[]) userService.call("searchCodeRelation", params1);
			if (existComponent.length > 0) {
				for (int i = 0; i < existComponent.length; i++) {
					try {
						String[] propNames2 = new String[] { AutoCodeManagerPropertyName.CodeRelation_parent_node, AutoCodeManagerPropertyName.CodeRelation_current_node, AutoCodeManagerPropertyName.CodeRelation_children_nodes, AutoCodeManagerPropertyName.CodeRelation_create_date,
								AutoCodeManagerPropertyName.CodeRelation_create_user };
						String[] propValues2 = new String[] { parentStr, currentStr, childrenStr, "", "" };

						Object[] params2 = { existComponent[i], propNames2, propValues2 };
						TCComponent updateTCcomponent = (TCComponent) userService.call("updateCodeRelation", params2);
						return updateTCcomponent;
					} catch (TCException e) {
						e.printStackTrace();
						return null;
					}
				}
			}
		} catch (TCException e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}

	public TCComponent createNewCodeRelation(AbstractTreeData currentData, TCComponent childrenComponent) {
		try {
			String currentDatas = "";
			String childrenDatas = "";
			childrenDatas = getChildrenData(currentData, childrenDatas, childrenComponent);
			String parentStr = "";
			parentStr = getParentStr(currentData, parentStr);
			if (currentData != null) {
				if (currentData.getPackCodeNodeInfo().getNodeComponent() != null) {
					currentDatas = currentData.getPackCodeNodeInfo().getNodeComponent().getUid();
				} else {
					currentDatas = "";
				}
			} else {
				currentDatas = "";
			}
			System.out.println("createNewCodeRelation::existComponent::parentStr===="+parentStr);
			System.out.println("createNewCodeRelation::existComponent::currentDatas===="+currentDatas);
			
			String[] propNames1 = new String[] { AutoCodeManagerPropertyName.CodeRelation_parent_node, AutoCodeManagerPropertyName.CodeRelation_current_node };
			String[] propValues1 = new String[] { parentStr, currentDatas };
			Object[] params1 = { propNames1, propValues1 };
			TCComponent[] existComponent = (TCComponent[]) userService.call("searchCodeRelation", params1);
			System.out.println("createNewCodeRelation::::existComponent===="+existComponent);
			if (existComponent.length > 0) {
				for (int i = 0; i < existComponent.length; i++) {
					try {
						System.out.println("createNewCodeRelation::::existComponent.length===="+existComponent.length);
						
						String[] propNames2 = new String[] { AutoCodeManagerPropertyName.CodeRelation_parent_node, AutoCodeManagerPropertyName.CodeRelation_current_node, AutoCodeManagerPropertyName.CodeRelation_children_nodes, AutoCodeManagerPropertyName.CodeRelation_create_date,
								AutoCodeManagerPropertyName.CodeRelation_create_user };
						String[] propValues2 = new String[] { parentStr, currentDatas, childrenDatas, "", "" };
						Object[] params2 = { existComponent[i], propNames2, propValues2 };
						
						TCComponent updateTCcomponent = (TCComponent) userService.call("updateCodeRelation", params2);
						return updateTCcomponent;
					} catch (TCException e) {
						e.printStackTrace();
						return null;
					}
				}
			} else {
				System.out.println("createNewCodeRelation::::createNewCodeRelation====");
				String[] propNames3 = new String[] { AutoCodeManagerPropertyName.CodeRelation_parent_node, AutoCodeManagerPropertyName.CodeRelation_current_node, AutoCodeManagerPropertyName.CodeRelation_children_nodes, AutoCodeManagerPropertyName.CodeRelation_create_date,
						AutoCodeManagerPropertyName.CodeRelation_create_user };
				String[] propValues3 = new String[] { parentStr, currentDatas, childrenDatas, "", "" };
				Object[] params3 = { propNames3, propValues3 };
				TCComponent newTCcomponent = (TCComponent) userService.call("createNewCodeRelation", params3);
				return newTCcomponent;
			}
		} catch (TCException e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}

	public String getParentStr(AbstractTreeData treeData, String parentStr) {
		System.out.println("i am in getParentStr == "+parentStr);
		
		if (treeData != null) {
			AbstractTreeData parentData = treeData.getParentTreeData();
			if ((parentData != null) && ((parentData.getPackCodeNodeInfo().getNodeComponent() != null))) {
				System.out.println("parentData.getPackCodeNodeInfo() "+parentData.getPackCodeNodeInfo().getNode_name());
				if(parentData.getPackCodeNodeInfo().getNodeComponent()!=null){
					System.out.println("i am not null");
				}
				if (parentStr.length() > 0) {
					parentStr = parentData.getPackCodeNodeInfo().getNodeComponent().getUid() + "," + parentStr;
				} else {
					parentStr = parentData.getPackCodeNodeInfo().getNodeComponent().getUid();
				}
				parentStr = getParentStr(parentData, parentStr);
				System.out.println("parentStr AAA === "+parentStr);
			}
		}else{
			System.out.println(" treeDtae =null");
		}
		if (parentStr.toString().endsWith(",")) {
			parentStr = parentStr.substring(0, parentStr.length() - 1).toString();
		}
		return parentStr;
	}

	private String getChildrenData(AbstractTreeData treeData, String str, TCComponent childrenComponent) {
		boolean isLoader = false;
		if (treeData != null) {
			AbstractTreeData[] childrenData = treeData.getChildrenDatas();
			if ((childrenData != null) && (childrenData.length > 0)) {
				for (int i = 0; i < childrenData.length; i++) {
					if (childrenData[i].getPackCodeNodeInfo().getNodeComponent() != null) {
						if (childrenComponent.equals(childrenData[i].getPackCodeNodeInfo().getNodeComponent())) {
							isLoader = true;
						}
						str = str + childrenData[i].getPackCodeNodeInfo().getNodeComponent().getUid();
						str = str + ",";
					}
				}
			}
		}
		if (!isLoader) {
			if (childrenComponent != null) {
				str = str + childrenComponent.getUid();
			}
		}
		if (str.endsWith(",")) {
			str = str.substring(0, str.length() - 1).toString();
		}
		return str;
	}

	public TCComponent[] searchTreeORListNodes(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("searchTreeORListNodes", params);
			// System.out.println("锟斤拷询锟斤拷锟轿拷锟? + searchTCcomponents.length + "锟斤拷");
			return searchTCcomponents;
		} catch (TCException e) {
			e.printStackTrace();
			return null;
		}
	}
	public PackCodeNodeInfo getTreeAndListStringValue(TCComponent component, String[] returnPropertyNames) throws ServiceException, TCException {
		PackCodeNodeInfo packCodeNodeInfo = new PackCodeNodeInfo();
		try {
			Object[] params = { component, returnPropertyNames };
			System.out.println("==================");
			String returnValues = (String) userService.call("getTreeAndListStringValue", params);
			System.out.println("1111111111111111111111111");
			String[] values = returnValues.split("&&");
			if ((values != null) && (values.length > 0)) {
				if (returnPropertyNames.equals(AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames)) {
					packCodeNodeInfo.setNodeComponent(component);
					packCodeNodeInfo.setNode_name(getStringValue(values[0]));
					packCodeNodeInfo.setNode_type(getStringValue(values[1]));
					packCodeNodeInfo.setNode_value(getStringValue(values[2]));
					packCodeNodeInfo.setNode_desc(getStringValue(values[3]));
					packCodeNodeInfo.setCode_style(getStringValue(values[4]));
					packCodeNodeInfo.setRule_node(getStringValue(values[5]));
					if (values[6].equalsIgnoreCase("yes")) {
						packCodeNodeInfo.setIs_root(true);
					}
					if (values[7].equalsIgnoreCase("yes")) {
						packCodeNodeInfo.setIs_lock(true);
					}
					if (values[8].equalsIgnoreCase("yes")) {
						packCodeNodeInfo.setDefaultSelected(true);
					}
					packCodeNodeInfo.setCreate_date(getStringValue(values[9]));
					packCodeNodeInfo.setLast_revise_date(getStringValue(values[10]));
					packCodeNodeInfo.setCreate_user(getStringValue(values[11]));
					packCodeNodeInfo.setLast_revise_user(getStringValue(values[12]));
					if (values[13].equalsIgnoreCase("yes")) {
						packCodeNodeInfo.setContainsChildren(true);
					}
				} else {
					packCodeNodeInfo.setNodeComponent(component);
					packCodeNodeInfo.setNode_name(getStringValue(values[0]));
					packCodeNodeInfo.setNode_type(getStringValue(values[1]));
					if (values[2].equalsIgnoreCase("yes")) {
						packCodeNodeInfo.setIs_root(true);
					}
					if (values[3].equalsIgnoreCase("yes")) {
						packCodeNodeInfo.setIs_lock(true);
					}
					if (values[4].equalsIgnoreCase("yes")) {
						packCodeNodeInfo.setContainsChildren(true);
					}
				}
			}
		} catch (TCException e) {
			e.printStackTrace();
		}
		return packCodeNodeInfo;
	}

	private List<PackCodeNodeInfo> getTreeORListNodeInfos(TCComponent[] searchTCcomponents, String parentString, String[] returnPropertyNames) {
		List<PackCodeNodeInfo> packCodeNodeInfos = new ArrayList<PackCodeNodeInfo>();
		try {
			if (searchTCcomponents != null && searchTCcomponents.length > 0) {
				String[] stringValues = getTreeAndListStringValues(searchTCcomponents, returnPropertyNames, parentString);
				System.out.println("stringPropNames====1111=========== "+stringValues);
//				System.out.println("stringPropNames====1111=========== "+stringValues.length);

				if (stringValues == null) {
					return null;
				}
				for (int i = 0; i < stringValues.length; i++) {
					String[] values = stringValues[i].split("&&");
					PackCodeNodeInfo packCodeNodeInfo = new PackCodeNodeInfo();
					if ((values != null) && (values.length > 0)) {
						if (returnPropertyNames.equals(AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames)) {
							packCodeNodeInfo.setNodeComponent(searchTCcomponents[i]);
							packCodeNodeInfo.setNode_name(getStringValue(values[0]));
							packCodeNodeInfo.setNode_type(getStringValue(values[1]));
							packCodeNodeInfo.setNode_value(getStringValue(values[2]));
							packCodeNodeInfo.setNode_desc(getStringValue(values[3]));
							packCodeNodeInfo.setCode_style(getStringValue(values[4]));
							packCodeNodeInfo.setRule_node(getStringValue(values[5]));
							if (values[6].equalsIgnoreCase("yes")) {
								packCodeNodeInfo.setIs_root(true);
							}
							if (values[7].equalsIgnoreCase("yes")) {
								packCodeNodeInfo.setIs_lock(true);
							}
							if (values[8].equalsIgnoreCase("yes")) {
								packCodeNodeInfo.setDefaultSelected(true);
							}
							packCodeNodeInfo.setCreate_date(getStringValue(values[9]));
							packCodeNodeInfo.setLast_revise_date(getStringValue(values[10]));
							packCodeNodeInfo.setCreate_user(getStringValue(values[11]));
							packCodeNodeInfo.setLast_revise_user(getStringValue(values[12]));
							if (values[13].equalsIgnoreCase("yes")) {
								packCodeNodeInfo.setContainsChildren(true);
							}
						} else {
							packCodeNodeInfo.setNodeComponent(searchTCcomponents[i]);
							packCodeNodeInfo.setNode_name(getStringValue(values[0]));
							packCodeNodeInfo.setNode_type(getStringValue(values[1]));
							if (values[2].equalsIgnoreCase("yes")) {
								packCodeNodeInfo.setIs_root(true);
							}
							if (values[3].equalsIgnoreCase("yes")) {
								packCodeNodeInfo.setIs_lock(true);
							}
							if (values[4].equalsIgnoreCase("yes")) {
								packCodeNodeInfo.setContainsChildren(true);
							}
						}
						packCodeNodeInfos.add(packCodeNodeInfo);
					}
				}
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (TCException e) {
			e.printStackTrace();
		}
		return packCodeNodeInfos;
	}

	public List<PackCodeNodeInfo> searchTreeORListNodeInfos(HashMap<?, ?> hashMap, String parentString, String[] returnPropertyNames) throws ServiceException, TCException {
		List<PackCodeNodeInfo> packCodeNodeInfos = new ArrayList<PackCodeNodeInfo>();
		try {
			TCComponent[] searchTCcomponents = searchTreeORListNodes(hashMap);
			System.out.println("parentString ========================>"+parentString);
			packCodeNodeInfos = getTreeORListNodeInfos(searchTCcomponents, parentString, returnPropertyNames);
			return packCodeNodeInfos;
		} catch (TCException e) {
			e.printStackTrace();
			return null;
		}
	}

	private String getStringValue(String value) {
		if ((value == null) || (value.equalsIgnoreCase("null"))) {
			return "";
		} else {
			return value;
		}
	}

	public String[] getTreeAndListStringValues(TCComponent[] components, String[] stringPropNames, String parentString) throws ServiceException, TCException {
		Object[] params = { components, stringPropNames, parentString };
		try {
//			for(int i=0;i<components.length;i++){
//				System.out.println("components============ "+components[i].getProperty("object_name")+" "+components[i].getDisplayType());
//			}
//			for(int i=0;i<stringPropNames.length;i++){
//				System.out.println("stringPropNames=============== "+stringPropNames[i]);
//			}
//			System.out.println("parentString===== "+parentString);
			String value =  (String) userService.call("getTreeAndListStringValues", params);
			return value.split("@@");
		} catch (TCException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
}
