/*
#=============================================================================
#																			   
#			copyright (c) 2009 Origin Enterprise Solution LTD.		   
#																			   
#=============================================================================
# File name: GetCommonInfo.java
# File description: 										   	
#=============================================================================
#	Date		Name		    Action	Description of Change					   
#	2011-4-20	liqz  			创建		得到一些通用信息(From shangu)				   
#=============================================================================
 */
package cn.com.customization.util;

import java.awt.HeadlessException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JFileChooser;

import com.teamcenter.rac.kernel.TCAccessControlService;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentUser;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

public class GetCommonInfo {
	private TCSession	session;

	public GetCommonInfo(TCSession session) {
		this.session = session;

	}

	/**
	 * getChoosePath:得到用户所选择的路径
	 * @param @return 设定文件
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public String getChoosePath() {
		String tempPath = System.getProperty("java.io.tmpdir");
		String currentPath = "";
		try {
			File file1 = new File(tempPath + "\\export.txt");
			JFileChooser jfc = null;
			if (file1.exists()) {
				BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file1)));
				String line = in.readLine();
				if (line != null) {
					jfc = new JFileChooser(new File(line));
				} else {
					jfc = new JFileChooser();
				}
			} else {
				jfc = new JFileChooser();
			}

			jfc.setFileSelectionMode(JFileChooser.SAVE_DIALOG);
			int openOrCann = jfc.showSaveDialog(null);
			if (openOrCann == 1) {
				return currentPath;
			}
			// 用来存储用户所选的路径：
			File file2 = new File(tempPath + "\\export.txt");
			if (!file2.exists()) {
				file2.createNewFile();
			}
			try {
				BufferedWriter output = new BufferedWriter(new FileWriter(file2));
				output.write(jfc.getSelectedFile().getAbsolutePath());
				output.close();
				currentPath = jfc.getSelectedFile().getAbsolutePath();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (HeadlessException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return currentPath;
	}

	public boolean isExistFiles(String path) {
		boolean flag = false;
		File file = new File(path);
		if (file.isDirectory()) {
			if (file.listFiles().length > 0) {
				flag = true;
			}
		}
		return flag;
	}

	/**
	 * existFile:判断文件是否存在
	 * @param @return 设定文件
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public boolean existFile(String filePath, String fileName) {
		boolean exist = false;
		try {
			if (fileName.indexOf('/') >= 0) {
				fileName = fileName.replace('/', '_');
			}
			File file = new File(filePath + "\\" + fileName);
			System.out.println("filePath_fileName:" + filePath + "\\" + fileName);
			if (file.exists()) {
				exist = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			MessageBox.post("在检查文件是否存在的时候，出现错误！", "ERROR", MessageBox.ERROR);
		}
		return exist;
	}

	/**
	 * 是否已经发布
	 * @param tccomponent 对象
	 * @return 是否发布
	 */
	public boolean isReleased(TCComponent tccomponent) {
		boolean flag = false;
		try {
			String s = tccomponent.getProperty("release_status_list");
			if (s != null && s.length() > 0)
				flag = true;
		} catch (TCException tcexception) {
			flag = false;
		}
		return flag;
	}

	/**
	 * existFile:检验是否有权限
	 * @param @return 设定文件
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public boolean checkAccessPrivilige(TCComponentUser user, TCComponent comp, String accessName) throws TCException {
		TCAccessControlService accessService = ((TCSession) session).getTCAccessControlService();
		boolean returnboolean = accessService.checkUsersPrivilege(user, comp, accessName);
		return returnboolean;
	}

}
