package com.exedio.cope.lib;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public final class Report extends ReportNode
{

	private final HashMap tables = new HashMap();
	
	Report(final List modelTables)
	{
		for(Iterator i = modelTables.iterator(); i.hasNext(); )
		{
			final com.exedio.cope.lib.Table modelTable = (com.exedio.cope.lib.Table)i.next();
			final ReportTable reportTable = new ReportTable(modelTable);
			if(tables.put(modelTable.id, reportTable)!=null)
				throw new RuntimeException();
	
			for(Iterator j = modelTable.getAllColumns().iterator(); j.hasNext(); )
			{
				final Column column = (com.exedio.cope.lib.Column)j.next();
				final ReportColumn reportColumn = reportTable.notifyRequiredColumn(column);
				
				if(column.primaryKey)
					reportTable.notifyRequiredConstraint(column.getPrimaryKeyConstraintID());
				else if(column.notNull)
					reportTable.notifyRequiredConstraint(column.getNotNullConstraintID());
					
				if(column instanceof StringColumn)
				{
					final StringColumn stringColumn = (StringColumn)column;

					if(stringColumn.minimumLength>0)
						reportTable.notifyRequiredConstraint(stringColumn.getMinimumLengthConstraintID());

					if(stringColumn.maximumLength!=Integer.MAX_VALUE)
						reportTable.notifyRequiredConstraint(stringColumn.getMaximumLengthConstraintID());
				}
				else if(column instanceof IntegerColumn)
				{
					final IntegerColumn intColumn = (IntegerColumn)column;
					if(intColumn.allowedValues!=null)
						reportTable.notifyRequiredConstraint(intColumn.getAllowedValuesConstraintID());

					if(intColumn instanceof ItemColumn)
					{
						final ItemColumn itemColumn = (ItemColumn)intColumn;
						reportTable.notifyRequiredConstraint(itemColumn.integrityConstraintName);
					}
				}
			}
			for(Iterator j = modelTable.getUniqueConstraints().iterator(); j.hasNext(); )
			{
				final UniqueConstraint uniqueConstraint = (UniqueConstraint)j.next();
				reportTable.notifyRequiredConstraint(uniqueConstraint.getID());
			}
		}
	}

	final ReportTable notifyExistentTable(final String tableName)
	{
		ReportTable result = (ReportTable)tables.get(tableName);
		if(result==null)
		{
			result = new ReportTable(tableName);
			tables.put(tableName, result);
		}
		else
			result.notifyExists();

		return result;
	}
	
	public ReportTable getTable(final String name)
	{
		return (ReportTable)tables.get(name);
	}
	
	public Collection getTables()
	{
		return tables.values();
	}
	
	void finish()
	{
		if(cumulativeColor!=COLOR_NOT_YET_CALC || particularColor!=COLOR_NOT_YET_CALC)
			throw new RuntimeException();
		
		particularColor = COLOR_OK;

		cumulativeColor = particularColor;
		for(Iterator i = tables.values().iterator(); i.hasNext(); )
		{
			final ReportTable table = (ReportTable)i.next();
			table.finish();
			cumulativeColor = Math.max(cumulativeColor, table.cumulativeColor);
		}
	}

}

@@ -23,16 +23,44 @@
 e.lib;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -80,16 +80,16 @@
 ection;%0A
-
 import j
@@ -233,17 +233,19 @@
 ap table
-s
+Map
  = new H
@@ -254,16 +254,70 @@
 hMap();%0A
+%09private final ArrayList tableList = new ArrayList();%0A
 %09%0A%09Repor
@@ -563,25 +563,27 @@
 %0A%09%09%09if(table
-s
+Map
 .put(modelTa
@@ -644,16 +644,47 @@
 tion();%0A
+%09%09%09tableList.add(reportTable);%0A
 %09%0A%09%09%09for
@@ -2291,25 +2291,27 @@
 tTable)table
-s
+Map
 .get(tableNa
@@ -2386,17 +2386,19 @@
 %09%09%09table
-s
+Map
 .put(tab
@@ -2414,16 +2414,42 @@
 esult);%0A
+%09%09%09tableList.add(result);%0A
 %09%09%7D%0A%09%09el
@@ -2578,17 +2578,19 @@
 le)table
-s
+Map
 .get(nam
@@ -2646,26 +2646,20 @@
 rn table
-s.values()
+List
 ;%0A%09%7D%0A%09%0A%09
@@ -2888,18 +2888,12 @@
 able
-s.values()
+List
 .ite

preserve order of tables in report