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

import org.bodington.server.BuildingServerException;
import org.bodington.database.PrimaryKey;
import org.bodington.database.IndexKey;
import org.bodington.sqldatabase.SqlPersistentObject;
import org.bodington.util.Base64Encoder;
import java.util.Enumeration;

/**
 * Subclass of PersistentObject represents the user name of a WebAuth User.
 * @author buckett
 * @version 1
 */

public class WebAuthUser extends SqlPersistentObject
	{

	/**
	 * The primary key of the object.
	 */
	private PrimaryKey web_auth_user_id;

	/**
	 * The user id of the user that owns this pass phrase.
	 */
	private PrimaryKey user_id;

	/**
	 * The log in user name of the user.
	 */
	private String user_name;
	
	
	private WebAuthUserIndexKey[] ikeys;
	

	public static WebAuthUser findWebAuthUser( PrimaryKey key )
	    throws BuildingServerException
	    {
	    return (WebAuthUser)findPersistentObject( key, "org.bodington.server.realm.WebAuthUser" );
	    }
	
	public static WebAuthUser findWebAuthUser( String where )
	    throws BuildingServerException
	    {
	    return (WebAuthUser)findPersistentObject( where, "org.bodington.server.realm.WebAuthUser" );
	    }
	
	public static WebAuthUser findWebAuthUserByUserName( String user_name )
	    throws BuildingServerException
	    {
	    WebAuthUserIndexKey ikey = new WebAuthUserIndexKey();
	    ikey.setUserName( user_name );
	    return (WebAuthUser)findPersistentObject( ikey, "org.bodington.server.realm.WebAuthUser" );
	    }
	
	public static Enumeration findWebAuthUsers( String where )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, "org.bodington.server.realm.WebAuthUser" );
	    }
	
	public static WebAuthUser findWebAuthUser( User user )
		throws BuildingServerException
	{
	    return findWebAuthUser( "user_id = "+ user.getPrimaryKey());
	}
	


	/**
	 * The Constructor does nothing at the moment.
	 */
	
	public WebAuthUser()
		{
		ikeys = new WebAuthUserIndexKey[1];
		ikeys[0] = new WebAuthUserIndexKey();
		}

	/**
	 * This is a required method for implementation of PersistentObject.
	 * 
	 * @return The primary key of this object.
	 */
		
	public PrimaryKey getPrimaryKey()
		{
		return getWebAuthUserId();
		}

	/**
	 * This is a required method for implementation of PersistentObject.
	 * 
	 * @param key The new primary key for this object.
	 */
	public void setPrimaryKey( PrimaryKey  key )
		{
		setWebAuthUserId( key );
		}
		
	public PrimaryKey getWebAuthUserId()
		{
		return web_auth_user_id;
		}
	public void setWebAuthUserId( PrimaryKey x )
		{
		web_auth_user_id=x;
    	setUnsaved();
		}
		
	public PrimaryKey getUserId()
		{
		return user_id;
		}
	public void setUserId( PrimaryKey x )
		{
        if (x == null)
            throw new IllegalArgumentException("User ID cannot be null");
        if (x.equals(user_id))
            return;
        user_id=x;
        setUnsaved();
        }
		
	public String getUserName()
		{
		return user_name;
		}
	public void setUserName( String x )
		{
        if (x == null)
            throw new IllegalArgumentException("Username cannot be null");
        if (x.equals(user_name))
            return;
        user_name=x;
        setUnsaved();
        ikeys[0].setUserName( x );
        }
		

	


    public void setUser( User u )
    	{
    	setUserId( u.getUserId() );
    	}

    public User getUser()
        throws BuildingServerException
    	{
		return User.findUser( user_id );
	    }
	    
	public IndexKey[] getIndexKeys()
	    {
	    
	    if ( ikeys[0].getUserName() == null )
	        return null;

	    return ikeys;
	    }
	    
	public boolean matchesKey( IndexKey testikey )
	    {
	    return testikey.equals( ikeys[0] );
	    }
	}
 
