/**
 * Copyright 2004, 2005 The Apache Software Foundation or its licensors, as applicable
 *
 *  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.apache.geronimo.st.core;

import java.io.IOException;
import java.net.MalformedURLException;

import javax.enterprise.deploy.shared.ModuleType;

import org.apache.geronimo.st.core.internal.Trace;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.jst.j2ee.internal.deployables.J2EEFlexProjDeployable;
import org.eclipse.jst.server.core.IWebModule;
import org.eclipse.wst.common.componentcore.ArtifactEdit;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.server.core.IModule;

public class GeronimoUtils {

	public static final String WEB_PLAN_NAME = "geronimo-web.xml";

	public static final String OPENEJB_PLAN_NAME = "openejb-jar.xml";

	public static final String APP_PLAN_NAME = "geronimo-application.xml";

	public static final String CONNECTOR_PLAN_NAME = "geronimo-connector.xml";
	
	public static final String SERVICE_PLAN_NAME = "geronimo-service.xml";

	public static boolean isWebModule(IModule module) {
		return "jst.web".equals(module.getModuleType().getId());
	}

	public static boolean isEjbJarModule(IModule module) {
		return "jst.ejb".equals(module.getModuleType().getId());
	}

	public static boolean isEarModule(IModule module) {
		return "jst.ear".equals(module.getModuleType().getId());
	}

	public static boolean isRARModule(IModule module) {
		return "jst.connnector".equals(module.getModuleType().getId());
	}
	
	public static boolean isUtilityModule(IModule module) {
		return "jst.utility".equals(module.getModuleType().getId());
	}

	public static ModuleType getJSR88ModuleType(IModule module) {
		if (isWebModule(module)) {
			return ModuleType.WAR;
		} else if (isEjbJarModule(module)) {
			return ModuleType.EJB;
		} else if (isEarModule(module)) {
			return ModuleType.EAR;
		} else if (isRARModule(module)) {
			return ModuleType.RAR;
		} else {
			Trace.trace(Trace.SEVERE, "getJSR88ModuleType = null");
			return null;
		}
	}

	public static IFile getWebDeploymentPlanFile(IVirtualComponent comp) {
		IPath deployPlanPath = comp.getRootFolder().getUnderlyingFolder().getProjectRelativePath().append("WEB-INF").append(WEB_PLAN_NAME);
		return comp.getProject().getFile(deployPlanPath);
	}

	public static IFile getOpenEjbDeploymentPlanFile(IVirtualComponent comp) {
		IPath deployPlanPath = comp.getRootFolder().getUnderlyingFolder().getProjectRelativePath().append("META-INF").append(OPENEJB_PLAN_NAME);
		return comp.getProject().getFile(deployPlanPath);
	}

	public static IFile getApplicationDeploymentPlanFile(IVirtualComponent comp) {
		IPath deployPlanPath = comp.getRootFolder().getUnderlyingFolder().getProjectRelativePath().append("META-INF").append(APP_PLAN_NAME);
		return comp.getProject().getFile(deployPlanPath);
	}

	public static IFile getConnectorDeploymentPlanFile(IVirtualComponent comp) {
		IPath deployPlanPath = comp.getRootFolder().getUnderlyingFolder().getProjectRelativePath().append("META-INF").append(CONNECTOR_PLAN_NAME);
		return comp.getProject().getFile(deployPlanPath);
	}
	
	public static IFile getServiceDeploymentPlanFile(IVirtualComponent comp) {
		IPath deployPlanPath = comp.getRootFolder().getUnderlyingFolder().getProjectRelativePath().append("META-INF").append(SERVICE_PLAN_NAME);
		return comp.getProject().getFile(deployPlanPath);
	}

	public static IVirtualComponent getVirtualComponent(IModule module) {
		return ComponentCore.createComponent(module.getProject());
	}

	public static String getContextRoot(IModule module) {
		String contextRoot = null;

		J2EEFlexProjDeployable j2eeModule = (J2EEFlexProjDeployable) module.loadAdapter(J2EEFlexProjDeployable.class, null);
		contextRoot = ((IWebModule) j2eeModule).getContextRoot();

		if (contextRoot == null)
			contextRoot = getId(module);

		return contextRoot;
	}

	public static String getId(IModule module) {
		// use the module ID
		String moduleId = module.getId();

		J2EEFlexProjDeployable j2eeModule = (J2EEFlexProjDeployable) module.loadAdapter(J2EEFlexProjDeployable.class, null);
		if (j2eeModule != null) {
			// j2eeFlex
			ArtifactEdit edit = null;
			try {
				edit = ArtifactEdit.getArtifactEditForRead(j2eeModule.getProject());
				XMIResource res = (XMIResource) edit.getContentModelRoot().eResource();
				moduleId = res.getID(edit.getContentModelRoot());
			} finally {
				if (edit != null)
					edit.dispose();
			}
		}

		if (moduleId != null && moduleId.length() > 0)
			return moduleId;

		// ...but if there is no defined module ID, pick the best alternative

		IPath moduleLocation = new Path(j2eeModule.getURI(module));
		if (moduleLocation != null) {
			moduleId = moduleLocation.removeFileExtension().lastSegment();
		}

		if (j2eeModule instanceof IWebModule) {
			// A better choice is to use the context root
			// For wars most appservers use the module name
			// as the context root
			String contextRoot = ((IWebModule) j2eeModule).getContextRoot();
			if (contextRoot.charAt(0) == '/')
				moduleId = contextRoot.substring(1);
		}

		return moduleId;
	}

	public static Resource load(IFile dpFile, ResourceSet resourceSet) {
		try {

			URI uri = URI.createPlatformResourceURI(dpFile.getFullPath().toString());

			Resource resource = resourceSet.createResource(uri);
			if (!resource.isLoaded()) {
				resource.load(null);
			}
			return resource;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void register(ResourceSet resourceSet,
			Resource.Factory factory, EPackage pkg, String nsUri) {
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, factory);
		resourceSet.getPackageRegistry().put(nsUri, pkg);
	}
}
@@ -1859,17 +1859,10 @@
 imo-
-connecto
 r
+a
 .xml

fix default connector plan name