From 042c18345ff7d38316ff42484018cdd747d5c797 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Tue, 14 Sep 2010 06:33:26 +0000 Subject: SONAR-1229 backup/restore quality profiles --- .../org/sonar/server/rules/ProfilesConsole.java | 23 +++++----- .../main/java/org/sonar/server/ui/JRubyFacade.java | 4 +- .../WEB-INF/app/controllers/profiles_controller.rb | 15 +++---- .../WEB-INF/app/views/profiles/index.html.erb | 49 ++++++++++++---------- 4 files changed, 44 insertions(+), 47 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/rules/ProfilesConsole.java b/sonar-server/src/main/java/org/sonar/server/rules/ProfilesConsole.java index 0a67c2a7fed..934575f3f47 100644 --- a/sonar-server/src/main/java/org/sonar/server/rules/ProfilesConsole.java +++ b/sonar-server/src/main/java/org/sonar/server/rules/ProfilesConsole.java @@ -24,10 +24,7 @@ import org.apache.commons.lang.StringUtils; import org.sonar.api.ServerComponent; import org.sonar.api.database.DatabaseSession; import org.sonar.api.profiles.*; -import org.sonar.api.rules.ActiveRule; -import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleFinder; -import org.sonar.api.rules.RuleQuery; import org.sonar.api.utils.ValidationMessages; import org.sonar.jpa.session.DatabaseSessionFactory; @@ -37,7 +34,6 @@ import java.io.Writer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Map; public final class ProfilesConsole implements ServerComponent { @@ -73,16 +69,19 @@ public final class ProfilesConsole implements ServerComponent { return null; } - public ValidationMessages restoreProfile(String profileName, String language, String xmlBackup) { - DatabaseSession session = sessionFactory.getSession(); - RulesProfile profile = session.getSingleResult(RulesProfile.class, "name", profileName, "language", language); + public ValidationMessages restoreProfile(String xmlBackup) { + ValidationMessages messages = ValidationMessages.create(); + RulesProfile profile = XMLProfileImporter.create(ruleFinder).importProfile(new StringReader(xmlBackup), messages); if (profile != null) { - session.remove(session); + DatabaseSession session = sessionFactory.getSession(); + RulesProfile existingProfile = session.getSingleResult(RulesProfile.class, "name", profile.getName(), "language", profile.getLanguage()); + if (existingProfile != null) { + messages.addErrorText("The profile " + profile + " already exists. Please delete it before restoring."); + } else if (!messages.hasErrors()) { + session.saveWithoutFlush(profile); + session.commit(); + } } - ValidationMessages messages = ValidationMessages.create(); - profile = XMLProfileImporter.create(ruleFinder).importProfile(new StringReader(xmlBackup), messages); - session.saveWithoutFlush(profile); - session.commit(); return messages; } 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 2c2cd383a4d..98f8ff8dce8 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 @@ -136,8 +136,8 @@ public class JRubyFacade { return getContainer().getComponent(ProfilesConsole.class).backupProfile(profileId); } - public ValidationMessages restoreProfile(String profileName, String language, String xmlBackup) { - return getContainer().getComponent(ProfilesConsole.class).restoreProfile(profileName, language, xmlBackup); + public ValidationMessages restoreProfile(String xmlBackup) { + return getContainer().getComponent(ProfilesConsole.class).restoreProfile(xmlBackup); } public List getProfileExportersForLanguage(String language) { 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 93dd9d3e065..50e9f6b27e2 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 @@ -154,15 +154,10 @@ class ProfilesController < ApplicationController # # def restore - profile_name=params[:name] - language=params[:language] - profile=Profile.find(:first, :conditions => {:name => profile_name, :language => language}) - if profile - flash[:error]='An existing profile can not be restored. Please delete it before.' - elsif params[:backup].blank? + if params[:backup].blank? flash[:warning]='Please upload a backup file.' else - messages=java_facade.restoreProfile(profile_name, language, read_file_param(params[:backup])) + messages=java_facade.restoreProfile(read_file_param(params[:backup])) flash_validation_messages(messages) end redirect_to :action => 'index' @@ -269,13 +264,13 @@ class ProfilesController < ApplicationController def flash_validation_messages(messages) # only 4 messages are kept each time to avoid cookie overflow. if messages.hasErrors() - flash[:error]=messages.getErrors().to_a[0...4].map{|m| m.getLabel()}.join('
') + flash[:error]=messages.getErrors().to_a[0...4].join('
') end if messages.hasWarnings() - flash[:warning]=messages.getWarnings().to_a[0...4].map{|m| m.getLabel()}.join('
') + flash[:warning]=messages.getWarnings().to_a[0...4].join('
') end if messages.hasInfos() - flash[:notice]=messages.getInfos().to_a[0...4].map{|m| m.getLabel()}.join('
') + flash[:notice]=messages.getInfos().to_a[0...4].join('
') end end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb index 0fb92332142..318ef909ae7 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb @@ -1,3 +1,29 @@ +<% if administrator? %> +
+ +
+ +<% end %> + + <% languages.sort{|x,y| x.getName() <=> y.getName()}.each do |language| importers=controller.java_facade.getProfileImportersForLanguage(language.getKey()) @@ -8,9 +34,6 @@
  • Create
  • -
  • - Restore -
  • <% end %>

    <%= language.getName() %> profiles

    @@ -42,26 +65,6 @@ - <% end %> -- cgit v1.2.3