/* ======================================================================
   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.Enumeration;

/**
 * Represents an alternative unique identifier for a specific user.
 * Contains reference to the type of unique identifier (alias_id), the value of
 * the unique identifier (user_alias) and the user it belongs to (user_id).
 * 
 * It's possible that objects of this type will be instantiated mostly when
 * editing values and not so much when searching for users.  Searching for users
 * can be done using a suitable where clause in User.findUsers().  E.g.
 * 
 * Users.findUsers( 
 * "user_id IN (SELECT user_id FROM alias_entries WHERE alias_id = 99 AND user_alias IN ('fred', 'mary, joe'))"
 *  );
 * 
 * The methods are not documented - there are usual methods to find instances from store and
 * methods to set/get properties.
 * 
 * @author Jon Maber
 * @version 1.0
 */
public class AliasEntry extends org.bodington.sqldatabase.SqlPersistentObject
	{

	/**
	 * Primary key of this entry.
	 */
	private PrimaryKey alias_entry_id;

	/**
	 * The type of alias for this entry.
	 */
	private PrimaryKey alias_id;

	/**
	 * The user_id of the user this entry refers to.
	 */
	private PrimaryKey user_id;

	/**
	 * The alias name of the user.
	 */
	private String user_alias;

	public static AliasEntry findAliasEntry( PrimaryKey key )
	    throws BuildingServerException
	    {
	    return (AliasEntry)findPersistentObject( key, "org.bodington.server.realm.AliasEntry" );
	    }
	
    /**
     * Find an AliasEntry. Other methods that don't require SQL should be used in
     * preference to this one.
     * @param where SQL WHERE query to find an AliasEntry.
     * @return An AliasEntry.
     * @see #findAliasEntry(Alias, User)
     * @see #findAliasEntry(Alias, String)
     */
	public static AliasEntry findAliasEntry( String where )
	    throws BuildingServerException
	    {
	    return (AliasEntry)findPersistentObject( where, "org.bodington.server.realm.AliasEntry" );
	    }
	
	public static Enumeration findAliasEntries( String where )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, "org.bodington.server.realm.AliasEntry" );
	    }
    
    /**
     * Find an AliasEntry. This method hides the SQL from the caller.
     * @param alias The alias under which the search is performed.
     * @param user_alias The alias we are looking for.
     */
    public static AliasEntry findAliasEntry ( Alias alias, String user_alias )
        throws BuildingServerException
        {
        return findAliasEntry("alias_id = "+ alias.getAliasId() + " AND user_alias = "+ SqlDatabase.quotedSQL(user_alias));
        }
	
    /**
     * Find an AliasEntry. This method hides the SQL from the caller. 
     * @param alias The alias to search under.
     * @param user The user to look for aliases for.
     */
    public static AliasEntry findAliasEntry ( Alias alias, User user )
    throws BuildingServerException
        {
        return findAliasEntry("alias_id = "+ alias.getAliasId() + " AND user_id = "+ user.getUserId());
        }

    public void setPrimaryKey(PrimaryKey key)
	    {
        setAliasEntryId( key );
    	}

    public PrimaryKey getPrimaryKey()
    	{
        return getAliasEntryId();
    	}

    public void setAliasEntryId( PrimaryKey key )
	    {
	    alias_entry_id = key;
	    setUnsaved();
	    }

    public PrimaryKey getAliasEntryId()
	    {
        return alias_entry_id;
	    }
	

    public void setAliasId( PrimaryKey key )
	    {
        if (key == null)
            throw new IllegalArgumentException("Alias ID cannot be null");
        if (key.equals(alias_id))
            return;
        alias_id = key;
        setUnsaved();
	    }

    public PrimaryKey getAliasId()
	    {
        return alias_id;
	    }
	
	public void setAlias( Alias a )
		{
		setAliasId( a.getAliasId() );
		}
	public Alias getAlias()
		throws BuildingServerException
		{
		return Alias.findAlias( alias_id );
		}
		
    public void setUserId( PrimaryKey key )
	    {
        if (key == null)
            throw new IllegalArgumentException("User ID cannot be null");
        if (key.equals(user_id))
            return;
        user_id = key;
        setUnsaved();
        }
	    
    public PrimaryKey getUserId()
	    {
        return user_id;
	    }
	
	public void setUser( User u )
		{
		setUserId( u.getUserId() );
		}
	public User getUser()
		throws BuildingServerException
		{
		return User.findUser( user_id );
		}
		
    public void setUserAlias( String n )
	    {
        if (n == null)
            throw new IllegalArgumentException("User Alias cannot be null");
        if (n.equals(user_alias))
            return;
        user_alias = n;
        setUnsaved();
        }

    public String getUserAlias()
	    {
        return user_alias;
	    }
    
    public String toString() 
    	{
    	return "AliasEntry aliasId: "+ alias_id+ "userId: "+ user_id+ " userAlias: "+ user_alias;
    	}

	}
