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

import java.util.*;
import javax.swing.tree.*;
import org.w3c.dom.*;


public class Package extends DefaultTreeModel
	{
	public static final int TYPE_ALL            = 0;
	public static final int TYPE_METADATA       = 1;
	public static final int TYPE_ORGANIZATIONS  = 2;
	public static final int TYPE_RESOURCES      = 3;

    private int type;
	private Document document;
	//private PackageManifest manifest;
	private PackageNode root_node;
	
	//maintain a hashtable of the resource nodes
	private Hashtable resources;

	
	public Package( Document d, int type )
		throws ImsException
		{
		// dummy node to start with...
		super( new DefaultMutableTreeNode() );
		this.type = type;
		document = d;
		if ( !parse() )
			throw new IllegalArgumentException( "Invalid IMS Package." );
		}
	
	public Document getDocument()
		{
		return document;
		}
		
	private boolean parse()
		throws ImsException
		{
		Element root = document.getDocumentElement();
		NodeList nl;
		
		switch ( type )
		    {
		    case TYPE_ALL:
		        root_node = new PackageManifest( this, root );
   	            setRoot( root_node );
		        break;
		    case TYPE_METADATA:
		        break;
		        
		    case TYPE_ORGANIZATIONS:
		        nl = root.getElementsByTagName( "organizations" );
		        Element o = (Element)nl.item( 0 );
		        root_node = new PackageOrganizations( this, o );
   	            setRoot( root_node );
		        break;
		    case TYPE_RESOURCES:
		        nl = root.getElementsByTagName( "resources" );
		        Element r = (Element)nl.item( 0 );
		        root_node = new PackageResources( this, r );
   	            setRoot( root_node );
		        break;
		    }
		    
   	
		return true;
		}
		
	public PackageNode getRootNode()
		{
		return root_node;
		}

	
	public PackageOrganizations findOrganizations()
		{
		
		switch ( type )
			{
			case TYPE_ORGANIZATIONS:
				return (PackageOrganizations)root_node;
		
			case TYPE_ALL:
				Enumeration enumeration = root_node.children();
				PackageNode node;
				PackageOrganizations orgs;

				while ( enumeration.hasMoreElements() )
					{
					node = (PackageNode)enumeration.nextElement();
					if ( !(node instanceof PackageOrganizations) )
						continue;
					orgs = (PackageOrganizations)node;
					if ( orgs !=null )
						return orgs;
					}
					
				return null;
			
			default:
				return null;
			}
		}

	public PackageOrganization findDefaultOrganization()
		{
		PackageOrganizations orgs = findOrganizations();
		
		if ( orgs == null )
			return null;

		Enumeration enumeration = orgs.children();
		PackageNode node;
		PackageOrganization org;

		String defid = orgs.getDefault();
		String oid;
		
		while ( enumeration.hasMoreElements() )
			{
			node = (PackageNode)enumeration.nextElement();
			if ( !(node instanceof PackageOrganization) )
				continue;
			org = (PackageOrganization)node;
			oid = org.getIdentifier();
			if ( oid !=null && oid.equals( defid ) )
				return org;
			}
			
		return null;
		}

	public PackageResource findResourceByIdentifier( String id )
		{
		Enumeration enumeration = root_node.preorderEnumeration();
		PackageNode node;
		PackageResource resource;
		String rid;
		while ( enumeration.hasMoreElements() )
			{
			node = (PackageNode)enumeration.nextElement();
			if ( !(node instanceof PackageResource) )
				continue;
			resource = (PackageResource)node;
			rid = resource.getIdentifier();
			if ( rid !=null && rid.equals( id ) )
				return resource;
			}
			
		return null;
		}
	}
	
