package dataProfileWebSite;

import java.sql.Connection;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.ListIterator;
import dataWarehousingTools.LogFile;
import dataWarehousingTools.Configuration;
import dataWarehousingTools.DashboardFile;

public class TableIndex 
{
	public TableIndex
	(
		Configuration configuration, 
		Connection connection, 
		LogFile logFile, 
		TableList tableProfileList,
		String databaseName,
		int databaseLinkId
	)
	{
		XmlElement xmlElement;
		ListIterator<TableElement> iterator;
		Calendar today;
		DateFormat longDateFormat;
		DecimalFormat integerFormat;
		DashboardFile tableIndexFile;
		TableElement tableProfileElement;
		
		tableIndexFile = new DashboardFile
		(
			configuration.getHtmlFileLocation() +
			"Schema" + databaseLinkId + ".htm"
		);
		
    	today = Calendar.getInstance();
    	longDateFormat = new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss");
        longDateFormat.format(today.getTime());
		integerFormat = new DecimalFormat("###,###,###,###,###");

		tableIndexFile.PutLine("<!DOCTYPE html>");
		tableIndexFile.PutLine("<head>");
		tableIndexFile.PutLine("  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>");  
		tableIndexFile.PutLine
	    (
	    	"  <link rel=\"stylesheet\" type=\"text/css\" href=\"" +
	    	configuration.getLinkToStylesheets() +
	    	"style.css\"/>"
	    );
	    
	    xmlElement = new XmlElement(2, "title", "", "List of Data Profiles Available");
	    tableIndexFile.PutLine(xmlElement.getXmlElement());
		
	    tableIndexFile.PutLine("</head>");
	    tableIndexFile.PutLine("  <body>");

	    xmlElement = new XmlElement(4, "h1", "", "List of Data Profiles Available");
	    tableIndexFile.PutLine(xmlElement.getXmlElement());
	    xmlElement = new XmlElement(4, "h4", "", "Published: " + longDateFormat.format(today.getTime()) + " GMT");
	    tableIndexFile.PutLine(xmlElement.getXmlElement());
	    
	    xmlElement = new XmlElement
	    (
	    	6, 
	    	"a", 
	    	"href=\"" +
	    	"index.html\"",
	    	"Back to List of Databases"
	    );
	    tableIndexFile.PutLine("<p>" + xmlElement.getXmlElement() + "</p>");

	    tableIndexFile.PutLine("    <table>");
	    tableIndexFile.PutLine("      <tr>");
	    tableIndexFile.PutLine("        <th style=\"text-align:left;\">Database</th>");
	    tableIndexFile.PutLine("        <th style=\"text-align:left;\">Schema</th>");
	    tableIndexFile.PutLine("        <th style=\"text-align:left;\">Table</th>");
	    tableIndexFile.PutLine("        <th style=\"text-align:right;\">Row Count</th>");
	    tableIndexFile.PutLine("        <th style=\"text-align:left;\">Date Statistics Collected</th>");
	    tableIndexFile.PutLine("      </tr>");
 
		for (iterator = tableProfileList.listIterator(); iterator.hasNext();)
		{
			tableProfileElement = (TableElement) iterator.next();
		
		    tableIndexFile.PutLine("      <tr>");
		    
		    xmlElement = new XmlElement(8, "td", "style=\"text-align:left;\"", tableProfileElement.getDatabaseName());
		    tableIndexFile.PutLine(xmlElement.getXmlElement());
		    xmlElement = new XmlElement(8, "td", "style=\"text-align:left;\"", tableProfileElement.getSchemaName());
		    tableIndexFile.PutLine(xmlElement.getXmlElement());

		    xmlElement = new XmlElement
		    (
		    	6, 
		    	"a", 
		    	"href=\"" +
		    	"TableProfile" +
		    	tableProfileElement.getTableProfileId() +
		    	".htm\"",
		    	tableProfileElement.getTableName()
		    );
		    tableIndexFile.PutLine
		    (
		    	"        <td style=\"text-align:left;\">" +	
		    	xmlElement.getXmlElement() + 
		    	"</td>"
		    );

		    tableIndexFile.PutLine
		    (
                  "        <td style=\"text-align:right;\">" +
                  integerFormat.format(tableProfileElement.getRowCount()) +
                  "</td>"
            );

		    tableIndexFile.PutLine
		    (
                  "        <td style=\"text-align:left;\">" +
                  longDateFormat.format(tableProfileElement.getDateTimeStatisticsCollected()) +
                  "</td>"
            );

		    tableIndexFile.PutLine("      </tr>");
		    
			new ColumnProfile
			(
				configuration, 
				connection, 
				logFile, 
				databaseLinkId,
				tableProfileElement.getTableProfileId(), 
				tableProfileElement.getDatabaseName(),
				tableProfileElement.getSchemaName(),
				tableProfileElement.getTableName(),
				tableProfileElement.getRowCount(),
				tableProfileElement.getDateTimeStatisticsCollected()
			); 

		}	

	    tableIndexFile.PutLine("    </table>");		    

	    xmlElement = new XmlElement(4, "h4", "", "Published: " + longDateFormat.format(today.getTime()) + " GMT");
	    tableIndexFile.PutLine(xmlElement.getXmlElement());
	    
	    tableIndexFile.PutLine("  </body>");
	    tableIndexFile.PutLine("</html>");
		
	    tableIndexFile.Close();
	}
}
