/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.deployment;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.jboss.metadata.MetaData;
import org.w3c.dom.Element;

/**
 * A representation of the application.xml and jboss-app.xml deployment
 * descriptors.
 *
 * @see org.jboss.metadata.XmlLoadable
 *
 * @author <a href="mailto:scott.stark@jboss.org">Scott Stark</a>
 * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
 * @version $Revision: 1.5.2.4 $
 */
public class J2eeApplicationMetaData extends MetaData
{
   // Constants -----------------------------------------------------
    
   // Attributes ----------------------------------------------------
   String displayName;
   String description;
   String smallIcon;
   String largeIcon;
   
   /** The application.xml modules in definition order */
   private Collection modules = new ArrayList();
   
   /** The jmx name */
   private String jmxName;
   
   // Static --------------------------------------------------------
   
   // Constructors --------------------------------------------------
   public J2eeApplicationMetaData (Element rootElement) throws DeploymentException
   {
      importXml (rootElement, false);
   }
	
   // Public --------------------------------------------------------

   public String getDisplayName ()
   {
      return displayName;
   }
   
   public String getDescription ()
   {
      return description;
   }
   
   public String getSmallIcon ()
   {
      return smallIcon;
   }
   
   public String getLargeIcon ()
   {
      return largeIcon;
   }
   
   public Iterator getModules ()
   {
      return modules.iterator ();
   }

   public boolean hasModule(String name)
   {
      for (Iterator i = modules.iterator(); i.hasNext(); )
      {
         J2eeModuleMetaData moduleMetaData = (J2eeModuleMetaData)i.next();
         if (moduleMetaData.getFileName().equals(name))
         {
            return true;
         }
      }
      return false;
   }
   
   public String getJMXName()
   {
      return jmxName;
   }

   public void importXml (Element element, boolean jbossSpecific) throws DeploymentException
   {
      String rootTag = element.getOwnerDocument().getDocumentElement().getTagName();
       
      if ((rootTag.equals("application") && !jbossSpecific) ||
          (rootTag.equals("jboss-app") && jbossSpecific)) 
      {
         // get some general info
         if (!jbossSpecific) 
         {
            displayName = getElementContent (getUniqueChild (element, "display-name"));
         }
          
         Element e = getOptionalChild (element, "description");
         description = e != null ? getElementContent (e) : "";

         e = getOptionalChild (element, "icon");
         if (e != null)
         {
            Element e2 = getOptionalChild (element, "small-icon");
            smallIcon = e2 != null ? getElementContent (e2) : "";
             
            e2 = getOptionalChild (element, "large-icon");
            largeIcon = e2 != null ? getElementContent (e2) : "";
         }
         else
         {
            smallIcon = "";
            largeIcon = "";
         }
         // Get any user defined JMX name
         if (jbossSpecific)
         {
            Element jmxNameElement = getOptionalChild(element, "jmx-name");
            if (jmxNameElement != null)
                jmxName = getElementContent(jmxNameElement);
         }
          
         // extract modules...
         for (Iterator it = getChildrenByTagName (element, "module"); it.hasNext (); )
         {
            modules.add(new J2eeModuleMetaData((Element)it.next(), jbossSpecific));
         }
      }		
      else 
      {
         throw new DeploymentException("Unrecognized root tag in EAR deployment descriptor: "+ element);
      }
   }
}
