/*
 #=======================================================================================================
 #
 #                   Copyright (c) 2007 Origin Enterprise Solution LTD.
 #
 #=======================================================================================================
 # File description:  搜索数据集
 #=======================================================================================================
 #	Date		Name		Action	Description of Change
 #	20090301	Matt Shan	Ini		搜索数据集
 #=======================================================================================================
 */
package cn.com.customization.util;

import java.io.File;

import com.teamcenter.rac.kernel.TCAccessControlService;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentDataset;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCComponentItemType;
import com.teamcenter.rac.kernel.TCComponentQuery;
import com.teamcenter.rac.kernel.TCComponentQueryType;
import com.teamcenter.rac.kernel.TCComponentTcFile;
import com.teamcenter.rac.kernel.TCComponentUser;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.kernel.TCTextService;
import com.teamcenter.rac.util.MessageBox;

public class DataSetFinder {

	private TCSession	session	= null;

	/**
	 * 搜索数据集
	 * @param session 系统session
	 */
	public DataSetFinder(TCSession pSession) {
		queryType = null;
		datasetQuery = null;
		boolean flag = false;
		try {
			session = pSession;
			its = session.getTextService();
			queryType = (TCComponentQueryType) session.getTypeComponent("ImanQuery");
		}
		catch (TCException imane) {
			imane.printStackTrace();
			imane.dump();
		}
		try {
			datasetQuery = (TCComponentQuery) queryType.find("数据集...");
		}
		catch (TCException imane) {
			imane.printStackTrace();
			imane.dump();
			flag = true;
		}
		if (datasetQuery == null || flag) {
			try {
				datasetQuery = (TCComponentQuery) queryType.find(its.getTextValue("DataSet..."));
			}
			catch (TCException imane) {
				imane.printStackTrace();
				imane.dump();
			}
		}
	}

	/**
	 * 查询数据集
	 * @param datasetname 数据集名称
	 * @return 根据数据集名称查询数据集
	 */
	public TCComponentDataset FindDatasetByName(String datasetname, String pOwningUser, String pOwningGroup, String pDatasetType) {
		System.out.println("-----------FindDatasetByName");
		try {
			//String f_dataset_name[] = { its.getTextValue("Name"), its.getTextValue("OwningUser"), its.getTextValue("OwningGroup"), its.getTextValue("DatasetType") };
			String f_dataset_name[] = new String[4];
			f_dataset_name[0] = its.getTextValue("Name");
			if(f_dataset_name[0] == null){
				f_dataset_name[0] = "Name";
			}
			f_dataset_name[1] = its.getTextValue("OwningUser");
			if(f_dataset_name[1] == null){
				f_dataset_name[1] = "Owning User";
			}
			f_dataset_name[2] = its.getTextValue("OwningGroup");
			if(f_dataset_name[2] == null){
				f_dataset_name[2] = "Owning Group";
			}
			f_dataset_name[3] = its.getTextValue("DatasetType");
			if(f_dataset_name[3] == null){
				f_dataset_name[3] = "Dataset Type";
			}
			f_dataset_name_value = new String[4];
			f_dataset_name_value[0] = datasetname;
			f_dataset_name_value[1] = pOwningUser;
			f_dataset_name_value[2] = pOwningGroup;
			f_dataset_name_value[3] = pDatasetType;
			com.teamcenter.rac.kernel.TCComponent dataset[] = datasetQuery.execute(f_dataset_name, f_dataset_name_value);
			if (dataset == null || dataset.length == 0) {
				return null;
			} else {
				TCComponentDataset dataset0 = (TCComponentDataset) dataset[0];
				TCComponentDataset dataset1 = dataset0.latest();
				return dataset1;
			}
		}
		catch (TCException imane) {
			imane.printStackTrace();
			imane.dump();
			return null;
		}
	}
	
	/**
	 * 获取模版数据集
	 * @param datasetname 数据集名称
	 * @return 根据数据集名称查询数据集
	 */
	public TCComponentDataset FindDatasetByName(String datasetname) {
		TCComponent[] components = null;
		try {
			TCComponentItemRevision revision =null;
			TCComponentItemType tccomItemType = (TCComponentItemType) session.getTypeComponent("Item");
			TCComponentItem[]items =tccomItemType.findItems(datasetname);
			if(items.length >0){
				revision=items[0].getLatestItemRevision() ;
			}
			components = revision.getRelatedComponents("IMAN_specification");// 通过关系找对象
			for (TCComponent component : components) {
				if (component instanceof TCComponentDataset) {
					TCComponentDataset dataset = (TCComponentDataset) component;
					return dataset;
				}
			}
		} catch (TCException e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}

	/**
	 * 导出数据集相关文件到指定路径
	 * @param dataset 数据集
	 * @param nameRef 数据集名称
	 * @param filename 新命名文件
	 * @param dir 导出到得路径
	 * @return 新生成文件
	 */
	public File ExportFileToDir(TCComponentDataset dataset, String nameRef, String filename, String dir) {
		File exportFile = null;
		try {
			String workdir = dir;
			File tFile = new File(workdir, filename);
			if (tFile.exists())
				tFile.delete();
			if (!checkAccessPrivilige(session.getUser(), dataset, "EXPORT")) {
				MessageBox.post("您没有权限下载报表模板文件！", "提示", MessageBox.WARNING);
				return null;
			}
			TCComponentTcFile files[] = dataset.getTcFiles();
			if (files.length != 1) {
				MessageBox.post("请检查报表模板文件是否配置正确！", "提示", MessageBox.WARNING);
				return null;
			}
			for (int i = 0; i < files.length; i++) {
				exportFile = files[i].getFile(workdir, filename);
			}
			return exportFile;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private boolean checkAccessPrivilige(TCComponentUser user, TCComponent comp, String accessName) throws TCException {
		TCAccessControlService accessService = ((TCSession) session).getTCAccessControlService();
		boolean returnboolean = accessService.checkUsersPrivilege(user, comp, accessName);
		return returnboolean;
	}

	/**
	 * 导出数据集相关文件到指定路径
	 * @param dataset 数据集
	 * @param nameRef 数据集名称
	 * @param filename 新命名文件
	 * @param dir 导出到得路径
	 * @return 新生成文件
	 */
	public File ExportFileToDir(TCComponentDataset dataset, String filename, String dir) {
		File exportFile = null;
		try {
			String workdir = dir;
			File tFile = new File(workdir, filename);
			if (tFile.exists())
				tFile.delete();
			if (!checkAccessPrivilige(session.getUser(), dataset, "EXPORT")) {
				MessageBox.post("您没有权限下载报表模板文件！", "提示", MessageBox.WARNING);
				return null;
			}
			TCComponentTcFile files[] = dataset.getTcFiles();
			if (files.length != 1) {
				MessageBox.post("请检查报表模板文件是否配置正确！", "提示", MessageBox.WARNING);
				return null;
			}
			for (int i = 0; i < files.length; i++) {
				exportFile = files[i].getFile(workdir, filename);
			}
			return exportFile;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public File ExportFileToDir(TCComponentDataset dataset, String dir) {
		File exportFile = null;
		try {
			String workdir = dir;
			if (!checkAccessPrivilige(session.getUser(), dataset, "EXPORT")) {
				MessageBox.post("您没有权限下载报表模板文件！", "提示", MessageBox.WARNING);
				return null;
			}
			TCComponentTcFile files[] = dataset.getTcFiles();
			if (files.length != 1) {
				MessageBox.post("请检查报表模板文件是否配置正确！", "提示", MessageBox.WARNING);
				return null;
			}
			for (int i = 0; i < files.length; i++) {
				exportFile = files[i].getFile(workdir);
			}
			return exportFile;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private TCComponentQueryType	queryType;
	private TCComponentQuery		datasetQuery;
	private TCTextService			its;
	private String					f_dataset_name_value[];
}
