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

import org.apache.log4j.Logger;

import java.util.Enumeration;
import java.util.Vector;
import java.util.Hashtable;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;

import org.bodington.database.PrimaryKey;
import org.bodington.server.BuildingContext;
import org.bodington.server.BuildingServerException;
import org.bodington.server.resources.*;
import org.bodington.text.BigString;
import org.bodington.util.VisitationMutableTreeNode;
import org.bodington.server.realm.User;


public class MessagingRoom extends org.bodington.server.resources.Resource
	{
    private static Logger log = Logger.getLogger(MessagingRoom.class);
    
	public static final int TYPE_PLAIN			=0;
	public static final int TYPE_MULTIPLEXED	=1;
	public static final int TYPE_PERSONAL		=2;
	
	PrimaryKey messaging_room_id;
	int type_of_room;
	

	private transient MessageTreeNode root;
	private transient Vector top;
	
	private transient Hashtable summaries=null;
	
	
    public Class sessionClass()
    	{
        // This method is derived from class org.bodington.server.resources.Resource
        // to do: code goes here
        return org.bodington.messaging.MessagingRoomSessionImpl.class;
    	}
    	
    	
   	public static MessagingRoom findMessagingRoom( PrimaryKey key )
		throws BuildingServerException
		{
		return (MessagingRoom)findPersistentObject( key, "org.bodington.messaging.MessagingRoom" );
		}
	public static Enumeration findMessagingRooms( String where )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, "org.bodington.messaging.MessagingRoom" );
	    }
	public static Enumeration findMessagingRooms( String where, String order )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, order, "org.bodington.messaging.MessagingRoom" );
	    }

    public PrimaryKey getPrimaryKey()
		{
        return getMessagingRoomId();
        }

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

    public PrimaryKey getMessagingRoomId()
		{
        return messaging_room_id;
        }

    public void setMessagingRoomId(PrimaryKey key)
    	{
    	messaging_room_id = key;
    	setResourceId( key );
    	setUnsaved();
    	}
    	
    	
	public int getTypeOfRoom()
		{
		return type_of_room;
		}
	public void setTypeOfRoom( int r )
		{
		if ( r==type_of_room ) return;
		type_of_room=r;
    	setUnsaved();
		}


	public Message createMessage()
		throws BuildingServerException
		{
		return createMessage( null, true );
		}
		
	public Message createMessage( PrimaryKey replyto_mid, boolean branch )
		throws BuildingServerException
		{
		MessageSummary summary;
		PrimaryKey parent_mid=replyto_mid;
		
		if ( replyto_mid!=null && !containsMessage( replyto_mid ) )
			throw new BuildingServerException( "Can't reply to message from another room here." );
		
		
		// follow on recorded as a reply to the same message that selected
		// message is a reply to
		if ( !branch && replyto_mid!=null )
			{
			summary = getMessageSummary( replyto_mid );
			parent_mid = summary.getParentMessageId();
			if ( parent_mid == null )
				throw new BuildingServerException( "Can't find message to place this message under." );
			}
		
		
		Message message = new Message();
		message.setMessagingRoomId( messaging_room_id );
		message.setCreatedTime( new java.sql.Timestamp(System.currentTimeMillis()) );
		message.setUpdatedTime( message.getCreatedTime() );
		message.setSeen( false );
		message.setLeftIndex( -1 );
		message.setRightIndex( -1 );
		message.setParentMessageId( parent_mid );
		message.setReplyToMessageId( replyto_mid );
			
		return message;
		}


	public void postMessage( Message message )
		throws BuildingServerException
		{
		MessageTreeNode[] path;
		MessageTreeNode root, parent, node;
		Message parent_message;
		MessageSummary summary;
		
		
		root = getMessageRoot();
		parent = root;
		
		message.save();
		if ( message.getParentMessageId() != null )
			{
			parent = (MessageTreeNode)root.findNode( message.getParentMessageId() );
			if ( parent == null )
				throw new BuildingServerException( "Can't find original message in this room to reply to." );
			}

		parent.add( message );

		saveMessages();
		
		getMessageSummaries();
		summary = message.getSummary();
		summaries.put( summary.getMessageId(), summary );
		}


	public void saveMessages()
		throws BuildingServerException
		{
		Enumeration enumeration;
		MessageTreeNode root, parent, node;
		PrimaryKey key;
		Message message;
		
		root = getMessageRoot();
		enumeration = root.findChangedNodes();
		while ( enumeration.hasMoreElements() )
			{
			node = (MessageTreeNode)enumeration.nextElement();
			key = node.getPrimaryKey();
			if ( key == null )
				continue;
			message = Message.findMessage( key );
			if ( message!=null )
				{
				message.setLeftIndex( node.getLeftIndex() );
				message.setRightIndex( node.getRightIndex() );
				message.save();
				node.indexSaved();
				}
			//Make sure node knows that message has been saved
			}
		}

/*
	public Vector getTopLevelMessageIds()
		throws BuildingServerException
		{
		Message message;
		if ( top == null )
			{
			top = new Vector();
			Enumeration enumeration = 
				Message.findMessages( 
					"parent_message_id IS NULL AND message_room_id = " + messaging_room_id.toString(),
					"created_time" );
			while ( enumeration.hasMoreElements() )
				{
				message = (Message)enumeration.nextElement();
				top.addElement( message );
				}
			}
		return top;
		}
*/

	public boolean containsMessage( PrimaryKey mid )
		throws BuildingServerException
		{
		MessageTreeNode root, node;
		
		root = getMessageRoot();
		node = (MessageTreeNode)root.findNode( mid );
		return node!=null;
		}

	public MessageSummary getMessageSummary( PrimaryKey key )
		throws BuildingServerException
		{
                    
                // in the case of a null key return a message summary that
                // represents the "root" which means this MessagingRoom
                if ( key == null )
                {
                    MessageSummary summary = new MessageSummary();
                    summary.setMessagingRoomId( this.getMessagingRoomId() );
                    summary.setSubject( this.getTitle() );
                    summary.setBigStringId( this.getIntroductionBigStringId() );
                    return summary;
                }
                
		Message m = Message.findMessage( key );
		
		if ( m==null )
		    return null;
		
		if ( m.getMessagingRoomId().equals( this.getMessagingRoomId() ) )
		    return m.getSummary();
		
		throw new BuildingServerException( "Technical problem: attempt to access message in another room." );
		}
		
	public Hashtable getMessageSummaries()
		throws BuildingServerException
		{
		Enumeration enumeration;
		Message message;
		MessageSummary summary;
		
		if ( summaries ==null )
			{
			summaries = new Hashtable();
			
			enumeration = Message.findMessages( "messaging_room_id = " + getMessagingRoomId() );
			while ( enumeration.hasMoreElements() )
				{
				message = (Message)enumeration.nextElement();
				summary = message.getSummary();
				summaries.put( summary.getMessageId(), summary );
				}
			}

		return summaries;
		}

	public Hashtable getDifferentMessageSummaries( Hashtable keys )
		throws BuildingServerException
		{
		Enumeration enumeration;
		PrimaryKey key;
		Message message;
		MessageSummary summary;
		Hashtable s = getMessageSummaries();
		Hashtable r = new Hashtable();
		
		if ( s == null )
			return r;
			
		enumeration = s.keys();
		while ( enumeration.hasMoreElements() )
			{
			key = (PrimaryKey)enumeration.nextElement();
			if ( !keys.containsKey( key ) )
				{
				summary = (MessageSummary)s.get( key );
				r.put( key, summary );
				}
			}

		return r;
		}

/*
	public Vector findAllMessageIds()
		{
		if ( top == null )
			{
			top = new Vector();
			Enumeration enumeration = Message.findMessage( 
					"message_room_id = " + message_room_id.toString(),
					"created_time" );
			while ( enumeration.hasMoreElements() )
				{
				}
			}
		}
*/


	public synchronized MessageTreeNode getMessageRoot()
		throws BuildingServerException
        {
            return getMessageNode( null );
        }
        
	public synchronized MessageTreeNode getMessageNode( PrimaryKey mid )
		throws BuildingServerException
		{
		int i;
		Vector nodes_in_order;
		Message message;
		PrimaryKey key, parentkey;
		MessageTreeNode current_node, parent_node;
		
		if ( root == null )
			{
			//the root is not a message - just an empty defaultmutabletreenode
			summaries = new Hashtable();
			root = new MessageTreeNode();

			
			//sorted this way we can be certain that parents are always loaded before children
			Enumeration enumeration = 
				Message.findMessages( 
					"messaging_room_id = " + messaging_room_id.toString(), "left_index, created_time" );

			nodes_in_order = new Vector();
			
			while ( enumeration.hasMoreElements() )
				{
				message = (Message)enumeration.nextElement();
				summaries.put( message.getPrimaryKey(), message.getSummary() );
				parentkey = message.getParentMessageId();
				
				current_node = new MessageTreeNode();
				current_node.setVisitationObject( message );

				if ( parentkey==null )
					{
					root.add( current_node );
					}
				else
					{
					//look backwards in previous nodes for parent
					for ( i=nodes_in_order.size()-1; i>=0; i-- )
						{
						parent_node = (MessageTreeNode)nodes_in_order.elementAt( i );
						key = parent_node.getPrimaryKey();
						if ( key.equals( parentkey ) )
							{
							parent_node.add( current_node );
							break;  //leave loop with i set to index of parent
							}
						}
					if ( i<0 ) //loop above is exited with i<0 if no parent was found.
						{
						// A message was found that can't be placed - abandon this method of
						// loading and use more laborious method.
						root = new MessageTreeNode();
						loadMessages( root );
						root.reindex();
						saveMessages();
						return root;
						}
					}

				nodes_in_order.addElement( current_node );
				}
			
			
			root.reindex();
			}

                if ( mid == null )
                    return root;
                
		return (MessageTreeNode)root.findNode( mid );
		}
		

	private static StringBuffer load_where;
	private static Message load_mess;
	private synchronized void loadMessages( MessageTreeNode node )
		throws BuildingServerException
		{
		Enumeration enumeration;
		MessageTreeNode child_node;
		
		if ( load_where==null )
			load_where = new StringBuffer( 100 );
		load_where.setLength( 0 );
		
		load_where.append( "messaging_room_id = " );
		load_where.append( messaging_room_id.toString() );
		
		if ( node.getPrimaryKey() == null )
			load_where.append( " AND parent_message_id IS NULL" );
		else
			{
			load_where.append( " AND parent_message_id = " );
			load_where.append( node.getPrimaryKey().toString() );
			}
		
		enumeration = Message.findMessages( load_where.toString(), "created_time" );
		while ( enumeration.hasMoreElements() )
			{
			load_mess = (Message)enumeration.nextElement();
			if ( summaries!=null )
				summaries.put( load_mess.getPrimaryKey(), load_mess.getSummary() );
			child_node = new MessageTreeNode();
			child_node.setVisitationObject( load_mess );
			node.add( child_node );
			
			loadMessages( child_node );
			}
		
		}
		
		
	public static void main( String[] params )
		{
		MessagingRoom room = new MessagingRoom();
		Message message1, message2, message3;
		BigString text;
		String t = "The quick brown fox jumped over the lazy dog.";
		
		try
			{
			room.setPrimaryKey( new PrimaryKey(0x1000) );
			
			text = new BigString();
			text.setString( t );
			
			message1 = room.createMessage();
			message1.setSubject( "Message Number 1" );
			message1.setBigStringId( text.getBigStringId() );
			room.postMessage( message1 );
			
			text = new BigString();
			text.setString( t );
			
			message2 = room.createMessage();
			message2.setSubject( "Message Number 2" );
			message2.setBigStringId( text.getBigStringId() );
			room.postMessage( message2 );
			
			text = new BigString();
			text.setString( t );
			
			message3 = room.createMessage( message1.getMessageId(), true );
			message3.setSubject( "Message Number 3" );
			message3.setBigStringId( text.getBigStringId() );
			room.postMessage( message3 );

			}
		catch ( Exception ex )
			{
			log.error( ex.getMessage(), ex );
			}
		
		log.info( "Done." );
		}

	
	public void checkUpload( String pathname, String mime_type )
		throws BuildingServerException
		{
		log.debug( "MessagingRoom.checkUpload( \"" + pathname + "\", \"" + mime_type + "\" );" );
		
		String p;
		
		p = pathname;
		if ( p.startsWith( "/" ) )
			 p = p.substring( 1 );
		
		// messaging room only allows files stored in folders that
		// correspond to message IDs.
		if ( p.indexOf( "/" )<1 )
			throw new BuildingServerException( "In messaging rooms you cannot upload to the root folder." );

		if ( p.indexOf( "/" ) != p.lastIndexOf( "/" ) )
			throw new BuildingServerException( "In messaging rooms you can only upload to a folder corresponding to one of your own messages. (a)" );
			
		String folder = p.substring( 0, p.indexOf( "/" ) );

		log.debug( folder );
		
		PrimaryKey mid;
		try
			{
			mid = new PrimaryKey( Integer.parseInt( folder ) );
			}
		catch ( NumberFormatException nfex )
			{
			throw new BuildingServerException( "In messaging rooms you can only upload to a folder corresponding to one of your own messages. (b)" );
			}
		if ( mid==null )
			throw new BuildingServerException( "In messaging rooms you can only upload to a folder corresponding to one of your own messages. (c)" );
			
		if ( !containsMessage( mid ) )
			throw new BuildingServerException( "In messaging rooms you can only upload to a folder corresponding to one of your own messages. (d)" );
		
		MessageSummary summary = getMessageSummary( mid );
		User user = (User)BuildingContext.getContext().getUser();
		if ( !user.getUserId().equals( summary.getUserId() ) )
			throw new BuildingServerException( "You can't attach a file to someone else's message." );
		}


    public int getResourceType()
    	{
    	return RESOURCE_MESSAGINGROOM;
    	}
	}

