/* ======================================================================
   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.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( "] )" );
			}
		}

	}
	
