From 76453df999839a38672dfaf3689a6d06bb1b2bcd Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Mon, 20 Sep 2010 16:44:22 +0000 Subject: fix activation of rule parameters with default value --- .../server/startup/RegisterProvidedProfiles.java | 33 +++++++++++++++++----- .../controllers/rules_configuration_controller.rb | 3 ++ .../WEB-INF/app/views/profiles/index.html.erb | 4 +-- .../app/views/rules_configuration/new.html.erb | 2 +- 4 files changed, 32 insertions(+), 10 deletions(-) (limited to 'sonar-server') diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java index 0e07aa95b0a..ad5ee2559d2 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java @@ -25,8 +25,7 @@ import org.slf4j.LoggerFactory; import org.sonar.api.database.DatabaseSession; import org.sonar.api.profiles.ProfileDefinition; import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.rules.ActiveRule; -import org.sonar.api.rules.ActiveRuleParam; +import org.sonar.api.rules.*; import org.sonar.api.utils.TimeProfiler; import org.sonar.api.utils.ValidationMessages; import org.sonar.jpa.session.DatabaseSessionFactory; @@ -43,17 +42,20 @@ public final class RegisterProvidedProfiles { private DatabaseSessionFactory sessionFactory; private List definitions = new ArrayList(); private DeprecatedProfiles deprecatedProfiles = null; + private RuleFinder ruleFinder; - public RegisterProvidedProfiles(DatabaseSessionFactory sessionFactory, + public RegisterProvidedProfiles(RuleFinder ruleFinder, DatabaseSessionFactory sessionFactory, DeprecatedProfiles deprecatedBridge, RegisterRules registerRulesBefore, ProfileDefinition[] definitions) { + this.ruleFinder = ruleFinder; this.sessionFactory = sessionFactory; this.definitions.addAll(Arrays.asList(definitions)); this.deprecatedProfiles = deprecatedBridge; } - public RegisterProvidedProfiles(DatabaseSessionFactory sessionFactory, + public RegisterProvidedProfiles(RuleFinder ruleFinder, DatabaseSessionFactory sessionFactory, DeprecatedProfiles deprecatedBridge, RegisterRules registerRulesBefore) { + this.ruleFinder = ruleFinder; this.sessionFactory = sessionFactory; this.deprecatedProfiles = deprecatedBridge; } @@ -117,9 +119,13 @@ public final class RegisterProvidedProfiles { RulesProfile persistedProfile = findOrCreate(profile.getName(), profile.getLanguage(), session); for (ActiveRule activeRule : profile.getActiveRules()) { - ActiveRule persistedRule = persistedProfile.activateRule(activeRule.getRule(), activeRule.getPriority()); - for (ActiveRuleParam param : activeRule.getActiveRuleParams()) { - persistedRule.setParameter(param.getKey(), param.getValue()); + Rule rule = getPersistedRule(activeRule); + ActiveRule persistedRule = persistedProfile.activateRule(rule, activeRule.getPriority()); + for (RuleParam param : rule.getParams()) { + String value = StringUtils.defaultString(activeRule.getParameter(param.getKey()), param.getDefaultValue()); + if (value != null) { + persistedRule.setParameter(param.getKey(), value); + } } } @@ -130,6 +136,19 @@ public final class RegisterProvidedProfiles { } + Rule getPersistedRule(ActiveRule activeRule) { + Rule rule = activeRule.getRule(); + if (rule!=null && rule.getId()==null) { + if (rule.getKey()!=null) { + rule = ruleFinder.findByKey(rule.getRepositoryKey(), rule.getKey()); + + } else if (rule.getConfigKey()!=null) { + rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(rule.getRepositoryKey()).withConfigKey(rule.getConfigKey())); + } + } + return rule; + } + private RulesProfile findOrCreate(String name, String language, DatabaseSession session) { RulesProfile profile = session.getSingleResult(RulesProfile.class, "name", name, "language", language); if (profile == null) { diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/rules_configuration_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/rules_configuration_controller.rb index 55695f47760..36fedc05dd6 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/rules_configuration_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/rules_configuration_controller.rb @@ -98,6 +98,9 @@ class RulesConfigurationController < ApplicationController # activate the rule if active_rule.nil? active_rule = ActiveRule.new(:profile_id => profile.id, :rule => rule) + rule.parameters.select{|p| p.default_value.present?}.each do |p| + active_rule.active_rule_parameters.build(:rules_parameter => p, :value => p.default_value) + end end active_rule.failure_level=Sonar::RulePriority.id(priority) active_rule.save! 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 6205dc9c149..166236e6481 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 @@ -32,7 +32,7 @@ <% if administrator? %> <% end %> @@ -45,7 +45,7 @@ - + <% importers.to_a.sort{|x,y| x.getName() <=> y.getName()}.each do |importer| %> diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/new.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/new.html.erb index 8875c042957..9eacb5dfcf6 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/new.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/new.html.erb @@ -1,4 +1,4 @@ -

<%= link_to 'Quality profiles', profiles_path %> / <%= h @profile.language -%> / <%= h @profile.name %>

+

<%= link_to 'Quality profiles', :controller => 'profiles', :action => 'index' -%> / <%= h @profile.language -%> / <%= h @profile.name %>

<%= render :partial => 'profiles/tabs', :locals => {:new_tab => 'New rule'} %> -- cgit v1.2.3
Name: