From 2be9d39965149fb5395e06c5036d79dab63447e4 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 24 Sep 2012 22:02:27 +0200 Subject: [PATCH] SONAR-3623 apply modal window to creation/restore Q profiles --- .../resources/org/sonar/l10n/core.properties | 5 +- .../app/controllers/profiles_controller.rb | 93 +++++++++---------- .../webapp/WEB-INF/app/models/api/utils.rb | 4 + .../main/webapp/WEB-INF/app/models/profile.rb | 88 ++++++++++++------ .../app/views/profiles/_create_form.html.erb | 37 ++++++++ .../app/views/profiles/_restore_form.html.erb | 22 +++++ .../WEB-INF/app/views/profiles/index.html.erb | 56 +---------- .../src/main/webapp/stylesheets/style.css | 2 +- 8 files changed, 175 insertions(+), 132 deletions(-) create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_create_form.html.erb create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_restore_form.html.erb diff --git a/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties b/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties index 228c8e39dea..abdbd4a4df6 100644 --- a/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties +++ b/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties @@ -1147,10 +1147,10 @@ quality_profiles.compare_profiles=Compare profiles quality_profiles.restore_profile=Restore profile quality_profiles.x_language_profiles= {0} profiles quality_profiles.optional_configuration_file=Optional configuration file -quality_profiles.create_x_language_profile=Create {0} profile +quality_profiles.create_x_language_profile=Create {0} Profile quality_profiles.are_you_sure_want_x_profile_as_default=Are you sure that you want to set the profile "{0}" as default ? quality_profiles.profile_x_created=Profile "{0}" created. Set it as default or link it to a project to use it for next measures. -quality_profiles.profile_x_already_exists=This profile already exists: {0}. +quality_profiles.already_exists=This profile already exists. quality_profiles.please_type_profile_name=Please type a profile name. quality_profiles.delete_confirm_title=Delete Profile quality_profiles.profile_x_deleted=Profile "{0}" is deleted. @@ -1159,7 +1159,6 @@ quality_profiles.profile_x_not_activated=Profile "{0}" is created but not activa quality_profiles.please_upload_backup_file=Please upload a backup file. quality_profiles.profile_x_associated_to_x_projects=Profile "{0}" associated to {1} projects. quality_profiles.profile_name_cant_be_blank=Profile name can not be blank. -quality_profiles.profile_name_already_exists=This profile name already exists. quality_profiles.new_name=New name quality_profiles.name_for_new_profile=Name for the new profile quality_profiles.delete_confirm_title=Delete Profile 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 2e41cc0eab8..308a84a491c 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 @@ -46,6 +46,13 @@ class ProfilesController < ApplicationController end + # GET /profiles/create_form?language= + def create_form + language = params[:language] + bad_request 'Missing parameter: language' if language.blank? + profile = Profile.new(:language => language) + render :partial => 'profiles/create_form', :locals => {:language_key => language} + end # # @@ -55,33 +62,25 @@ class ProfilesController < ApplicationController def create profile_name=params[:name] language=params[:language] - if profile_name.blank?|| language.blank? - flash[:warning]=message('quality_profiles.please_type_profile_name') - else - profile=Profile.find_by_name_and_language(profile_name, language) - if profile - flash[:error]=message('quality_profiles.profile_x_already_exists', :params => profile_name) - else - profile = Profile.create(:name => profile_name, :language => language, :default_profile => false, :enabled => true) - ok=profile.errors.empty? - if ok && params[:backup] - params[:backup].each_pair do |importer_key, file| - if !file.blank? && ok - messages = java_facade.importProfile(profile_name, language, importer_key, read_file_param(file)) - flash_validation_messages(messages) - ok &= !messages.hasErrors() - end - end - end - if ok - flash[:notice]=message('quality_profiles.profile_x_created', :params => profile.name) - else - profile.reload - profile.destroy + profile = Profile.create(:name => profile_name, :language => language, :default_profile => false, :enabled => true) + ok = profile.errors.empty? + if ok && params[:backup] + params[:backup].each_pair do |importer_key, file| + if !file.blank? && ok + profile.import_configuration(importer_key, file) + ok &= profile.errors.empty? end end end + + flash_profile(profile) + if ok + flash[:notice]=message('quality_profiles.profile_x_created', :params => profile.name) + elsif profile.id + Profile.destroy(profile.id) + end + redirect_to :action => 'index' end @@ -155,18 +154,17 @@ class ProfilesController < ApplicationController end + def restore_form + render :partial => 'profiles/restore_form' + end - # - # # POST /profiles/restore/ - # - # def restore if params[:backup].blank? flash[:warning]=message('quality_profiles.please_upload_backup_file') else - messages=java_facade.restoreProfile(read_file_param(params[:backup]), false) - flash_validation_messages(messages) + messages=java_facade.restoreProfile(Api::Utils.read_post_request_param(params[:backup]), false) + flash_messages(messages) end redirect_to :action => 'index' end @@ -186,7 +184,7 @@ class ProfilesController < ApplicationController profile = Profile.find_by_name_and_language(CGI::unescape(params[:name]), language) end not_found('Profile not found') unless profile - + if (params[:format].blank?) # standard sonar format result = java_facade.backupProfile(profile.id) @@ -205,7 +203,7 @@ class ProfilesController < ApplicationController # def inheritance @profile = Profile.find(params[:id]) - + profiles=Profile.find(:all, :conditions => ['language=? and id<>? and (parent_name is null or parent_name<>?) and enabled=?', @profile.language, @profile.id, @profile.name, true], :order => 'name') @select_parent = [[message('none'), nil]] + profiles.collect{ |profile| [profile.name, profile.name] } end @@ -256,7 +254,7 @@ class ProfilesController < ApplicationController else messages = java_facade.changeParentProfile(id, parent_name, current_user.name) end - flash_validation_messages(messages) + flash_messages(messages) redirect_to :action => 'inheritance', :id => id end @@ -278,8 +276,8 @@ class ProfilesController < ApplicationController # def projects @profile = Profile.find(params[:id]) - @available_projects=Project.find(:all, - :include => ['profile','snapshots'], + @available_projects=Project.find(:all, + :include => ['profile','snapshots'], :conditions => ['projects.qualifier=? AND projects.scope=? AND snapshots.islast=?', Project::QUALIFIER_PROJECT, Project::SCOPE_SET, true], :order => 'projects.name asc') @available_projects-=@profile.projects @@ -326,7 +324,7 @@ class ProfilesController < ApplicationController else existing=Profile.find(:first, :conditions => {:name => name, :language => @profile.language, :enabled => true}) if existing - @error=message('quality_profiles.profile_name_already_exists') + @error=message('quality_profiles.already_exists') elsif !@profile.provided? java_facade.renameProfile(@profile.id, name) success=true @@ -351,7 +349,7 @@ class ProfilesController < ApplicationController if params[:id1].present? && params[:id2].present? @profile1 = Profile.find(params[:id1]) @profile2 = Profile.find(params[:id2]) - + arules1 = ActiveRule.find(:all, :include => [{:active_rule_parameters => :rules_parameter}, :rule], :conditions => ['active_rules.profile_id=?', @profile1.id]) arules2 = ActiveRule.find(:all, :order => 'rules.plugin_name, rules.plugin_rule_key', :include => [{:active_rule_parameters => :rules_parameter}, :rule], @@ -385,7 +383,7 @@ class ProfilesController < ApplicationController DIFF_IN2=2 DIFF_MODIFIED=3 DIFF_SAME=4 - + private class RuleDiff @@ -417,7 +415,7 @@ class ProfilesController < ApplicationController end else @removed_params<<@arule1.parameter(param.name) - end + end elsif v2 @added_params<<@arule2.parameter(param.name) end @@ -435,7 +433,7 @@ class ProfilesController < ApplicationController rule.name()<=>other.rule.name end end - + # # Remove active rules that are identical in both collections (same severity and same parameters) # and return a map with results {:added => X, :removed => Y, :modified => Z, @@ -497,19 +495,20 @@ class ProfilesController < ApplicationController rules[rule] = [active_rule1, active_rule2] end end while !arules1.empty? || !arules2.empty? - return {:same => same, :added => added, :removed => removed, :modified => modified, :rules => rules} + return {:same => same, :added => added, :removed => removed, :modified => modified, :rules => rules} end - def read_file_param(configuration_file) - # configuration file is a StringIO - if configuration_file.respond_to?(:read) - return configuration_file.read + def flash_profile(profile) + # only 4 messages are kept each time to avoid cookie overflow. + if !profile.errors.empty? + flash[:error]=profile.errors.full_messages.to_a[0...4].join('
') + end + if profile.warnings? + flash[:warning]=profile.warnings.full_messages.to_a[0...4].join('
') end - # configuration file is not a readable object - nil end - def flash_validation_messages(messages) + def flash_messages(messages) # only 4 messages are kept each time to avoid cookie overflow. if messages.hasErrors() flash[:error]=messages.getErrors().to_a[0...4].join('
') diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb index 1fe7ec88aa4..ddf214b88e2 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb @@ -165,4 +165,8 @@ class Api::Utils nil end end + + def self.java_facade + Java::OrgSonarServerUi::JRubyFacade.getInstance() + end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb index 72d5ee07cd5..079645ff6c6 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb @@ -24,25 +24,41 @@ class Profile < ActiveRecord::Base has_many :active_rules, :class_name => 'ActiveRule', :foreign_key => 'profile_id', :dependent => :destroy, :include => ['rule'] has_many :projects, :order => 'name asc' has_many :active_rules_with_params, :class_name => 'ActiveRule', :foreign_key => 'profile_id', - :include => ['active_rule_parameters', 'active_rule_note'] + :include => ['active_rule_parameters', 'active_rule_note'] + + validates_uniqueness_of :name, :scope => :language, :case_sensitive => false, :message => Api::Utils.message('quality_profiles.already_exists') + validates_presence_of :name, :message => Api::Utils.message('quality_profiles.please_type_profile_name') + + # The warnings that are set on this record, equivalent to normal ActiveRecord errors but does not prevent + # the record from saving. + def warnings + @warnings ||= ActiveRecord::Errors.new(self) + end + + def warnings? + not warnings.empty? + end + + def notices + @notices ||= ActiveRecord::Errors.new(self) + end + + def notices? + not notices.empty? + end - validates_uniqueness_of :name, :scope => :language, :case_sensitive => false - validates_presence_of :name - - DEFAULT_PROFILE_NAME = 'Sun checks' - def active? active end - + def key "#{language}_#{name}" end - + def provided? provided end - + def validate_copy(name) new_rule_profile = Profile.new(:name => name, :provided => false, :default_profile => false, :language => language) new_rule_profile.valid? @@ -90,6 +106,7 @@ class Profile < ActiveRecord::Base end @active_hash_by_rule_id=nil + def active_hash_by_rule_id if @active_hash_by_rule_id.nil? @active_hash_by_rule_id={} @@ -106,9 +123,9 @@ class Profile < ActiveRecord::Base def count_overriding_rules @count_overriding_rules||= - begin - active_rules.count(:conditions => ['inheritance=?', 'OVERRIDES']) - end + begin + active_rules.count(:conditions => ['inheritance=?', 'OVERRIDES']) + end end def inherited? @@ -117,35 +134,48 @@ class Profile < ActiveRecord::Base def parent @parent||= - begin - if parent_name.present? - Profile.find(:first, :conditions => ['language=? and name=? and enabled=?', language, parent_name, true]) - else - nil + begin + if parent_name.present? + Profile.find(:first, :conditions => ['language=? and name=? and enabled=?', language, parent_name, true]) + else + nil + end end - end end def count_active_rules - active_rules.select{|ar| ar.rule.enabled}.size + active_rules.select { |ar| ar.rule.enabled }.size end def ancestors @ancestors ||= - begin - array=[] - if parent - array< ['language=? and parent_name=? and enabled=?', language, name, true], :order => 'name') - end + begin + Profile.find(:all, :conditions => ['language=? and parent_name=? and enabled=?', language, name, true], :order => 'name') + end + end + + def import_configuration(importer_key, file) + messages = Api::Utils.java_facade.importProfile(name, language, importer_key, Api::Utils.read_post_request_param(file)) + messages.getErrors().each do |msg| + errors.add_to_base msg + end + messages.getWarnings().each do |msg| + warnings.add_to_base msg + end + messages.getInfos().each do |msg| + notices.add_to_base msg + end end end \ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_create_form.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_create_form.html.erb new file mode 100644 index 00000000000..f4e34e07c33 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_create_form.html.erb @@ -0,0 +1,37 @@ +<% + language=controller.java_facade.getLanguages().find { |l| l.getKey()==language_key } + importers=controller.java_facade.getProfileImportersForLanguage(language_key) +%> +
+
+ + +
+

<%= h message('quality_profiles.create_x_language_profile', :params => language.getName()) -%>

+
+ +
+ +
+ + +
+ + <% importers.to_a.sort { |x, y| x.getName() <=> y.getName() }.each do |importer| %> +
+ + <%= file_field_tag "backup[#{importer.getKey()}]" %> +
<%= h message('quality_profiles.optional_configuration_file') -%>
+
+ <% end %> +
+ + +
+
+ diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_restore_form.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_restore_form.html.erb new file mode 100644 index 00000000000..a2274ec1d4f --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_restore_form.html.erb @@ -0,0 +1,22 @@ +
+
+ +
+

<%= h message('quality_profiles.restore_profile') -%>

+
+ +
+ +
+ + <%= file_field_tag 'backup' %> +
+ +
+ + +
+
\ No newline at end of file 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 c193a57642f..59f703807f1 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 @@ -3,79 +3,31 @@ <% end %>

<%= message('quality_profiles.page') -%>

-<% if administrator? %> - -<% end %> - - <% languages.sort { |x, y| x.getName() <=> y.getName() }.each do |language| - importers=controller.java_facade.getProfileImportersForLanguage(language.getKey()) %>
<% if administrator? %> <% end %>

<%= message('quality_profiles.x_language_profiles', :params => language.getName()) -%>

- <% if administrator? %> - - <% end %> - diff --git a/sonar-server/src/main/webapp/stylesheets/style.css b/sonar-server/src/main/webapp/stylesheets/style.css index eb256ea622d..dabef843723 100644 --- a/sonar-server/src/main/webapp/stylesheets/style.css +++ b/sonar-server/src/main/webapp/stylesheets/style.css @@ -2398,7 +2398,7 @@ select.medium-width { .form-field-description { clear: both; - font-size: 11px; + font-size: 93%; color: #777; } -- 2.39.5