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

import org.bodington.server.*;
import org.bodington.server.resources.Resource;
import org.bodington.server.realm.User;
import org.bodington.server.realm.Permission;
import org.bodington.sqldatabase.*;
import org.bodington.database.*;

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



public class Event extends org.bodington.sqldatabase.SqlPersistentObject
	{
	PrimaryKey event_id;
	java.sql.Timestamp event_time;
	PrimaryKey resource_id;
	PrimaryKey active_user_id;
	PrimaryKey passive_user_id;
	int event_code;
	int importance;

	public static final int IMPORTANCE_SYSTEM_MIN		=    0;
	public static final int IMPORTANCE_SYSTEM_MAX		=   99;
	public static final int IMPORTANCE_ADMIN_MIN		=  100;
	public static final int IMPORTANCE_ADMIN_MAX		=  199;
	public static final int IMPORTANCE_MANAGEMENT_MIN	=  200;
	public static final int IMPORTANCE_MANAGEMENT_MAX	=  299;
	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();
    	}


	public boolean checkPermission()
		{
		//default is to let importance level govern
		//who can see the event but if there is a more
		//complex algorithm subclass can overide this.
		//E.g. to allow a non-manager user to only see 
		//events relating to themselves.
		return true;
		}



	public String messageTitle()
		{
		return "Unknown type of event.";
		}

		
	public void printProperties( PrintWriter writer, boolean html )
		throws IOException
		{
		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>" );
		}

	public void printMessage( PrintWriter writer, boolean html )
		throws IOException
		{
		writer.print( "This type of event should not normally occur - could be due to program error." );
		}

	public int lifetime()
		{
		// sub classes best not overide this method
		// so all types of event will be short lived in cache.
		return 10;
		}
	}


