package tableDefinition;

import java.util.ListIterator;
import dataWarehousingTools.LogFile;

public class CheckDataTypes
{

	public CheckDataTypes(String tableName, ColumnList columnList, LogFile logFile)
	{
		ListIterator<ColumnElement> iterator;
		ColumnElement columnElement;
		String dataType;
		String message;

		for (iterator = columnList.listIterator(); iterator.hasNext();)
		{
			columnElement = (ColumnElement) iterator.next();
			dataType = new String(columnElement.getDataType().toUpperCase());

			message = new String("");
			if (dataType.startsWith("NVARCHAR"))
				message = new String
				(
					"The data type " + 
					dataType + 
					" should not be used.  All character fields in the database should " +
					"use the same encoding, and this should normally be UTF-8."
				);
			else if (dataType.equals("CHAR"))
			{
				message = new String
				(
					"The data type CHAR should not be used.  Since CHAR and VARCHAR fields " +
					"have different comparison rules, using CHAR often causes confusion. " +
					"Use VARCHAR for all character columns."
				);

			}
			else if 
			(
					(dataType.equals("LOB")) ||
					(dataType.equals("BLOB")) ||
					(dataType.equals("CLOB")) ||
					(dataType.equals("NCLOB")) ||
					(dataType.equals("BFILE")) ||
					(dataType.equals("RAW")) ||
					(dataType.equals("LONG")) ||
					(dataType.equals("LONG RAW")) ||
					(dataType.equals("TEXT"))
			)
			{
				message = new String
				(
					"The data type " +
				    dataType +
				    " should not be used.  Fields that are too long for VARCHAR or are in some " +
				    "binary format (audio, image, video, etc.) should be stored in separate " +
				    "files outside the database "
				);
			}
			else if (dataType.equals("ROWID"))
			{
				message = new String("The data type ROWID should not be used.");
			}
			else if (dataType.equals("XMLTYPE"))
			{
				message = new String
				(
					"The data type XMLTYPE should not be used.  XML data should be parsed into " +
					"its constituent structures and fields before loading into tables and " +
					"columns in the database."
				);
			}
			else if 
			(
				(dataType.equals("NUMBER")) &&
				(columnElement.isPrecisionIsNull()) &&
				(columnElement.isScaleIsNull())
			)
			{
				message = new String
				(
					"Do not use naked NUMBERs in Oracle.  A naked number is one without precision " +
					"and scale, (NUMBER, rather than NUMBER(9,2) for example).  Naked NUMBERs in " +
					"Oracle hold numbers with any precision and scale (up to 38 digits).  We should " +
					"always know what precision and scale is acceptable in any number in the database."
				);
			}
				
			if (!message.isEmpty())
			{
				logFile.logMessage
				(
					"Table: " + 
					tableName +
					" column: " +
					columnElement.getColumnName() +
					message
				);
			}				
		}
	}
}
