package com.teamcenter.rac.kernel;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

import com.teamcenter.rac.aif.kernel.AIFComponentContext;
import com.teamcenter.rac.util.FileUtility;
import com.teamcenter.rac.util.Registry;

// Referenced classes of package com.teamcenter.rac.kernel:
//            AbstractDatasetOpenPreAction, AEShell, TCComponentDataset, TCSession, 
//            TCComponentItemRevision, TCComponent, TCComponentItem, TCTextService

public class OpentionAction extends AbstractDatasetOpenPreAction {

	public OpentionAction(AEShell aeshell) {
		super(aeshell);
		System.out.println("打开数据集操作");
	}

	@Override
	protected void cleanSession() {
		// TODO Auto-generated method stub
		// super.cleanSession();
		System.out.println("清楚路径");
		TCComponentDataset tccomponentdataset = shell.askSelectedDataset();
		TCSession tcsession = tccomponentdataset.getSession();
		AEShellManager aeshellmanager = tcsession.getAEShellManager();
		try {
			String s = tccomponentdataset.getWorkingDir();
			// tccomponentdataset.removeWorkingDir(s);
		} catch (TCException _ex) {
		}
		aeshellmanager.removeShell(shell);
	}

	@Override
	public void startPreAction() {
		selectedDataset = shell.askSelectedDataset();
		session = selectedDataset.getSession();
		aliasDatasetName = selectedDataset.toString();
		referenceName = shell.askSelectedNamedRef();
		appReg = Registry.getRegistry(this);
		try {
			userId = session.getUserName();
			AIFComponentContext aaifcomponentcontext[] = null;
			if (selectedDataset != null) {
				aliasDatasetType = selectedDataset.getType();
				aaifcomponentcontext = selectedDataset.getPrimary();
			}
			if (aaifcomponentcontext[0] != null) {
				TCComponentItemRevision tccomponentitemrevision = (TCComponentItemRevision) aaifcomponentcontext[0]
						.getComponent();
				TCComponent atccomponent[] = selectedDataset
						.getNamedRefComponents("Alias_Wire");
				aliasDatasetUid = selectedDataset.getUid();
				if (atccomponent.length > 0)
					fileUid = atccomponent[0].getUid();
				revID = tccomponentitemrevision.getProperty("item_revision_id");
				itemID = tccomponentitemrevision.getItem().getProperty(
						"item_id");
				String s = aaifcomponentcontext[0].getContext().toString();
				TCTextService tctextservice = session.getTextService();
				String s1 = tctextservice.getTextValue(s);
				relationName = appReg.getString(s1);
			}
		} catch (Exception exception) {
			System.out.println(exception.getClass().getName());
		}
		createInputXMLFile();
	}

	private void createInputXMLFile() {
		String s = FileUtility.getExportDir();
		String s1 = aliasDatasetName;
		ouputXMLFilePath = (new StringBuilder(String.valueOf(s)))
				.append(File.separator).append(s1).append(".xml").toString();
		try {
			aliasXMLFile = new File(ouputXMLFilePath);
			aliasXMLWriter = new PrintWriter(new BufferedWriter(new FileWriter(
					aliasXMLFile)), true);
			aliasXMLWriter.println("<?xml version=\"1.0\"?>");
			aliasXMLWriter.println("<AliasTcEncapsulationInfo>");
			aliasXMLWriter.println((new StringBuilder("<userId>"))
					.append(userId).append("</userId>").toString());
			aliasXMLWriter.println((new StringBuilder("<itemId>"))
					.append(itemID).append("</itemId>").toString());
			aliasXMLWriter.println((new StringBuilder("<itemRevisionId>"))
					.append(revID).append("</itemRevisionId>").toString());
			aliasXMLWriter.println((new StringBuilder("<datasetType>"))
					.append(aliasDatasetType).append("</datasetType>")
					.toString());
			aliasXMLWriter.println((new StringBuilder("<datasetName>"))
					.append(aliasDatasetName).append("</datasetName>")
					.toString());
			aliasXMLWriter.println((new StringBuilder("<referenceType>"))
					.append(referenceName).append("</referenceType>")
					.toString());
			aliasXMLWriter.println((new StringBuilder("<referenceName> "))
					.append(referenceName).append("</referenceName>")
					.toString());
			aliasXMLWriter.println((new StringBuilder("<relationship>"))
					.append(relationName).append("</relationship>").toString());
			aliasXMLWriter
					.println((new StringBuilder("<datasetUid>"))
							.append(aliasDatasetUid).append("</datasetUid>")
							.toString());
			aliasXMLWriter.println((new StringBuilder("<fileUid>"))
					.append(fileUid).append("</fileUid>").toString());
			aliasXMLWriter.println("</AliasTcEncapsulationInfo>");
		} catch (Exception exception) {
			System.out.println(exception.getClass().getName());
		}
	}

	// private static final Logger logger =
	// Logger.getLogger(com/teamcenter/rac/kernel/AliasOpenAction);
	private File aliasXMLFile;
	private PrintWriter aliasXMLWriter;
	private TCSession session;
	private TCComponentDataset selectedDataset;
	private Registry appReg;
	private String itemID;
	private String revID;
	private String aliasDatasetName;
	private String referenceName;
	private String relationName;
	private String userId;
	private String aliasDatasetType;
	private String aliasDatasetUid;
	private String fileUid;
	public static String ouputXMLFilePath;

}

/*
 * DECOMPILATION REPORT
 * 
 * Decompiled from:
 * F:\Teamcenter10.1.4Env\portal\plugins\com.teamcenter.rac.kernel_10000.1.0.jar
 * Total time: 59 ms Jad reported messages/errors: Exit status: 0 Caught
 * exceptions:
 */
