/* ======================================================================
   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 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 = getOriginalMessage();
			}
		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 getOriginalMessage() throws BuildingServerException
        {
            if (message_id == null)
                return null;
            return Message.findMessage(new PrimaryKey(message_id.intValue()));
        }
	}
	
