/**
 * @author tyl
 * @Action  ini
 * 2011-3-21
 */
package cn.com.origin.autocodemanager.views.codeview;

import java.util.HashMap;

import org.eclipse.swt.widgets.Shell;

import cn.com.origin.autocodemanager.common.AutoCodeManagerPropertyName;
import cn.com.origin.autocodemanager.common.operations.PackCodeNodeInfo;
import cn.com.origin.autocodemanager.common.operations.TreeAndListCodeOperation;
import cn.com.origin.autocodemanager.common.tree.AbstractCreateTreeView;
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 CopyCodeNodeDialog extends NewCodeNodeDialog {

	TCComponent ruleComponent = null;

	public CopyCodeNodeDialog(Shell parentShell, AbstractCreateTreeView codeTreeView, String title, boolean isEdit, String actionType) {
		super(parentShell, codeTreeView, actionType, title, isEdit);
	}

	// 检查节点和组是否为空
	@Override
	protected void okPressed() {
		String nodename = nodeNameText.getText().trim();
		if (nodename == null || nodename.trim().length() == 0) {
			postMesssageBox("规则名称不能为空！");
			nodeNameText.setFocus();
			return;
		}
		AbstractTreeData targetCopyNode = saveNewCodeNode(selectedTreeData.getParentTreeData(), selectedTreeData);
		String parentStr = "";
		copyCodeNodes(treeAndListCodeOperation.getParentStr(selectedTreeData, parentStr), selectedTreeData.getPackCodeNodeInfo().getNodeComponent().getUid(), targetCopyNode);
		try {
			codeSegmentView.createView.createOrgAutoCodeNodeChildren(new AbstractTreeData[] { targetCopyNode });
		} catch (Exception e) {
			e.printStackTrace();
		}
		close();
	}

	private AbstractTreeData saveNewCodeNode(AbstractTreeData parentTreeData, AbstractTreeData selectedTreeData) {
		AbstractTreeData treeData = null;
		try {
			HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_index, "");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_name, getTextValue(nodeNameText));
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_value, getTextValue(nodeValueText));
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_type, selectedTreeData.getPackCodeNodeInfo().getNode_type());
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_desc, getTextValue(nodeDescriptionText));
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_root, String.valueOf(selectedTreeData.getPackCodeNodeInfo().is_root));
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_create_date, "");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_rule_node, "");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_date, "");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_create_user, "");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_user, "");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_lock, "false");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_isDefaultSelected, "false");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_code_style, getTextValue(codeStyleText));
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_code_length, getTextValue(codeLengthText));
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_begin_value, getTextValue(seqBeginText));
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_length, getSeqLength());
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_max_value, getTextValue(seqMaxText));
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_reference_nodes, "");
			if (selectedTreeData.getParentTreeData().getPackCodeNodeInfo().getNodeComponent() == null) {
				ruleComponent = null;
			}
			TCComponent newComponent = new TreeAndListCodeOperation().createNewTreeORListNode(ruleComponent, parentTreeData, hashMap);
			if (newComponent == null) {
				return null;
			}
			if (selectedTreeData.getPackCodeNodeInfo().is_root) {
				ruleComponent = newComponent;
			}
			TreeAndListCodeOperation treeAndListCodeOperation = new TreeAndListCodeOperation();
			PackCodeNodeInfo packCodeNodeInfo = treeAndListCodeOperation.getTreeAndListStringValue(newComponent, AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames);
			packCodeNodeInfo.setNodeComponent(newComponent);
			treeData = new AbstractTreeData(parentTreeData, packCodeNodeInfo);
			treeViewOperation.addExistCodeNode(createView.treeViewer, parentTreeData, treeData);
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (TCException e) {
			e.printStackTrace();
		}
		return treeData;
	}

	private void copyCodeNodes(String parentStr, String currentStr, AbstractTreeData parentNode) {
		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;
			}
			for (int i = 0; i < tccomponents.length; i++) {
				AbstractTreeData copyNode = copyTreeData(parentNode, tccomponents[i]);
				String parentStr2 = parentStr + "," + currentStr;
				if (parentStr2.trim().startsWith(",")) {
					parentStr2 = parentStr2.substring(1);
				}
				copyCodeNodes(parentStr2, tccomponents[i].getUid().trim(), copyNode);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

	private AbstractTreeData copyTreeData(AbstractTreeData parentTreeData, TCComponent sourceComponent) {
		AbstractTreeData treeData = null;

		try {
			sourceComponent.refresh();
			TreeAndListCodeOperation treeAndListCodeOperation = new TreeAndListCodeOperation();
			PackCodeNodeInfo packCodeNodeInfo = treeAndListCodeOperation.getTreeAndListStringValue(sourceComponent, AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames);
			String[] propertyNames = { AutoCodeManagerPropertyName.TreeAndListCodeNode_code_length, AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_begin_value, AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_length, AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_max_value };
			String[] propertyValues = sourceComponent.getProperties(propertyNames);
			HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_index, "");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_name, packCodeNodeInfo.getNode_name());
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_value, packCodeNodeInfo.getNode_value());
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_type, packCodeNodeInfo.getNode_type());
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_desc, packCodeNodeInfo.getNode_desc());
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_root, String.valueOf(packCodeNodeInfo.is_root));
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_create_date, "");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_rule_node, "");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_date, "");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_create_user, "");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_user, "");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_lock, "false");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_isDefaultSelected, "false");
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_code_style, packCodeNodeInfo.getCode_style());
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_code_length, propertyValues[0]);
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_begin_value, propertyValues[1]);
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_length, propertyValues[2]);
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_max_value, propertyValues[3]);
			hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_reference_nodes, "");
			if (packCodeNodeInfo.is_root) {
				ruleComponent = null;
			}
			TCComponent newComponent = new TreeAndListCodeOperation().createNewTreeORListNode(ruleComponent, parentTreeData, hashMap);
			if (packCodeNodeInfo.is_root) {
				ruleComponent = newComponent;
			}
			packCodeNodeInfo.setNodeComponent(newComponent);
			treeData = new AbstractTreeData(parentTreeData, packCodeNodeInfo);
			treeViewOperation.addChildrenRelations(parentTreeData, treeData);
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (TCException e) {
			e.printStackTrace();
		}
		return treeData;
	}
}
