From 8c305fd210becd5f62c643eeadc0ae2f6c91b8a4 Mon Sep 17 00:00:00 2001 From: Godin Date: Tue, 21 Dec 2010 17:59:22 +0000 Subject: [PATCH] SONAR-1722: Take inheritance into account during rename of profiles --- .../server/configuration/ProfilesManager.java | 14 ++++++++++++++ .../main/java/org/sonar/server/ui/JRubyFacade.java | 4 ++++ .../WEB-INF/app/controllers/profiles_controller.rb | 3 +-- .../configuration/InheritedProfilesTest.java | 7 +++++++ .../shouldRenameInheritedProfile-result.xml | 12 ++++++++++++ 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldRenameInheritedProfile-result.xml diff --git a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java index 1c6114ba386..bae9f3bff68 100644 --- a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java +++ b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java @@ -38,6 +38,20 @@ public class ProfilesManager extends BaseDao { this.rulesDao = rulesDao; } + public void renameProfile(int profileId, String newProfileName) { + RulesProfile profile = getSession().getSingleResult(RulesProfile.class, "id", profileId); + if (profile != null && !profile.getProvided()) { + String hql = "UPDATE " + RulesProfile.class.getSimpleName() + " o SET o.parentName=:newName WHERE o.parentName=:oldName"; + getSession().getEntityManager().createQuery(hql) + .setParameter("oldName", profile.getName()) + .setParameter("newName", newProfileName) + .executeUpdate(); + profile.setName(newProfileName); + getSession().save(profile); + getSession().commit(); + } + } + public void copyProfile(int profileId, String newProfileName) { RulesProfile profile = getSession().getSingleResult(RulesProfile.class, "id", profileId); RulesProfile toImport = (RulesProfile) profile.clone(); diff --git a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java index d4e89669dbd..5aec6c8f921 100644 --- a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java +++ b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java @@ -189,6 +189,10 @@ public final class JRubyFacade implements ServerComponent { return getContainer().getComponent(ProfilesConsole.class).getProfileExporter(exporterKey).getMimeType(); } + public void renameProfile(int profileId, String newProfileName) { + getProfilesManager().renameProfile(profileId, newProfileName); + } + public void copyProfile(long profileId, String newProfileName) { getProfilesManager().copyProfile((int) profileId, newProfileName); } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb index eb0235793aa..4fdd7bd2380 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb @@ -244,8 +244,7 @@ class ProfilesController < ApplicationController if existing flash[:warning]='This profile name already exists.' elsif !profile.provided? - profile.name=name - profile.save + java_facade.renameProfile(profile.id, name) end end redirect_to :action => 'index' diff --git a/sonar-server/src/test/java/org/sonar/server/configuration/InheritedProfilesTest.java b/sonar-server/src/test/java/org/sonar/server/configuration/InheritedProfilesTest.java index d3ea548b69e..500846033fe 100644 --- a/sonar-server/src/test/java/org/sonar/server/configuration/InheritedProfilesTest.java +++ b/sonar-server/src/test/java/org/sonar/server/configuration/InheritedProfilesTest.java @@ -41,6 +41,13 @@ public class InheritedProfilesTest extends AbstractDbUnitTestCase { checkTables("shouldNotDeleteInheritedProfile", "rules_profiles"); } + @Test + public void shouldRenameInheritedProfile() { + setupData("shouldCheckCycles"); + profilesManager.renameProfile(1, "newName"); + checkTables("shouldRenameInheritedProfile", "rules_profiles"); + } + @Test public void shouldSetParent() { setupData("shouldSetParent"); diff --git a/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldRenameInheritedProfile-result.xml b/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldRenameInheritedProfile-result.xml new file mode 100644 index 00000000000..d42f80577cb --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldRenameInheritedProfile-result.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + -- 2.39.5