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