/* ======================================================================
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.sqldatabase.*;
import org.bodington.server.realm.User;
import org.bodington.server.BuildingServerException;

import java.math.BigInteger;

import java.io.PrintWriter;
import java.io.IOException;

public class UserFileEvent extends org.bodington.server.events.Event
	{
	PrimaryKey user_file_event_id;
	Integer file_entry_id;
	BigInteger file_size;
	String file_name;
	
	public static final int EVENT_UPLOAD		=1;
	public static final int EVENT_DOWNLOAD		=2;
	public static final int EVENT_DELETE		=3;
	public static final int EVENT_EDIT		=4;
	public static final int EVENT_UPLOAD_ZIP	=5;


	static final String[] event_message_a = 
		{
		"",
		" uploaded ",
		" downloaded ",
		" deleted ",
		" edited ",
		" uploaded files in a ZIP archive ",
		};
	
	static final String[] event_message_b = 
		{
		"",
		" replacing the file that was uploaded by ",
		" which was uploaded by ",
		" which was uploaded by ",
		" which was uploaded by ",
		" replacing the ZIP archive that was uploaded by "
		};
	
	
	public UserFileEvent()
		{
		super();
		}
	
	public UserFileEvent( int event_code, PrimaryKey resource_id, PrimaryKey active_user_id, 
							PrimaryKey passive_user_id, 
							Integer file_entry_id, BigInteger file_size, String file_name )
		{
		super();
		
		this.event_code = event_code;
		if ( event_code >= EVENT_UPLOAD && event_code <= EVENT_UPLOAD_ZIP ) 
			importance = Event.IMPORTANCE_MANAGEMENT_MIN;
			
		this.resource_id=resource_id;
		this.active_user_id=active_user_id;
		this.passive_user_id=passive_user_id;
		
		this.file_entry_id = file_entry_id;
		this.file_size = file_size;
		setFileName( file_name );
		}
	
	
	
	
    public PrimaryKey getPrimaryKey()
	    {
        return getUserFileEventId();
    	}

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

	public PrimaryKey getUserFileEventId()
		{
		return user_file_event_id;
		}
		
    public void setUserFileEventId(PrimaryKey key)
    	{
    	user_file_event_id = key;
    	setEventId( key );
    	setUnsaved();
    	}


	public Integer getFileEntryId()
		{
		return file_entry_id;
		}
		
    public void setFileEntryId( Integer e )
    	{
    	file_entry_id = e;
    	setUnsaved();
    	}


	public BigInteger getFileSize()
		{
		return file_size;
		}
		
    public void setFileSize( BigInteger s )
    	{
    	file_size = s;
    	setUnsaved();
    	}


	public void setFileName( String u )
		{
		if ( u==null || u.length()<=63 )
			file_name = u;
		else
			file_name = u.substring( 60 ) + "...";
		setUnsaved();
		}
		
	public String getFileName()
		{
		return file_name;
		}
		

	public String messageTitle()
		{
		if ( event_code>=EVENT_UPLOAD && event_code<=EVENT_UPLOAD_ZIP )
			return "Someone " + event_message_a[event_code] + ".";

		return "Some generated a user file event.";
		}

	public void printProperties( PrintWriter writer, boolean html )
		throws IOException
		{
		super.printProperties( writer, html );
		
		if ( html )
			writer.println( "<PRE>" );
		writer.print( "File entry id : " );   writer.println( file_entry_id==null?"no file entry of":file_entry_id.toString() );
		writer.print( "File size     : " );   writer.println( file_size==null    ?"no file size"    :file_size.toString()     );
		writer.print( "File name     : " );   writer.println( file_name==null    ?"no file name"    :file_name                );
		if ( html )
			writer.println( "</PRE>" );
		}
		
	public void printMessage( PrintWriter writer, boolean html )
		throws IOException
		{
		User active = null;
		User passive = null;
		try
			{
			active = getActiveUser();
			passive = getPassiveUser();
			}
		catch ( BuildingServerException bsex )
			{
			// do nothing
			}

		if ( active==null )
			writer.print( "Someone" );
		else
			writer.print( active.getName() );
			
		if ( event_code>=EVENT_UPLOAD && event_code<=EVENT_UPLOAD_ZIP )
			writer.print( event_message_a[event_code] );
		else
			{
			writer.print( " generated a user file event." );
			return;
			}
		 
		if ( file_name==null )
			writer.print( "a file" );
		else
			writer.print( file_name );
		
		if ( passive!=null )
			{
			writer.print( event_message_b[event_code] );
			writer.print( passive.getName() );
			}
			
		writer.print( "." );
		}

	public void appendMessageBody( StringBuffer buf )
		{
		switch ( event_code )
			{
			case EVENT_UPLOAD:
				buf.append( "Uploaded " + ((file_name==null)?"a file":file_name) );
				break;
			case EVENT_DOWNLOAD:
				buf.append( "Downloaded " + ((file_name==null)?"a file":file_name) );
				break;
			case EVENT_DELETE:
				buf.append( "Deleted " + ((file_name==null)?"a file":file_name) );
				break;
			default:
				buf.append( "Unknown file operation." );
			}
		}
	}
	
