/* ======================================================================
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.apache.log4j.Logger;
import org.bodington.database.*;
import org.bodington.server.BuildingServerException;
import org.bodington.sqldatabase.*;

import org.bodington.database.*;

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


public class NavigationEvent extends org.bodington.server.events.Event
	{
    private static Logger log = Logger.getLogger(NavigationEvent.class);
    
	PrimaryKey navigation_event_id;
	String url;
	
	public static final int EVENT_PAGE_ACCESS = 1;
    public static final int EVENT_RESOURCE_ACCESS =2;
    
    public static Event findEvent( PrimaryKey key )
    throws BuildingServerException
    {
        return (Event)findPersistentObject( key, "org.bodington.server.events.NavigationEvent" );
    }
    
    public static Event findEvent( String where )
    throws BuildingServerException
    {
        return (Event)findPersistentObject( where, "org.bodington.server.events.NavigationEvent" );
    }
    
    public static Enumeration findEvents( String where )
    throws BuildingServerException
    {
        return findPersistentObjects( where, "org.bodington.server.events.NavigationEvent" );
    }
    
    public static Enumeration findEvents( String where, String order )
    throws BuildingServerException
    {
        return findPersistentObjects( where, order, "org.bodington.server.events.NavigationEvent" );
    }
    
    public static Enumeration findEventPrimaryKeys( String where, String order )
    throws BuildingServerException
    {
        return findPrimaryKeys( where, order, "org.bodington.server.events.NavigationEvent" );
    }
    
    /**
     * Find events for a resource after a particular date. The results are sorted
     * by the time the event occured.
     * @param resourceId The resource ID to find events for.
     * @param from The time to start looking from.
     * @return An Enumeration of Events.
     */
    public static Enumeration findEvents( PrimaryKey resourceId, long from )
    throws BuildingServerException
    {
        return findEvents("resource_id = "+ resourceId+ " AND event_time > {ts '"+ new Timestamp(from)+ "'}", "event_time");
    }
    
	public NavigationEvent()
		{
		super();
		url=null;
		}
	
	public NavigationEvent( int event_code, PrimaryKey resource_id, PrimaryKey user_id, String url )
		{
		super();
		
		this.event_code = event_code;
		if ( event_code == EVENT_PAGE_ACCESS || event_code == EVENT_RESOURCE_ACCESS) 
			importance = Event.IMPORTANCE_MANAGEMENT_MIN;
			
		this.resource_id=resource_id;
		active_user_id=user_id;
		passive_user_id=null;
		
		this.url = url;
		}
	
	
	
	
    public PrimaryKey getPrimaryKey()
	    {
        return getNavigationEventId();
    	}

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

	public PrimaryKey getNavigationEventId()
		{
		return navigation_event_id;
		}
		
    public void setNavigationEventId(PrimaryKey key)
    	{
    	navigation_event_id = key;
    	setEventId( key );
    	setUnsaved();
    	}


	public void setUrl( String u )
		{
        if ( u == null )
            u = null;
        else
        {
            if ( u.length()<=128 )
                url = u;
            else
                url = u.substring( 128 );
        }
    	setUnsaved();
		}
		
	public String getUrl()
		{
		return url;
		}
		

	public String messageTitle()
		{
		return "User accessed bodington system via a web page.";
		}

		
	public void printProperties( PrintWriter writer, boolean html )
		{
		super.printProperties( writer, html );
		
		if ( html )
			writer.println( "<PRE>" );
		writer.print( "Url           : " );   writer.println( url==null?"no url":url );
		if ( html )
			writer.println( "</PRE>" );
		}

	public void printMessage( PrintWriter writer, boolean html )
		{
        if ( active_user_id == null)
            writer.print(" An Unknown User");
        else
            try
            {
                writer.print( getActiveUser().getName() );
            }
            catch (BuildingServerException e)
            {
                writer.print( "User "+ active_user_id );
                log.warn("Failed to load user ID: "+ active_user_id);
            }
        writer.print(" accessed ");
		if ( url==null || url.length() == 0)
			writer.print( "a resource" );
		else
			writer.print( url );
		writer.print( " via the web." );
		}
	}
	
