/* ======================================================================
   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.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" );
		}
	}
