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

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Enumeration;

import org.apache.log4j.Logger;
import org.bodington.database.PersistentObject;
import org.bodington.database.PrimaryKey;
import org.bodington.server.BuildingServerException;
import org.bodington.server.realm.User;
import org.bodington.server.resources.Resource;


/**
 * This is the base class for records events that happen in Bodington. An
 * example might be recording when a user downloaded a file. In almost all cases
 * you should subclass this class to store the events that you are wanting to
 * record. When you subclass this method look at overriding
 * {@link #printMessage(PrintWriter, boolean)} and
 * {@link #messageTitle()}.
 * @author Jon Maber
 * @author buckett
 */
public class Event extends org.bodington.sqldatabase.SqlPersistentObject
	{
    
    private static Logger log = Logger.getLogger(Event.class);
    
    /**
     * Primary key for an event.
     */
	protected PrimaryKey event_id;
    /**
     * The time at which the event occured.
     */
	protected java.sql.Timestamp event_time;
    /**
     * The resource in which the event happened or to which it is related.
     * This may be empty for events about the system.
     */
	protected PrimaryKey resource_id;
    /**
     * The user who caused the event. This may be empty if no user was involved.
     */
	protected PrimaryKey active_user_id;
    /**
     * If another user was involved but wasn't active in generating the event
     * the passive user should be set to this user. Can be empty.
     */
	protected PrimaryKey passive_user_id;
    /**
     * The event type of the generated event. Subclasses should define the 
     * an enumeration of event codes.
     */
	protected int event_code;
    /**
     * How important the event is considered, this affects who is able to see
     * the event.
     */
	protected int importance;
    
    /**
     * All events if this level and higher can be seen by sysadmins of a resource.
     */
	public static final int IMPORTANCE_SYSTEM_MIN		=    0;
	public static final int IMPORTANCE_SYSTEM_MAX		=   99;
    /**
     * All events if this level and higher can be seen by admins of a resource.
     */
	public static final int IMPORTANCE_ADMIN_MIN		=  100;
	public static final int IMPORTANCE_ADMIN_MAX		=  199;
    /**
     * All events if this level and higher can be seen by managers of a resource.
     */
	public static final int IMPORTANCE_MANAGEMENT_MIN	=  200;
	public static final int IMPORTANCE_MANAGEMENT_MAX	=  299;
    /**
     * All events if this level and higher can be seen by users of a resource.
     */
	public static final int IMPORTANCE_USER_MIN			=  300;
	public static final int IMPORTANCE_USER_MAX			=  399;


	public static Event findEvent( PrimaryKey key )
	    throws BuildingServerException
	    {
	    return (Event)findPersistentObject( key, "org.bodington.server.events.Event" );
	    }
	
	public static Event findEvent( String where )
	    throws BuildingServerException
	    {
	    return (Event)findPersistentObject( where, "org.bodington.server.events.Event" );
	    }
	
	public static Enumeration findEvents( String where )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, "org.bodington.server.events.Event" );
	    }
	
	public static Enumeration findEvents( String where, String order )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, order, "org.bodington.server.events.Event" );
	    }

	public static Enumeration findEventPrimaryKeys( String where, String order )
	    throws BuildingServerException
	    {
	    return findPrimaryKeys( where, order, "org.bodington.server.events.Event" );
	    }

	public Event()
		{
		event_time = new java.sql.Timestamp( System.currentTimeMillis() );
		event_code = -1;
		importance = IMPORTANCE_SYSTEM_MIN;
		resource_id=null;
		active_user_id=null;
		passive_user_id=null;
		}
	
	
    public PrimaryKey getPrimaryKey()
	    {
        return getEventId();
    	}

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

	public PrimaryKey getEventId()
		{
		return event_id;
		}
		
    public void setEventId(PrimaryKey key)
    	{
    	event_id = key;
        setUnsaved();
    	}




	public java.sql.Timestamp getEventTime()
		{
		return event_time;
		}
	public void setEventTime( java.sql.Timestamp t )
		{
		event_time = t;
        setUnsaved();
		}




	public PrimaryKey getResourceId()
		{
		return resource_id;
		}
		
    public void setResourceId(PrimaryKey key)
    	{
    	resource_id = key;
        setUnsaved();
    	}

	public Resource getResource()
		throws BuildingServerException
		{
		if ( resource_id == null )
			return null;
		return (Resource)Resource.findResource( resource_id );
		}

	public void setResource( Resource r )
		{
		setResourceId( r.getResourceId() );
		}



	public PrimaryKey getActiveUserId()
		{
		return active_user_id;
		}
		
    public void setActiveUserId(PrimaryKey key)
    	{
    	active_user_id = key;
        setUnsaved();
    	}

	public User getActiveUser()
		throws BuildingServerException
		{
		if ( active_user_id == null )
			return null;
		return (User)User.findUser( active_user_id );
		}

	public void setActiveUser( User u )
		{
		setActiveUserId( u.getUserId() );
		}

	
	
	
	
	public PrimaryKey getPassiveUserId()
		{
		return passive_user_id;
		}
		
    public void setPassiveUserId(PrimaryKey key)
    	{
    	passive_user_id = key;
        setUnsaved();
    	}

	public User getPassiveUser()
		throws BuildingServerException
		{
		if ( passive_user_id == null )
			return null;
		return (User)User.findUser( passive_user_id );
		}

	public void setPassiveUser( User u )
		{
		setPassiveUserId( u.getUserId() );
		}




	public int getEventCode()
		{
		return event_code;
		}
		
    public void setEventCode( int c )
    	{
    	event_code = c;
        setUnsaved();
    	}


	public int getImportance()
		{
		return importance;
		}
		
    public void setImportance( int n )
    	{
    	importance = n;
        setUnsaved();
    	}


    /**
     * Check if this event can be seen. By default this always returns
     * true and the importance should be used to check who can see an
     * event. Subclasses can overide this method providing more complex 
     * behaviour.
     * @return <code>true</code> if this event can be displayed.
     */
	public boolean checkPermission()
		{
		return true;
		}


	/**
	 * Wrap up the {@link #printMessage(PrintWriter, boolean)} output
	 * into a String so it can be used from JSPs (JSTL).
	 * @return The message as a String.
	 */
	public String getMessage()
		{
		StringWriter message = new StringWriter();
		printMessage(new PrintWriter(message), true);
		return message.toString();
		}

    /**
     * Wrap up link {@link #messageTitle()} output into a String so it can be
     * used from JSPs.
     * @return The message title as a String.
     */
	public String getMessageTitle()
		{
		return messageTitle();
		}
	
    /**
     * Print out a short message title. This should not involve any intensive
     * database access.
     * @return A String for the event title.
     */
	public String messageTitle()
		{
		return "Unknown type of event.";
		}

		
    /**
     * Display the event details. This should display the full event details and
     * can access the database todo so.
     * @param writer The writer to output the event to.
     * @param html If <code>true</code> then HTML tags can be outputted.
     * @throws IOException If there is a problem writing to the writer.
     * @see #printMessage(PrintWriter, boolean)
     * @see #messageTitle()
     */
	public void printProperties( PrintWriter writer, boolean html )
		{
		Resource resource = null;
		User active = null;
		User passive = null;
		String title = null;
		
		try
			{
			active = getActiveUser();
			passive = getPassiveUser();
			resource = getResource();
			if ( resource!=null )
				title = resource.getTitle();
			}
		catch ( Exception ex )
			{
			}
			
		if ( html )
			writer.println( "<PRE>" );
		writer.print( "Event ID      : " ); writer.println( event_id.toString()   );
		writer.print( "Event code    : " ); writer.println( event_code );
		writer.print( "Importance    : " ); writer.println( importance );
		writer.print( "Happened      : " );	writer.println( event_time.toString() );
		writer.print( "Title         : " );	writer.println( messageTitle() );
		writer.print( "At            : " );	writer.println( title==null?"nowhere":title );
		writer.print( "Active        : " );	writer.println( active==null?"noone":active.getName() );
		writer.print( "Passive       : " );	writer.println( passive==null?"noone":passive.getName() );
		if ( html )
			writer.println( "</PRE>" );
		}

    /**
     * Print a nice version of the message. This can be more intensive than
     * {@link #messageTitle()} and can access the database layer.
     * @param writer The message is written here.
     * @param html If <code>true</code> then HTML tags can be used.
     * @throws IOException
     */
	public void printMessage( PrintWriter writer, boolean html )
		{
		writer.print( "This type of event should not normally occur - could be due to program error." );
		}
   
    /**
     * Save an event but ignore any errors we get.
     * This should be used when saving events to the database as just because
     * we can't save an event doesn't mean that the operation shouldn't happen.
     * Really there should be an event manager that acts as a queue for events
     * so that we don't have to wait for the DB operation to complete before
     * returning.
     * @see PersistentObject#save()
     */
    public void save()
        {
        try
            {
            super.save();
            }
        catch (BuildingServerException bse)
            {
            log.info("Problem saving: "+ this, bse);
            }
        }
    
	}


