package de.lcag.common;

import java.util.Set;

public class GenericTable extends HashTable {
	private static final String KEY_COLUMN = "Key";
	private static final String ROW_NUMBER_COLUMN = "RowNo";
	private static final String COLUMN_NUMBER_COLUMN = "ColNo";
	private static final String COLUMN_NAME_COLUMN = "ColName";
	private static final String COLUMN_VALUE_COLUMN = "ColValue";
	AbstractTable sourceTable = null;
	private char encloseBy = '"';

	public GenericTable(AbstractTable pSrcTable) {
		super(KEY_COLUMN, ROW_NUMBER_COLUMN, COLUMN_NUMBER_COLUMN, COLUMN_NAME_COLUMN, COLUMN_VALUE_COLUMN);

		this.sourceTable = pSrcTable;

		convertToGenericTable();
	}

	public GenericTable(ExcelWorkbook pSrcTable) {
		super(KEY_COLUMN, ROW_NUMBER_COLUMN, COLUMN_NUMBER_COLUMN, COLUMN_NAME_COLUMN, COLUMN_VALUE_COLUMN);

		this.sourceTable = pSrcTable.getFirstTable();

		convertToGenericTable();
	}

	private void convertToGenericTable() {
		AbstractTable srcTable = this.sourceTable;
		int noRows = srcTable.lenght();
		Set<String> columnNames = srcTable.getColumnNames();

		for (int srcRowNumber = 0; srcRowNumber < noRows; srcRowNumber++) {
			int columnNumber = 0;

			for (String columnName : columnNames) {
				int n = this.newRow();
				Object cellValue = srcTable.getCellValue(srcRowNumber, columnName);

				columnNumber += 1;

				this.setCellValue(n, KEY_COLUMN, srcRowNumber);
				this.setCellValue(n, ROW_NUMBER_COLUMN, srcRowNumber);
				this.setCellValue(n, COLUMN_NUMBER_COLUMN, columnNumber);
				this.setCellValue(n, COLUMN_NAME_COLUMN, columnName);
				this.setCellValue(n, COLUMN_VALUE_COLUMN, cellValue);
			}

		}
	}

	public String getKey(int pRowNo) {
		String result = this.getCellValue(pRowNo, KEY_COLUMN).toString();

		return result;
	}

}
