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

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import com.exedio.cope.pattern.Qualifier;
import com.exedio.cope.search.AndCondition;
import com.exedio.cope.search.Condition;
import com.exedio.cope.search.EqualCondition;
import com.exedio.dsmf.ReportTable;
import com.exedio.dsmf.ReportUniqueConstraint;

public final class UniqueConstraint extends TypeComponent
{
	
	private final ObjectAttribute[] uniqueAttributes;
	private final List uniqueAttributeList;
	private Qualifier qualifier;
	private String databaseID;

	private UniqueConstraint(final ObjectAttribute[] uniqueAttributes)
	{
		this.uniqueAttributes = uniqueAttributes;
		this.uniqueAttributeList = Collections.unmodifiableList(Arrays.asList(uniqueAttributes));
		for(int i = 0; i<uniqueAttributes.length; i++)
			if(uniqueAttributes[i]==null)
				throw new RuntimeException(String.valueOf(i));
	}
	
	/**
	 * @see Item#uniqueConstraint(ObjectAttribute)
	 */
	UniqueConstraint(final ObjectAttribute uniqueAttribute)
	{
		this(new ObjectAttribute[]{uniqueAttribute});
	}
	
	/**
	 * @see Item#uniqueConstraint(ObjectAttribute, ObjectAttribute)
	 */
	UniqueConstraint(final ObjectAttribute uniqueAttribute1, final ObjectAttribute uniqueAttribute2)
	{
		this(new ObjectAttribute[]{uniqueAttribute1, uniqueAttribute2});
	}
	
	/**
	 * @see Item#uniqueConstraint(ObjectAttribute, ObjectAttribute, ObjectAttribute)
	 */
	UniqueConstraint(final ObjectAttribute uniqueAttribute1, final ObjectAttribute uniqueAttribute2, final ObjectAttribute uniqueAttribute3)
	{
		this(new ObjectAttribute[]{uniqueAttribute1, uniqueAttribute2, uniqueAttribute3});
	}
	
	/**
	 * @return a list of {@link ObjectAttribute}s.
	 */
	public final List getUniqueAttributes()
	{
		return uniqueAttributeList;
	}
	
	public Qualifier getQualifier()
	{
		return qualifier;
	}
	
	void setQualifier(final Qualifier qualifier)
	{
		if(this.qualifier!=null)
			throw new RuntimeException("unique constraint can have at most one qualifier");

		this.qualifier = qualifier;
	}
	
	final void materialize(final Database database)
	{
		if(this.databaseID!=null)
			throw new RuntimeException();

		this.databaseID = database.trimName(getType().getID()+"_"+getName()+"_Unq").intern();
		database.addUniqueConstraint(databaseID, this);
	}

	public final String getDatabaseID()
	{
		if(databaseID==null)
			throw new RuntimeException();
			
		return databaseID;
	}
	
	final void report(final ReportTable reportTable)
	{
		final StringBuffer bf = new StringBuffer();
		bf.append('(');
		for(int i = 0; i<uniqueAttributes.length; i++)
		{
			if(i>0)
				bf.append(',');
			final Attribute uniqueAttribute = uniqueAttributes[i];
			bf.append(uniqueAttribute.getMainColumn().protectedID);
		}
		bf.append(')');

		new ReportUniqueConstraint(reportTable, getDatabaseID(), true, bf.toString());
	}
	

	private String toStringCache = null;
	
	public final String toString()
	{
		if(toStringCache!=null)
			return toStringCache;
		
		final StringBuffer buf = new StringBuffer();
		
		//buf.append(super.toString());
		buf.append("unique(");
		buf.append(uniqueAttributes[0].getName());
		for(int i = 1; i<uniqueAttributes.length; i++)
		{
			buf.append(',');
			buf.append(uniqueAttributes[i].getName());
		}
		buf.append(')');
		
		toStringCache = buf.toString();
		return toStringCache;
	}
	
	public final Item searchUnique(final Object[] values)
	{
		// TODO: search nativly for unique constraints
		final List attributes = getUniqueAttributes();
		if(attributes.size()!=values.length)
			throw new RuntimeException();

		final Iterator attributeIterator = attributes.iterator();
		final Condition[] conditions = new Condition[attributes.size()];
		for(int j = 0; attributeIterator.hasNext(); j++)
			conditions[j] = new EqualCondition((ObjectAttribute)attributeIterator.next(), values[j]);

		return getType().searchUnique(new AndCondition(conditions));
	}
	
}

@@ -3137,21 +3137,22 @@
 ;%0A%09%7D%0A%0A%09p
-ublic
+rivate
  final S

make getDatabaseID private