summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2010-09-14 06:33:26 +0000
committersimonbrandhof <simon.brandhof@gmail.com>2010-09-14 06:33:26 +0000
commit042c18345ff7d38316ff42484018cdd747d5c797 (patch)
treee36626b2c1ef54ab989c7c6929f40ab85ef23276
parentb384d1804d4d4759be92094ceb417276ed1dfecf (diff)
downloadsonarqube-042c18345ff7d38316ff42484018cdd747d5c797.tar.gz
sonarqube-042c18345ff7d38316ff42484018cdd747d5c797.zip
SONAR-1229 backup/restore quality profiles
-rw-r--r--sonar-server/src/main/java/org/sonar/server/rules/ProfilesConsole.java23
-rw-r--r--sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java4
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb15
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb49
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<ProfileExporter> 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('<br/>')
+ flash[:error]=messages.getErrors().to_a[0...4].join('<br/>')
end
if messages.hasWarnings()
- flash[:warning]=messages.getWarnings().to_a[0...4].map{|m| m.getLabel()}.join('<br/>')
+ flash[:warning]=messages.getWarnings().to_a[0...4].join('<br/>')
end
if messages.hasInfos()
- flash[:notice]=messages.getInfos().to_a[0...4].map{|m| m.getLabel()}.join('<br/>')
+ flash[:notice]=messages.getInfos().to_a[0...4].join('<br/>')
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? %>
+<div class="line-block marginbottom10">
+ <ul style="float: right" class="horizontal">
+ <li class="marginleft10 restore">
+ <a href="#" onclick="$('restore-form').show();return false;" id="restore-link">Restore profile</a>
+ </li>
+ </ul>
+</div>
+<form class="admin marginbottom10" id="restore-form" action="<%= url_for :action => 'restore' -%>" enctype="multipart/form-data" style="display: none" method="post">
+ <table class="spaced width100">
+ <tr>
+ <td width="1%" nowrap>Backup: </td>
+ <td><%= file_field_tag 'backup' %></td>
+ </tr>
+
+ <tr>
+ <td colspan="2">
+ <input type="submit" value="Restore profile"></input>
+ <a href="#" onclick="$('restore-form').reset();$('restore-form').hide();return false;">Cancel</a>
+ </td>
+ </tr>
+ </table>
+ </form>
+<% end %>
+
+
<%
languages.sort{|x,y| x.getName() <=> y.getName()}.each do |language|
importers=controller.java_facade.getProfileImportersForLanguage(language.getKey())
@@ -8,9 +34,6 @@
<li class="marginleft10 add">
<a href="#" onClick="$('create-form-<%= language.getKey() -%>').show();return false;" id="create-link-<%= language.getKey() -%>">Create</a>
</li>
- <li class="marginleft10 restore">
- <a href="#" onclick="$('restore-form-<%= language.getKey() -%>').show();return false;" id="restore-link-<%= language.getKey() -%>">Restore</a>
- </li>
</ul>
<% end %>
<h2><%= language.getName() %> profiles</h2>
@@ -42,26 +65,6 @@
</tr>
</table>
</form>
- <form class="admin" id="restore-form-<%= language.getKey()-%>" action="<%= url_for :action => 'restore' -%>" enctype="multipart/form-data" style="display: none" method="post">
- <input type="hidden" name="language" value="<%= language.getKey() -%>"></input>
- <table class="spaced width100">
- <tr>
- <td width="1%" nowrap>Name: </td>
- <td><input type="text" name="name"></input></td>
- </tr>
- <tr>
- <td width="1%" nowrap>Backup: </td>
- <td><%= file_field_tag 'backup' %></td>
- </tr>
-
- <tr>
- <td colspan="2">
- <input type="submit" value="Restore <%= language.getName() %> profile"></input>
- <a href="#" onclick="$('restore-form-<%= language.getKey()-%>').reset();$('restore-form-<%= language.getKey()-%>').hide();return false;">Cancel</a>
- </td>
- </tr>
- </table>
- </form>
<% end %>
<table class="data2 width100" id="profiles_<%= language.getKey() -%>">