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

import org.apache.log4j.Logger;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Hashtable;
import java.sql.Timestamp;
import java.io.*;

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.ParserConfigurationException;  


import org.bodington.assessment.ims.QuestionnaireImsQtiHandler;
import org.bodington.database.PrimaryKey;
import org.bodington.logging.LoggingUtils;
import org.bodington.server.*;
import org.bodington.text.BigString;
import org.bodington.server.realm.User;
import org.bodington.server.realm.Permission;
import org.bodington.server.resources.Resource;
import org.bodington.util.CSVWriter;


/**
 * Concrete implementation of a questionnaire session.
 * @see QuestionnaireQuestion
 * @author Jon Maber
 * @author Alexis O'Connor
 */
public class QuestionnaireSessionImpl
	extends org.bodington.server.SingleResourceSession 
	implements QuestionnaireSession 
	{
    private static Logger log = Logger.getLogger(QuestionnaireSessionImpl.class);
    private Questionnaire questionnaire;
	
	public QuestionnaireSessionImpl()
		throws BuildingServerException
		{
		super();
		}
	
	public Questionnaire getQuestionnaire()
		throws BuildingServerException
		{
        return questionnaire;
		}
		
	public List getQuestionnaireQuestionIds() 
        throws BuildingServerException
        {
        Vector list = new Vector();
        Iterator iterator = questionnaire.getQuestions().iterator();
        while ( iterator.hasNext() )
            {
            QuestionnaireQuestion question 
                = (QuestionnaireQuestion) iterator.next();
            list.addElement( question.getQuestionnaireQuestionId() );
            }

        return list;
        }
	
	public Map getQuestionnaireQuestions()
		throws BuildingServerException
		{
		Hashtable table = new Hashtable();
        Iterator iterator = questionnaire.getQuestions().iterator();
		while ( iterator.hasNext() )
			{
            QuestionnaireQuestion question 
                = (QuestionnaireQuestion)iterator.next();
			table.put( question.getQuestionnaireQuestionId(), question );
			}
		
		return table;
		}
	
	public List getQuestionnaireQuestionsInOrder()
		throws BuildingServerException
		{
        return questionnaire.getQuestions();
		}
	
	public Map getAllQuestionnaireResults() throws BuildingServerException
        {
        Map allResults = new Hashtable();
        List userList = getQuestionnaireUsers();
        Iterator users = userList.iterator();
        while ( users.hasNext() )
            {
            User user = (User)users.next();
            List results = getQuestionnaireResults( user.getUserId() );
            allResults.put( user, results );
            }

        return allResults;
        }
	
	public QuestionnaireQuestion createQuestion()
		throws BuildingServerException
		{
		return questionnaire.createQuestion();
		}
    
	public QuestionnaireQuestion createQuestion( int ordinal ) 
        throws BuildingServerException
        {
        return questionnaire.createQuestion( ordinal );
        }

    public void removeQuestion( PrimaryKey id )
		throws BuildingServerException
		{
        questionnaire.removeQuestion( id );
		}
	
	public void changeQuestion( PrimaryKey id, QuestionnaireQuestion model )
		throws BuildingServerException
		{
        questionnaire.changeQuestion( id, model);
		}
	
	public void changeQuestionText( PrimaryKey qid, PrimaryKey tid, String new_text )
		throws BuildingServerException
		{
        questionnaire.changeQuestionText( qid, tid, new_text );
		}
	
	public PrimaryKey record( boolean newResult, PrimaryKey[] q_question_ids,
        List selections, String[] comments ) throws BuildingServerException
        {
        User user = (User)BuildingContext.getContext().getUser();
        Map q_questions = this.getQuestionnaireQuestions();
        QuestionnaireResponse q_response;
        QuestionnaireQuestion q_question;
        QuestionnaireResult q_result 
            = newResult ? null : getQuestionnaireResult( user.getUserId() );
        if ( q_result == null )
            {
            q_result = new QuestionnaireResult();
            q_result.setQuestionnaireId( getResource().getResourceId() );
            q_result.setUserId( user.getUserId() );
            q_result.setWhenStarted( new Timestamp( System.currentTimeMillis() ) );
            q_result.setWhenSaved( new Timestamp( System.currentTimeMillis() ) );
            q_result.save();
            }
        Map responses = q_result.getQuestionnaireResponses();
        q_result.setWhenSaved( new Timestamp( System.currentTimeMillis() ) );
        q_result.save();

        for ( int i = 0; i < q_question_ids.length; i++ )
            {
            q_question = (QuestionnaireQuestion)q_questions.get( 
                q_question_ids[i] );
            if ( q_question == null )
                throw new BuildingServerException(
                    "Unable to find question data." );
            q_response = (QuestionnaireResponse)responses.get( 
                q_question_ids[i] );
            if ( q_response == null )
                {
                q_response = new QuestionnaireResponse();
                q_response.setQuestionnaireResultId( 
                    q_result.getQuestionnaireResultId() );
                q_response.setQuestionnaireQuestionId( q_question_ids[i] );
                q_response.save();
                }
            byte[] selection = (byte[])selections.get( i );
            
            if ( q_question.isMultipleResponse() )
                {
                q_response.setSelection( selection );
                }
            else
                {
                byte item = 100; // no selection.
                if ( selection.length > 0 )
                    {
                    item = selection[0];
                    if ( item < 0 || item >= q_question.getStatementCount() )
                        item = 100;
                    }
                q_response.setItem( item );
                }
            q_response.setComment( comments[i] );
            q_response.save();
            }

        return q_result.getQuestionnaireResultId();
        }
	
	public QuestionnaireResult getQuestionnaireResult()
		throws BuildingServerException
		{
		return getQuestionnaireResult( getUserId() );
		}
		
	public QuestionnaireResult getQuestionnaireResult( PrimaryKey uid )
        throws BuildingServerException
        {
        List list = getQuestionnaireResults( uid );
        return list.isEmpty() 
            ? null : (QuestionnaireResult)list.get( list.size() - 1 );
        }
    
    private List getQuestionnaireResults( PrimaryKey userID )
        throws BuildingServerException
        {
        Vector list = new Vector();
        // NOTE: the resource_id of the result is that of the questionnaire!
        Enumeration enumeration = QuestionnaireResult.findQuestionnaireResults( 
                "resource_id = " + getQuestionnaire().getQuestionnaireId() + 
                " AND user_id = " + userID );

        while ( enumeration.hasMoreElements() )
            list.addElement( enumeration.nextElement() );

        return list;
        }
		
	public List getQuestionnaireResults() throws BuildingServerException
        {
        return getQuestionnaireResults( getUserId() );
        }

	public List analyse( String whereSQL, QuestionnaireAnalysis analysis )
		throws BuildingServerException
		{
		Vector list = new Vector();
		Enumeration enumeration;
		QuestionnaireResult q_result;
		QuestionnaireResponse q_response;
		QuestionnaireQuestion q_question;
		Map responses;
		List questions=null;
		
        if (whereSQL == null) 
            whereSQL = "resource_id = " + getResource().getResourceId();
            
		enumeration = QuestionnaireResult.findQuestionnaireResults( whereSQL );

		if ( analysis != null )
			questions = getQuestionnaireQuestionsInOrder();	

		while ( enumeration.hasMoreElements() )
			{
			q_result = (QuestionnaireResult)enumeration.nextElement();
			list.addElement( q_result );
			
			if ( analysis == null )
				continue;
			
			//now add analysis...
			
			analysis.addQuestionnaireResult( q_result );
			responses = q_result.getQuestionnaireResponses();
			
			for ( int i=0; i<questions.size(); i++ )
				{
				q_question = (QuestionnaireQuestion)questions.get( i );
				q_response = (QuestionnaireResponse)responses.get( q_question.getQuestionnaireQuestionId() );
				if ( q_response != null )
					analysis.addQuestionnaireResponse( q_question, q_result, q_response );
				}
			
			}

		return list;
		}
	
	public String exportXML( boolean include_results, boolean include_responses )
        throws BuildingServerException
        {
        Questionnaire paper = getQuestionnaire();
        List questions = getQuestionnaireQuestionsInOrder();
        String fileName = paper.getName() + ".xml";

        if ( paper == null || questions == null )
            throw new BuildingServerException(
                "Unable to find questionnaire data." );

        try
            {
            File xmlfile = BuildingContext.createTempFile( "bod", ".xml" );

            PrintWriter xmlout = new PrintWriter( new FileWriter( xmlfile ) );

            xmlout.print( "<?xml version=\"1.0\" standalone=\"no\"?>" );
            xmlout.println();
            xmlout.print( "<!DOCTYPE questestinterop SYSTEM "
                    + "\"http://www.imsproject.org/xml/question/FinalQTIv101/xmla/dtds/QTIcorencdtd/IMS_QTIv1p01.dtd\">" );
            xmlout.println();
            xmlout.print( "<!-- Experimental IMS output -->" );

            xmlout.println();
            xmlout.println();

            xmlout.print( "<questestinterop>" );

            xmlout.println();

            for ( int i = 0; i < questions.size(); i++ )
                {
                QuestionnaireQuestion question = 
                    (QuestionnaireQuestion)questions.get( i );

                // ask the question to export itself, allowing composite output
                question.exportXml( xmlout, false );
                }
            xmlout.print( "</questestinterop>" );

            xmlout.println();
            xmlout.println();

            xmlout.close();
            getUploadedFileSession().transferFile( xmlfile.getAbsolutePath(), fileName,
                "application/octet-stream" );
            }
        catch ( IOException ioex )
            {
            log.error( ioex.getMessage(), ioex );
            throw new BuildingServerException( "Problem creating XML file: "
                + ioex.getMessage() );
            }
        return fileName;
        }
	    
	public void importXML( String file_name )
		throws BuildingServerException
	    {
        try
            {
            // Parse the input
            XMLReader reader = XMLReaderFactory.createXMLReader( 
            		BuildingContext.getProperty( "xmlrepository.driver", "org.apache.xerces.parsers.SAXParser" ) );
            QuestionnaireImsQtiHandler handler = new QuestionnaireImsQtiHandler( reader );
            reader.setContentHandler( handler );
            
            reader.setProperty( "http://xml.org/sax/properties/lexical-handler", handler );
            reader.setEntityResolver( handler );
            
            File input_file = new File( file_name );
            if ( !input_file.exists() )
                throw new BuildingServerException( "Couldn't open the uploaded file." );
                
            reader.parse( "file:" + input_file );
            
            Vector questions = handler.getQuestions();
            QuestionnaireQuestion question=null, previous_question=null;
            
            for ( int i=0; i<questions.size(); i++ )
                {
                previous_question = question;
                question = (QuestionnaireQuestion)questions.elementAt( i );
                
                if (    previous_question != null &&
                        question.getStatementCount() == 0 && 
                        question.isCanComment() && 
                        question.getIntroduction().indexOf( "This is the comment box for the previous question." )>=0 )
                    {
                    // merge this question with previous
                    previous_question.setCanComment( true );
                    previous_question.save();
                    //remove current
                    questions.removeElementAt( i );
                    //fix index so we point at next question that has now moved down one place
                    i--;
                    continue;
                    }
                
                question.setQuestionnaireId( getResource().getResourceId() );
                question.setOrdinal( questionnaire.nextOrdinal());
		        question.save();
                }
            // Tell the questionnaire to refresh itself.
            questionnaire.refresh();
            }
       catch (SAXParseException spe)
            {
            LoggingUtils.logSAXException(log,spe);
            throw new BuildingServerException( "Problem importing XML file: " + spe.getMessage() );
            }
        catch (Throwable t)
            {
            log.error(	t.getMessage(), t );
            throw new BuildingServerException( "Problem importing XML file: " + t.getMessage() );
            }
	    }
    
    /**
     * Overridden so that we can initialize our corresponding questionnaire
     * instance. Otherwise, we just delegate to the corresponding method in our
     * superclass.
     * @see Questionnaire
     */
    public void setResource( PrimaryKey resource_id ) 
        throws BuildingServerException
        {
        // NOTE: we override this method so as to initialize our questionnaire:
        super.setResource( resource_id );
        this.questionnaire = Questionnaire.findQuestionnaire( 
            getResource().getResourceId() );
        }

    /**
     * Indicates whether or not it is acceptable to display the non-respondents.
     * This implementation of the method will return <code>true</code> in the
     * following circumstances:
     * <ul>
     * <li>for <em>all</em> non-anonymous surveys.
     * <li>anonymous surveys with only 1 respondent.
     * </ul>
     * @return <code>true</code> if it is possible to show the
     *         non-respondents, otherwise <code>false</code>.
     * @see Questionnaire#isAnonymous()
     * @throws BuildingServerException
     */
    public boolean isCanShowNonRespondents() throws BuildingServerException
        {
        // TODO: it's likely that a more sophisticated algorithm is needed.
        return !getQuestionnaire().isAnonymous()
            || getQuestionnaireUsers().size() != 1;
        }

    public List getNonRespondents() throws BuildingServerException
        {
        List nonRespondents = Collections.list( 
            getQuestionnaire().everyoneWhoCan( Permission.RECORD, false ) );
        nonRespondents.removeAll( getQuestionnaireUsers() );

        return nonRespondents;
        }
    
    public List getSpecialGroupsWithPermission() throws BuildingServerException
    {
        Hashtable groups = getQuestionnaire().everySpecialGroupWhoCan(
            Permission.RECORD, false );
        
        return Collections.list( groups.elements() );
    }

    public List getQuestionnaireUsers() throws BuildingServerException
        {
        List users = new Vector();
        // TODO: factoring out candidate - (taken from TextQ equivalent).
        Enumeration enumeration = User.findUsers( "user_id IN (SELECT user_id "
            + "FROM questionnaire_results WHERE resource_id = "
            + getResource().getResourceId() + ")", "surname, initials" );

        while ( enumeration.hasMoreElements() )
            {
            users.add( enumeration.nextElement() );
            }

        return users;
        }

    public boolean isAvailable() throws BuildingServerException
        {
        // TODO Auto-generated method stub
        Timestamp deadline = getQuestionnaire().getDeadline();
        if (deadline == null)
            return true;
        
        return new Date().before( deadline );
        }

    public boolean isReviewable() throws BuildingServerException
        {
        // TODO: is this OK?
        return !(getQuestionnaire().isPartiallyAnonymous() && isAvailable());
        }

    public boolean getCanModifyDeadline() throws BuildingServerException
        {
        return isAvailable();
        }
	}
	
