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

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


/**
 * This object represents a section with a logbook.
 * 
 * @author Jon Maber
 */
public class LogBookSection extends org.bodington.sqldatabase.SqlPersistentObject
	{
	private static final byte FLAG_ONE_TRUE = 1;
	
	
	private PrimaryKey log_book_section_id;
	private PrimaryKey log_book_id;
	private PrimaryKey title_big_string_id;
	private PrimaryKey introduction_big_string_id;
	private int ordinal;
	
	private transient LogBookQuestion[] questions=null;
	
	/**
     * Simply redirects to getLogBookSectionId().
     * 
     * @return The primary key of the object.
     */
    public PrimaryKey getPrimaryKey()
    	{
        return getLogBookSectionId();
    	}

    /**
     * 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)
    	{
    	setLogBookSectionId( key );
    	}

	
	/**
	 * Gets the unique id of the LogBookSection.
	 * 
	 * @return A PrimaryKey.
	 */
	public PrimaryKey getLogBookSectionId()
		{
		return log_book_section_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 setLogBookSectionId( PrimaryKey id )
		{
		log_book_section_id = id;
		}
		
		
	/**
	 * Which log book does this section 
	 * belong to.
	 * 
	 * @return The id of the log book page.
	 */
	public PrimaryKey getLogBookId()
		{
		return log_book_id;
		}
		
	/**
	 * When creating a new section or loading one from the 
	 * database, sets the id of the logbook this section belongs to.
	 * 
	 * @param id The id of the logbook page.
	 */
	public void setLogBookId( PrimaryKey id )
		{
		log_book_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 introduction.
	 * 
	 * @return PrimaryKey of the big_string entry.
	 */
	public PrimaryKey getIntroductionBigStringId()
		{
		return introduction_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 setIntroductionBigStringId( PrimaryKey id )
		{
		introduction_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 getIntroduction()
		throws BuildingServerException
		{
		if ( introduction_big_string_id == null )
			return null;
		BigString bstr = BigString.findBigString( introduction_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 setIntroduction( String s )
		throws BuildingServerException
		{
		BigString bstr=null;
		if ( introduction_big_string_id != null )
			bstr = BigString.findBigString( introduction_big_string_id );
		if ( bstr == null )
			{
			bstr = new BigString();
			bstr.setString( s );
			bstr.save();
			introduction_big_string_id = bstr.getBigStringId();
			setUnsaved();
			return;
			}

		bstr.setString( s );
		bstr.save();
		}
	
		
		
	/**
	 * Gets the number that defines where in the list of sections
	 * this section will appear.
	 * 
	 * @return The ordinal integer value.
	 */
	public int getOrdinal()
		{
		return ordinal;
		}
		
	/**
	 * Sets the number that determines the position of this section in the
	 * list of sections.
	 * 
	 * @param o The new ordinal value.
	 */
	public void setOrdinal( int o )
		{
		if ( o==ordinal )
			return;
		ordinal = o;
		setUnsaved();
		}
	    

	/**
	 * LogBookSection overrides the delete method so that it
	 * can simultaneously delete BigString objects that it 
	 * references.  Without this we could end up with entries
	 * in the big_strings table of the database that are 
	 * unreferenced and taking up space.  An alternate approach
	 * would be for any code that calls the standard delete
	 * method to remember to delete the BigStrings too.
	 * 
	 * @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 ( introduction_big_string_id !=null )
			{
			bs = BigString.findBigString( introduction_big_string_id );
			if ( bs!=null ) bs.delete();
			}

		}


	/**
	 * This class can hold references to the questions that belong to it and
	 * so avoid a lot of database queries but this relies on this invalidate
	 * method being called whenever a question is added, deleted or reordered
	 * in the section.  Since the only class that will do that is the
	 * LogBookSessionImpl it can easily arrange to call this method every time.
	 * When this object is garbage collected the references to the questions will
	 * be lost and so the questions can be garbage collected too.
	 */
	public void invalidateQuestions()
		{
		questions = null;
		}

	/**
	 * Find the biggest ordinal number of all the sections in the log book.
	 * 
	 * @return Ordinal of last section.
	 */
	public int getMaxSectionOrdinal()
		throws BuildingServerException
		{
		findLogBookQuestions();
		if ( questions.length == 0 )
			return 0;
		return questions[questions.length-1].getOrdinal();
		}

   	/**
   	 * If questions haven't changed and they were already loaded from the database
   	 * this just returns an array that was prepared earlier.  Otherwise it calls
   	 * a static finder method in LogBookQuestion with the log_book_section_id as the only
   	 * search criterion.
   	 * 
   	 * @return A array of questions in the correct order.
   	 * @exception org.bodington.server.BuildingServerException
   	 */
		
   	public LogBookQuestion[] findLogBookQuestions()
		throws BuildingServerException
		{
		if ( questions != null )
			return questions;
			
		Enumeration enumeration = LogBookQuestion.findLogBookQuestions( "log_book_section_id = " + log_book_section_id, "ordinal" );
		Vector list = new Vector();
		while ( enumeration.hasMoreElements() )
			list.addElement( enumeration.nextElement() );
			
		questions =  (LogBookQuestion[])list.toArray( new LogBookQuestion[0] );
		return questions;
		}

   	/**
   	 * This static method is used to find and a LogBookSection 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 LogBookSection findLogBookSection( PrimaryKey id )
		throws BuildingServerException
		{
		return (LogBookSection)findPersistentObject( id, "org.bodington.logbook.server.LogBookSection" );
		}
		
	/**
   	 * This static method is used to find and load LogBookSections from the database.  
   	 * 
   	 * @param where The where clause of an SQL query.
   	 * @param order The order by clause of an SQL query.
   	 * @return Enumeration of the loaded LogBookSections.
   	 * @exception org.bodington.server.BuildingServerException
   	 */
   	public static Enumeration findLogBookSections( String where, String order )
		throws BuildingServerException
		{
		return findPersistentObjects( where, order, "org.bodington.logbook.server.LogBookSection" );
		}
	}
