package de.start.startportal.web;

import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.jdom.Document;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.Element;
import org.jdom.Attribute;
import java.util.List;
import java.util.Iterator;

/**
 *  PortalAction: Base Action class for all portal related actions.
 *
 *@author     <a href=MAILTO:itg@traventec.com>Internet Technologies Group, Traventec Ltd</a>
 *@author     <a href=MAILTO:john.oreilly@traventec.com>John O'Reilly</a>
 *@created    5th July 2001
 */

public class PortalAction extends Action
{

  /**
   *  setError  Sets the Error XML in request context
   *
   *@param  request  HttpServletRequest object
   *@param  error    The new Error value
   */
  protected void setError( HttpServletRequest request, String error )
  {
    request.setAttribute( Constants.ERROR_XML, "<error>" + error + "</error>" );
  }



  /**
   *  setContent  Sets the XML associated with MainDisplay area in session context
   *
   *@param  request  HttpServletRequest object
   *@param  error    The xml to be stored
   */
  protected void setContent( HttpServletRequest request, String xmlContent )
  {
    request.getSession().setAttribute( Constants.MAIN_DISPLAY_XML, xmlContent );
  }


  /**
   *  LoadMenu    Load menu from static XML file and update to based on option just selected.
   *
   *@param  path    Path to static XML file.
   *@param  menuID  ID of option just selected (if not first time).
   *@return         Returned Value
   */

  protected void loadMenu( HttpServletRequest request, String menuFile )
  {
    try
    {
      String menuID = request.getParameter(Constants.MENU_ID_PARAM);
      Document doc = getXMLDocument( Constants.MENU_PATH + menuFile );
      if( menuID != null && menuID.length() > 0 )
      {
        Element root = doc.getRootElement() ;
        processMenu( root, menuID );
      }
      XMLOutputter outputter = new XMLOutputter();
      String xmlMenu = outputter.outputString( doc );
      request.getSession().setAttribute( Constants.AREA_NAVIGATION_XML, xmlMenu );
    }
    catch ( Exception ex )
    {
      ex.printStackTrace();
    }

  }

  /**
   *  Method
   *
   *@param  path  Parameter
   *@return       Returned Value
   */
  protected Document getXMLDocument( String path )
  {
    Document doc = null;
    try
    {
      SAXBuilder builder = new SAXBuilder( false );
      InputStream is = getServlet().getServletContext().getResourceAsStream( path );
      doc = builder.build( is );
    }
    catch( Exception ex )
    {
      ex.printStackTrace();
    }

    return doc;
  }


  protected String loadXMLDocument( String path )
  {
    String xml = "";

    try
    {
      Document doc = getXMLDocument( path );
      XMLOutputter outputter = new XMLOutputter();
      xml = outputter.outputString( doc );
    }
    catch( Exception ex )
    {
      ex.printStackTrace();
    }

    return xml;
  }



  private void processMenu( Element root, String productID )
  {
      // note root represents the Menu element.

      List options = root.getChildren() ;
      Iterator iterator = options.iterator() ;
      while( iterator.hasNext() )
      {
        Element option = ( Element )iterator.next() ;


        // set selected attribute if this option is being selected
        Attribute idAttribute = option.getAttribute( Constants.MENU_ID_PARAM );
        if ( ( idAttribute != null ) && ( productID.compareTo( idAttribute.getValue() ) == 0 ) )
        {
          // All parent MenuOptions must also have selected set to false and expanded to true.
          Element parent=option;
          do
          {
            Attribute selected = new Attribute( "selected", "false" );
            parent.addAttribute( selected ) ;

            Attribute expanded = new Attribute( "expanded", "true" );
            parent.addAttribute(expanded);
          }
          while ( ( (parent = parent.getParent() ) != null ) && ( (parent = parent.getParent() ) != null ));
          break;
        }

        // process sub menu
        Element menu = option.getChild("Menu");
        if ( menu != null )
        {
          processMenu( menu, productID);
        }
      }
  }
}

