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

import org.apache.log4j.Logger;

import java.io.*;
import java.util.*;

public class ImsPackageItem extends java.util.Vector
    {
    
    private static Logger log = Logger.getLogger(ImsPackageItem.class);
    
    public final static int ORGANIZATIONS=0;
    public final static int ORGANIZATION=1;
    public final static int ITEM=2;

    private int type;
    
    private String identifier;
    private String resourceref;
    private String title;
    
    private String def;
    private ImsPackageItem deforg;
    
    public ImsPackageItem( int type, String identifier, String resourceref, String title )
        {
        if ( type<0 || type >2 )
            throw new ArrayIndexOutOfBoundsException( "Invalid type." );
            
        switch ( type )
            {
            case ORGANIZATIONS:
                if ( identifier != null || title != null || resourceref!=null )
                    throw new NullPointerException( "An organizations must not have an identifier, a resourceref or a title." );
                break;
            case ORGANIZATION:
                if ( identifier == null )
                    throw new NullPointerException( "An organization must have an identifier." );
                if ( resourceref!=null )
                    throw new NullPointerException( "An organizastion must not have a resourceref." );
                break;
            case ITEM:
                if ( identifier == null || title == null )
                    throw new NullPointerException( "An item must have an identifier and a title." );
                break;
            }
            
        this.type = type;
        this.identifier = identifier;
        this.resourceref = resourceref;
        this.title = title;
        def=null;
        deforg=null;
        }
    
    
    public int getType() { return type; }
    public String getIdentifier() { return identifier; }
    public String getResourceref() { return resourceref; }
    public String getTitle() { return title; }
    public void setTitle( String title )
        { 
        if ( title == null )
            throw new NullPointerException( "Null title is not allowed." );
        this.title = title;
        }
        
    public String getDefault() { return def; }
    public void setDefault( String def )
        { 
        if ( def == null )
            throw new NullPointerException( "Null default is not allowed." );
        this.def = def;
        }
        
    public ImsPackageItem getDefaultOrganization() { return deforg; }
    public void setDefaultOrganization( ImsPackageItem deforg )
        { 
        this.deforg = deforg;
        }
        
    public void appendItem( ImsPackageItem item )
        {
        if ( item.getType() == ORGANIZATIONS )
            throw new IllegalArgumentException( "Can't add an orgnizations element to an item." );
        if ( item.getType() == ORGANIZATION && this.getType() != ORGANIZATIONS )
            throw new IllegalArgumentException( "The organization element can only be added to an orgnizations element." );
        if ( item.getType() == ITEM && this.getType() != ORGANIZATION  && this.getType() != ITEM)
            throw new IllegalArgumentException( "The item element can only be added to an orgnization element or another item element." );
            
        if ( item.getType() == ORGANIZATION && deforg == null )
      		deforg = item;

        this.addElement( item );
        }
        
    public void printHtml( PrintWriter out, Hashtable resources )
        {
        printHtml( out, resources, 1 );
        }
        
    private void printHtml( PrintWriter out, Hashtable resources, int level )
        {
        String href;
        
        if ( type == ORGANIZATIONS )
            {
            out.print( "<HTML><BODY><H" );
            out.print( level );
            out.print( ">Experimental output of IMS Package organizations</H" );
            out.print( level );
            out.println( ">" );
            }
        else if  ( type == ORGANIZATION )
            {
            out.print( "<H" );
            out.print( level );
            out.print( ">" );
            out.print( title );
            out.print( "</H" );
            out.print( level );
            out.println( ">" );
            }
        else
            {
            href=null;
            if ( resourceref!=null )
                href = (String)resources.get( resourceref );

            out.print( "<H" );
            out.print( level );
            out.print( ">" );
            if ( href!=null )
                {
                out.print( "<A HREF=\"" );
                out.print( href );
                out.print( "\">" );
                }
            out.print( title );
            if ( href!=null )
                out.print( "</A>" );
            out.print( "</H" );
            out.print( level );
            out.println( ">" );
            }
     
        ImsPackageItem child;
        boolean deforgdone=false;
        
        if ( type == ORGANIZATIONS && deforg!=null )
            {
            deforg.printHtml( out, resources, level+1 );
            deforgdone=true;
            
            if ( this.size() > 1 )
                {
                out.println( "<H1>Alternate organizations of content</H1>" );
                }
            }
            
        for ( int i=0; i<this.size(); i++ )
            {
            child = (ImsPackageItem)this.elementAt( i );

            //skip an organization if it is the default and
            //has already been output

            if ( deforgdone && child == deforg )
                continue;
            child.printHtml( out, resources, level+1 );
            }

        if ( type == ORGANIZATIONS )
            out.println( "</PRE></BODY></HTML>" );
        }

    public void printMenuXML( PrintWriter out, Hashtable resources )
        {
        out.println( "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" );
        out.println( "<menu automatic=\"true\">" );

        if ( deforg == null )
      		printHtml( out, resources, 1 );
        else
      		deforg.printMenuXML( out, resources, 1 );

        out.println( "</menu>" );
        }
        
    private void printMenuXML( PrintWriter out, Hashtable resources, int level )
        {
        String href;
        
        log.debug( "Another node for menu, resourceref = " + resourceref );
        
        if  ( type != ORGANIZATION || level>1 )
            {
            href=null;
            if ( resourceref!=null )
                href = (String)resources.get( resourceref );

      		log.debug( "Another node for menu, href = " + href );

            out.print( "<item" );
            if ( this.size()>0 || href==null )
            	out.print( " type=\"container\"" );
            else
            	out.print( " type=\"file\"" );
            if ( href!=null )
                {
                out.print( " href=\"" );
                out.print( href );
                out.print( "\"" );
                }
            out.println( ">" );
            out.print( "<title>" );
            if ( title!=null )
            	out.print( title );
            else
            	out.print( "untitled item" );
            out.println( "</title>" );
            }

        ImsPackageItem child;

   		for ( int i=0; i<this.size(); i++ )
      		{
      		child = (ImsPackageItem)this.elementAt( i );
      		child.printMenuXML( out, resources, level+1 );
      		}

        if  ( type != ORGANIZATION || level>1 )
            out.println( "</item>" );
        }

    }
