/* ======================================================================
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 java.util.Enumeration;

import org.bodington.sqldatabase.*;
import org.bodington.database.*;
import org.bodington.server.BuildingServerException;
import org.bodington.text.BigString;

/**
 * Class that encapsulates a users response to a questionnaire question. This
 * class pertains to an individual questionnaire question. It includes
 * information such as a reference to the containing questionnaire result, the
 * response comment and the statement that was chosen.
 * @see org.bodington.assessment.QuestionnaireQuestion
 * @see org.bodington.assessment.QuestionnaireResult
 * @author Jon Maber
 * @author Alexis O'Connor
 */
public class QuestionnaireResponse extends org.bodington.sqldatabase.SqlPersistentObject
	{
	private PrimaryKey primary_key;
	private PrimaryKey questionnaire_result_id;
	private PrimaryKey questionnaire_question_id;
	private PrimaryKey comment_big_string_id;
	private byte item;
    private QuestionnaireResult result;
    
    public QuestionnaireResponse()
        {
        }
    
	public static QuestionnaireResponse findQuestionnaireResponse( PrimaryKey key )
	    throws BuildingServerException
	    {
	    return (QuestionnaireResponse)findPersistentObject( key, QuestionnaireResponse.class.getName() );
	    }
	
	public static QuestionnaireResponse findQuestionnaireResponse( String where )
	    throws BuildingServerException
	    {
	    return (QuestionnaireResponse)findPersistentObject( where, QuestionnaireResponse.class.getName() );
	    }
	
	public static Enumeration findQuestionnaireResponses( String where )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, QuestionnaireResponse.class.getName() );
	    }
	
	public static Enumeration findQuestionnaireResponses( String where, String order )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, order, QuestionnaireResponse.class.getName() );
	    }
	
    public PrimaryKey getPrimaryKey()
    	{
        return primary_key;
    	}

    public void setPrimaryKey(PrimaryKey key)
    	{
        primary_key = key;
        setUnsaved();
    	}

	public PrimaryKey getQuestionnaireResponseId()
		{
		return getPrimaryKey();
		}
		
	public void setQuestionnaireResponseId( PrimaryKey id )
		{
		setPrimaryKey(id);
		}

	/**
     * Get the ID of the containing questionnaire result. This object
     * encapsulates the user response to the questionnaire as a whole.
     * @return the ID of the containing questionnaire result.
     * @see QuestionnaireResult
     * @see #getQuestionnaireResult()
     */
	public PrimaryKey getQuestionnaireResultId()
		{
		return questionnaire_result_id;
		}
		
	/**
     * Set the ID of the containing questionnaire result.
	 * @param id the ID if the containing questionnaire result.
     * @see #getQuestionnaireResultId()
	 */
	public void setQuestionnaireResultId( PrimaryKey id )
		{
		questionnaire_result_id = id;
		}


    /**
     * Get the ID of the corresponding questionnaire question. This is the
     * questionnaire question to which this object is a response.
     * @return the ID of the corresponding questionnaire question.
     * @see QuestionnaireQuestion
     */
	public PrimaryKey getQuestionnaireQuestionId()
		{
		return questionnaire_question_id;
		}
		
    /**
     * Set the ID of the corresponding questionnaire question.
     * @param id the ID of the corresponding questionnaire question.
     * @see #getQuestionnaireQuestionId()
     */
	public void setQuestionnaireQuestionId( PrimaryKey id )
		{
		questionnaire_question_id = id;
		}


    /**
     * Get the index of the statement that the user chose.
     * @return the index of the statement that the user chose.
     * @see QuestionnaireQuestion#getValidStatementIndices()
     */
	public byte getItem()
		{
		return item;
		}
		
	/**
     * Set the index of the statement that the user chose.
	 * @param r the index of the statement that the user chose.
     * @see #getItem()
	 */
	public void setItem( byte r )
		{
		if ( item == r )
			return;
		item = r;
		setUnsaved();
		}
		
	public PrimaryKey getCommentBigStringId()
		{
		return comment_big_string_id;
		}
		
	public void setCommentBigStringId( PrimaryKey id )
		{
		comment_big_string_id = id;
		setUnsaved();
		}
		
	/**
     * Get the comment string of the corresponding questionnaire question. This
     * is the free text response given to a question.
	 * @return the comment string of the corresponding questionnaire question.
	 * @throws BuildingServerException
     * @see QuestionnaireQuestion#isCanComment()
	 */
	public String getComment()
		throws BuildingServerException
		{
		if ( comment_big_string_id == null )
			return null;
		BigString bstr = BigString.findBigString( comment_big_string_id );
		if ( bstr == null )
			return null;
		return bstr.getString();
		}
		
	/**
     * Set the comment string of the corresponding questionnaire question.
     * @param s the comment string of the corresponding questionnaire question.
     * @throws BuildingServerException
     * @see #getComment()
     */
	public void setComment( String s )
		throws BuildingServerException
		{
		BigString bstr=null;

		if ( comment_big_string_id == null && s == null )
			return;
			
		if ( comment_big_string_id != null && s == null )
			{
			bstr = BigString.findBigString( comment_big_string_id );
			if ( bstr!=null )
				{
				if ( !bstr.isUnsaved() )
					bstr.delete();
				}
			comment_big_string_id = null;
			setUnsaved();
			}
			
		if ( comment_big_string_id != null )
			bstr = BigString.findBigString( comment_big_string_id );
		if ( bstr == null )
			{
			bstr = new BigString();
			bstr.setString( s );
			bstr.save();
			comment_big_string_id = bstr.getBigStringId();
			setUnsaved();
			return;
			}

		bstr.setString( s );
		bstr.save();
		}
    
    /**
     * Get the containing questionnaire result. This object encapsulates the
     * user response to the questionnaire as a whole.
     * @return the containing questionnaire result.
     * @see #getQuestionnaireResultId()
     */
    public QuestionnaireResult getQuestionnaireResult() 
        throws BuildingServerException
        {
        if (result == null)
            {
            result 
                = QuestionnaireResult.findQuestionnaireResult(
                    getQuestionnaireResultId());
            }
        return result;
        }
    
    /**
     * Get the value of {@link #getItem()} as a string. This method always
     * returns a non-null result. If the user selected a multiple-choice item,
     * then this method will return a value between <code>A</code> to
     * <code>J</code> (inclusive). Otherwise, it be will an empty string.
     * @return the value of {@link #getItem()} as a string.
     * @see #getItem()
     */
    public String itemToString()
        {
        byte b = getItem();
        if ( b < 0 || b > 9)
            return "";
        
        return String.valueOf( (char)('A' + b) );
        }
    
    /**
     * Get the value of {@link #getComment()} as a string. This method always
     * returns a non-null result.
     * @return the value of {@link #getComment()} as a string.
     * @see #getComment()
     */
    public String commentToString()
        {
        try
            {
            String s = getComment();
            return (s == null) ? "" : s;
            }
        catch ( BuildingServerException e )
            {
            return "";
            }
        }
	}
