]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1722: Take inheritance into account during rename of profiles
authorGodin <mandrikov@gmail.com>
Tue, 21 Dec 2010 17:59:22 +0000 (17:59 +0000)
committerGodin <mandrikov@gmail.com>
Tue, 21 Dec 2010 17:59:22 +0000 (17:59 +0000)
sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java
sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java
sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb
sonar-server/src/test/java/org/sonar/server/configuration/InheritedProfilesTest.java
sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldRenameInheritedProfile-result.xml [new file with mode: 0644]

index 1c6114ba386af753c5fdb7d30efe729ae9735868..bae9f3bff68b0ca00ff55bedc7112c1e334aa9d0 100644 (file)
@@ -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();
index d4e89669dbd2ea13c1c70efcf3b1cdc035d0a7d9..5aec6c8f921bdfee1f5a215b2a1f31910bd7d156 100644 (file)
@@ -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);
   }
index eb0235793aab6c2dd126c3626635d0ab3c18ca34..4fdd7bd23802d3f9e121b0848dbf2ba266d91bb1 100644 (file)
@@ -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'
index d3ea548b69e6d4ea84086c547928d186dcc26198..500846033fe82a6c8838e03637685c313367c0f2 100644 (file)
@@ -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 (file)
index 0000000..d42f805
--- /dev/null
@@ -0,0 +1,12 @@
+<dataset>
+
+  <rules id="1" name="foo" description="test" plugin_config_key="checker/foo"
+         plugin_rule_key="checkstyle.rule1" plugin_name="plugin" enabled="true" cardinality="SINGLE" parent_id="[null]"/>
+
+  <rules_profiles id="1" provided="false" name="newName" default_profile="0" language="java" parent_name="[null]"/>
+  
+  <rules_profiles id="2" provided="false" name="level2" default_profile="0" language="java" parent_name="newName"/>
+  
+  <rules_profiles id="3" provided="false" name="level3" default_profile="0" language="java" parent_name="level2"/>
+
+</dataset>