500197 : Take care of existing actions

in case a ProjectFacetVersion is already planned to be removed or
changed, the uninstall action is useless and will break, so don't add it 

Change-Id: If5101a99673c3eb4047a705bef8a298b9b223f7f
Signed-off-by: Aurelien Pupier <apupier@redhat.com>
diff --git a/org.eclipse.m2e.wtp/src/org/eclipse/m2e/wtp/WTPProjectsUtil.java b/org.eclipse.m2e.wtp/src/org/eclipse/m2e/wtp/WTPProjectsUtil.java
index 342076c..340aa8e 100644
--- a/org.eclipse.m2e.wtp/src/org/eclipse/m2e/wtp/WTPProjectsUtil.java
+++ b/org.eclipse.m2e.wtp/src/org/eclipse/m2e/wtp/WTPProjectsUtil.java
@@ -438,13 +438,37 @@
       throw new IllegalArgumentException(Messages.WTPProjectsUtil_Actions_Cant_Be_Null);
     }
     for (IProjectFacetVersion existingFacetVersion : project.getProjectFacets()) {
-      if (facetVersion.conflictsWith(existingFacetVersion)) {
-        actions.add(new IFacetedProject.Action(IFacetedProject.Action.Type.UNINSTALL, existingFacetVersion, null));
-      }
+    	if (facetVersion.conflictsWith(existingFacetVersion)
+    			&& !containsVersionChange(actions, existingFacetVersion)
+    			&& !containsVersionUninstall(actions, existingFacetVersion)){
+    		//TODO: in case of version change, we may need to search if the new version is conflicting. What about also of chained modifications?
+    			actions.add(new IFacetedProject.Action(IFacetedProject.Action.Type.UNINSTALL, existingFacetVersion, null));
+    	}
     }
   }
 
   /**
+   * return if the list of Action contains an uninstall action for the provided facetVersion
+   */
+  private static boolean containsVersionUninstall(Set<Action> actions, IProjectFacetVersion facetVersion) {
+	  return actions.stream()
+			  .filter(action -> IFacetedProject.Action.Type.UNINSTALL.equals(action.getType()))
+			  .filter(action -> facetVersion.equals(action.getProjectFacetVersion()))
+			  .findAny().isPresent();
+  }
+
+  /**
+   * return if the list of Action contains a version change action for the provided facetVersion
+   */
+  private static boolean containsVersionChange(Set<Action> actions, IProjectFacetVersion facetVersion) {
+	  IProjectFacet projectFacet = facetVersion.getProjectFacet();
+	  return actions.stream()
+			  .filter(action -> IFacetedProject.Action.Type.VERSION_CHANGE.equals(action.getType()))
+			  .filter(action -> projectFacet.equals(action.getProjectFacetVersion().getProjectFacet()))
+			  .findAny().isPresent();
+  }
+
+  /**
    * @param actions
    * @param project
    * @param facetedProject
@@ -462,7 +486,6 @@
    * @param project
    * @return
    */
-  @SuppressWarnings("restriction")
   public static boolean hasWebFragmentFacet(IProject project) {
     return FacetedProjectUtilities.isProjectOfType(project, WTPProjectsUtil.WEB_FRAGMENT_FACET.getId());
   }