/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.deployment.application;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jfox.deployment.ejb.EJBModuleDescriptor;
import org.jfox.deployment.ejb.NestedEJBModule;
import org.jfox.deployment.web.NestedWebModule;
import org.jfox.deployment.web.WebModuleDescriptor;
import org.jfox.ejb.meta.Descriptor;
import org.jfox.ejb.meta.EJBDescriptionException;
import org.jfox.ejb.meta.XmlUtil;
import org.jfox.framework.deploy.DeployException;
import org.jfox.framework.util.Files;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class EARDescriptor extends Descriptor {
    private String displayName;
    private String smallIcon;
    private String largeIcon;
    private List ejbModuleDescriptors = new ArrayList();
    private List webModuleDescriptors = new ArrayList();
    // not support client modules now!!!
    private List clientModules = new ArrayList();

    private EARModule earModule;

    public EARDescriptor(EARModule earModule) {
        this.earModule = earModule;
    }

    public void processXML(Node node) throws EJBDescriptionException {
        super.processXML(node);
        setSmallIcon(XmlUtil.getChildNodeValueOf(node, "small-icon"));
        setLargeIcon(XmlUtil.getChildNodeValueOf(node, "large-icon"));
        setDisplayName(XmlUtil.getChildNodeValueOf(node, "display-name"));

        Iterator modules = XmlUtil.getElementsByTagName((Element) node, "module");
        while(modules.hasNext()) {
            Element element = (Element) modules.next();
            Iterator _modules = XmlUtil.getElementsByTagNames(element, new String[]{"ejb", "web", "java"});
            while(_modules.hasNext()) {
                Element _element = (Element) _modules.next();
                if(_element.getNodeName().equals("ejb")) {
                    processEjbModule(_element);
                }
                else if(_element.getNodeName().equals("web")) {
                    processWebModule(_element);
                }

                // Sorry, Not support Client Module now !!!
                else if(_element.getNodeName().equals("java")) {

                }
            }
        }

    }

    private void processEjbModule(Element element) throws EJBDescriptionException {
        EJBModuleDescriptor ejbModuleDesc = new EJBModuleDescriptor();
        ejbModuleDesc.processXML(element);
        ejbModuleDescriptors.add(ejbModuleDesc);

        // create EJBModule
        try {
            File target = Files.move(new File(earModule.getTempDeployDirectory(), ejbModuleDesc.getJarName()),
                    new File(earModule.getTempDeployDirectory(), NestedEJBModule.Target));
            NestedEJBModule ejbModule = new NestedEJBModule(earModule, target);
            earModule.addNestedEJBModule(ejbModule);
        }
        catch(IOException e) {
            throw new EJBDescriptionException("move ejb module " + ejbModuleDesc.getJarName() + " to " + NestedEJBModule.Target + " error");
        }
        catch(DeployException e) {
            throw new EJBDescriptionException(e);
        }
    }

    private void processWebModule(Element element) throws EJBDescriptionException {
        WebModuleDescriptor webModuleDesc = new WebModuleDescriptor();
        webModuleDesc.processXML(element);
        webModuleDescriptors.add(webModuleDesc);
        try {
            File target = Files.move(new File(earModule.getTempDeployDirectory(), webModuleDesc.getWarName()),
                    new File(earModule.getTempDeployDirectory(), NestedWebModule.Target));
            NestedWebModule webModule = new NestedWebModule(earModule, target, webModuleDesc.getWebContext());
            earModule.addNestedWebModule(webModule);
        }
        catch(IOException e) {
            e.printStackTrace();
            throw new EJBDescriptionException("move ejb module " + webModuleDesc.getWarName() + " to " + NestedWebModule.Target + " error");
        }
        catch(DeployException e) {
            throw new EJBDescriptionException(e);
        }

    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getSmallIcon() {
        return smallIcon;
    }

    public void setSmallIcon(String smallIcon) {
        this.smallIcon = smallIcon;
    }

    public String getLargeIcon() {
        return largeIcon;
    }

    public void setLargeIcon(String largeIcon) {
        this.largeIcon = largeIcon;
    }

    public List getEjbModuleDescriptors() {
        return ejbModuleDescriptors;
    }

    public List getWebModuleDescriptors() {
        return webModuleDescriptors;
    }

    public List getClientModules() {
        return clientModules;
    }

    public static void main(String[] args) {

    }
}

