/* ======================================================================
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.database.*;
import org.bodington.messaging.Message;
import org.bodington.sqldatabase.*;
import org.bodington.server.*;

import org.bodington.server.realm.User;

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


public class MessagingEvent extends org.bodington.server.events.Event
	{
	PrimaryKey messaging_event_id;
	Integer message_id;
	Integer thread_id;
	
	public static final int EVENT_READ   = 1;
	public static final int EVENT_POST   = 2;
	public static final int EVENT_REPLY  = 3;
	public static final int EVENT_DRAFT  = 4;
	public static final int EVENT_DRAFT_REPLY  = 5;

	static final String[] event_message_a = 
		{
		"",
		" read a message",
		" posted a message",
		" replied to a message",
		" drafted a message",
		" drafted a reply to a message"
		};
	
	static final String[] event_message_b = 
		{
		"",
		".",
		".",
		".",
		".",
		"."
		};

	static final String[] event_message_c = 
		{
		"",
		" that was posted by ",
		" in response to ",
		" that was posted by ",
		" in response to ",
		" that was posted by "
		};

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




	
	public MessagingEvent()
		{
		super();
		message_id=null;
		thread_id=null;
		}
	
	public MessagingEvent(	int event_code, 
							PrimaryKey resource_id, 
							PrimaryKey active_user_id, 
							PrimaryKey passive_user_id, 
							Integer message_id, 
							Integer thread_id             )
		{
		super();
		
		this.event_code = event_code;
		if ( event_code == EVENT_READ || event_code == EVENT_DRAFT || event_code == EVENT_DRAFT_REPLY ) 
			importance = Event.IMPORTANCE_MANAGEMENT_MIN;
		if ( event_code == EVENT_POST || event_code == EVENT_REPLY )
			importance = Event.IMPORTANCE_USER_MIN;
			
		this.resource_id		= resource_id;
		this.active_user_id		= active_user_id;
		this.passive_user_id	= passive_user_id;
		this.message_id 		= message_id;
		this.thread_id			= thread_id;
		}
	
	
	
	
    public PrimaryKey getPrimaryKey()
	    {
        return getMessagingEventId();
    	}

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

	public PrimaryKey getMessagingEventId()
		{
		return messaging_event_id;
		}
		
    public void setMessagingEventId(PrimaryKey key)
    	{
    	messaging_event_id = key;
    	setEventId( key );
    	setUnsaved();
    	}



	public Integer getMessageId()
		{
		return message_id;
		}
		
    public void setMessageId( Integer i )
    	{
    	message_id = i;
    	setUnsaved();
    	}


	public Integer getThreadId()
		{
		return thread_id;
		}
		
    public void setThreadId( Integer i )
    	{
    	thread_id = i;
    	setUnsaved();
    	}


		
		

	public String messageTitle()
		{
		if ( event_code>=EVENT_READ && event_code<=EVENT_DRAFT_REPLY )
			return "Someone" + event_message_a[event_code] + ".";
		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( "Message ID    : " );   writer.println( message_id==null?"no url":message_id.toString() );
		writer.print( "Thread ID     : " );   writer.println( thread_id==null?"no url":thread_id.toString() );
		if ( html )
			writer.println( "</PRE>" );
		}


	public void printMessage( PrintWriter writer, boolean html )
		{
		User active = null;
		User passive = null;
        Message message = null;
		try
			{
			active = getActiveUser();
			passive = getPassiveUser();
            message = getOrginalMessage();
			}
		catch ( BuildingServerException bsex )
			{
			// do nothing
			}

		if ( active==null )
			writer.print( "Someone" );
		else
			writer.print( active.getName() );
			
		if ( event_code>=EVENT_READ && event_code<=EVENT_DRAFT_REPLY )
			writer.print( event_message_a[event_code] );
		else
			{
			writer.print( " generated a messaging event." );
			return;
			}
		 
        if (message != null)
            {
            String subject = message.getSubject();
            writer.print( " titled '"+ ((subject.length() > 30)?subject.substring(0,28)+"..":subject)+ "'");
            }
		if ( passive==null )
			writer.print( event_message_b[event_code] );
		else
			{
			writer.print( event_message_c[event_code] );
			writer.print( passive.getName() );
			}
		}
    
    /**
     * Get the message associated with this event.
     * @return The Message object
     * @throws BuildingServerException If we have a problem loading the message.
     */
    public Message getOrginalMessage() throws BuildingServerException
        {
            if (message_id == null)
                return null;
            return Message.findMessage(new PrimaryKey(message_id.intValue()));
        }
	}
	
