/*
 * Copyright (C) 2004-2008  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 java.util.Map;

public final class UniqueConstraint extends Feature
{
	private final FunctionField<?>[] fields;
	private final List<FunctionField<?>> fieldList;
	private String databaseID;

	private UniqueConstraint(final FunctionField<?>[] fields)
	{
		this.fields = fields;
		this.fieldList = Collections.unmodifiableList(Arrays.asList(fields));
		for(final FunctionField f : fields)
			f.registerUniqueConstraint(this);
	}
	
	/**
	 * Is not public, because one should use {@link Item#UNIQUE} etc.
	 */
	UniqueConstraint(final FunctionField field)
	{
		this(new FunctionField[]{field});
	}
	
	public UniqueConstraint(final FunctionField field1, final FunctionField field2)
	{
		this(new FunctionField[]{field1, field2});
	}
	
	public UniqueConstraint(final FunctionField field1, final FunctionField field2, final FunctionField field3)
	{
		this(new FunctionField[]{field1, field2, field3});
	}
	
	public UniqueConstraint(final FunctionField field1, final FunctionField field2, final FunctionField field3, final FunctionField field4)
	{
		this(new FunctionField[]{field1, field2, field3, field4});
	}
	
	/**
	 * @deprecated Renamed to {@link #getFields()}.
	 */
	@Deprecated
	public List<FunctionField<?>> getUniqueAttributes()
	{
		return getFields();
	}
	
	public List<FunctionField<?>> getFields()
	{
		return fieldList;
	}
	
	static final String IMPLICIT_UNIQUE_SUFFIX = "ImplicitUnique";
	
	void connect(final Table table)
	{
		if(this.databaseID!=null)
			throw new RuntimeException();

		final Database database = table.database;
		final String databaseName;
		if(fields.length==1)
		{
			final FunctionField field = fields[0];
			final CopeSchemaName annotation = field.getAnnotation(CopeSchemaName.class);
			databaseName = annotation!=null ? annotation.value() : field.getName();
		}
		else
		{
			final CopeSchemaName annotation = getAnnotation(CopeSchemaName.class);
			databaseName = annotation!=null ? annotation.value() : getName();
		}
		this.databaseID = database.intern(database.makeName(table.id + '_' + databaseName + "_Unq"));
		database.addUniqueConstraint(databaseID, this);
	}

	void disconnect()
	{
		if(this.databaseID==null)
			throw new RuntimeException();

		this.databaseID = null;
	}

	private String getDatabaseID()
	{
		if(databaseID==null)
			throw new RuntimeException();
			
		return databaseID;
	}
	
	void makeSchema(final com.exedio.dsmf.Table dsmfTable)
	{
		final StringBuilder bf = new StringBuilder();
		bf.append('(');
		for(int i = 0; i<fields.length; i++)
		{
			if(i>0)
				bf.append(',');
			final FunctionField<?> f = fields[i];
			bf.append(f.getColumn().protectedID);
		}
		bf.append(')');

		new com.exedio.dsmf.UniqueConstraint(dsmfTable, getDatabaseID(), bf.toString());
	}
	
	@Override
	void toStringNonInitialized(final StringBuilder bf)
	{
		bf.append("unique(");
		bf.append(fields[0].toString());
		for(int i = 1; i<fields.length; i++)
		{
			bf.append(',');
			bf.append(fields[i].toString());
		}
		bf.append(')');
	}
	
	/**
	 * Finds an item by its unique fields.
	 * @return null if there is no matching item.
	 */
	public Item searchUnique(final Object... values)
	{
		// TODO: search natively for unique constraints
		final List<FunctionField<?>> fields = getFields();
		if(fields.size()!=values.length)
			throw new RuntimeException(String.valueOf(fields.size())+'-'+values.length);

		for(int i = 0; i<values.length; i++)
			if(values[i]==null)
				throw new NullPointerException("cannot search uniquely for null on " + getID() + " for " + fields.get(i).getID());
		
		final Iterator<FunctionField<?>> fieldIter = fields.iterator();
		final Condition[] conditions = new Condition[fields.size()];
		for(int j = 0; fieldIter.hasNext(); j++)
			conditions[j] = Cope.equalAndCast(fieldIter.next(), values[j]);

		return getType().searchSingleton(new CompositeCondition(CompositeCondition.Operator.AND, conditions));
	}
	
	/**
	 * Finds an item by its unique fields.
	 * @return null if there is no matching item.
	 */
	public <P extends Item> P searchUnique(final Class<P> typeClass, final Object... values)
	{
		return Cope.verboseCast(typeClass, searchUnique(values));
	}
	
	void check(final Item item, final Map<? extends Field, ?> fieldValues)
	{
		final List<FunctionField<?>> fields = getFields();
		field:
		for(FunctionField testField : fields)
		{
			if(fieldValues.containsKey(testField))
			{
				final Object[] values = new Object[fields.size()];
				int i = 0;
				
				for(FunctionField<?> f : fields)
				{
					final Object value = fieldValues.containsKey(f) ? fieldValues.get(f) : (item!=null ? f.get(item) : null);
					if(value==null)
						break field;
					values[i++] = value;
				}
				
				final Item collision = searchUnique(values);
				if(collision!=null && (item==null || !item.equals(collision)))
					throw new UniqueViolationException(this, item);
				
				break field;
			}
		}
	}
}

@@ -5153,61 +5153,8 @@
 %0A%09%7B%0A
-%09%09final List%3CFunctionField%3C?%3E%3E fields = getFields();%0A
 %09%09fi
@@ -5291,30 +5291,30 @@
 ject%5Bfields.
-size()
+length
 %5D;%0A%09%09%09%09int i

small optimize