/* ======================================================================
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.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 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 )
		{
		user_id=x;
    	setUnsaved();
		}
		
	public PrimaryKey getZoneId()
		{
		return zone_id;
		}
	public void setZoneId( PrimaryKey  x )
		{
		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 )
		{
		name=x;
    	setUnsaved();
		}

	public String getSurname()
		{
		return surname;
		}
	public void setSurname( String x )
		{
		surname=x;
    	setUnsaved();
		}

	public String getInitials()
		{
		return initials;
		}
	public void setInitials( String x )
		{
		if ( x==null )
		    {
		    initials=null;
		    setUnsaved();
		    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 )
		{
		special_groups_a=x;
    	setUnsaved();
		}

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

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

	public int getSpecialGroupsD()
		{
		return special_groups_d;
		}
	public void setSpecialGroupsD( int x )
		{
		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();
	    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();
	    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.");
	    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;
	    }

    }  
