/*
 * Copyright (C) 2004-2006  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.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * An <code>attribute</code> represents a persistently
 * stored attribute of a {@link Type}.
 * Subclasses specify the type of information to be stored
 * in an attribute.
 * <p>
 * For an overview here is a
 * <a href="attributes.png">UML diagram</a>.
 * 
 * @author Ralf Wiebicke
 */
public abstract class Attribute extends Feature
{
	final boolean isfinal;
	final boolean mandatory;

	Attribute(final boolean isfinal, final boolean mandatory)
	{
		this.isfinal = isfinal;
		this.mandatory = mandatory;
	}
	
	public final boolean isFinal()
	{
		return isfinal;
	}
	
	public final boolean isMandatory()
	{
		return mandatory;
	}
	
	// patterns ---------------------------------------------------------------------
	
	private ArrayList<Pattern> patternsWhileTypeInitialization = null;
	private List<Pattern> patterns = null;
	
	void registerPattern(final Pattern pattern)
	{
		if(isInitialized())
			throw new RuntimeException("registerPattern cannot be called after initialization of the attribute.");
		if(pattern==null)
			throw new NullPointerException();
		
		if(patternsWhileTypeInitialization==null)
			patternsWhileTypeInitialization = new ArrayList<Pattern>();
		
		patternsWhileTypeInitialization.add(pattern);
	}
	
	public List<Pattern> getPatterns()
	{
		if(!isInitialized())
			throw new RuntimeException("getPatterns cannot be called before initialization of the attribute.");
		if(patterns==null)
			throw new RuntimeException();

		return patterns;
	}
	
	// second initialization phase ---------------------------------------------------

	private Column column;
	
	void initialize(final Type type, final String name, final java.lang.reflect.Type genericType)
	{
		super.initialize(type, name, genericType);
		
		final ArrayList<Pattern> patterns = patternsWhileTypeInitialization;
		patternsWhileTypeInitialization = null;
		this.patterns =
			patterns==null
			? Collections.<Pattern>emptyList()
			: Collections.unmodifiableList(Arrays.asList(patterns.toArray(new Pattern[patterns.size()])));
	}
	
	final void materialize(final Table table)
	{
		if(table==null)
			throw new NullPointerException();
		if(this.column!=null)
			throw new RuntimeException();

		this.column = createColumn(table, getName(), mandatory);
	}
	
	final Column getColumn()
	{
		if(this.column==null)
			throw new RuntimeException();

		return column;
	}
	
	/**
	 * Returns the name of database column for this attribute - use with care!
	 * <p>
	 * This information is needed only, if you want to access
	 * the database without cope.
	 * In this case you should really know, what you are doing.
	 * Please note, that this string may vary,
	 * if a cope model is configured for different databases.
	 * 
	 * @see Type#getTableName()
	 */
	public final String getColumnName()
	{
		return column.id;
	}
	
	abstract Column createColumn(Table table, String name, boolean notNull);
	
	public static enum Option
	{
		MANDATORY(false, false, true),
		OPTIONAL(false, false, false),
		UNIQUE(false, true, true),
		UNIQUE_OPTIONAL(false, true, false),
		FINAL(true, false, true),
		FINAL_OPTIONAL(true, false, false),
		FINAL_UNIQUE(true, true, true),
		FINAL_UNIQUE_OPTIONAL(true, true, false);
		
		public final boolean isFinal;
		public final boolean unique;
		public final boolean mandatory;

		private Option(final boolean isFinal, final boolean unique, final boolean mandatory)
		{
			this.isFinal = isFinal;
			this.unique = unique;
			this.mandatory = mandatory;
		}
	}
	
}



@@ -3848,16 +3848,28 @@
 ANDATORY
+            
 (false, 
@@ -3884,32 +3884,45 @@
 rue),%0A%09%09OPTIONAL
+             
 (false, false, f
@@ -3936,16 +3936,31 @@
 %09%09UNIQUE
+               
 (false, 
@@ -3956,32 +3956,33 @@
    (false, true,
+ 
  true),%0A%09%09UNIQUE
@@ -3990,16 +3990,22 @@
 OPTIONAL
+      
 (false, 
@@ -4005,24 +4005,25 @@
 false, true,
+ 
  false),%0A%09%09F
@@ -4026,22 +4026,39 @@
 %0A%09%09FINAL
+                
 (true,
+ 
  false, 
@@ -4080,22 +4080,30 @@
 OPTIONAL
+       
 (true,
+ 
  false, 
@@ -4124,28 +4124,39 @@
 L_UNIQUE
+         
 (true,
+ 
  true,
+ 
  true),%0A
@@ -4184,22 +4184,24 @@
 AL(true,
+ 
  true,
+ 
  false);

better format of Option