package cn.com.origin.autocodemanager.common;

import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;

public class IsShowAutocodeManagerPerspective {

	//只有在此首选项中配置的用户才有权限
	private String showAutocodeManagerUserOptionName = "origin_showAutocodeManagerUsers";

	public IsShowAutocodeManagerPerspective() {
		
	}
	/**
	 *  通过配置首先项检查当前用户是否有权限操作
	 * @return
	 */
	public boolean getValue() {
		boolean flag = false;
		try {
			TCSession session = (TCSession) AIFUtility.getCurrentApplication().getSession();
			String userID = session.getUser().getUserId();
			flag = isExist(userID, showAutocodeManagerUserOptionName);
		} catch (TCException e) {
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 检查用户是否和首选项匹配
	 * @param value
	 * @param optionName
	 * @return
	 */
	public boolean isExist(String value, String optionName) {
		boolean flag = false;
		try {
			TCSession session = (TCSession) AIFUtility.getCurrentApplication().getSession();
			TCPreferenceService preferenceService = session.getPreferenceService();
			String preferenceValues[] = preferenceService.getStringArray(TCPreferenceService.TC_preference_site, optionName);
			if (preferenceValues != null && preferenceValues.length > 0) {
				for (int i = 0; i < preferenceValues.length; i++) {
					// System.out.println("preferenceValues===========" +
					// preferenceValues[i]);
					if (preferenceValues[i].trim().equalsIgnoreCase(value)) {
						flag = true;
						return flag;
					}
				}
			} else {
				flag = true;
				return flag;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
}
