/*
 * Copyright 2012-2015 the original author or authors.
 *
 * 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.springframework.boot.cli.compiler.grape;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.Exclusion;
import org.eclipse.aether.util.artifact.JavaScopes;
import org.springframework.boot.cli.compiler.dependencies.ArtifactCoordinatesResolver;
import org.springframework.boot.cli.compiler.dependencies.CompositeDependencyManagement;
import org.springframework.boot.cli.compiler.dependencies.DependencyManagement;
import org.springframework.boot.cli.compiler.dependencies.DependencyManagementArtifactCoordinatesResolver;
import org.springframework.boot.cli.compiler.dependencies.SpringBootDependenciesDependencyManagement;

/**
 * Context used when resolving dependencies.
 *
 * @author Andy Wilkinson
 * @since 1.1.0
 */
public class DependencyResolutionContext {

	private final Map<String, Dependency> managedDependencyByGroupAndArtifact = new HashMap<String, Dependency>();

	private final List<Dependency> managedDependencies = new ArrayList<Dependency>();

	private DependencyManagement dependencyManagement = new SpringBootDependenciesDependencyManagement();

	private ArtifactCoordinatesResolver artifactCoordinatesResolver = new DependencyManagementArtifactCoordinatesResolver(
			this.dependencyManagement);

	public DependencyResolutionContext() {
		addDependencyManagement(this.dependencyManagement);
	}

	private String getIdentifier(Dependency dependency) {
		return getIdentifier(dependency.getArtifact().getGroupId(), dependency
				.getArtifact().getArtifactId());
	}

	private String getIdentifier(String groupId, String artifactId) {
		return groupId + ":" + artifactId;
	}

	public ArtifactCoordinatesResolver getArtifactCoordinatesResolver() {
		return this.artifactCoordinatesResolver;
	}

	public String getManagedVersion(String groupId, String artifactId) {
		Dependency dependency = getManagedDependency(groupId, artifactId);
		if (dependency == null) {
			dependency = this.managedDependencyByGroupAndArtifact.get(getIdentifier(
					groupId, artifactId));
		}
		return dependency != null ? dependency.getArtifact().getVersion() : null;
	}

	public List<Dependency> getManagedDependencies() {
		return Collections.unmodifiableList(this.managedDependencies);
	}

	private Dependency getManagedDependency(String group, String artifact) {
		return this.managedDependencyByGroupAndArtifact
				.get(getIdentifier(group, artifact));
	}

	void addManagedDependencies(List<Dependency> dependencies) {
		this.managedDependencies.addAll(dependencies);
		for (Dependency dependency : dependencies) {
			this.managedDependencyByGroupAndArtifact.put(getIdentifier(dependency),
					dependency);
		}
	}

	public void addDependencyManagement(DependencyManagement dependencyManagement) {
		for (org.springframework.boot.cli.compiler.dependencies.Dependency dependency : dependencyManagement
				.getDependencies()) {
			List<Exclusion> aetherExclusions = new ArrayList<Exclusion>();
			for (org.springframework.boot.cli.compiler.dependencies.Dependency.Exclusion exclusion : dependency
					.getExclusions()) {
				aetherExclusions.add(new Exclusion(exclusion.getGroupId(), exclusion
						.getArtifactId(), "*", "*"));
			}
			Dependency aetherDependency = new Dependency(new DefaultArtifact(
					dependency.getGroupId(), dependency.getArtifactId(), "jar",
					dependency.getVersion()), JavaScopes.COMPILE, false, aetherExclusions);
			this.managedDependencies.add(0, aetherDependency);
			this.managedDependencyByGroupAndArtifact.put(getIdentifier(aetherDependency),
					aetherDependency);
		}
		this.artifactCoordinatesResolver = new DependencyManagementArtifactCoordinatesResolver(
				new CompositeDependencyManagement(dependencyManagement,
						this.dependencyManagement));
	}

}

@@ -1853,55 +1853,11 @@
  = n
-ew SpringBootDependenciesDependencyManagement()
+ull
 ;%0A%0A%09
@@ -1923,93 +1923,8 @@
 lver
- = new DependencyManagementArtifactCoordinatesResolver(%0A%09%09%09this.dependencyManagement)
 ;%0A%0A%09
@@ -1988,22 +1988,43 @@
 agement(
-this.d
+new SpringBootDependenciesD
 ependenc
@@ -2034,16 +2034,18 @@
 nagement
+()
 );%0A%09%7D%0A%0A%09
@@ -2151,33 +2151,37 @@
 ().getGroupId(),
- 
+%0A%09%09%09%09
 dependency%0A%09%09%09%09.
@@ -2174,21 +2174,16 @@
 pendency
-%0A%09%09%09%09
 .getArti
@@ -2660,16 +2660,22 @@
 Artifact
+%0A%09%09%09%09%09
 .get(get
@@ -2685,22 +2685,16 @@
 ntifier(
-%0A%09%09%09%09%09
 groupId,
@@ -3809,17 +3809,23 @@
 oupId(),
- 
+%0A%09%09%09%09%09%09
 exclusio
@@ -3825,23 +3825,16 @@
 xclusion
-%0A%09%09%09%09%09%09
 .getArti
@@ -3908,16 +3908,22 @@
 endency(
+%0A%09%09%09%09%09
 new Defa
@@ -3934,22 +3934,16 @@
 rtifact(
-%0A%09%09%09%09%09
 dependen
@@ -3958,17 +3958,24 @@
 oupId(),
- 
+%0A%09%09%09%09%09%09%09
 dependen
@@ -4000,22 +4000,17 @@
 , %22jar%22,
-%0A%09%09%09%09%09
+ 
 dependen
@@ -4026,17 +4026,22 @@
 sion()),
- 
+%0A%09%09%09%09%09
 JavaScop
@@ -4252,95 +4252,98 @@
 his.
-artifactCoordinatesResolver = new DependencyManagementArtifactCoordinatesResolver(
+dependencyManagement = this.dependencyManagement == null%0A%09%09%09%09? dependencyManagement
 %0A%09%09%09%09
+: 
 new 
@@ -4426,16 +4426,137 @@
 agement)
+;%0A%09%09this.artifactCoordinatesResolver = new DependencyManagementArtifactCoordinatesResolver(%0A%09%09%09%09this.dependencyManagement
 );%0A%09%7D%0A%0A%7D

Fix DependencyResolutionContext for multiple boms