/*
 * 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;

final class ItemColumn extends IntegerColumn
{
	static final int SYNTETIC_PRIMARY_KEY_PRECISION = 10;

	final Class targetTypeClass;
	final String integrityConstraintName;
	final ItemAttribute attribute;

	ItemColumn(final Table table, final String id,
					  final boolean notNull,
					  final Class targetTypeClass, final ItemAttribute attribute)
	{
		super(table, id, notNull, SYNTETIC_PRIMARY_KEY_PRECISION, false, null);
		if(targetTypeClass==null)
			throw new RuntimeException();
		this.targetTypeClass = targetTypeClass;
		this.integrityConstraintName = table.database.trimName(table.id+"_"+id+"_Fk").intern();
		this.attribute = attribute;
		table.database.addIntegrityConstraint(this);
	}

	/**
	 * Creates a primary key column with a foreign key contraint.
	 */	
	ItemColumn(final Table table, final Class targetTypeClass)
	{
		super(table);
		if(targetTypeClass==null)
			throw new RuntimeException();
		this.targetTypeClass = targetTypeClass;
		this.integrityConstraintName = table.id+"_Sup";
		this.attribute = null;
	}

	// TODO: remove these, when report is creating database
	String getForeignTableNameProtected()
	{
		if(targetTypeClass!=null)
			return Type.findByJavaClass(targetTypeClass).getTable().protectedID;
		else
			return null; 
	}
	
	// TODO: remove these, when report is creating database
	String getForeignTablePkNameProtected()
	{
		if(targetTypeClass!=null)
			return Type.findByJavaClass(targetTypeClass).getTable().getPrimaryKey().protectedID;
		else
			return null; 
	}
	
	void report(final ReportTable reportTable)
	{
		super.report(reportTable);
		new ReportForeignKeyConstraint(reportTable, integrityConstraintName, true, id, getForeignTableNameProtected(), getForeignTablePkNameProtected());
	}
		
}

@@ -1864,554 +1864,158 @@
 %7D%0A%0A%09
-// TODO: remove these, when report is creating database%0A%09String getForeignTableNameProtected()%0A%09%7B%0A%09%09if(targetTypeClass!=null)%0A%09%09%09return Type.findByJavaClass(targetTypeClass).getTable().protectedID;%0A%09%09else%0A%09%09%09return null; %0A%09%7D%0A%09%0A%09// TODO: remove these, when report is creating database%0A%09String getForeignTablePkNameProtected()%0A%09%7B%0A%09%09if(targetTypeClass!=null)%0A%09%09%09return Type.findByJavaClass(targetTypeClass).getTable().getPrimaryKey().protectedID;%0A%09%09else%0A%09%09%09return null; %0A%09%7D%0A%09%0A%09void report(final ReportTable reportTable)%0A%09%7B%0A%09%09super.report(reportTable
+void report(final ReportTable reportTable)%0A%09%7B%0A%09%09super.report(reportTable);%0A%09%09final Table targetTable = Type.findByJavaClass(targetTypeClass).getTable(
 );%0A%09
@@ -2098,70 +2098,70 @@
 id, 
-getForeignTableNameProtected(), getForeignTablePkNameP
+targetTable.protectedID, targetTable.getPrimaryKey().p
 rotected
 ());
@@ -2156,18 +2156,18 @@
 rotected
-()
+ID
 );%0A%09%7D%0A%09%09

remove getForeignTableNameProtected/getForeignTablePkNameProtected