/* ======================================================================
   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.database.PrimaryKey;
import org.bodington.sqldatabase.SqlPersistentObject;
import org.bodington.server.realm.Principal;

import java.util.Hashtable;
import java.util.Enumeration;

import org.apache.log4j.Logger;

/**
 * Subclasses PersistentObject.  Represents essential information about
 * a user.  Also ties in with the class Group. When a User is loaded it
 * also finds and loads group membership entries.
 * 
 * @author Jon Maber
 * @version 1
 */

public class User extends Principal
	{
    
    private static Logger log = Logger.getLogger(User.class);
    
	private PrimaryKey user_id;
	private PrimaryKey zone_id;
	private String name;
	private String surname;
	private String initials;
	private int special_groups_a;
	private int special_groups_b;
	private int special_groups_c;
	private int special_groups_d;
	private String language;
	
	private int status_expired = VALID;
	
	public final static int VALID = 0;
    public final static int EXPIRED = 1;
    public final static int ALWAYS_VALID = 2;
    public final static int ALWAYS_EXPIRED = 3;
	
    private Hashtable groups;      // Member objects hashed against group_id;
    private Hashtable new_groups;  // Member objects hashed against group_id;
    private Hashtable old_groups;  // Member objects hashed against group_id;
	
	public static User findUser( PrimaryKey key )
	    throws BuildingServerException
	    {
	    return (User)findPersistentObject( key, "org.bodington.server.realm.User" );
	    }
	
	public static User findUser( String where )
	    throws BuildingServerException
	    {
	    return (User)findPersistentObject( where, "org.bodington.server.realm.User" );
	    }
	
	public static Enumeration findUsers( String where )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, "org.bodington.server.realm.User" );
	    }
	
	public static Enumeration findUsers( String where, String order )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, order, "org.bodington.server.realm.User" );
	    }
	    
	public static Enumeration findUserPrimaryKeys( String where, String order )
	    throws BuildingServerException
	    {
	    return findPrimaryKeys( where, order, "org.bodington.server.realm.User" );
	    }


	/**
	 * Initialises group lists etc.
	 */
	

	public User()
		{
		groups = new Hashtable();
		new_groups = new Hashtable();
		old_groups = new Hashtable();
        }
	
	/**
	 * Create a user. Has all the required fields.
	 * @param zone The zone that this user will be a member of.
	 * @param name The name of the user.
	 * @param surname The surname of the user.
	 * @param initials The initials of the user.
	 */
	public User(Zone zone, String name, String surname, String initials)
	{
	    this();
	    setZone(zone);
	    setName(name);
	    setSurname(surname);
	    setInitials(initials);
	}


	/**
	 * This is a required method for implementation of Principal
	 * 
	 * @return Returns the name of the user.
	 */

	public String toString()
		{
		String n = getName();
		if ( n==null )
			return "(null)";
		return n;
		}
		
		
	/**
	 * This is a required method for implementation of PersistentObject.
	 * 
	 * @return The PrimaryKey for this object or null if it hasn't been stored yet.
	 */
		
	// implementation of PersistentObject
	public PrimaryKey getPrimaryKey()
		{
		return getUserId();
		}

	/**
	 * This is a required method for implementation of PersistentObject.
	 * 
	 * @param key The new primary key for this object.
	 */
	public void setPrimaryKey( PrimaryKey key )
		{
		setUserId( key );
		}
		
	public PrimaryKey getUserId()
		{
		return user_id;
		}
	public void setUserId( PrimaryKey  x )
		{
        if (user_id != x)
            {
            user_id=x;
            setUnsaved();
            }
		}
		
	public PrimaryKey getZoneId()
		{
		return zone_id;
		}
	public void setZoneId( PrimaryKey  x )
		{
        if (x == null)
            throw new IllegalArgumentException("Zone ID cannot be null");
        if (x.equals(zone_id))
            return;
        zone_id=x;
        setUnsaved();
		}
	public Zone getZone()
		throws BuildingServerException
		{
		return Zone.findZone( zone_id );
		}
	public void setZone( Zone z )
		{
		setZoneId( z.getZoneId() );
		}
		
		
	public String getName()
		{
		return name;
		}
	public void setName( String x )
		{
        if (x == null)
            throw new IllegalArgumentException("Name cannot be null");
        if (x.equals(name))
            return;
        
        name=x;
        setUnsaved();
		}

	public String getSurname()
		{
		return surname;
		}
	public void setSurname( String x )
		{
        if (x == null)
            throw new IllegalArgumentException("Surname cannot be null");
        if (x.equals(surname))
            return;
        surname=x;
        setUnsaved();
		}

	public String getInitials()
		{
		return initials;
		}
	public void setInitials( String x )
		{
        if (x == null)
            throw new IllegalArgumentException("Initials cannot be null");
        if (x.equals(initials))
            return;
		StringBuffer buffer = new StringBuffer( 8 );
		char c;
		for ( int i=0; i<x.length(); i++ )
		    {
		    c=x.charAt( i );
		    if ( Character.isLetter( c ) )
		        buffer.append( c );
		    }
		initials=buffer.toString();
    	setUnsaved();
		}

	public int getSpecialGroupsA()
		{
		return special_groups_a;
		}
	public void setSpecialGroupsA( int x )
		{
        if (special_groups_a == x)
            return;
		special_groups_a=x;
    	setUnsaved();
		}

	public int getSpecialGroupsB()
		{
		return special_groups_b;
		}
	public void setSpecialGroupsB( int x )
		{
        if (special_groups_b == x)
            return;
		special_groups_b=x;
    	setUnsaved();
		}

	public int getSpecialGroupsC()
		{
		return special_groups_c;
		}
	public void setSpecialGroupsC( int x )
		{
        if (special_groups_c == x)
            return;
		special_groups_c=x;
    	setUnsaved();
		}

	public int getSpecialGroupsD()
		{
		return special_groups_d;
		}
	public void setSpecialGroupsD( int x )
		{
        if (special_groups_d == x)
            return;
		special_groups_d=x;
    	setUnsaved();
		}

	/**
	 * Gets the expired status for this user.
	 * @return An int to represent the status.
	 * @see User#EXPIRED
	 * @see User#VALID
	 */
	public int getStatusExpired()
	{
		return status_expired;
	}
	
	/**
	 * Sets the expired status for this user.
	 * @param x An int to represent the status
	 */
	public void setStatusExpired(int x)
	{
		if (x != status_expired)
        {
            status_expired = x;
            setUnsaved();
        }
	}
    
    /**
     * Check if this user is allowed to use the system
     * @return <code>true</code> is this user is allowed to use the system.
     */
    public boolean isValid()
    {
        return status_expired == VALID || status_expired == ALWAYS_VALID;
    }
    
    /**
     * Check if the status should be automatically updated.
     * @return <code>true</code> is the user importer should not change
     * the status.
     */
    public boolean isFixedStatus()
    {
        return status_expired == ALWAYS_EXPIRED || status_expired == ALWAYS_VALID;
    }

	/**
	 * Saves properties then if user has been added or removed to
	 * groups since last save the relavent Member entries are inserted
	 * or deleted from store.
	 * 
	 * @exception org.bodington.server.BuildingServerException Thrown if there is a problem saving data.
	 */
	public synchronized void save()
		throws BuildingServerException
	    {
	    super.save();
	    Enumeration enumeration;
	    
	    Member member;
	    enumeration=old_groups.elements();
	    while ( enumeration.hasMoreElements() )
	        {
	        member = (Member)enumeration.nextElement();
	        member.delete();
	        }
	    old_groups.clear();
	    enumeration=new_groups.elements();
	    while ( enumeration.hasMoreElements() )
	        {
	        member = (Member)enumeration.nextElement();
	        member.save();
	        }
	    new_groups.clear();
        }
	/**
	 * Adds this user to the specified group.
	 * 
	 * @param g The group to add to.
	 * @return True is sucecssful.
	 * @exception java.lang.ArrayIndexOutOfBoundsException If this is a special group and the special group number is out of
	 * range.
	 */
	
	public synchronized boolean addToGroup( Group g )
	    throws ArrayIndexOutOfBoundsException
	    {
	    if ( g==null )
	        return false;

	    if ( isMember( g ) )
	        return false;
	        
	    PrimaryKey key = g.getPrimaryKey();
	    Integer special = g.getSpecialGroup();
	    if ( special==null )
	        {
            Member member;
            //was it removed before and this is just a reversal?
            member = (Member)old_groups.get( key );
	        if ( member!=null )
	            {
	            old_groups.remove( key );
    	        groups.put( key, member );
    	        setUnsaved();
    	        return true;
	            }
	        //genuinely new membership
	        member = new Member();
	        member.setGroupId( g.getPrimaryKey() );
	        member.setUserId( this.getPrimaryKey() );
	        new_groups.put( key, member );
	        groups.put( key, member );
   	        setUnsaved();
	        return true;
	        }
	    int s = special.intValue();
        if (s == 0)
            return false;
	    int mask;
	    if ( s<0 || s>=128 )
	        throw new ArrayIndexOutOfBoundsException();
	    mask = 1 << (s & 0x1f);
	    switch ( s & 0x60 )
	        {
	        case 0x00:
	            special_groups_a |= mask;
	            break;
	        case 0x20:
	            special_groups_b |= mask;
	            break;
	        case 0x40:
	            special_groups_c |= mask;
	            break;
	        case 0x60:
	            special_groups_d |= mask;
	        }
	    setUnsaved();
	    return true;
	    }

	/**
	 * Remove from specified group.
	 * 
	 * @param g The group to remove user from.
	 * @return True if successful.
	 * @exception java.lang.ArrayIndexOutOfBoundsException If this is a special group and the special group number is out of
	 * range.
	 */
	public synchronized boolean removeFromGroup( Group g )
	    throws ArrayIndexOutOfBoundsException
	    {
	    if ( g==null )
	        return false;
	    if ( !isMember( g ) )
	        return false;

	    PrimaryKey groupPk = g.getPrimaryKey();
	    Integer special = g.getSpecialGroup();
	    if ( special==null )
	        {
            Member member;
            //was it added before and this is just a reversal?
            member = (Member)new_groups.get( groupPk );
	        if ( member!=null )
	            {
	            new_groups.remove( groupPk );
    	        groups.remove( groupPk );
    	        setUnsaved();
    	        return true;
	            }
	        //removal of stored membership
	        member = (Member)groups.get( groupPk );
	        old_groups.put( groupPk, member );
	        groups.remove( groupPk );
   	        setUnsaved();
	        return true;
	        }
	    int s = special.intValue();
        if (s == 0)
            return false;
	    int mask;
	    if ( s<0 || s>=128 )
	        throw new ArrayIndexOutOfBoundsException();
	    mask = ~(1 << (s & 0x1f));
	    switch ( s & 0x60 )
	        {
	        case 0x00:
	            special_groups_a &= mask;
	            break;
	        case 0x20:
	            special_groups_b &= mask;
	            break;
	        case 0x40:
	            special_groups_c &= mask;
	            break;
	        case 0x60:
	            special_groups_d &= mask;
	        }
        setUnsaved();
	    return true;
	    }

	/**
	 * Tests membership of this user in specified group.
	 * 
	 * @param g The group to test against.
	 * @return True if this user is a member of the group.
	 * @exception java.lang.ArrayIndexOutOfBoundsException If this is a special group and the special group number is out of
	 * range.
	 */
	public synchronized boolean isMember( Group g )
	    throws ArrayIndexOutOfBoundsException
	    {
	    if ( g==null )
	        return false;
	    PrimaryKey key = g.getPrimaryKey();
	    Integer special = g.getSpecialGroup();
        Member member;
	    if ( special==null )
	        {
	        if ( groups.containsKey( key ) )
	            return true;
	        try
	            {
	            member = Member.findMemberByUserIdAndGroupId( getUserId(),  key );
	            }
	        catch ( BuildingServerException ex )
	            {
		    log.error( ex.getMessage(), ex );	       
	            return false;
	            }
	        if ( member==null )
	            return false;
	        groups.put( key, member );
	        return true;
	        }

	    int s = special.intValue();
	    int mask, bit=0;
	    if ( s<0 || s>=128 )
	        throw new ArrayIndexOutOfBoundsException(
                "Special group must be between 0 and 128.");
        if ( s == 0 ) return true;
	    mask = 1 << (s & 0x1f);
	    switch ( s & 0x60 )
	        {
	        case 0x00:
	            bit = special_groups_a & mask;
	            break;
	        case 0x20:
	            bit = special_groups_b & mask;
	            break;
	        case 0x40:
	            bit = special_groups_c & mask;
	            break;
	        case 0x60:
	            bit = special_groups_d & mask;
	        }
	    return bit != 0;
	    }
	 
  public void setLanguage(String inLanguage)
  {
      language = inLanguage;
  }

  public String getLanguage()
  {
      if (language != null)
          return language;
      // mod agb 29/3/2006
      // to default to English
      else
          return "en";
  }

	}
