Maven: suspend/resume conter bugfix
[idea/community.git] / plugins / maven / src / main / java / org / jetbrains / idea / maven / dom / generate / GenerateAddManagedDependencyAction.java
1 /*
2  * Copyright 2000-2010 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.jetbrains.idea.maven.dom.generate;
17
18 import com.intellij.openapi.editor.Editor;
19 import com.intellij.openapi.util.text.StringUtil;
20 import com.intellij.util.Processor;
21 import com.intellij.util.containers.hash.HashSet;
22 import com.intellij.util.xml.ui.actions.generate.GenerateDomElementAction;
23 import org.jetbrains.annotations.NotNull;
24 import org.jetbrains.idea.maven.dom.MavenDomBundle;
25 import org.jetbrains.idea.maven.dom.MavenDomProjectProcessorUtils;
26 import org.jetbrains.idea.maven.dom.model.MavenDomDependencies;
27 import org.jetbrains.idea.maven.dom.model.MavenDomDependency;
28 import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel;
29 import org.jetbrains.idea.maven.project.MavenId;
30 import org.jetbrains.idea.maven.project.MavenProjectsManager;
31 import org.jetbrains.idea.maven.utils.MavenIcons;
32
33 import java.util.List;
34 import java.util.Set;
35
36 public class GenerateAddManagedDependencyAction extends GenerateDomElementAction {
37   public GenerateAddManagedDependencyAction() {
38     super(new MavenOverridingDependencyGenerateProvider(), MavenIcons.DEPENDENCY_ICON);
39   }
40
41   @Override
42   protected boolean startInWriteAction() {
43     return false;
44   }
45
46   private static class MavenOverridingDependencyGenerateProvider extends MavenGenerateProvider<MavenDomDependency> {
47     public MavenOverridingDependencyGenerateProvider() {
48       super(MavenDomBundle.message("generate.managed.dependency"), MavenDomDependency.class);
49     }
50
51     @Override
52     protected MavenDomDependency doGenerate(MavenDomProjectModel mavenModel, Editor editor) {
53       Set<MavenDomDependency> managingDependencies = collectManagingDependencies(mavenModel);
54
55       List<MavenDomDependency> dependenciesToOverride = GenerateDependencyUtil.chooseDependencies(managingDependencies, mavenModel.getManager().getProject());
56
57       for (MavenDomDependency parentDependency : dependenciesToOverride) {
58         String groupId = parentDependency.getGroupId().getStringValue();
59         String artifactId = parentDependency.getArtifactId().getStringValue();
60
61         if (!StringUtil.isEmptyOrSpaces(groupId) && !StringUtil.isEmptyOrSpaces(artifactId)) {
62           MavenId id = new MavenId(groupId, artifactId, parentDependency.getVersion().getStringValue());
63
64           MavenProjectsManager manager = MavenProjectsManager.getInstance(editor.getProject());
65
66           MavenDomDependency dependency = manager.addOverridenDependency(manager.findProject(mavenModel.getModule()), id);
67
68           dependency.getVersion().undefine();
69         }
70
71       }
72
73       return null;
74     }
75   }
76
77   @NotNull
78   public static Set<MavenDomDependency> collectManagingDependencies(@NotNull MavenDomProjectModel model) {
79     final Set<MavenDomDependency> dependencies = new HashSet<MavenDomDependency>();
80
81     final List<MavenDomDependency> existingDependencies = model.getDependencies().getDependencies();
82
83     Processor<MavenDomDependencies> collectProcessor = new Processor<MavenDomDependencies>() {
84       public boolean process(MavenDomDependencies mavenDomDependencies) {
85         for (MavenDomDependency dependency : mavenDomDependencies.getDependencies()) {
86           String groupId = dependency.getGroupId().getStringValue();
87           String artifactId = dependency.getArtifactId().getStringValue();
88           if (StringUtil.isEmptyOrSpaces(groupId) || StringUtil.isEmptyOrSpaces(artifactId)) continue;
89
90           if (!isDependencyExist(groupId, artifactId, existingDependencies)) {
91             dependencies.add(dependency);
92           }
93         }
94         return false;
95       }
96     };
97
98     MavenDomProjectProcessorUtils.processDependenciesInDependencyManagement(model, collectProcessor, model.getManager().getProject());
99
100     return dependencies;
101   }
102
103   private static boolean isDependencyExist(String groupId, String artifactId, List<MavenDomDependency> existingDependencies) {
104     for (MavenDomDependency existingDependency : existingDependencies) {
105        if(groupId.equals(existingDependency.getGroupId().getStringValue()) || artifactId.equals(existingDependency.getArtifactId().getStringValue())) {
106          return true;
107       }
108     }
109     return false;
110   }
111 }