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

import java.io.PrintWriter;


public class OpinionEvent extends org.bodington.server.events.Event
	{
	PrimaryKey opinion_event_id;
	Integer result_id;
	
	public static final int EVENT_START       =1;
	public static final int EVENT_RECORD      =2;

	
	public OpinionEvent()
		{
		super();
		result_id=null;
		}
	
	public OpinionEvent( int event_code, PrimaryKey resource_id, PrimaryKey user_id, Integer result_id )
		{
		super();
		
		this.event_code = event_code;
		if ( event_code == EVENT_START || event_code == EVENT_RECORD ) 
			importance = Event.IMPORTANCE_MANAGEMENT_MIN;
			
		this.resource_id=resource_id;
		active_user_id=user_id;
		passive_user_id=null;
		
		this.result_id = result_id;
		}
	
	
	
	
    public PrimaryKey getPrimaryKey()
	    {
        return getOpinionEventId();
    	}

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

	public PrimaryKey getOpinionEventId()
		{
		return opinion_event_id;
		}
		
    public void setOpinionEventId(PrimaryKey key)
    	{
    	opinion_event_id = key;
    	setEventId( key );
    	setUnsaved();
    	}


	public void setResultId( Integer i )
		{
		result_id = i;
    	setUnsaved();
		}
		
	public Integer getResultId()
		{
		return result_id;
		}
		

	public String messageTitle()
		{
		if ( event_code == EVENT_START )
			return "Someone started to complete a questionnaire.";
		if ( event_code == EVENT_RECORD )
			return "Someone recorded responses to a questionnaire.";
		return "Someone accessed a questionnaire.";
		}

		
		
	public void printProperties( PrintWriter writer, boolean html )
		{
		super.printProperties( writer, html );
		
		if ( html )
			writer.println( "<PRE>" );
		writer.print( "Result Id     : " );   writer.println( result_id==null?"no result id":result_id.toString() );
		if ( html )
			writer.println( "</PRE>" );
		}

	public void printMessage( PrintWriter writer, boolean html )
		{
		User active = null;
		try
			{
			active = getActiveUser();
			}
		catch ( BuildingServerException bsex )
			{
			// do nothing
			}

		//if ( active==null )
		writer.print( "Someone" );
		//else
			//writer.print( active.getName() );
			
		if ( event_code == EVENT_START )
			writer.print( " started to complete a questionnaire (" );
		else
			if ( event_code == EVENT_RECORD )
				writer.print( " recorded responses to a questionnaire" );
			else
				writer.print( " generated an assessment event" );
				
		if ( result_id != null )
			{
			writer.print( " (" );
			writer.print( result_id.toString() );
			writer.print( ")" );
			}

		writer.print( "." );
		}

	}
	
