/*
#=======================================================================================================
#
#                   Copyright (c) 2007 Origin Enterprise Solution LTD.
#
#=======================================================================================================
# File description:  UtilApi.java
#=======================================================================================================
#	 Date	       time   	   Name	 Action	   Description of Change
#	2011-1-17      下午03:56:51    tyl  	Ini	            初始化文件
#=======================================================================================================
 */
package cn.com.customization.util;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import javax.swing.JComboBox;

import com.teamcenter.rac.aif.AbstractAIFUIApplication;
import com.teamcenter.rac.aif.kernel.AIFComponentContext;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentBOMLine;
import com.teamcenter.rac.kernel.TCComponentContextList;
import com.teamcenter.rac.kernel.TCComponentQuery;
import com.teamcenter.rac.kernel.TCComponentQueryType;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.kernel.TCTextService;
import com.teamcenter.rac.util.MessageBox;

public class UtilApi {
	/**
	 * Logger for this class
	 */
	private static HashMap<String, String>	loginUserInfoHm	= new HashMap<String, String>();

	/**
	 * 系统session
	 */
	private TCSession						session;

	/**
	 * 通用api
	 * @param pseapplication 抽象界面应用程序
	 */
	public UtilApi(AbstractAIFUIApplication pseapplication) {
		session = (TCSession) pseapplication.getSession();
	}

	/**
	 * 通用api
	 * @param newsession session
	 */
	public UtilApi(TCSession newsession) {
		session = newsession;
	}

	private String getDefaultExportDirectory() {
		String s = null;
		if (s == null || s.length() == 0) {
			TCPreferenceService tcpreferenceservice = session.getPreferenceService();
			s = tcpreferenceservice.getString(0, "defaultExportDirectory");
		}
		if (s != null) {
			s = s.trim();
			if (s.length() == 0)
				s = null;
		}
		return s;
	}

	public String getEnv(String name) {
		String str = "";
		Iterator it = System.getenv().entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			System.out.println("entry-" + entry.getKey() + ":" + entry.getValue());
			if (entry.getKey().toString().equals(name)) {
				str = entry.getValue() + "";
				if (!str.endsWith("\\")) {
					str = str + "\\";
				}
				break;
			}
		}
		return str;
	}

	/**
	 * Enter 按键
	 */
	public static String	ENTER	= new String(new byte[] { 13, 10 }, 0);

	/**
	 * 按照指定分隔符，将string转换为vector
	 * @param msg 源字符串
	 * @param sepStr 分隔符
	 * @return vector对象
	 */
	public static Vector getVector(String msg, String sepStr) {
		System.out.println("***********getVector()");
		System.out.println("msg===" + msg);
		System.out.println("sepStr===" + sepStr);
		Vector vt = new Vector();
		try {
			String tmpStr = null;
			boolean flag = false;
			int pos = -1;
			if (msg == null || msg.trim().length() == 0) { return null; }
			if (msg.startsWith(sepStr)) {
				msg = msg.substring(1);
			}
			if (msg.lastIndexOf(sepStr) == msg.length() - 1) {
				flag = true;
				msg = msg.substring(0, msg.length() - 1);
			}
			while ((pos = msg.indexOf(sepStr)) > 0) {
				try {
					tmpStr = msg.substring(0, pos);
					vt.addElement(tmpStr);
					if (msg.length() > pos + 1)
						msg = msg.substring(pos + 1);
				}
				catch (RuntimeException e) {
					break;
				}
			}
			vt.add(msg);
			if (flag)
				vt.add("");
		}
		catch (RuntimeException e) {
			e.printStackTrace();
		}
		return vt;
	}

	/**
	 * 提取字符串中数字
	 * @param src 源字符串
	 * @return double数字
	 */
	public static double getFloatFromStr(String src) {
		double d = 0d;
		try {
			if (src == null || src.length() == 0) { return 0d; }
			src = src.trim();
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < src.length(); i++) {
				if (src.charAt(i) == '.' || src.charAt(i) == '+' || src.charAt(i) == '-') {
					sb.append(src.charAt(i));
				} else if (src.charAt(i) >= '0' && src.charAt(i) <= '9') {
					sb.append(src.charAt(i));
				}
			}
			d = Double.parseDouble(sb.toString());
		}
		catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * getMsgboxLocate:设定窗体位置
	 * @param @return 窗体位置
	 * @return Point 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static Point getMsgboxLocate() {
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int frmLocateWidth = screenSize.width * 2 / 5;
		int frmLocateHeight = screenSize.height * 4 / 7;
		Point p = new Point(frmLocateWidth, frmLocateHeight);
		return p;
	}

	/**
	 * 获得消息提示窗体位置
	 * @param formLocHeigh 当前窗体纵向起始位置
	 * @return 窗体位置
	 */
	public static Point getMsgboxLocate(double formLocHeigh) {
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		String tmp = formLocHeigh + "";
		if (tmp.indexOf(".") >= 0) {
			tmp = tmp.substring(0, tmp.indexOf("."));
		}
		int t = Integer.parseInt(tmp);
		int frmLocateWidth = screenSize.width * 2 / 5;
		int frmLocateHeight = t - 120;
		if (frmLocateHeight <= 0)
			frmLocateHeight = 0;
		Point p = new Point(frmLocateWidth, frmLocateHeight);
		return p;
	}

	/**
	 * double数字转换为指定小数长度的数字
	 * @param srcdouble 原数字
	 * @param length 长度
	 * @return 指定长度的数字
	 */
	public static double round(double srcdouble, int length) {
		double t = 0d;
		try {
			t = (double) Math.pow(10, length);
		}
		catch (RuntimeException e) {
			e.printStackTrace();
		}
		return (Math.round(srcdouble * t)) / t;
	}

	/**
	 * 验证选中bomline的所有者是否是当前用户
	 * @param selectedLine 选中bomline
	 * @param newsession 系统session
	 * @return 验证结果
	 */
	public static boolean validateOwnringUser(TCComponentBOMLine selectedLine, TCSession newsession) {
		String userName = "";
		String ownerName = "";
		try {
			userName = newsession.getUserName();
			userName = "(" + userName + ")";

			ownerName = selectedLine.getProperty("bl_rev_owning_user");
		}
		catch (TCException e) {
			e.printStackTrace();
			return false;
		}
		if (ownerName.indexOf(userName) < 0) {
			MessageBox messagebox1 = new MessageBox("错误：您非当前选中对象的所有者，不能进行此操作！", "错误...", MessageBox.ERROR);
			messagebox1.setVisible(true);
			return false;
		}
		return true;
	}

	private MessageBox	messagebox1;

	/**
	 * getTimeNum:获得时间字符串
	 * @param @return 设定文件
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static String getTimeNum() {
		String strTime = "";
		try {
			SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			strTime = bartDateFormat.format(new Date());
			System.out.println(strTime);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return strTime;
	}

	/**
	 * getTimeNum:获得时间字符串 例: format为yyyyMMddHHmmssSSS
	 * @param @param format 格式
	 * @param @return 设定文件
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static String getTimeNum(String format) {
		String strTime = "";
		try {
			SimpleDateFormat bartDateFormat = new SimpleDateFormat(format);
			strTime = bartDateFormat.format(new Date());
			System.out.println(strTime);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return strTime;
	}

	/**
	 * 获得格式化时间字符串
	 * @param pDate 日期
	 * @param pFormat 字符串格式
	 * @return 格式化时间字符串 例：pFormat=yyyyMMddHHmmssSSS
	 */
	public static String getFormatTimeNum(Date pDate, String pFormat) {
		String strTime = "";
		try {
			if (pDate == null) { return ""; }
			SimpleDateFormat bartDateFormat = new SimpleDateFormat(pFormat);
			strTime = bartDateFormat.format(pDate);
			System.out.println(strTime);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return strTime;
	}

	/**
	 * 获得当前系统java临时路径
	 * @return 当前系统java临时路径
	 */
	public static String getJavaTmpDir() {
		String filepath = System.getProperty("java.io.tmpdir");
		if (!filepath.endsWith("\\")) {
			filepath = filepath + "\\";
		}
		System.out.println("getJavaTmpDir() - String filepath=" + filepath); //$NON-NLS-1$
		return filepath;
	}

	/**
	 * getLimLenTxtIgnSomeVt:以固定长度拆分可能包含中文的字符串(忽略指定字符),返回矢量数组
	 * @param src 待处理字符串
	 * @param rowLength 长度
	 * @param repStr 分隔符
	 * @param someStr 忽略的字符
	 * @param @return 设定文件
	 * @return Vector 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static Vector getLimLenTxtIgnSomeVt(String src, int rowLength, String repStr, String someStr) {
		// if (logger.isDebugEnabled()) {
		//			logger.debug("getLimLenTxtIgnSomeVt(String, int, String, String) - start"); //$NON-NLS-1$
		// }
		String ret = "";
		String tmpstr = "";
		Vector v = new Vector();
		Vector vt = new Vector();
		Vector vt1 = new Vector();
		if (src != null || UtilApi.length(src) > 0) {
			v = UtilApi.str2vt(src, someStr);
			for (int i = 0; i < v.size(); i++) {
				tmpstr = v.get(i).toString();
				tmpstr = UtilApi.getLimRowLenText(tmpstr, rowLength, repStr);
				tmpstr = UtilApi.nbsp2space(tmpstr);
				if (tmpstr.indexOf(repStr) >= 0) {
					vt = UtilApi.str2vt(tmpstr, repStr);
					for (int j = 0; j < vt.size(); j++) {
						vt1.add(vt.get(j).toString());
					}
				} else {
					vt1.add(tmpstr);
				}
			}
		}

		// if (logger.isDebugEnabled()) {
		//			logger.debug("getLimLenTxtIgnSomeVt(String, int, String, String) - end"); //$NON-NLS-1$
		// }
		return vt1;
	}

	/**
	 * getLimRowLenText:以固定长度拆解可能包含中文的字符串(忽略指定字符)
	 * @param src 待处理字符串
	 * @param rowLength 长度
	 * @param repStr 分隔符
	 * @param @return 添加了分隔符的字符串
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static String getLimRowLenText(String src, int rowLength, String repStr) {
		String ret = "";
		try {
			if (src != null || UtilApi.length(src) > 0) {
				src = UtilApi.nbsp2space(src);
				while (UtilApi.length(src) > 0) {
					if (UtilApi.length(src) > rowLength) {
						ret += UtilApi.substring(src, 0, rowLength) + repStr;
						src = UtilApi.substring(src, rowLength);
					} else {
						ret += src;
						src = "";
					}
				}
			}
		}
		catch (Exception e) {

			e.printStackTrace();
		}
		String returnString = UtilApi.space2nbsp(ret);
		return returnString;
	}

	/**
	 * space2nbsp:空格转换为nbsp
	 * @param pStr 待处理字符串
	 * @param @return 转换后的字符串
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static String space2nbsp(String pStr) {
		if (pStr == null || pStr.length() < 1)
			return pStr;
		String s1 = "";
		for (int i = 0; i < pStr.length(); i++)
			if (pStr.charAt(i) == ' ')
				s1 = s1 + "&nbsp;";
			else s1 = s1 + pStr.charAt(i);
		return s1;
	}

	/**
	 * substring: 拆分字符串
	 * @param source 待处理字符串
	 * @param iniPos 初始化位置
	 * @param endPos 结束为止
	 * @param @return 处理后的字符串
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static String substring(String source, int iniPos, int endPos) {
		String tmpStr = "";
		String tmpStr1 = "";
		String retStr = "";
		int tmpLen = 0;
		int len = 0;
		try {
			len = endPos - iniPos;
			if (iniPos >= UtilApi.length(source))
				return "";
			tmpStr = UtilApi.substr(source, iniPos);
			tmpLen = tmpStr.length();
			tmpStr1 = source.substring(tmpLen);
			if (UtilApi.length(tmpStr1) <= len)
				retStr = tmpStr1;
			else retStr = UtilApi.substr(tmpStr1, len);
		}
		catch (Exception e) {}
		return retStr;
	}

	/*
	 * 截取一段字符的长度,不区分中英文,如果数字不正好，则少取一个字符位
	 * @author patriotlml @param String origin, 原始字符串 @param int len,
	 * 截取长度(一个汉字长度按2算的) @return String, 返回的字符串(0位置到len位置)
	 */
	public static String substr(String source, int len) {
		String retStr = "";
		if (source == null || source.equals(""))
			return "";
		byte[] strByte = new byte[len];
		if (len > UtilApi.length(source))
			return source;
		System.arraycopy(source.getBytes(), 0, strByte, 0, len);
		int count = 0;
		for (int i = 0; i < len; i++) {
			int value = (int) strByte[i];
			if (value < 0) {
				count++;
			}
		}
		if (count % 2 != 0) {
			len = (len == 1) ? ++len : --len;
		}

		try {
			retStr = new String(strByte, 0, len);
		}
		catch (Exception e) {

			try {
				System.arraycopy(source.getBytes(), 0, strByte, 0, len);
				retStr = new String(strByte, 0, len);
			}
			catch (Exception ex) {}
		}
		return retStr;
	}

	/*
	 * 截取一段字符的长度,不区分中英文,如果数字不正好，则少取一个字符位
	 * @author patriotlml @param String origin, 原始字符串 @param int iniPos,
	 * 截取字符串的初始位置 @param int len, 截取长度(一个汉字长度按2算的) @return String,
	 * 返回的字符串(0位置到len位置)
	 */
	public static String substring(String source, int iniPos) {
		String tmpStr = "";
		String tmpStr1 = "";
		String retStr = "";
		int endPos = 0;
		try {
			endPos = UtilApi.length(source);
			tmpStr = UtilApi.substring(source, iniPos, endPos + 1);
		}
		catch (Exception e) {}
		return tmpStr;
	}

	/**
	 * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为2,英文字符长度为1
	 * @param String s ,需要得到长度的字符串
	 * @return int, 得到的字符串长度
	 */
	public static int length(String s) {
		if (s == null)
			return 0;
		char[] c = s.toCharArray();
		int len = 0;
		for (int i = 0; i < c.length; i++) {
			len++;
			if (!isLetter(c[i])) {
				len++;
			}
		}
		return len;
	}

	/**
	 * getLimRowLenTextWithSplitstr: 处理字符串,获得固定长度的字符串,并且替换指定字符串
	 * @param src 待处理字符串
	 * @param rowLength 每行长度
	 * @param repStr 替换的字符串
	 * @param splitStr 分隔符
	 * @param @return 处理后的字符串
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static String getLimRowLenTextWithSplitstr(String src, int rowLength, String repStr, String splitStr) {
		Vector v = new Vector();
		String retStr = "";
		try {
			src = UtilApi.nbsp2space(src);
			v = UtilApi.getLimLenTxtIgnSomeVt(src, rowLength, repStr, splitStr);
			for (int i = 0; i < v.size(); i++) {
				retStr += v.get(i).toString();
				if (i != v.size() - 1)
					retStr += repStr;
			}
		}
		catch (Exception e) {}
		return retStr;
	}

	/**
	 * nbsp2space:(描述这个方法的作用)
	 * @param s
	 * @param @return 设定文件
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static String nbsp2space(String s) {
		if (s == null || s.length() < 1)
			return s;
		String s1 = "";
		if (s.indexOf("&nbsp;") >= 0)
			s1 = UtilApi.ReplaceAll(s, "&nbsp;", " ");
		else s1 = s;
		return s1;
	}

	/**
	 * 判断一个字符是Ascill字符还是其它字符（如汉，日，韩文字符）
	 * @param char c, 需要判断的字符
	 * @return boolean, 返回true,Ascill字符
	 */
	public static boolean isLetter(char c) {
		int k = 0x80;
		return c / k == 0 ? true : false;
	}

	/**
	 * ReplaceAll:替换字符串中的特定字符串
	 * @param source 待处理字符串
	 * @param regex 代替换的字符串
	 * @param replacement 替换的字符串
	 * @param @return 替换后的字符串
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static String ReplaceAll(String source, String regex, String replacement) {
		if (source == null)
			return "";
		String stmp1 = source;
		String stmp2 = "";
		String stmp3 = "";
		int posRegex = stmp1.indexOf(regex);
		int regexLen = regex.length();
		while (posRegex >= 0) {
			stmp2 = stmp1.substring(0, posRegex);
			stmp3 += stmp2 + replacement;
			stmp1 = stmp1.substring(posRegex + regexLen);
			posRegex = stmp1.indexOf(regex);
		}
		return stmp3 += stmp1;
	}

	/**
	 * str2vt:将字符串以指定分隔符分隔,生成矢量数组
	 * @param source 待处理字符串
	 * @param sepStr 分隔符
	 * @param @return 矢量数组
	 * @return Vector 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static Vector str2vt(String source, String sepStr) {
		String stmp1 = source;
		String stmp2 = "";
		String stmp3 = "";
		Vector vt = new Vector();
		int sepPos = 0;
		int sepLen = sepStr.length();
		try {
			do {
				sepPos = stmp1.indexOf(sepStr);
				if (sepPos < 0 || sepLen == 0) {
					vt.add(stmp1);
					return vt;
				}
				stmp2 = stmp1.substring(0, sepPos);
				vt.add(stmp2);
				stmp1 = stmp1.substring(sepPos + sepLen);
			} while (stmp1.indexOf(sepStr) >= 0);
			vt.add(stmp1);
		}
		catch (Exception e) {}
		return vt;
	}

	/**
	 * str2vt:将字符串以指定分隔符分隔,生成二维矢量数组
	 * @param source 待处理字符串
	 * @param sepStr1 分隔符一
	 * @param sepStr2 分隔符二
	 * @param @return 矢量数组
	 * @return Vector 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static Vector str2vt(String source, String sepStr1, String sepStr2) {

		if (source.indexOf(sepStr1) < 0) { return str2vt(source, sepStr2); }
		Vector vector = str2vt(source, sepStr1);
		Vector vector1 = new Vector();
		for (int i = 0; i < vector.size(); i++)
			vector1.addElement(str2vt((String) vector.get(i), sepStr2));
		return vector1;
	}

	/**
	 * str2vt:将字符串以指定分隔符分隔,生成三维矢量数组
	 * @param source 待处理字符串
	 * @param sepStr1 分隔符一
	 * @param sepStr2 分隔符二
	 * @param sepStr3 分隔符三
	 * @param @return 矢量数组
	 * @return Vector 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static Vector str2vt(String source, String sepStr1, String sepStr2, String sepStr3) {
		if (source.indexOf(sepStr1) < 0) { return str2vt(source, sepStr2, sepStr3); }
		Vector vector = str2vt(source, sepStr1);
		Vector vector1 = new Vector();
		for (int i = 0; i < vector.size(); i++) {
			vector1.addElement(str2vt((String) vector.get(i), sepStr2, sepStr3));
		}
		return vector1;
	}

	/**
	 * str2vt:将字符串以指定分隔符分隔,生成四维矢量数组
	 * @param source 待处理字符串
	 * @param sepStr1 分隔符一
	 * @param sepStr2 分隔符二
	 * @param sepStr3 分隔符三
	 * @param sepStr4 分隔符四
	 * @param @return 矢量数组
	 * @return Vector 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static Vector str2vt(String source, String sepStr1, String sepStr2, String sepStr3, String sepStr4) {
		if (source.indexOf(sepStr1) < 0) { return str2vt(source, sepStr2, sepStr3, sepStr4); }
		Vector vector = str2vt(source, sepStr1);
		Vector vector1 = new Vector();
		for (int i = 0; i < vector.size(); i++)
			vector1.addElement(str2vt((String) vector.get(i), sepStr2, sepStr3, sepStr4));
		return vector1;
	}

	/**
	 * getPreferenceHashMap:获得系统配置项属性值
	 * @param session tcsession
	 * @param scope 作用域
	 * @param pid 选项名
	 * @param @return 设定文件
	 * @return HashMap<String,String> 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static String[] getPreference(TCSession session, int scope, String pid) {
		try {
			String options[] = session.getPreferenceService().getStringArray(scope, pid);
			return options;
		}
		catch (Exception e) {
			System.out.println("没找到选项:" + pid);
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * getPreferenceHashMap:获得系统配置项属性值
	 * @param session tcsession
	 * @param scope 作用域
	 * @param pid 选项名
	 * @param @return 设定文件
	 * @return HashMap<String,String> 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public static HashMap<String, String> getPreferenceHashMap(TCSession pSession, int pScope, String pid) {
		HashMap<String, String> hm = new HashMap<String, String>();
		try {
			String[] options = getPreference(pSession, pScope, pid);
			if (options == null || options.length == 0) {
				System.out.println("processRows(Vector, HashMap) - ************ 没有配置" + pid + "选项!");
			}
			String key = "";
			String value = "";
			for (int i = 0; i < options.length; i++) {
				if (options[i].indexOf("=") > 0) {
					key = options[i].substring(0, options[i].indexOf("="));
					value = options[i].substring(options[i].indexOf("=") + 1);
					hm.put(key, value);
				} else {
					hm.put("" + i, options[i]);
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return hm;
	}

	public static InterfaceAIFComponent[] getQueryList(TCSession session, String queryName, String as[], String as1[]) {
		System.out.println("begin=====query【" + queryName + "】");
		InterfaceAIFComponent[] components = null;
		// 使用查询构建器：
		try {
			components = session.search(queryName, as, as1);
		}
		catch (Exception e) {
			e.printStackTrace();
			MessageBox.post("querying 【" + queryName + "】   error！" + e.getMessage(), "INFO", MessageBox.INFORMATION);
		}
		System.out.println("end=====query【" + queryName + "】");
		return components;
	}

	// ======利用系统查询构建器查询数据集=======
	public static TCComponent[] searchComponentCollection(TCSession session, String queryName, String as[], String as1[]) {
		TCComponent returnaimancomponent[] = null;
		try {
			TCTextService textService = session.getTextService();
			TCComponentQueryType querytype = (TCComponentQueryType) session.getTypeComponent("ImanQuery");
			TCComponentQuery query = (TCComponentQuery) querytype.find(queryName);
			querytype.clearCache();
			if(query == null){
				System.out.println("未找到查询构建器:" + queryName);
				return null;
			}
			int asLen = as.length;
			String tValue = "";
			for(int ix = 0; ix < asLen; ix++){
				tValue = textService.getTextValue(as[ix]);
				if(tValue != null && !tValue.equals("")){
					as[ix] = tValue;
				}
			}
			TCComponentContextList list = query.getExecuteResultsList(as, as1);
			if (list != null && list.getListCount() > 0) {
				int count = list.getListCount();
				returnaimancomponent = new TCComponent[count];
				for (int i = 0; i < count; i++) {
					AIFComponentContext context = list.get(i);
					returnaimancomponent[i] = (TCComponent) context.getComponent();	
				}
				return returnaimancomponent;
			} else {
				return null;
			}
		}
		catch (TCException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 将特定格式的字符串转化成日期
	 * @param str 日期字符串
	 * @param pattern 字符串模式，如：yyyy年M月d日，yyyy-MM-dd等
	 * @return 日期对象
	 * @throws java.text.ParseException
	 */
	public static Date convertStringToDate(String pattern, String str) {
		Date date = null;
		try {
			if (str.equals("") || str == null)
				return date;

			SimpleDateFormat formatter = new SimpleDateFormat(pattern);
			date = formatter.parse(str);
		}
		catch (Exception e) {
			e.printStackTrace();
		}

		return date;
	}

	private static HashMap	msghm	= new HashMap();

	public static synchronized void setMsg(String key, String msg) {
		msghm.put(key, msg);
	}

	public static synchronized String getMsg(String key) {
		String msg = "";
		try {
			if (msghm.get(key) == null) {
				msg = "";
			} else {
				msg = "" + msghm.get(key);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}
	
	/**
	  * getOptionValues:: 读取首选项值，并且填充到comboBox中
	  * @param JComboBox
	  * @param String
	  * @author liqz 
	  * @since 20111215
	  * */
	 public static void getOptionValues(TCSession pSession,JComboBox cbo,String strOptionName){
		 String[] arrValue= pSession.getPreferenceService().getStringArray(4, strOptionName);
			cbo.addItem("");
			for (int i = 0; i < arrValue.length; i++) {
				cbo.addItem(arrValue[i]);
			}
	 }

	/**
	 * getDaysBetween:：计算间隔的天数
	 * 
	 * @param Date
	 *            较早的日期
	 * @param Date
	 *            较晚的日期
	 * @return int 间隔的天数
	 */
	public static int getDaysBetween(Date dtFormer, Date dtLater) {
		GregorianCalendar vFormer = (GregorianCalendar) GregorianCalendar.getInstance();
		GregorianCalendar vLatter = (GregorianCalendar) GregorianCalendar.getInstance();

		vFormer.setTime(dtFormer);
		vLatter.setTime(dtLater);

		vFormer.set(Calendar.MILLISECOND, 0);
		vFormer.set(Calendar.SECOND, 0);
		vFormer.set(Calendar.MINUTE, 0);
		vFormer.set(Calendar.HOUR_OF_DAY, 0);
		vLatter.set(Calendar.MILLISECOND, 0);
		vLatter.set(Calendar.SECOND, 0);
		vLatter.set(Calendar.MINUTE, 0);
		vLatter.set(Calendar.HOUR_OF_DAY, 0);

		int iCounter = 0;
		while (vFormer.before(vLatter)) {
			vFormer.add(Calendar.DATE, 1);
			iCounter++;
		}
		// iCounter = iCounter + 1;
		return iCounter;
	}
}