/* ======================================================================
The Bodington System Software License, Version 1.0
  
Copyright (c) 2001 The University of Leeds.  All rights reserved.
  
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1.  Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2.  Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3.  The end-user documentation included with the redistribution, if any,
must include the following acknowledgement:  "This product includes
software developed by the University of Leeds
(http://www.bodington.org/)."  Alternately, this acknowledgement may
appear in the software itself, if and wherever such third-party
acknowledgements normally appear.

4.  The names "Bodington", "Nathan Bodington", "Bodington System",
"Bodington Open Source Project", and "The University of Leeds" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
d.gardner@leeds.ac.uk.

5.  The name "Bodington" may not appear in the name of products derived
from this software without prior written permission of the University of
Leeds.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO,  TITLE,  THE IMPLIED WARRANTIES 
OF QUALITY  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO 
EVENT SHALL THE UNIVERSITY OF LEEDS OR ITS CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
=========================================================

This software was originally created by the University of Leeds and may contain voluntary 
contributions from others.  For more information on the Bodington Open Source Project, please 
see http://bodington.org/

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

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" );
	    }
	
	/**
	 * 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 )
	    {
	    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 ( 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() );
		}
	}
	
