/**
 * @author tyl
 * @Action  ini
 * 2011-3-22
 */
package cn.com.origin.autocodemanager.export;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import cn.com.origin.autocodemanager.common.AbstractTreeViewPart;
import cn.com.origin.autocodemanager.common.AutoCodeManagerPropertyName;
import cn.com.origin.autocodemanager.common.operations.ExcelOperation;
import cn.com.origin.autocodemanager.common.operations.TreeAndListCodeOperation;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeData;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.schemas.soa._2006_03.exceptions.ServiceException;

public class ExportNodeOperation {
	private TreeAndListCodeOperation treeAndListCodeOperation;
	// private TCSession session;
	private AbstractTreeData treeData;
	private String filePath;

	public ExportNodeOperation(AbstractTreeViewPart viewPart, AbstractTreeData treeData, String filePath) {
		// this.viewPart = viewPart;
		this.treeData = treeData;
		this.filePath = filePath;
		initParams();
	}

	public void exportNode() {
		ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
		File excelFile = new File(filePath);
		String sheetTitle = treeData.getPackCodeNodeInfo().getNode_name();
		Object[] cloumnNames = AutoCodeManagerPropertyName.TreeAndListCodeNode_Titles;

		exportTreeNode(treeData.getPackCodeNodeInfo().getNodeComponent(), arrayList, 0);
		String parentStr = "";
		exportTreeNodes(treeAndListCodeOperation.getParentStr(treeData, parentStr), treeData.getPackCodeNodeInfo().getNodeComponent().getUid(), arrayList, 0);

		try {
			ExcelOperation.writeExcel(sheetTitle, excelFile, cloumnNames, arrayList);
		} catch (IOException e) {
		}
	}

	public void initParams() {
		this.treeAndListCodeOperation = new TreeAndListCodeOperation();
		// session = (TCSession) AIFUtility.getCurrentApplication().getSession();
	}

	private void exportTreeNodes(String parentStr, String currentStr, ArrayList<HashMap<String, String>> arrayList, int level) {
		try {
			TCComponent[] tccomponents = null;
			HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
			hashMap.put(AutoCodeManagerPropertyName.CodeRelation_parent_node, parentStr);
			hashMap.put(AutoCodeManagerPropertyName.CodeRelation_current_node, currentStr);
			tccomponents = treeAndListCodeOperation.searchTreeORListNodes(hashMap);
			if (tccomponents == null) {
				return;
			}

			level = level + 1;
			for (int i = 0; i < tccomponents.length; i++) {
				exportTreeNode(tccomponents[i], arrayList, level);
				String parentStr2 = parentStr + "," + currentStr;
				if (parentStr2.trim().startsWith(",")) {
					parentStr2 = parentStr2.substring(1);
				}
				exportTreeNodes(parentStr2, tccomponents[i].getUid().trim(), arrayList, level);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

	public void exportTreeNode(TCComponent tccomponent, ArrayList<HashMap<String, String>> arrayList, int level) {
		if (tccomponent == null) {
			return;
		}
		HashMap<String, String> hashMap = new HashMap<String, String>();
		try {
			tccomponent.refresh();
			String[] propertyNames = { AutoCodeManagerPropertyName.TreeAndListCodeNode_node_type, AutoCodeManagerPropertyName.TreeAndListCodeNode_node_name, AutoCodeManagerPropertyName.TreeAndListCodeNode_node_value, AutoCodeManagerPropertyName.TreeAndListCodeNode_node_desc,
					AutoCodeManagerPropertyName.TreeAndListCodeNode_code_style, AutoCodeManagerPropertyName.TreeAndListCodeNode_code_length, AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_begin_value, AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_max_value,
					AutoCodeManagerPropertyName.TreeAndListCodeNode_is_root, AutoCodeManagerPropertyName.TreeAndListCodeNode_isDefaultSelected, AutoCodeManagerPropertyName.TreeAndListCodeNode_is_lock, AutoCodeManagerPropertyName.TreeAndListCodeNode_create_date,
					AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_date, AutoCodeManagerPropertyName.TreeAndListCodeNode_create_user, AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_user };
			String[] propertyValues = tccomponent.getProperties(propertyNames);
			hashMap.put("层号", Integer.toString(level));
			hashMap.put("类型", propertyValues[0]);
			hashMap.put("名称", propertyValues[1]);
			hashMap.put("码值", propertyValues[2]);
			hashMap.put("备注", propertyValues[3]);
			hashMap.put("编码样式", propertyValues[4]);
			hashMap.put("编码长度", propertyValues[5]);
			hashMap.put("流水码初始值", propertyValues[6]);
			hashMap.put("流水码最大值", propertyValues[7]);
			if (propertyValues[8].equalsIgnoreCase("Y")) {
				hashMap.put("是否为根节点", "true");
			} else {
				hashMap.put("是否为根节点", "false");
			}
			if (propertyValues[9].equalsIgnoreCase("Y")) {
				hashMap.put("是否为默认值", "true");
			} else {
				hashMap.put("是否为默认值", "false");
			}
			if (propertyValues[10].equalsIgnoreCase("Y")) {
				hashMap.put("是否锁定", "true");
			} else {
				hashMap.put("是否锁定", "false");
			}
			hashMap.put("创建日期", propertyValues[11]);
			hashMap.put("最后修改日期", propertyValues[12]);
			hashMap.put("创建者", propertyValues[13]);
			hashMap.put("最后修改者", propertyValues[14]);
		} catch (TCException e) {
			e.printStackTrace();
		}
		arrayList.add(hashMap);
	}
}
