/*
 * Copyright (C) 2004-2005  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package com.exedio.cope;

public final class ReportColumn extends ReportNode
{
	public final String name;
	public final ReportTable table;
	private final String requiredType;
	private boolean exists;
	private String existingType;
		
	ReportColumn(final String name, final String type, final boolean required, final ReportTable table)
	{
		if(name==null)
			throw new RuntimeException(type);
		if(type==null)
			throw new RuntimeException(name);
		if(table==null)
			throw new RuntimeException(name);

		this.name = name;
		this.table = table;
		if(required)
		{
			this.requiredType = type;
			this.existingType = null;
		}
		else
		{
			this.requiredType = null;
			this.existingType = type;
		}
		this.exists = !required;
	}

	void notifyExists(final String existingType)
	{
		if(exists && !this.existingType.equals(existingType))
			throw new RuntimeException(name);

		this.existingType = existingType;
		exists = true;
	}

	protected void finish()
	{
		if(cumulativeColor!=COLOR_NOT_YET_CALC || particularColor!=COLOR_NOT_YET_CALC)
			throw new RuntimeException();

		if(!exists)
		{
			error = "missing";
			particularColor = COLOR_ERROR;
		}
		else if(requiredType==null)
		{
			error = "not used";
			particularColor = COLOR_WARNING;
		}
		else
		{
			if(requiredType!=null &&
				existingType!=null &&
				!requiredType.equals(existingType))
			{
				error = "different type in database: >"+existingType+"<";
				particularColor = COLOR_ERROR;
			}
			else
				particularColor = COLOR_OK;
		}
				
		cumulativeColor = particularColor;
	}
		
	public final boolean required()
	{
		return requiredType!=null;
	}
	
	public final boolean exists()
	{
		return exists;
	}
		
	public final String getDatabaseType()
	{
		if(requiredType!=null)
			return requiredType;
		else
			return existingType;
	}
		
	public final void create()
	{
		table.report.database.createColumn(this);
	}

	public final void renameTo(final String newName)
	{
		table.report.database.renameColumn(table.name, name, newName, existingType);
	}

	public final void modify(final String newType)
	{
		table.report.database.modifyColumn(table.name, name, newType);
	}

	public final void drop()
	{
		table.report.database.dropColumn(table.name, name);
	}

}
	

@@ -975,33 +975,8 @@
 pe;%0A
-%09private boolean exists;%0A
 %09pri
@@ -1001,16 +1001,16 @@
 ngType;%0A
+
 %09%09%0A%09Repo
@@ -1472,35 +1472,8 @@
 %09%09%7D%0A
-%09%09this.exists = !required;%0A
 %09%7D%0A%0A
@@ -1531,17 +1531,96 @@
 if(exist
-s
+ingType==null)%0A%09%09%09throw new RuntimeException(name);%0A%09%09if(this.existingType!=null
  && !thi
@@ -1734,25 +1734,8 @@
 pe;%0A
-%09%09exists = true;%0A
 %09%7D%0A%0A
@@ -1886,15 +1886,26 @@
 %09if(
-!
 exist
-s
+ingType==null
 )%0A%09%09
@@ -2485,17 +2485,29 @@
 rn exist
-s
+ingType!=null
 ;%0A%09%7D%0A%09%09%0A

remove ReportColumn#exists, use existingType instead