/* ======================================================================
   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.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
 * An instance of this class representsa type of action a user might
 * be allowed to carry out with a resource item.  A permission is
 * displayed as a verb.  This class is used by the ACL code.
 * 
 * @author Jon Maber
 * @version 1.0
 */
public class Permission implements java.security.acl.Permission
	{
	private String name;
	
	private static Hashtable permissions;
	private static Vector permissions_in_order;
	
	
	/**
	 * The "see" permission.
	 */
	public static final Permission SEE;
	/**
	 * The "view" permission.
	 */
	public static final Permission VIEW;
	/**
	 * The "edit" permission.  Etc......
	 */
	public static final Permission EDIT;
	public static final Permission RECORD;
	public static final Permission REVIEW;
	public static final Permission POST;
	public static final Permission MARK;
	public static final Permission CREATE;
	public static final Permission UPLOAD;
	public static final Permission MANAGE;
	public static final Permission ADMINISTER;
	public static final Permission SYSADMIN;
	
	static
		{
		permissions = new Hashtable();
		permissions_in_order = new Vector();
		permissions_in_order.addElement( SEE         = new Permission( "see" ) );
		permissions_in_order.addElement( VIEW        = new Permission( "view" ) );
		permissions_in_order.addElement( EDIT        = new Permission( "edit" ) );
		permissions_in_order.addElement( RECORD      = new Permission( "record" ) );
		permissions_in_order.addElement( REVIEW      = new Permission( "review" ) );
		permissions_in_order.addElement( POST        = new Permission( "post" ) );
		permissions_in_order.addElement( MARK        = new Permission( "mark" ) );
		permissions_in_order.addElement( CREATE      = new Permission( "create" ) );
		permissions_in_order.addElement( UPLOAD      = new Permission( "upload" ) );
		permissions_in_order.addElement( MANAGE      = new Permission( "manage" ) );
		permissions_in_order.addElement( ADMINISTER  = new Permission( "administer" ) );
		permissions_in_order.addElement( SYSADMIN    = new Permission( "sysadmin" ) );
		for ( int i=0; i<permissions_in_order.size(); i++ )
		    {
		    permissions.put( ((Permission)permissions_in_order.elementAt( i )).toString(), 
		                        permissions_in_order.elementAt( i ) );
            }
		}

	/**
	 * This static method attempts to find the permissions from the supplied name.
	 * @param name The action verb of the requested Permission.
	 * @return The permission requested.
	 */
	public static Permission forName( String name )
		{
		return (Permission)permissions.get( name );
		}
	
	/**
	 * This static method returns an enumeration of all possible
	 * permissions.  The order is in increasing "seriousness".
	 * 
	 * @return An Enumeration whose elements are of type Permission.
	 */
	public static Enumeration permissions()
		{
		return permissions_in_order.elements();
		}
		
	/**
	 * Constructs a new Permission with a given name.
	 * 
	 * @param name The action verb that this Persmission will represent.
	 */
	private Permission( String name )
		{
		this.name = name;
		}
		
    /**
     * String representation of this Permission.
     * 
     * @return Returns the action verb for the permission.
     */
    public String toString()
    	{
        return name;
	    }
	
    /**
     * Tests equality.
     * 
     * @param another The Persmission to test.
     * @return Returns true if the other Permission represents the same action
     * verb as this one.
     */
    public boolean equals(Object another)
	    {
        return another.toString().equals( name );
    	}

	}
