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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.teamcenter.rac.aif.AbstractAIFUIApplication;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.kernel.TCComponentContextList;
import com.teamcenter.rac.kernel.TCComponentQuery;
import com.teamcenter.rac.kernel.TCComponentQueryType;
import com.teamcenter.rac.kernel.TCComponentUser;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.kernel.TCTextService;

public class GetAllUserIds {
	private AbstractAIFUIApplication	pseapp;
	/**
	 * TC中定义的Query对象
	 */
	private String						findType	= "__WEB_find_user";
	/**
	 * TC中定义的Query对象的搜索条件中的用户本地化键
	 */
	private String						str_key		= "UserId";
	/**
	 * TC中定义的Query对象中本地化键的取值
	 */
	private String						str_value	= "*";
	/**
	 * 用来包装用户信息
	 */
	private List<String>				allUsers	= null;
	private Map<String, String>			userMap;

	public GetAllUserIds(AbstractAIFUIApplication app) {
		System.out.println("_______________GetAllUserIds________");
		this.pseapp = app;
		userMap = new HashMap<String, String>();
		allUsers = new ArrayList<String>();
		getAllUser();
	}

	/**
	 * getAllUser:通过查询构建器得到所有的用户
	 * @param @return 设定文件
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public void getAllUser() {
		System.out.println("__________getAllUser__________");
		TCSession session = (TCSession) pseapp.getSession();
		InterfaceAIFComponent components[] = null;
		TCComponentUser user = null;
		try {
			TCTextService textService = session.getTextService();
			TCComponentQueryType querytype = (TCComponentQueryType) session.getTypeComponent("ImanQuery");
			TCComponentQuery query = (TCComponentQuery) querytype.find(findType);
			//querytype.clearCache();
			String as[] = { textService.getTextValue(str_key) };
			String as1[] = new String[1];
			as1[0] = str_value;
			//query.clearCache();
			TCComponentContextList list = query.getExecuteResultsList(as, as1);
			System.out.println("list = "+list);
			if (list != null) {
				int count = list.getListCount();
				System.out.println("count = " + count);
				if(count > 0){
					components = new InterfaceAIFComponent[count];
					for (int i = 0; i < count; i++) {
						if(list.get(i) != null){
							components[i] = list.get(i).getComponent();						
						}
					}
				}
			} else {
				System.out.println("__WEB_find_user==null");
			}
		}
		catch (TCException e) {
			e.printStackTrace();
		}
		for (int i = 0; i < components.length; i++) {
			if (components[i] instanceof TCComponentUser) {
				user = (TCComponentUser) components[i];
				try {
					String userid = user.getUserId().toString();
					String userName = user.getProperty("user_name");
					userMap.put(userid + "(" + userName + ")", user.toString());
					allUsers.add(userid + "(" + userName + ")");
				}
				catch (TCException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static List<String>	userIDs	= new ArrayList<String>();

	public static synchronized void setUsers(List<String> users) {
		userIDs = users;
	}

	public static synchronized List<String> getUsers() {
		return userIDs;
	}

	public List<String> getAllUsers() {
		return allUsers;
	}

	public Map<String, String> getUserMap() {
		return userMap;
	}
}
