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

import java.sql.*;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Vector;
import java.math.BigDecimal;
import java.math.BigInteger;

import org.bodington.server.*;
import org.bodington.assessment.*;
import org.bodington.server.resources.UploadedFileSummary;
import org.bodington.server.resources.UploadedFile;
import org.bodington.server.realm.User;
import org.bodington.server.realm.Permission;
import org.bodington.server.events.*;
import org.bodington.database.*;
import org.bodington.text.BigString;


public class PigeonHoleSessionImpl 
	extends org.bodington.server.SingleResourceSession 
	implements org.bodington.assessment.PigeonHoleSession
	{
	int max_ordinal=0;
	boolean transfer_in_progress;
	
	
	
	public PigeonHoleSessionImpl()
		{
		super();
		transfer_in_progress=false;
		}
	
	

	public PigeonHole getPigeonHole()
		throws BuildingServerException
		{
		PigeonHole phole = PigeonHole.findPigeonHole( getResource().getResourceId() );
		return phole;
		}
		
    public Enumeration analyse(String where_clause, PigeonHoleAnalysis analysis)
    	{
	    // This method is derived from interface org.bodington.assessment.PigeonHoleSession
        // to do: code goes here
        return null;
    	}



    public Vector getPigeonHoleQuestionIds()
    	throws BuildingServerException
    	{
		Vector list = new Vector();
		
		PigeonHoleQuestion question;
		Enumeration enumeration = PigeonHoleQuestion.findPigeonHoleQuestions( "resource_id = " + getResource().getResourceId().toString(), "ordinal" );
		while ( enumeration.hasMoreElements() )
			{
			question = (PigeonHoleQuestion)enumeration.nextElement();
			list.addElement( question.getPigeonHoleQuestionId() ); 
			if ( question.getOrdinal() > max_ordinal )
				max_ordinal = question.getOrdinal();
			}
		
		return list;
    	}
	
    public Hashtable getPigeonHoleQuestions()
    	throws BuildingServerException
    	{
		Hashtable table = new Hashtable();
		
		PigeonHoleQuestion question;
		Enumeration enumeration = PigeonHoleQuestion.findPigeonHoleQuestions( "resource_id = " + getResource().getResourceId().toString() );
		while ( enumeration.hasMoreElements() )
			{
			question = (PigeonHoleQuestion)enumeration.nextElement();
			table.put( question.getPigeonHoleQuestionId(), question );
			if ( question.getOrdinal() > max_ordinal )
				max_ordinal = question.getOrdinal();
			}
		
		return table;
	    }

	public Vector getPigeonHoleQuestionsInOrder()
		throws BuildingServerException
		{
		Vector list = new Vector();
		
		PigeonHoleQuestion question;
		Enumeration enumeration = PigeonHoleQuestion.findPigeonHoleQuestions( "resource_id = " + getResource().getResourceId().toString(), "ordinal" );
		while ( enumeration.hasMoreElements() )
			{
			question = (PigeonHoleQuestion)enumeration.nextElement();
			list.addElement( question ); 
			if ( question.getOrdinal() > max_ordinal )
				max_ordinal = question.getOrdinal();
			}
		
		return list;
		}
	
    public Enumeration getResultsByUser()
    {
        // This method is derived from interface org.bodington.assessment.PigeonHoleSession
        // to do: code goes here
        return null;
    }

    public Hashtable getStrings() throws BuildingServerException
    	{
		Hashtable table = getPigeonHoleQuestions();
		
		PigeonHoleQuestion question;
		BigString big_string;
		PrimaryKey id;
		Enumeration enumeration = table.elements();
		while ( enumeration.hasMoreElements() )
			{
			question = (PigeonHoleQuestion)enumeration.nextElement();
			id = question.getNotesBigStringId();
			big_string = BigString.findBigString( id );
			table.put( id, big_string.getString() );
			}
			
		return table;
	    }

    public void setDates(Timestamp open, Timestamp deadline, Timestamp penalty)
    	throws BuildingServerException
    	{
		PigeonHole paper = PigeonHole.findPigeonHole( getResource().getResourceId() );
		if ( paper == null )
			throw new BuildingServerException( "Can't find pigeon hole." );
		paper.setDateOpen( open );
		paper.setDateDeadline( deadline );
		paper.setDatePenalty( penalty );
		paper.save();
    	}

    public void setMaximumMark( int m ) throws BuildingServerException
    	{
		PigeonHole paper = PigeonHole.findPigeonHole( getResource().getResourceId() );
		if ( paper == null )
			throw new BuildingServerException( "Can't find pigeon hole." );
		paper.setMaximumMark( m );
		paper.save();
    	}

	public PigeonHoleQuestion createQuestion()
		throws BuildingServerException
		{
		PigeonHoleQuestion question = new PigeonHoleQuestion();
		
		question.setResourceId( getResource().getResourceId() );
		question.setNotes( "Notes for marker here." );
		question.setOrdinal( max_ordinal+100 );
		question.setAvailable( 1 );
		question.setWeight( new BigDecimal( "1.0" ) );
		question.save();
		
		return question;
		}
	
	public void removeQuestion( PrimaryKey id )
		throws BuildingServerException
		{
		PigeonHoleQuestion question =  PigeonHoleQuestion.findPigeonHoleQuestion( id );
		if ( question == null )
			throw new BuildingServerException( "Question not found." );

		if ( !question.getResourceId().equals( getResource().getResourceId() ) )
			throw new BuildingServerException( "Question doesn't belong in this MCQ paper." );

		question.delete();
		}
	
	public void changeQuestion( PrimaryKey id, PigeonHoleQuestion model )
		throws BuildingServerException
		{
		PigeonHoleQuestion question =  PigeonHoleQuestion.findPigeonHoleQuestion( id );
		if ( question == null )
			throw new BuildingServerException( "Question not found." );

		if ( !question.getResourceId().equals( getResource().getResourceId() ) )
			throw new BuildingServerException( "Question doesn't belong in this MCQ paper." );

		question.setOrdinal( model.getOrdinal() );
		question.setAvailable( model.getAvailable() );
		question.setWeight( model.getWeight() );
		question.save();

		if ( question.getOrdinal() > max_ordinal )
			max_ordinal = question.getOrdinal();
		}
	
	public void changeQuestionText( PrimaryKey qid, PrimaryKey tid, String new_text )
		throws BuildingServerException
		{
		PigeonHoleQuestion question =  PigeonHoleQuestion.findPigeonHoleQuestion( qid );
		if ( question == null )
			throw new BuildingServerException( "Question not found." );
			
		if ( !question.getResourceId().equals( getResource().getResourceId() ) )
			throw new BuildingServerException( "Question doesn't belong in this MCQ paper." );

		boolean found = false;
		found = tid.equals( question.getNotesBigStringId() );

		if ( !found )
			throw new BuildingServerException( "Can't identify qhich part of question to edit." );

		BigString big = BigString.findBigString( tid );
		big.setString( new_text );
		big.save();
		}
	
	public boolean canUpload() throws BuildingServerException
		{
		return canUpload( null, new StringBuffer() );
		}
		
	/**
	 * Work out if an upload can be made.
	 * Currently is does several checks, the main one being that the users has 
	 * upload permission.
	 * @param uid The primary key of the user to check. If <code>null</code> check
	 * the current user.
	 * @param message The StringBuffer to output any messages to.
	 * @return <code>true</code> if the user can upload a file.
	 */
	private boolean canUpload( PrimaryKey uid, StringBuffer message ) throws BuildingServerException
		{
		PigeonHole ph = getPigeonHole();
		PigeonHoleEntry entry;
		User user;
		
		if ( uid==null )
			{
			user = (User)BuildingContext.getContext().getUser();
			entry = getPigeonHoleEntry();
			}
		else
			{
			user = User.findUser( uid );
			entry = getPigeonHoleEntry( uid );
			}
		
		if ( user == null )
			{
			message.append( "it wasn't possible to find a record of the user in the user directory." );
			return false;
			}
		
		if ( !getResource().checkPermission( user, Permission.UPLOAD ) )
			{
			if ( uid == null )
				message.append( "you havn't been granted the upload access right." );
			else
				message.append( "the user hasn't been granted the upload access right." );
			return false;
			}
			
		if ( entry == null )
			{
			entry = new PigeonHoleEntry();
			entry.setStatus( PigeonHoleEntry.STATUS_UPLOAD_BY_DATE );
			}
			
		java.util.Date now					=new java.util.Date();
		java.util.Date now_minus_24_hours	=new java.util.Date( now.getTime()-(24L*60L*60L*1000L) );
		
		switch ( entry.getStatus() )
			{
			case PigeonHoleEntry.STATUS_UPLOAD_BY_DATE:
				if ( ph.getDateOpen() !=null && ph.getDateOpen().after( now ) )
					{
					message.append( " the pigeon hole is not yet available for use " );
					return false;
					}
				
				if ( ph.getDateDeadline() == null )
					return true;
				if ( ph.getDateDeadline().after( now ) )
					return true;
				if ( ph.getDatePenalty() == null )
					{
					message.append( " the deadline for uploading to this pigeon hole has passed and uploading files after the deadline is not allowed " );
					return false;
					}
				if ( ph.getDatePenalty().before( now ) )
					{
					message.append( " the penalty period deadline for uploading to this pigeon hole has passed " );
					return false;
					}
				// at this point we know that "now" lies between deadline and penalty
				// allowing them to upload depends on whether and when they previously
				// uploaded a file.

				//  if user has never uploaded a file let them do so now
				if ( entry.getUploadedFileId() == null )
					return true;

				// if user uploaded before deadline they can't change it
				if ( entry.getWhenSaved().before( ph.getDateDeadline() ) )
					{
					message.append( " a file was uploaded before the deadline and one can't change a file now the deadline has passed " );
					return false;
					}

				// if user uploaded during the penalty period but more than
				// 24 hours ago they can't change it.
				if ( entry.getWhenSaved().before( now_minus_24_hours ) )
					{
					message.append( " a file was uploaded during the penalty period but more than 24 hours ago " );
					return false;
					}

				// if we reached here then the user must have uploaded within the
				// last 24 hours and they can replace their work now and get another
				// 24 hours.
				return true;

			case PigeonHoleEntry.STATUS_UPLOAD_CHANGEABLE:
				return true;

			case PigeonHoleEntry.STATUS_UPLOAD_LOCKED:
			case PigeonHoleEntry.STATUS_UPLOAD_PROCESSED:
			case PigeonHoleEntry.STATUS_UPLOAD_MARKED:
			case PigeonHoleEntry.STATUS_UPLOAD_RETURNED:
				message.append( " the manager of this pigeon hole has locked your file " );
				return false;			

			default:
				message.append( " a technical problem has been detected " );
				return false;			
			}
		}
		
	public boolean canUpload( PrimaryKey user_id ) throws BuildingServerException
		{
		return canUpload( user_id, new StringBuffer() );
		}
	
	public boolean canSeeMark() throws BuildingServerException
		{
		return canSeeMark( null );
		}
		
	public boolean canSeeMark( PrimaryKey uid ) throws BuildingServerException
		{
		PigeonHole paper = getPigeonHole();
		PigeonHoleEntry entry;
		if ( uid==null )
			entry = getPigeonHoleEntry();
		else
			entry = getPigeonHoleEntry( uid );
		
		if ( entry == null )
			return false;
			
		return entry.getStatus() == PigeonHoleEntry.STATUS_UPLOAD_RETURNED;
		}
		
	
	public String denyUploadMessage() throws BuildingServerException
		{
		StringBuffer message = new StringBuffer();
		canUpload( null, message );
		return message.toString();
		}
		
	public String denyUploadMessage( PrimaryKey user_id ) throws BuildingServerException
		{
		StringBuffer message = new StringBuffer();
		canUpload( user_id, message );
		return message.toString();
		}
	
	public PigeonHoleEntry transferFileForAssessment( String current_pathname, String file_name, String mime_type )
		throws BuildingServerException
		{
		// forces uppload to sub directory named after user id
		// only allows one file per user.  (Fixes file name?)
		User user = (User)BuildingContext.getContext().getUser();
		PrimaryKey uid = user.getUserId();
		PigeonHoleEntry entry;
		UploadedFileSummary uf;

		if ( !canUpload() )
			throw new BuildingServerException( "You are not allowed to upload a file at this time." );
	
		try
			{
			synchronized ( this )
				{
				if ( transfer_in_progress )
					throw new BuildingServerException( "The system is currently receiving a file from you and you can't send another until it has finished." );
				transfer_in_progress = true;
				}
				
			entry = getPigeonHoleEntry();
			if ( entry == null )
				{
				entry = new PigeonHoleEntry();
				entry.setResourceId( getResource().getResourceId() );
				entry.setUserId( uid );
				entry.setFeedback( "" );
				entry.setComments( "" );
				}

			if ( entry.getUploadedFileId() !=null )
				{
				UploadedFile old_uf = UploadedFile.findUploadedFile( entry.getUploadedFileId() );
				if ( old_uf != null )
					old_uf.setDeleted( true );
				}
			
			uf = getUploadedFileSession().transferFile( current_pathname, uid.toString() + "/" + file_name, mime_type );
			
			entry.setUploadedFileId( uf.getUploadedFileId() );
			entry.setWhenSaved( uf.getUpdatedTime() );
			entry.save();
			
	    	UserFileEvent event = new UserFileEvent(UserFileEvent.EVENT_UPLOAD,
                getResource(), uf);
			event.save();
			}
		finally
			{
			transfer_in_progress = false;
			}

		return entry;
		}

	public PigeonHoleEntry recordStatus( PrimaryKey entry_id, int status )
		throws BuildingServerException
		{
		PigeonHoleEntry entry = PigeonHoleEntry.findPigeonHoleEntry( entry_id );
		if ( entry==null )
			throw new BuildingServerException( "Can't find entry." );
			
		entry.setStatus( status );
		entry.save();
		
		return entry;
		}

	public PigeonHoleEntry recordMarks( PrimaryKey entry_id, PrimaryKey[] pigeonhole_question_ids, int[] marks, String feedback, String comments )
		throws BuildingServerException
		{
		PigeonHole ph = getPigeonHole();
		Hashtable responses = getPigeonHoleResponses( entry_id );
		PigeonHoleEntry entry = PigeonHoleEntry.findPigeonHoleEntry( entry_id );
		PigeonHoleResponse response;
		Hashtable questions = getPigeonHoleQuestions();
		PigeonHoleQuestion question;
		
		double total_mark=0.0;
		double total_weight=0.0;
		boolean incomplete=false;
		
		for ( int i=0; i<pigeonhole_question_ids.length; i++ )
			{
			question = (PigeonHoleQuestion)questions.get( pigeonhole_question_ids[i] );
			response = (PigeonHoleResponse)responses.get( pigeonhole_question_ids[i] );
			if ( response == null )
				{
				response = new PigeonHoleResponse();
				response.setPigeonHoleEntryId( entry_id );
				response.setPigeonHoleQuestionId( pigeonhole_question_ids[i] );
				}
			if ( marks[i]<0 )
				{
				response.setMark( null );
				incomplete=true;
				}
			else
				{
				response.setMark( new Integer( marks[i] ) );
				total_mark += question.getWeight().doubleValue() * (double)marks[i] / (double)question.getAvailable();
				total_weight += question.getWeight().doubleValue();
				}
			response.save();
			}
		
		if ( incomplete )
			entry.setMark( null );
		else
			entry.setMark( new Integer( (int)Math.round((double)ph.getMaximumMark() * total_mark/total_weight )) );
		
		entry.setStatus( PigeonHoleEntry.STATUS_UPLOAD_PROCESSED );
		entry.setWhenMarked( new Timestamp( System.currentTimeMillis() ) );
		User user = (User)BuildingContext.getContext().getUser();
		entry.setMarkerUserId( user.getUserId() );
		entry.setFeedback( feedback );
		entry.setComments( comments );
		entry.save();
		
		return entry;
		}
		
	public PigeonHoleEntry recordAdjustedMark( PrimaryKey entry_id, int adjusted_mark, String feedback, String comments )
		throws BuildingServerException
		{
		PigeonHole tq = getPigeonHole();
		PigeonHoleEntry entry = PigeonHoleEntry.findPigeonHoleEntry( entry_id );
		
		entry.setAdjustedMark( new Integer( adjusted_mark ) );
		entry.setFeedback( feedback );
		entry.setComments( comments );
		entry.save();
		
		return entry;
		}


	public PigeonHoleEntry getPigeonHoleEntry()
		throws BuildingServerException
		{
		User user = (User)BuildingContext.getContext().getUser();
		return getPigeonHoleEntry( user.getUserId() );
		}
		
	public PigeonHoleEntry getPigeonHoleEntry( PrimaryKey uid )
		throws BuildingServerException
		{
		PigeonHoleEntry entry = 
			PigeonHoleEntry.findPigeonHoleEntry( 
				"resource_id = " + getResource().getResourceId() + " AND user_id = " + uid );
		
		return entry;
		}
		
	public Hashtable getPigeonHoleEntries() throws BuildingServerException
		{
		Hashtable table = new Hashtable();
		PigeonHoleEntry entry;
		
		Enumeration enumeration= 
			PigeonHoleEntry.findPigeonHoleEntries( "resource_id = " + getResource().getResourceId() );
		
		while ( enumeration.hasMoreElements() )
			{
			entry = (PigeonHoleEntry)enumeration.nextElement();
			table.put( entry.getUserId(), entry );
			}

		return table;
		}
		
		
	public Vector getPigeonHoleUsers() throws BuildingServerException
		{
		Vector users= new Vector();
		
		Enumeration enumeration = 
			User.findUsers(
				"user_id IN (SELECT user_id FROM pigeonhole_entries WHERE resource_id = " +
				getResource().getResourceId() +
				")",
				"surname, initials" );
		
		while ( enumeration.hasMoreElements() )
			{
			users.addElement( enumeration.nextElement() );
			}
		
		return users;
		}


	public Hashtable getPigeonHoleResponses( PrimaryKey entryid )
		throws BuildingServerException
		{
		Hashtable table = new Hashtable();
		PigeonHoleResponse response;
		
		Enumeration enumeration = PigeonHoleResponse.findPigeonHoleResponses( "pigeonhole_entry_id = " + entryid );
		while ( enumeration.hasMoreElements() )
			{
			response = (PigeonHoleResponse)enumeration.nextElement();
			table.put( response.getPigeonHoleQuestionId(), response );
			}
		
		return table;
		}

	}
