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

import java.util.Enumeration;

import org.bodington.sqldatabase.SqlPersistentObject;
import org.bodington.database.PrimaryKey;
import org.bodington.text.BigString;
import org.bodington.server.BuildingServerException;


/**
 * Represents a question that exists within a section of a logbook.
 * 
 * @author Jon Maber
 */
public class LogBookQuestion extends org.bodington.sqldatabase.SqlPersistentObject
	{
	private static final byte FLAG_ONE_TRUE = 1;
	
	
	private PrimaryKey log_book_question_id;
	private PrimaryKey log_book_section_id;
	private PrimaryKey title_big_string_id;
	private PrimaryKey question_big_string_id;
	private PrimaryKey help_big_string_id;
	private int ordinal;
	
    /**
     * Simply redirects to getLogBookQuestionId().
     * 
     * @return The primary key of the object.
     */
    public PrimaryKey getPrimaryKey()
    	{
        return getLogBookQuestionId();
    	}

    /**
     * Called only by bodington database code when loading an object from
     * the database or inserting a new record in the database.
     * 
     * @param key The primary key of the object.
     */
    public void setPrimaryKey(PrimaryKey key)
    	{
    	setLogBookQuestionId( key );
    	}

	
	/**
	 * Gets the unique id of the LogBookQuestion.
	 * 
	 * @return A PrimaryKey.
	 */
	public PrimaryKey getLogBookQuestionId()
		{
		return log_book_question_id;
		}
		
	/**
	 * Called only when the object is loaded from the database
	 * or a new object is saved.
	 * 
	 * @param id The primary key from the database.
	 */
	public void setLogBookQuestionId( PrimaryKey id )
		{
		log_book_question_id = id;
		}
		
		
	/**
	 * Which log book section does this question 
	 * belong to.
	 * 
	 * @return The id of the log book page.
	 */
	public PrimaryKey getLogBookSectionId()
		{
		return log_book_section_id;
		}
		
	/**
	 * When creating a new question or loading one from the 
	 * database, sets the id of the section this question belongs to.
	 * 
	 * @param id The id of the logbook page.
	 */
	public void setLogBookSectionId( PrimaryKey id )
		{
		log_book_section_id = id;
		}
		
		
		
	/**
	 * Gets the id of the entry in the big_strings table that
	 * contains the text of the title.
	 * 
	 * @return PrimaryKey of the big_string entry.
	 */
	public PrimaryKey getTitleBigStringId()
		{
		return title_big_string_id;
		}

	/**
	 * When creating or editing an entry sets the id of the
	 * record that contains the actual text.  Normally only called
	 * when making a new entry or loading an entry from the
	 * database because editing the text of an existing entry
	 * can be done by modifying the existing BigString object.
	 * 
	 * @param id The id of a big_string entry.
	 */
	public void setTitleBigStringId( PrimaryKey id )
		{
		title_big_string_id = id;
		setUnsaved();
		}
		
	/**
	 * Convenience method that loads the BigString object that
	 * is referenced by the big_string_id property.
	 * 
	 * @return Text of the entry.
	 * @exception org.bodington.server.BuildingServerException
	 */
	public String getTitle()
		throws BuildingServerException
		{
		if ( title_big_string_id == null )
			return null;
		BigString bstr = BigString.findBigString( title_big_string_id );
		if ( bstr == null )
			return null;
		return bstr.getString();
		}
		
	/**
	 * Convenience method that finds the big string entry
	 * referenced by big_string_id and changes its text
	 * value.
	 * 
	 * @param s The new text for this entry.
	 * @exception org.bodington.server.BuildingServerException
	 */
	public void setTitle( String s )
		throws BuildingServerException
		{
		BigString bstr=null;
		if ( title_big_string_id != null )
			bstr = BigString.findBigString( title_big_string_id );
		if ( bstr == null )
			{
			bstr = new BigString();
			bstr.setString( s );
			bstr.save();
			title_big_string_id = bstr.getBigStringId();
			setUnsaved();
			return;
			}

		bstr.setString( s );
		bstr.save();
		}
	
	/**
	 * Gets the id of the entry in the big_strings table that
	 * contains the text of the question.
	 * 
	 * @return PrimaryKey of the big_string entry.
	 */
	public PrimaryKey getQuestionBigStringId()
		{
		return question_big_string_id;
		}

	/**
	 * When creating or editing an entry sets the id of the
	 * record that contains the actual text.  Normally only called
	 * when making a new entry or loading an entry from the
	 * database because editing the text of an existing entry
	 * can be done by modifying the existing BigString object.
	 * 
	 * @param id The id of a big_string entry.
	 */
	public void setQuestionBigStringId( PrimaryKey id )
		{
		question_big_string_id = id;
		setUnsaved();
		}
		
	/**
	 * Convenience method that loads the BigString object that
	 * is referenced by the big_string_id property.
	 * 
	 * @return Text of the entry.
	 * @exception org.bodington.server.BuildingServerException
	 */
	public String getQuestion()
		throws BuildingServerException
		{
		if ( question_big_string_id == null )
			return null;
		BigString bstr = BigString.findBigString( question_big_string_id );
		if ( bstr == null )
			return null;
		return bstr.getString();
		}
		
	/**
	 * Convenience method that finds the big string entry
	 * referenced by big_string_id and changes its text
	 * value.
	 * 
	 * @param s The new text for this entry.
	 * @exception org.bodington.server.BuildingServerException
	 */
	public void setQuestion( String s )
		throws BuildingServerException
		{
		BigString bstr=null;
		if ( question_big_string_id != null )
			bstr = BigString.findBigString( question_big_string_id );
		if ( bstr == null )
			{
			bstr = new BigString();
			bstr.setString( s );
			bstr.save();
			question_big_string_id = bstr.getBigStringId();
			setUnsaved();
			return;
			}

		bstr.setString( s );
		bstr.save();
		}
	
		

	/**
	 * Gets the id of the entry in the big_strings table that
	 * contains the text of the question help.
	 * 
	 * @return PrimaryKey of the big_string entry.
	 */
	public PrimaryKey getHelpBigStringId()
		{
		return help_big_string_id;
		}

	/**
	 * When creating or editing an entry sets the id of the
	 * record that contains the actual text.  Normally only called
	 * when making a new entry or loading an entry from the
	 * database because editing the text of an existing entry
	 * can be done by modifying the existing BigString object.
	 * 
	 * @param id The id of a big_string entry.
	 */
	public void setHelpBigStringId( PrimaryKey id )
		{
		help_big_string_id = id;
		setUnsaved();
		}
		
	/**
	 * Convenience method that loads the BigString object that
	 * is referenced by the big_string_id property.
	 * 
	 * @return Text of the entry.
	 * @exception org.bodington.server.BuildingServerException
	 */
	public String getHelp()
		throws BuildingServerException
		{
		if ( help_big_string_id == null )
			return null;
		BigString bstr = BigString.findBigString( help_big_string_id );
		if ( bstr == null )
			return null;
		return bstr.getString();
		}
		
	/**
	 * Convenience method that finds the big string entry
	 * referenced by big_string_id and changes its text
	 * value.
	 * 
	 * @param s The new text for this entry.
	 * @exception org.bodington.server.BuildingServerException
	 */
	public void setHelp( String s )
		throws BuildingServerException
		{
		BigString bstr=null;
		if ( question_big_string_id != null )
			bstr = BigString.findBigString( help_big_string_id );
		if ( bstr == null )
			{
			bstr = new BigString();
			bstr.setString( s );
			bstr.save();
			help_big_string_id = bstr.getBigStringId();
			setUnsaved();
			return;
			}

		bstr.setString( s );
		bstr.save();
		}

		
	/**
	 * Gets the number that defines where in the list of questions
	 * this question will appear.
	 * 
	 * @return The ordinal integer value.
	 */
	public int getOrdinal()
		{
		return ordinal;
		}
		
	/**
	 * Sets the number that determines the position of this question in the
	 * list of questions.
	 * 
	 * @param o The new ordinal value.
	 */
	public void setOrdinal( int o )
		{
		if ( o==ordinal )
			return;
		ordinal = o;
		setUnsaved();
		}
	    
	/**
	 * This class overrides the delete() method provided by
	 * PersistentObject so that it deletes all the BigString
	 * objects associated with the question.  Without this any
	 * application code that deletes a question would have to
	 * remember to delete the text objects too.  If the BigString
	 * objects weren't deleted they would clutter up the database.
	 * 
	 * @exception org.bodington.server.BuildingServerException
	 */
	public void delete()
		throws BuildingServerException
		{
		super.delete();

		BigString bs;
		if ( title_big_string_id !=null )
			{
			bs = BigString.findBigString( title_big_string_id );
			if ( bs!=null ) bs.delete();
			}

		if ( question_big_string_id !=null )
			{
			bs = BigString.findBigString( question_big_string_id );
			if ( bs!=null ) bs.delete();
			}

		if ( help_big_string_id !=null )
			{
			bs = BigString.findBigString( help_big_string_id );
			if ( bs!=null ) bs.delete();
			}
		}

   	/**
   	 * This static method is used to find and a LogBookQuestion from the database.  
   	 * 
   	 * @param id The id of the object required.
   	 * @return Reference to the loaded LogBookSection or null if not found.
   	 * @exception org.bodington.server.BuildingServerException
   	 */
   	public static LogBookQuestion findLogBookQuestion( PrimaryKey id )
		throws BuildingServerException
		{
		return (LogBookQuestion)findPersistentObject( id, "org.bodington.logbook.server.LogBookQuestion" );
		}
		
   	/**
   	 * This static method is used to find and load LogBookQuestions from the database.  
   	 * 
   	 * @param where The where clause of an SQL query.
   	 * @param order The order by clause of an SQL query.
   	 * @return Reference to the loaded LogBook or null if not found.
   	 * @exception org.bodington.server.BuildingServerException
   	 */
   	public static Enumeration findLogBookQuestions( String where, String order )
		throws BuildingServerException
		{
		return findPersistentObjects( where, order, "org.bodington.logbook.server.LogBookQuestion" );
		}
	}
