package picscout.depend.dependency.classes;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import picscout.depend.dependency.interfaces.IProjectDependencyMapper;
import picscout.depend.dependency.interfaces.IProjectDescriptor;
import picscout.depend.dependency.interfaces.ISolution;

/**
 * Map solution to all projects in it. Also knows given a changed project to
 * tell which solutions should be built by order.
 * 
 * @author OSchliefer
 *
 */
public class SolutionMapper {

	private HashSet<ISolution> map;
	private IProjectDependencyMapper projectMapper;

	public SolutionMapper(IProjectDependencyMapper projectMapper) {
		this.projectMapper = projectMapper;
		map = new HashSet<ISolution>();
	}

	public void add(ISolution solution) {
		map.add(solution);
	}

	/**
	 * Get all solutions that should be built as a result of project change,
	 * either because this project is in them, or because a dependent project is
	 * in them. The result is by build order.
	 * 
	 * @param projectDescriptor
	 * @return
	 */
	public List<ISolution> getSolutionsByProject(
			IProjectDescriptor projectDescriptor) {
		List<ISolution> reuslt = new ArrayList<ISolution>();
		List<IProjectDescriptor> chain = projectMapper
				.getProjectsThatDepeantOn(projectDescriptor);
		for (ISolution solution : map) {
			List<IProjectDescriptor> projectInSolution = solution
					.getProjectsDescriptors();
			if (projectInSolution.contains(projectDescriptor)) {
				if (!reuslt.contains(solution)) {
					reuslt.add(solution);
					break;
				}
			}
			for (IProjectDescriptor descriptor : chain) {
				if (projectInSolution.contains(descriptor)) {
					if (!reuslt.contains(solution)) {
						reuslt.add(solution);
						break;
					}
				}
			}
		}

		return reuslt;

	}

}

@@ -1294,21 +1294,19 @@
 %0A%09%09f
-or (I
+ill
 Solution
  sol
@@ -1305,35 +1305,80 @@
 tion
- solution : map
+ByProject(projectDescriptor, reuslt);%0A%09%09%0A%09%09if (chain != null
 ) %7B%0A%09%09%09
-List%3C
+for (
 IPro
@@ -1395,57 +1395,58 @@
 ptor
-%3E projectInSolution = solution%0A%09%09%09%09%09.get
+ descriptor : chain) %7B%0A%09%09%09%09fillSolutionBy
 Project
-sD
+(d
 escr
@@ -1454,102 +1454,182 @@
 ptor
-s();%0A%09%09%09if (projectInSolution.contains(projectDescriptor)) %7B%0A%09%09%09%09if (!reuslt.contains(
+, reuslt);%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return reuslt;%0A%0A%09%7D%0A%0A%09private void fillSolutionByProject(IProjectDescriptor projectDescriptor,%0A%09%09%09List%3CISolution%3E reuslt) %7B%0A%09%09%0A%09%09for (ISolution 
 solution
 )) %7B
@@ -1628,37 +1628,75 @@
 tion
-)
+ : map
 ) %7B%0A%09%09%09
-%09%09reuslt.add(
+List%3CIProjectDescriptor%3E projectsInSolution = 
 solution
 );%0A%09
@@ -1695,50 +1695,26 @@
 tion
-);
 %0A%09%09%09%09%09
-break;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09for (I
+.get
 Project
+s
 Desc
@@ -1723,32 +1723,13 @@
 ptor
- descriptor : chain) %7B%0A%09
+s();%0A
 %09%09%09i
@@ -1738,16 +1738,17 @@
 (project
+s
 InSoluti
@@ -1759,17 +1759,24 @@
 ontains(
-d
+projectD
 escripto
@@ -1777,25 +1777,24 @@
 criptor)) %7B%0A
-%09
 %09%09%09%09if (!reu
@@ -1824,17 +1824,16 @@
  %7B%0A%09%09%09%09%09
-%09
 reuslt.a
@@ -1854,57 +1854,18 @@
 %09%09%09%09
-%09%09break;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return reuslt;%0A
+%7D%0A%09%09%09%7D%0A%09%09%7D
 %0A%09%7D%0A

add tests for solution map