final PsiFile psiFile = problemDescriptor.getPsiElement().getContainingFile();
new WriteCommandAction(project, psiFile) {
protected void run(Result result) throws Throwable
{
MavenDomProjectModel model =
MavenDomUtil.getMavenDomProjectModel(getProject(), psiFile.getVirtualFile());
// adds a new dependency to the end of the list
MavenDomDependency dummy = model.getDependencies().addDependency();
dummy.getArtifactId().setStringValue(mavenResult.getArtifactId());
dummy.getVersion().setStringValue(mavenResult.getVersion());
dummy.getGroupId().setStringValue(mavenResult.getGroupId());
// copy over scope from old item
if (!"".equals(dependency.getScope().getStringValue())) {
dummy.getScope().setStringValue(dependency.getScope().getStringValue());
}
PsiElement newDep = dummy.getXmlElement();
PsiElement oldDep = dependency.getXmlElement();
// add after the old element a copy of our dummy element (which is at the end of the list so far)
oldDep.getParent().addAfter(newDep.copy(), oldDep);
// kill old dependency and the dummy
oldDep.delete();
newDep.delete();
// finally check the repo urls, if we need to add a new one.
MavenRepository[] repos = mavenResult.getBundleRepository().getMavenRepositories();
List<MavenDomRepository> repositories = model.getRepositories().getRepositories();
List<MavenRepository> knownRepositories = new ArrayList<MavenRepository>();
for (MavenDomRepository repository : repositories) {
String knownRepoUrl = repository.getUrl().getStringValue();
for (MavenRepository repo : repos) {
if (repo.getRepositoryUrl().equals(knownRepoUrl)) {
knownRepositories.add(repo);
break;
}
}
}
List<MavenRepository> unknownRepositories =
new ArrayList<MavenRepository>(Arrays.asList(repos));
unknownRepositories.removeAll(knownRepositories);
// now we have a list of Repos we still don't know.
// add these to the repo list
for (MavenRepository unknownRepository : unknownRepositories) {
// fix up IDEA-24324, we use the ID provided by MavenRepository instead of using its URL
// which breaks maven on windows systems.
MavenDomRepository repo = model.getRepositories().addRepository();
repo.getId().setStringValue(unknownRepository.getRepositoryId());
repo.getUrl().setStringValue(unknownRepository.getRepositoryUrl());
repo.getName().setStringValue(unknownRepository.getRepositoryDescription());
}
}