// Path: me.gladwell.eclipse.m2e.android/src/main/java/me/gladwell/eclipse/m2e/android/configuration/DependencyNotFoundInWorkspace.java
// public class DependencyNotFoundInWorkspace extends ProjectConfigurationError {
// 
//     private static final long serialVersionUID = -3959048624226951719L;
// 
//     public DependencyNotFoundInWorkspace(Dependency dependency) {
//         super("dependency=[" + dependency + "] not found in workspace");
//     }
// 
//     @Override
//     public String getType() {
//         return AndroidMavenPlugin.APKLIB_ERROR_TYPE;
//     }
// 
// }
// 
// Path: me.gladwell.eclipse.m2e.android/src/main/java/me/gladwell/eclipse/m2e/android/configuration/ProjectConfigurationException.java
// public class ProjectConfigurationException extends AndroidMavenException {
// 
//     private static final long serialVersionUID = -4510508504367403748L;
// 
//     public ProjectConfigurationException(String message) {
//         super(message);
//     }
// 
//     public ProjectConfigurationException(Throwable cause) {
//         super(cause);
//     }
// 
//     public ProjectConfigurationException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
// }


import java.util.ArrayList;
import java.util.List;
import me.gladwell.eclipse.m2e.android.configuration.DependencyNotFoundInWorkspace;
import me.gladwell.eclipse.m2e.android.configuration.ProjectConfigurationException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.StringUtils;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.m2e.core.embedder.MavenModelManager;
import com.google.inject.Inject;
import com.google.inject.Singleton;
/*******************************************************************************
 * Copyright (c) 2012, 2015 Ricardo Gladwell
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package me.gladwell.eclipse.m2e.android.project;





@Singleton
public class EclipseAndroidWorkspace implements AndroidWorkspace {

    private IWorkspace workspace;
    private IDEAndroidProjectFactory projectFactory;
    private MavenModelManager mavenModelManager;

    @Inject
    public EclipseAndroidWorkspace(IWorkspace workspace,
            IDEAndroidProjectFactory projectFactory,
            MavenModelManager mavenModelManager) {
        super();
        this.workspace = workspace;
        this.projectFactory = projectFactory;
        this.mavenModelManager = mavenModelManager;
    }

    // TODO delegate workspace dep resolution to EclipseWorkspaceArtifactRepository
    public IDEAndroidProject findOpenWorkspaceDependency(Dependency dependency) {
        for (IProject project : workspace.getRoot().getProjects()) {
            if (!project.isOpen()) {
                continue;
            }
            IDEAndroidProject androidProject = projectFactory.createAndroidProject(project);
            if (androidProject.isMavenised()) {
                MavenProject mavenProject;
                try {
                    mavenProject = mavenModelManager.readMavenProject(androidProject.getPom(), null);
                } catch (CoreException e) {
// Path: me.gladwell.eclipse.m2e.android/src/main/java/me/gladwell/eclipse/m2e/android/configuration/DependencyNotFoundInWorkspace.java
// public class DependencyNotFoundInWorkspace extends ProjectConfigurationError {
// 
//     private static final long serialVersionUID = -3959048624226951719L;
// 
//     public DependencyNotFoundInWorkspace(Dependency dependency) {
//         super("dependency=[" + dependency + "] not found in workspace");
//     }
// 
//     @Override
//     public String getType() {
//         return AndroidMavenPlugin.APKLIB_ERROR_TYPE;
//     }
// 
// }
// 
// Path: me.gladwell.eclipse.m2e.android/src/main/java/me/gladwell/eclipse/m2e/android/configuration/ProjectConfigurationException.java
// public class ProjectConfigurationException extends AndroidMavenException {
// 
//     private static final long serialVersionUID = -4510508504367403748L;
// 
//     public ProjectConfigurationException(String message) {
//         super(message);
//     }
// 
//     public ProjectConfigurationException(Throwable cause) {
//         super(cause);
//     }
// 
//     public ProjectConfigurationException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
// }


// Path: me.gladwell.eclipse.m2e.android/src/main/java/me/gladwell/eclipse/m2e/android/project/EclipseAndroidWorkspace.java
import java.util.ArrayList;
import java.util.List;
import me.gladwell.eclipse.m2e.android.configuration.DependencyNotFoundInWorkspace;
import me.gladwell.eclipse.m2e.android.configuration.ProjectConfigurationException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.StringUtils;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.m2e.core.embedder.MavenModelManager;
import com.google.inject.Inject;
import com.google.inject.Singleton;

/*******************************************************************************
 * Copyright (c) 2012, 2015 Ricardo Gladwell
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package me.gladwell.eclipse.m2e.android.project;





@Singleton
public class EclipseAndroidWorkspace implements AndroidWorkspace {

    private IWorkspace workspace;
    private IDEAndroidProjectFactory projectFactory;
    private MavenModelManager mavenModelManager;

    @Inject
    public EclipseAndroidWorkspace(IWorkspace workspace,
            IDEAndroidProjectFactory projectFactory,
            MavenModelManager mavenModelManager) {
        super();
        this.workspace = workspace;
        this.projectFactory = projectFactory;
        this.mavenModelManager = mavenModelManager;
    }

    // TODO delegate workspace dep resolution to EclipseWorkspaceArtifactRepository
    public IDEAndroidProject findOpenWorkspaceDependency(Dependency dependency) {
        for (IProject project : workspace.getRoot().getProjects()) {
            if (!project.isOpen()) {
                continue;
            }
            IDEAndroidProject androidProject = projectFactory.createAndroidProject(project);
            if (androidProject.isMavenised()) {
                MavenProject mavenProject;
                try {
                    mavenProject = mavenModelManager.readMavenProject(androidProject.getPom(), null);
                } catch (CoreException e) {
                    throw new ProjectConfigurationException(e);