/* ======================================================================
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.*;
import org.bodington.server.realm.User;
import org.bodington.server.realm.Permission;
import org.bodington.server.resources.Resource;
import org.bodington.server.BuildingServerException;
import org.bodington.text.BigString;

import java.math.BigDecimal;
import java.io.PrintWriter;



public class UserManagementEvent extends org.bodington.server.events.Event
	{
	PrimaryKey user_management_event_id;
	PrimaryKey big_string_id;
	String field;
	String old_value;
	String value;
	
	public static final int EVENT_MIN					     =1;
	
	public static final int EVENT_CREATED           		 =1;
	public static final int EVENT_DATA_CHANGE		         =2;
	public static final int EVENT_PASSPHRASE_SET             =3;
	public static final int EVENT_PASSPHRASE_RESET           =4;
	public static final int EVENT_MEMO                       =5;
	public static final int EVENT_DATA_REQUEST               =6;
	public static final int EVENT_RELEASE_CONSENT            =7;
    public static final int EVENT_HTMLFILTER_CHANGE    =8;
	
	public static final int EVENT_MAX					     =8;


	static final String[] event_message_a = 
		{
		"",
		" created a user",
		" changed",
		" set",
		" reset",
		" recorded a user management memo",
        " requested personal data",
        " changed personal data release consent setting",
        " changed HTML filtering options"
		};
	
	static final String[] event_message_b = 
		{
		"",
		".",
		" his/her own personal data.",
		" his/her own pass phrase.",
		" his/her own pass phrase.",
		" against his/her own record.",
		" against his/her own record.",
		" against his/her own record.",
        " against his/her own record."
		};

	static final String[] event_message_c = 
		{
		"",
		"",
		" the personal data of ",
		" the pass phrase of ",
		" the pass phrase of ",
		" against ",
		" belonging to ",
		" for ",
        " for "
        
		};



	
	public UserManagementEvent()
		{
		super();
		field=null;
		value=null;
		}
	
	public UserManagementEvent( int event_code, 
							PrimaryKey resource_id, 
							PrimaryKey active_user_id, 
							PrimaryKey passive_user_id, 
							PrimaryKey big_string_id,
							String field,
							String old_value,
							String value )
		{
		super();
		
		this.event_code = event_code;
		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.big_string_id = big_string_id;
		
		this.field = field;
		this.old_value = old_value;
		this.value = value;
		}
	
	
    public PrimaryKey getPrimaryKey()
	    {
        return getUserManagementEventId();
    	}

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

	public PrimaryKey getUserManagementEventId()
		{
		return user_management_event_id;
		}
		
    public void setUserManagementEventId(PrimaryKey key)
    	{
    	user_management_event_id = key;
    	setEventId( key );
    	setUnsaved();
    	}


	public PrimaryKey getBigStringId()
		{
		return big_string_id;
		}
		
    public void setBigStringId( PrimaryKey key )
    	{
    	big_string_id = key;
    	setUnsaved();
    	}



	public void setField( String f )
		{
		if ( f!=null && f.length()>16 )
			field = f.substring( 0, 16 );
		else
			field = f;
    	setUnsaved();
		}
		
	public String getField()
		{
		return field;
		}
		

	public void setValue( String f )
		{
		if ( f!=null && f.length()>64 )
			value = f.substring( 0, 64 );
		else
			value = f;
    	setUnsaved();
		}
		
	public String getValue()
		{
		return value;
		}
		

	public void setOldValue( String f )
		{
		if ( f!=null && f.length()>64 )
			old_value = f.substring( 0, 64 );
		else
			old_value = f;
    	setUnsaved();
		}
		
	public String getOldValue()
		{
		return old_value;
		}
		
		

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

		return "Someone generated a user management event.";
		}

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

	public void printMessage( PrintWriter writer, boolean html )
		{
		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_MIN && event_code<=EVENT_MAX )
			writer.print( event_message_a[event_code] );
		else
			{
			writer.print( " generated a user management event." );
			return;
			}

		if ( passive==null )
			writer.print( event_message_b[event_code] );
		else
			{
			writer.print( event_message_c[event_code] );
			writer.print( passive.getName() );
			writer.print( "." );
			}

		if ( big_string_id != null )
			try
				{
				BigString big = BigString.findBigString( big_string_id );
				writer.print( " Memo: " );
				writer.print( big.getString() );
				writer.print( " " );
				}
			catch ( Exception ex )
				{
				writer.print( " It wasn't possible to find the memo for this event." );
				}


		if ( field!=null )
			{
			writer.print( " ( " );
			writer.print( field );
			writer.print( " changed from [" );
			writer.print( old_value==null?"null":old_value );
			writer.print( "] to [" );
			writer.print( value==null?"null":value );
			writer.print( "] )" );
			}
		}

	}
	