package org.postgresforest.tool.commandline.action;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;

import org.postgresforest.tool.action.ChangePriority;
import org.postgresforest.tool.action.PartitionAction;
import org.postgresforest.tool.action.TableList;
import org.postgresforest.tool.bean.ColumnInfo;
import org.postgresforest.tool.bean.PriorityInfo;
import org.postgresforest.tool.bean.ServerInfo;
import org.postgresforest.tool.bean.ServerPartition2Info;
import org.postgresforest.tool.bean.ServerPriorityInfo;
import org.postgresforest.tool.bean.TableInfo;
import org.postgresforest.tool.db.GSCdata;
import org.postgresforest.tool.util.CommandParser;
import org.postgresforest.tool.util.MessagesCommandLine;

/**
 * テーブルへのコマンドラインツール用のアクションを提供するクラス
 */
public class TableActions {

	private static final String REST_MODE = "1"; //$NON-NLS-1$
	private static final String NOREST_MODE = "0"; //$NON-NLS-1$
	private static final String DEFAULT_HASH = "default"; //$NON-NLS-1$
	private static final String HASH_OPTION_NAME = "hashname"; //$NON-NLS-1$
	private static final String REST_OPTION_NAME = "rest"; //$NON-NLS-1$

	protected static DecimalFormat m_dcFmt = new DecimalFormat("00"); //$NON-NLS-1$
	/**
	 * パーティション化1設定
	 * @param cp コマンド解析結果
	 * @param gsc GSCヘの接続情報
	 * @param dbName データベース名
	 */
	public void part(CommandParser cp, GSCdata gsc, String dbName) {

		boolean isPart2 = false;
		// テーブル名設定
		String tableName = (String)cp.getTables().get(0);
		String hashName = null;
		boolean  isDataRestore = false;
		ArrayList paramNames = cp.getParamName();
		ArrayList paramValues = cp.getParamValue();
		for (int i=0; i<paramNames.size(); i++) {
			String paramName = (String)paramNames.get(i);
			if (paramName.compareToIgnoreCase(HASH_OPTION_NAME) == 0) {
				// パーティション関数の設定
				hashName = (String)paramValues.get(i);
				if (hashName.equals(DEFAULT_HASH)) {
					hashName = null;
				}
			} else if (paramName.compareToIgnoreCase(REST_OPTION_NAME) == 0) {
				// 移行モードの設定
				String shiftFlag = (String)paramValues.get(i);
				if (shiftFlag.equals(REST_MODE)) {
					isDataRestore = true;
				} else if (shiftFlag.equals(NOREST_MODE)) {
					isDataRestore = false;
				} else {
					System.out.println(MessagesCommandLine.getString("cui.error.commandparser.param.invalid")); //$NON-NLS-1$
					return;
				}
			} else {
				System.out.println(MessagesCommandLine.getString("cui.error.commandparser.param.invalid")); //$NON-NLS-1$
				return;
			}
		}
		// テーブル情報の取得
		TableInfo tableInfo = getTableInfo(gsc, dbName, tableName);
		if (tableInfo == null) {
			System.out.println(MessagesCommandLine.getString("cui.error.table.nosuchtable", new Object[]{tableName})) ;			 //$NON-NLS-1$
			return ;
		}
		
		//テーブルのパーティション化
		if (partTable(cp, gsc, dbName, tableInfo, hashName, isDataRestore, isPart2)) {
			System.out.println(MessagesCommandLine.getString("cui.message.table.part", new Object[]{tableName}) ); //$NON-NLS-1$
			return;
		}
		
		System.out.println(MessagesCommandLine.getString("cui.error.table.part", new Object[]{tableName}) ); //$NON-NLS-1$

	}
	
	/**
	 * パーティション化2設定
	 * @param cp コマンド解析結果
	 * @param gsc GSCヘの接続情報
	 * @param dbName データベース名
	 */
	public void part2(CommandParser cp, GSCdata gsc, String dbName) {
		boolean isPart2 = true;
		// テーブル名設定
		String tableName = (String)cp.getTables().get(0);
		String hashName = null;
		boolean  isDataRestore = false;
		ArrayList paramNames = cp.getParamName();
		ArrayList paramValues = cp.getParamValue();
		for (int i=0; i<paramNames.size(); i++) {
			String paramName = (String)paramNames.get(i);
			if (paramName.compareToIgnoreCase(HASH_OPTION_NAME) == 0) {
				// パーティション関数の設定
				hashName = (String)paramValues.get(i);
				if (hashName.equals(DEFAULT_HASH)) {
					hashName = null;
				}
			} else if (paramName.compareToIgnoreCase(REST_OPTION_NAME) == 0) {
				// 移行モードの設定
				String shiftFlag = (String)paramValues.get(i);
				if (shiftFlag.equals(REST_MODE)) {
					isDataRestore = true;
				} else if (shiftFlag.equals(NOREST_MODE)) {
					isDataRestore = false;
				} else {
					System.out.println(MessagesCommandLine.getString("cui.error.commandparser.param.invalid")); //$NON-NLS-1$
					return;
				}
			} else {
				System.out.println(MessagesCommandLine.getString("cui.error.commandparser.param.invalid")); //$NON-NLS-1$
				return;
			}
		}
		// テーブル情報の取得
		TableInfo tableInfo = getTableInfo(gsc, dbName, tableName);
		if (tableInfo == null) {
			System.out.println(MessagesCommandLine.getString("cui.error.table.nosuchtable", new Object[]{tableName}) );			 //$NON-NLS-1$
			return ;
		}
		
		// パーティション2化テーブルのパーティションテーブル配置設定の取得
		ArrayList setServerList = cp.getServer();
		ArrayList part2NoList = cp.getPart2NoList();
		
		
		ArrayList servers = tableInfo.getServer();
		int partCount =  Integer.parseInt((String)cp.getPartCnt().get(0));		
		// インスタンス毎にパーティションテーブルの配置有無を設定
		for (int i = 0; i < servers.size(); i++) {
			ServerInfo servInfo = (ServerInfo)servers.get(i);
			ServerPartition2Info partition2Info = new ServerPartition2Info(servInfo, partCount);
			servers.set(i, partition2Info);
			for (int j = 0; j < setServerList.size(); j++) {
				int setServerId = Integer.parseInt((String)setServerList.get(j)); 
				//パーティションテーブル毎の処理
				if ( setServerId == servInfo.getId()) {
					ArrayList part2No = (ArrayList)part2NoList.get(j);
					// 全てのパーティションテーブル配置情報の初期化
					for (int k = 0 ; k < partCount; k++) {
						partition2Info.setMakeInstance(k, Boolean.FALSE);
					}
					// 指定されたパーティションテーブル配置情報の設定
					for (int k = 0 ; k < part2No.size(); k++) {
						int partNo = Integer.parseInt((String)part2No.get(k));
						partition2Info.setMakeInstance(partNo, Boolean.TRUE);
					}
					servers.set(i, partition2Info);
				}
			}
		}
		// パーティションテーブル配置設定の格納
		tableInfo.setServer(servers);
		//テーブルのパーティション化
		if (partTable(cp, gsc, dbName, tableInfo, hashName, isDataRestore, isPart2)) {
			System.out.println(MessagesCommandLine.getString("cui.message.table.part2", new Object[]{tableName}) ); //$NON-NLS-1$
			return;
		}		
		
		System.out.println(MessagesCommandLine.getString("cui.error.table.part2", new Object[]{tableName})); //$NON-NLS-1$
	}

	/**
	 * パーティション化解除
	 * @param cp コマンド解析結果
	 * @param gsc GSCヘの接続情報
	 * @param dbName データベース名
	 */
	public void unpart(CommandParser cp, GSCdata gsc, String dbName) {
		// テーブル名設定
		String tableName = (String)cp.getTables().get(0);		
		// テーブル情報の取得
		TableInfo tableInfo = getTableInfo(gsc, dbName, tableName);		
		if (tableInfo == null) {
			System.out.println(MessagesCommandLine.getString("cui.error.table.nosuchtable", new Object[]{tableName}) );			 //$NON-NLS-1$
			return;
		}
		
		PartitionAction exec = new PartitionAction(gsc,dbName);
		try {
			if(exec.del(tableInfo)){
				System.out.println(MessagesCommandLine.getString("cui.message.table.unpart", new Object[]{tableName}));	 //$NON-NLS-1$
				return;
			}
		} catch (Exception e) {
			System.err.println(e);
		}
		System.out.println(MessagesCommandLine.getString("cui.error.table.unpart", new Object[]{tableName})) ; //$NON-NLS-1$
	}

	/**
	 * リパーティション化設定
	 * @param cp コマンド解析結果
	 * @param gsc GSCヘの接続情報
	 * @param dbName データベース名
	 */
	public void repart(CommandParser cp, GSCdata gsc, String dbName) {
		//To Do	
		//PL/Javaを入れてテストする必要あり
		// パーティション数設定
		int partCount =  Integer.parseInt((String)cp.getPartCnt().get(0));		
		// テーブル名設定
		String tableName = (String)cp.getTables().get(0);		
		// テーブル情報の取得
		TableInfo tableInfo = getTableInfo(gsc, dbName, tableName);	
		if (tableInfo == null) {
			System.out.println(MessagesCommandLine.getString("cui.error.table.nosuchtable", new Object[]{tableName}) );			 //$NON-NLS-1$
			return;
		}
		// パーティション数の妥当性チェック
		int serverSize = tableInfo.getServer().size();
		if (partCount < 2 || partCount > serverSize) {
			System.out.println(MessagesCommandLine.getString("cui.error.part.partno.invalid", new Object[]{new Integer(serverSize)}) ); //$NON-NLS-1$		
			return ;
		}
		// パーティション関数の設定
		String hashName = null;
		ArrayList paramNames = cp.getParamName();
		ArrayList paramValues = cp.getParamValue();
		for (int i=0; i<paramNames.size(); i++) {
			String paramName = (String)paramNames.get(i);
			if (paramName.compareToIgnoreCase(HASH_OPTION_NAME) == 0) {
				hashName = (String)paramValues.get(i);
				if (hashName.equals(DEFAULT_HASH)) {
					hashName = null;
				}
			} else {
				System.out.println(MessagesCommandLine.getString("cui.error.commandparser.param.invalid")); //$NON-NLS-1$
				return;
			}
		}


		// パーティション1テーブル以外の場合
		if (tableInfo.getPartType() != 1){
			System.err.println(MessagesCommandLine.getString("cui.error.table.repart.notpart1", new Object[]{tableName}));	 //$NON-NLS-1$
			return;
		} else {
			PartitionAction rePartition = new PartitionAction(gsc,dbName);
			boolean result = false;
			result = rePartition.repartition(partCount, tableInfo, hashName);
			if(result){
				System.out.println(MessagesCommandLine.getString("cui.message.table.repart", new Object[]{tableName})); //$NON-NLS-1$
				return;
			}
			System.err.println(MessagesCommandLine.getString("cui.error.table.repart", new Object[]{tableName})); //$NON-NLS-1$			
		}
	}

	/**
	 * テーブル一覧表示
	 * @param gsc GSCヘの接続情報
	 * @param dbName データベース名
	 */
	public void showTable(GSCdata gsc, String dbName) {
		//Table情報取得
		ArrayList tableList = null;
		TableList tableListAction = new TableList(gsc);
		try {
			tableList =  tableListAction.getTableList(dbName);
		} catch (Exception e) {
			System.out.println(MessagesCommandLine.getString("cui.error.table.show")) ; //$NON-NLS-1$
			System.err.println(e);
			return;
		}

		//Table情報表示
		System.out.println(MessagesCommandLine.getString("cui.message.table.show")) ; //$NON-NLS-1$
		Iterator it = tableList.iterator();
		System.out.println(" TABLENAME \t| TABLETYPE \t| PARTITION_FUNC \t| STATUS"); //$NON-NLS-1$
		System.out.println("----------------------------------------------"); //$NON-NLS-1$
		while (it.hasNext()) {
			TableInfo tableInfo = (TableInfo)it.next();
			System.out.print(tableInfo.getName() + "\t| "); //$NON-NLS-1$
			System.out.print(convertTableType(tableInfo.getPartType()) + "\t| "); //$NON-NLS-1$
			System.out.print(convertTableHash(tableInfo.getHashName(), tableInfo) + "\t| ");
			System.out.println(convertTableStatus(tableInfo.getStatus()));
		}
	}

	/**
	 * テーブル情報表示
	 * @param cp コマンド解析結果
	 * @param gsc GSCヘの接続情報
	 * @param dbName データベース名
	 */
	public void showTableColumn(CommandParser cp, GSCdata gsc, String dbName) {
		String tableName = (String)cp.getTables().get(0);
		this.showTableColumn(tableName, gsc, dbName);
	}

	/**
	 * テーブル情報表示
	 * @param tableName テーブル名
	 * @param gsc GSCヘの接続情報
	 * @param dbName データベース名
	 */
	public void showTableColumn(String tableName, GSCdata gsc, String dbName) {
		TableInfo tableInfo = getTableInfo(gsc, dbName, tableName);
		if (tableInfo == null) {
			System.out.println(MessagesCommandLine.getString("cui.error.table.nosuchtable", new Object[]{tableName}) );			 //$NON-NLS-1$
			return;
		}
		//Column情報表示
		System.out.println(MessagesCommandLine.getString("cui.message.tablecolumn.show", new Object[]{tableName}) ); //$NON-NLS-1$
		Iterator it = tableInfo.getColumn().iterator();
		System.out.println(" PARTITION_KEY\t| COLUMNNAME\t| DATATYPE"); //$NON-NLS-1$
		System.out.println("----------------------------------------------"); //$NON-NLS-1$
		while (it.hasNext()) {
			ColumnInfo columnInfo = (ColumnInfo)it.next();
			System.out.print(convertPartKey(columnInfo.getPartition()) + "\t| "); //$NON-NLS-1$
			System.out.print(columnInfo.getName()  + "\t| "); //$NON-NLS-1$
			System.out.println(columnInfo.getTypeDetail());
		}
	}
	/**
	 * 優先度設定
	 * @param cp コマンド解析結果
	 * @param gsc GSCヘの接続情報
	 * @param dbName データベース名
	 */
	public void setPriority(CommandParser cp, GSCdata gsc, String dbName) {
		
		String tableName = (String)cp.getTables().get(0);
		String setPartNo = (String)cp.getPartNo().get(0);
		ArrayList setServerList = cp.getServer();

		//テーブル一覧を取得
		TableInfo tableInfo = getTableInfo(gsc, dbName, tableName);
		if (tableInfo == null) {
			System.out.println(MessagesCommandLine.getString("cui.error.table.nosuchtable", new Object[]{tableName}) );			 //$NON-NLS-1$
			return;
		}
		
		// 優先度の設定
		TableList tableList = new TableList(gsc);
		try {
			TableInfo priorityTableInfo = tableList.getTableInfo(dbName, tableInfo);
			ArrayList priorityServerList = priorityTableInfo.getServer();
			//指定されたサーバの数が、インスタンス数より多い場合
			if (setServerList.size() > priorityServerList.size()) {
				System.out.println(MessagesCommandLine.getString("cui.error.priority.set.invalid", new Object[]{tableName}) );	 //$NON-NLS-1$
				return;
			}
			int setPriorityNum = 0; // テーブル優先度に実際に設定する数
			
			Iterator it = priorityServerList.iterator();
			// インスタンス毎の処理
			while (it.hasNext()) {
				ServerPriorityInfo serverPriorityInfo = (ServerPriorityInfo)it.next();
				int serverId = serverPriorityInfo.getId();
				for (int i = 0 ; i < setServerList.size() ; i++) {
					int setServerId = Integer.parseInt((String)setServerList.get(i));
					if (serverId == setServerId) {				
						// パーティションテーブル毎の処理
						ArrayList priorityInfoList = serverPriorityInfo.getPartition();
						Iterator it2 = priorityInfoList.iterator();
						while (it2.hasNext()) {
							PriorityInfo priorityInfo = (PriorityInfo)it2.next();
							int partNo =  priorityInfo.getPartitionNo().intValue();
							if (partNo == Integer.parseInt(setPartNo)) {
								priorityInfo.setPriority(new Integer(i));
								setPriorityNum++;
							}
						}
					}
				}
			}
			// 指定されたサーバの数が、実際にテーブルが配置されているサーバ数と異る場合
			if (setServerList.size() != setPriorityNum) {
				System.out.println(MessagesCommandLine.getString("cui.error.priority.set.invalid", new Object[]{tableName}) );	 //$NON-NLS-1$
				return;
			}			
		} catch (Exception e) {
			System.out.println(MessagesCommandLine.getString("cui.error.priority.set", new Object[]{tableName}) ); //$NON-NLS-1$
			e.printStackTrace();
			return;
		}
		

		//優先度設定の妥当性チェック
		if (!validatePriority(tableInfo)) {
			System.out.println(MessagesCommandLine.getString("cui.error.priority.set.invalid", new Object[]{tableName}) );	 //$NON-NLS-1$
			return;
		} 
		
		//優先度の設定
		ChangePriority changePriority = new ChangePriority(gsc);
		try {
			if (changePriority.execute(dbName, tableInfo)) {
				System.out.println(MessagesCommandLine.getString("cui.message.priority.set", new Object[]{tableName})) ;			 //$NON-NLS-1$
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println(MessagesCommandLine.getString("cui.error.priority.set", new Object[]{tableName}) ); //$NON-NLS-1$
	}

	/**
	 * 優先度表示(show priority)
	 * @param gsc GSCヘの接続情報
	 * @param dbName データベース名
	 */
	public void showPriority(CommandParser cp, GSCdata gsc, String dbName) {
		String tableName = (String)cp.getTables().get(0);	
		this.showPriority(tableName, gsc, dbName);
	}

	/**
	 * 優先度表示(\fp)
	 * @param gsc GSCヘの接続情報
	 * @param dbName データベース名
	 */
	public void showPriority(String tableName, GSCdata gsc, String dbName) {
		TableInfo tableInfo = getTableInfo(gsc, dbName, tableName);
		if (tableInfo == null) {
			System.out.println(MessagesCommandLine.getString("cui.error.table.nosuchtable", new Object[]{tableName}) );			 //$NON-NLS-1$
			return;
		}

		ArrayList columnList = new ArrayList();
		for (int i = 0; i < tableInfo.getPartCount(); i++) {
			columnList.add(tableName + "_" + m_dcFmt.format(i)); //$NON-NLS-1$
		}
		
		System.out.println(MessagesCommandLine.getString("cui.message.priority.show", new Object[]{tableName}) ); //$NON-NLS-1$
		
		TableList tableList = new TableList(gsc);
		try {
			TableInfo priorityTableInfo = tableList.getTableInfo(dbName, tableInfo);
			ArrayList priorityServerList = priorityTableInfo.getServer();
			Iterator it = priorityServerList.iterator();

			// カラム名の表示
			System.out.print("SERVERID"); //$NON-NLS-1$
			if (priorityTableInfo.isPartition()) {
				for ( int i = 0; i< columnList.size(); i++ ) {
					System.out.print("\t| " + columnList.get(i) + " ");			 //$NON-NLS-1$ //$NON-NLS-2$
				}
				System.out.println();
			} else {
				System.out.println("\t| " + tableName);							 //$NON-NLS-1$
			}
			System.out.println("----------------------------------------------"); //$NON-NLS-1$
			
			// インスタンス毎の処理
			while (it.hasNext()) {
				ServerPriorityInfo serverPriorityInfo = (ServerPriorityInfo)it.next();
				int serverId = serverPriorityInfo.getId();
				System.out.print(serverId);
				if (priorityTableInfo.isPartition()) {
					// パーティションテーブル毎の処理
					for (int i = 0; i < columnList.size(); i++ ) {
						PriorityInfo priorityInfo = serverPriorityInfo.getPartition(i);
						if (priorityInfo != null) {
							int priority = priorityInfo.getPriority().intValue();
							System.out.print( "\t| " + priority ); //$NON-NLS-1$
						} else {
							System.out.print( "\t|  " );							 //$NON-NLS-1$
						}
					}
					System.out.println();	
				} else {
					System.out.println("\t| not partitioned");						 //$NON-NLS-1$
				}
			}
		} catch (Exception e) {
			// TODO 自動生成された catch ブロック
			System.out.println(MessagesCommandLine.getString("cui.error.priority.show", new Object[]{tableName}) ); //$NON-NLS-1$
			e.printStackTrace();
		}

	}

	private boolean partTable(CommandParser cp, GSCdata gsc, String dbName,
			TableInfo tableInfo, String hashName, boolean isDataRestore, boolean isPart2) {
		boolean ret = false;
		
		// テーブル設定の取得
		ArrayList partColumns = cp.getKeyCol();
		int partCount =  Integer.parseInt((String)cp.getPartCnt().get(0));
		// パーティション数の妥当性チェック
		int serverSize = tableInfo.getServer().size();
		if (partCount < 2 || partCount > serverSize) {
			System.out.println(MessagesCommandLine.getString("cui.error.part.partno.invalid", new Object[]{new Integer(serverSize)}) ); //$NON-NLS-1$		
			return ret;
		}

		// パーティション属性の設定
		for (Iterator it = partColumns.iterator(); it.hasNext();) {
			String partColumnName = (String)it.next();
			for (Iterator it2 = tableInfo.getColumn().iterator(); it2.hasNext();) {
				ColumnInfo columnInfo = (ColumnInfo) it2.next();
				String columnName = columnInfo.getName();
				if(columnName.compareToIgnoreCase(partColumnName) == 0){
					columnInfo.setPartition(true);
				}
			}
		}
		//入力チェック
		if (!validate(tableInfo))
			return ret;

		//パーティション化2
		if (isPart2) {
			tableInfo.setPartType(TableInfo.TYPE_PARTITION_2);
		} else {
			tableInfo.setPartType(TableInfo.TYPE_PARTITION);
		}
		
		PartitionAction makePartition = new PartitionAction(gsc,dbName);
		boolean result = false;
		try {
			result = makePartition.make(isDataRestore, partCount, tableInfo, hashName);
			if (result){
				ret = true;
			} 
			return ret;
		} catch (Exception e) {
			System.err.println(e);
		}
		return ret;
	}
	
	/**
	 * パーティション化の入力チェック
	 * @param tableInfo テーブル情報
	 */
	private boolean validate(TableInfo tableInfo) {

		//パーティション項目選択チェック
		boolean isChecked = false;

		for (Iterator iter = tableInfo.getColumn().iterator(); iter.hasNext();) {
			ColumnInfo columnInfo = (ColumnInfo) iter.next();
			if (columnInfo.isPartition()) {
				isChecked = true;
				break;
			}
		}

		return isChecked;

	}
	
	
	private boolean validatePriority(TableInfo tableInfo) {
		int m_partCount = tableInfo.getPartCount();
		ArrayList m_servers = tableInfo.getServer();
		boolean result = true;
		
		//優先順チェック	
		for (int partNo = 0; partNo < m_partCount; partNo++) {
			ArrayList priorityNoList = new ArrayList();
			//優先順配列を作成
			for (Iterator iterator = m_servers.iterator(); iterator.hasNext();) {		
				ServerPriorityInfo svrPriorityInfo =
					(ServerPriorityInfo) iterator.next();

				PriorityInfo priorityInfo =	svrPriorityInfo.getPartition(partNo);
				if(priorityInfo != null){
					priorityNoList.add(priorityInfo.getPriority());
				}
			}

			//優先順配列をチェック
			for (int priority = 0; priority < priorityNoList.size(); priority++) {
				boolean check = false;
				boolean duplicate = false;
				for (int i = 0; i < priorityNoList.size(); i++) {
					Integer priorityNo = (Integer)priorityNoList.get(i);
					if (priority == priorityNo.intValue()) {
						if(check){
							duplicate = true;
							break;
						}
						check = true;
					}
				}
				if (duplicate) {
					System.out.println(MessagesCommandLine.getString("errors.duplicate", m_dcFmt.format(partNo))); //$NON-NLS-1$
					result = false;
					break;
				}
				if (!check) {
					String[] param = {m_dcFmt.format(partNo),Integer.toString(priority)};
					System.out.println(MessagesCommandLine.getString("errors.priority", param)); //$NON-NLS-1$
					result = false;
					break;
				}
			}
		}
		
		
		return result;
	}

	
	/**
	 * テーブル情報取得
	 * 指定したテーブル名のTableInfoオブジェクトを取得する。
	 * @param cp コマンド解析結果
	 * @param gsc GSCヘの接続情報
	 * @param tableName テーブル名
	 */
	private TableInfo getTableInfo(GSCdata gsc, String dbName, String tableName) {
		TableInfo retTableInfo = null;

		TableList tableListAction = new TableList(gsc);
		try {
			ArrayList tableList =  tableListAction.getTableList(dbName);
			for (Iterator it = tableList.iterator(); it.hasNext();) {
				TableInfo tableInfo = (TableInfo) it.next();
				if( tableName.equals(tableInfo.getName()) ){
    				TableInfo tableInfo2 =  tableListAction.getTableInfo(dbName, tableInfo);
    				retTableInfo = tableInfo2;
				}
			}
		} catch (Exception e) {
			System.err.println(e);
		}
		return retTableInfo;
	}
	
	private String convertTableType(int type) {
		String ret = "";
		if (type == 0) {
			ret = MessagesCommandLine.getString("form.partition.type0");
		} else if (type == 1){
			ret = MessagesCommandLine.getString("form.partition.type1");
		} else if (type == 2){
			ret = MessagesCommandLine.getString("form.partition.type2");
		}
		return ret;
	}
	
	private String convertTableHash(String hash, TableInfo tableInfo) {
		String ret = "";
		if (tableInfo.getPartType() == 0) {
			ret = "";
		} else if ((tableInfo.getPartType() != 0) && (hash == null)) {
			ret = MessagesCommandLine.getString("form.partition.hash.default");
		} else {
			ret = hash;
		}
		return ret;
	}
	
	private String convertPartKey(boolean key) {
		String ret = " ";
		if (key) {
			ret = " * ";
		} else if (!key){
			ret = "   ";
		}
		return ret;
	}
	
	
	private String convertTableStatus(int status) {
		String ret = "";
		if (status == 1) {
			ret = MessagesCommandLine.getString("form.admintree.status.ng");
		} else if (status == 0){
			ret = MessagesCommandLine.getString("form.admintree.status.ok");
		} 
		return ret;
	}

}
