/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.server.realm;

import org.bodington.server.*;
import org.bodington.sqldatabase.*;
import org.bodington.database.*;
import java.util.Vector;
import java.util.StringTokenizer;
import java.util.Enumeration;

/**
 * Represents a type of alternative unique identifier for a user.  For example,
 * a college may have unique library numbers for all students.  An object of this
 * type holds the zone, e.g. the college and the name of the alias, e.g. "libaryid".
 * AliasEntries will be associated with this Alias.
 * 
 * @author Jon Maber
 * @version 1.0
 */
public class Alias extends org.bodington.sqldatabase.SqlPersistentObject
	{
	private PrimaryKey alias_id;
	private PrimaryKey zone_id;
	private String alias_name;
	private int alias_type;
	private String[] user_categories = new String[8];
	
	public final static int TYPE_SECONDARY=0;
	public final static int TYPE_PRIMARY=1;
	
	/**
	 * Finds an Alias given its primary key.
	 * 
	 * @param key The key to search for.
	 * @return An Alias or null.
	 * @exception org.bodington.server.BuildingServerException If there is a database problem.
	 */
	
	
	public static Alias findAlias( PrimaryKey key )
	    throws BuildingServerException
	    {
	    return (Alias)findPersistentObject( key, "org.bodington.server.realm.Alias" );
	    }
	
	/**
	 * Finds an alias using a search expression.
	 * 
	 * @param where An SQL WHERE clause.
	 * @return An Alias or null.
	 * @exception org.bodington.server.BuildingServerException If there is a database problem.
	 */
	public static Alias findAlias( String where )
	    throws BuildingServerException
	    {
	    return (Alias)findPersistentObject( where, "org.bodington.server.realm.Alias" );
	    }
    
    /**
     * Find an alias by its name.
     * @param name The name of the alias to look for.
     * @param zone The zone to look in.
     * @return An Alias or null if it couldn't be found.
     * @throws BuildingServerException If there was a database problem.
     */
    public static Alias findAlias( String name, Zone zone )
        throws BuildingServerException
        {
        return (Alias) findPersistentObject("alias_name = "
            + SqlDatabase.quotedSQL(name) + " AND zone_id = "
            + zone.getPrimaryKey(), "org.bodington.server.realm.Alias");
        }
	
	/**
	 * Finds a set of aliases.
	 * 
	 * @param where An SQL where clause.
	 * @return An enumeration of Aliases.
	 * @exception org.bodington.server.BuildingServerException If there is a database problem.
	 */
	public static Enumeration findAliases( String where )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, "org.bodington.server.realm.Alias" );
	    }
    /**
     * Calls setAliasId().
     * 
     * @param key New PrimaryKey.
     */
	
    public void setPrimaryKey( PrimaryKey key )
	    {
	    setAliasId( key );
	    }

    /**
     * Calls getAliasId().
     * 
     * @return The primary key.
     */
    public PrimaryKey getPrimaryKey()
	    {
        return getAliasId();
	    }

    /**
     * Sets property.
     * 
     * @param key The new alias id.
     */
    public void setAliasId( PrimaryKey key )
	    {
	    alias_id = key;
	    setUnsaved();
	    }
    /**
     * Gets property.
     * 
     * @return The alias id.
     */

    public PrimaryKey getAliasId()
	    {
        return alias_id;
	    }
    /**
     * Sets property.
     * 
     * @param key The new zone id.
     */
	

    public void setZoneId( PrimaryKey key )
	    {
	    zone_id = key;
	    setUnsaved();
	    }
    /**
     * Sets property.
     * 
     * @return The zone id.
     */

    public PrimaryKey getZoneId()
	    {
        return zone_id;
	    }
	/**
	 * Sets zone id from given zone.
	 * 
	 * @param z The new zone.
	 */
	
	public void setZone( Zone z )
		{
		setZoneId( z.getZoneId() );
		}

	/**
	 * Finds the ZOne object associated with this ALias.
	 * 
	 * @return A Zone or null.
	 * @exception org.bodington.server.BuildingServerException If there is a database problem.
	 */
	public Zone getZone()
		throws BuildingServerException
		{
		return Zone.findZone( zone_id );
		}
    /**
     * Sets property.
     * 
     * @param n The new alias name.
     */
	
    public void setAliasName( String n )
	    {
        if (n != null && n.equals( alias_name ))
            return;
	    alias_name = n;
	    setUnsaved();
	    }
    /**
     * Sets property.
     * 
     * @return The alias name.
     */

    public String getAliasName()
	    {
        return alias_name;
	    }
    /**
     * Sets property.
     * @param t The new alias type.
     */
    public void setAliasType( Integer t )
	    {
        if (alias_type == t.intValue())
            return;
	    if ( t==null )
	    	alias_type = TYPE_SECONDARY;
	    else
	    	alias_type = t.intValue();
	    setUnsaved();
	    }
    /**
     * Gets property.
     * 
     * @return The alias type.
     */

    public Integer getAliasType()
	    {
        return new Integer( alias_type );
	    }
	public boolean isPrimary()
		{
		return alias_type == TYPE_PRIMARY;
		}

    /**
     * Sets property.
     * 
     * @param n The new alias name.
     */
	
    public void setUserCategory( String n )
	    {
	    StringTokenizer tok=null;
	    if ( n!=null )
	    	tok = new StringTokenizer( n, "," );
	    for ( int i=0; i<8; i++ )
	    	{
	    	if ( tok!=null && tok.hasMoreTokens() )
	    		user_categories[i] = tok.nextToken();
	    	else
	    		user_categories[i] = null;
	    	}
	    setUnsaved();
	    }
    /**
     * Sets property.
     * 
     * @return The alias name.
     */

    public String getUserCategory()
	    {
	    StringBuffer buf = new StringBuffer();
	    
	    int n=0;
	    for ( int i=0; i<8; i++ )
	    	{
	    	if ( user_categories[i]!=null )
	    		{
	    		if ( n>0 )
	    			buf.append( "," );
				buf.append( user_categories[i] );
				n++;
	    		}
	    	}
	    
        return buf.toString();
	    }

	public Enumeration getUserCategories()
		{
		Vector v = new Vector();
	    for ( int i=0; i<8; i++ )
	    	if ( user_categories[i]!=null )
	    		v.addElement( user_categories[i] );
	    return v.elements();
		}
	
	public boolean hasUserCategory( String c )
		{
		if ( c==null )
			return false;
	    for ( int i=0; i<8; i++ )
	    	if ( user_categories[i]!=null && user_categories[i].equals( c ) )
	    		return true;
		return false;
		}
		
	public boolean hasEntries()
		throws BuildingServerException
		{
		StringBuffer where = new StringBuffer();
		where.append( "alias_id IN (SELECT alias_id FROM alias_entries WHERE alias_id = " );
		where.append( getAliasId().toString() );
		where.append( ")" );
		
		Alias alias = findAlias( where.toString() );
		return alias!=null && alias.getAliasId().equals( getAliasId() );
		}
	}
	
