From 5cd839bbecd061d9463694d025788754bfca0f08 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Fri, 10 Sep 2010 12:27:33 +0000 Subject: simplify ValidationMessages and ProfileDefinition --- .../org/sonar/server/rules/DeprecatedProfiles.java | 3 +- .../org/sonar/server/rules/ProfilesConsole.java | 4 +-- .../server/startup/RegisterProvidedProfiles.java | 37 ++++++++++++++-------- .../WEB-INF/app/views/profiles/index.html.erb | 4 +-- .../sonar/server/rules/DeprecatedProfilesTest.java | 4 +-- 5 files changed, 30 insertions(+), 22 deletions(-) (limited to 'sonar-server/src') diff --git a/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedProfiles.java b/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedProfiles.java index bb3db460eff..17b42d3df23 100644 --- a/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedProfiles.java +++ b/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedProfiles.java @@ -132,8 +132,7 @@ public final class DeprecatedProfiles { private ProfilePrototype prototype; DefaultProfileDefinition(String name, String language) { - super(name, language); - this.prototype = ProfilePrototype.create(); + this.prototype = ProfilePrototype.create(name, language); } public static DefaultProfileDefinition create(String name, String language) { 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 ed5c23fe520..9e66f2ca222 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 @@ -92,13 +92,13 @@ public final class ProfilesConsole implements ServerComponent { for (ProfilePrototype.RulePrototype rulePrototype : prototype.getRules()) { Rule rule = findRule(rulePrototype); if (rule == null) { - messages.addWarning("profiles.missingRule", "The following rule has been ignored: " + rulePrototype); + messages.addWarningText("The following rule has been ignored: " + rulePrototype); } else { ActiveRule activeRule = profile.activateRule(rule, rulePrototype.getPriority()); for (Map.Entry entry : rulePrototype.getParameters().entrySet()) { if (rule.getParam(entry.getKey())==null) { - messages.addWarning("profiles.missingRuleParameter", "The rule " + rulePrototype + " has no parameter named '" + entry.getKey() + "'."); + messages.addWarningText("The rule " + rulePrototype + " has no parameter named '" + entry.getKey() + "'."); } else { activeRule.setParameter(entry.getKey(), entry.getValue()); 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 a270e602beb..999d065f605 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 @@ -69,19 +69,29 @@ public final class RegisterProvidedProfiles { public void start() { TimeProfiler profiler = new TimeProfiler().start("Load provided profiles"); + + List prototypes = createPrototypes(); DatabaseSession session = sessionFactory.getSession(); - deleteDeprecatedProfiles(session); - saveProvidedProfiles(session); + deleteDeprecatedProfiles(prototypes, session); + saveProvidedProfiles(prototypes, session); profiler.stop(); } - void deleteDeprecatedProfiles(DatabaseSession session) { + List createPrototypes() { + List result = new ArrayList(); + for (ProfileDefinition definition : definitions) { + result.add(definition.createPrototype()); + } + return result; + } + + void deleteDeprecatedProfiles(List prototypes, DatabaseSession session) { TimeProfiler profiler = new TimeProfiler().start("Delete deprecated profiles"); List existingProfiles = session.getResults(RulesProfile.class, "provided", true); for (RulesProfile existingProfile : existingProfiles) { boolean isDeprecated = true; - for (ProfileDefinition definition : definitions) { - if (StringUtils.equals(existingProfile.getName(), definition.getName()) && StringUtils.equals(existingProfile.getLanguage(), definition.getLanguage())) { + for (ProfilePrototype profile: prototypes) { + if (StringUtils.equals(existingProfile.getName(), profile.getName()) && StringUtils.equals(existingProfile.getLanguage(), profile.getLanguage())) { isDeprecated = false; break; } @@ -101,16 +111,15 @@ public final class RegisterProvidedProfiles { } - void saveProvidedProfiles(DatabaseSession session) { - for (ProfileDefinition definition : definitions) { - TimeProfiler profiler = new TimeProfiler().start("Save profile " + definition); - ProfilePrototype prototype = definition.createPrototype(); - RulesProfile profile = findOrCreate(definition, session); + void saveProvidedProfiles(List prototypes, DatabaseSession session) { + for (ProfilePrototype prototype : prototypes) { + TimeProfiler profiler = new TimeProfiler().start("Save profile " + prototype); + RulesProfile profile = findOrCreate(prototype, session); for (ProfilePrototype.RulePrototype rulePrototype : prototype.getRules()) { Rule rule = findRule(rulePrototype); if (rule == null) { - LOGGER.warn("The profile " + definition + " defines an unknown rule: " + rulePrototype); + LOGGER.warn("The profile " + prototype + " defines an unknown rule: " + rulePrototype); } else { ActiveRule activeRule = profile.activateRule(rule, rulePrototype.getPriority()); @@ -135,10 +144,10 @@ public final class RegisterProvidedProfiles { return null; } - private RulesProfile findOrCreate(ProfileDefinition definition, DatabaseSession session) { - RulesProfile profile = session.getSingleResult(RulesProfile.class, "name", definition.getName(), "language", definition.getLanguage()); + private RulesProfile findOrCreate(ProfilePrototype prototype, DatabaseSession session) { + RulesProfile profile = session.getSingleResult(RulesProfile.class, "name", prototype.getName(), "language", prototype.getLanguage()); if (profile == null) { - profile = RulesProfile.create(definition.getName(), definition.getLanguage()); + profile = RulesProfile.create(prototype.getName(), prototype.getLanguage()); profile.setProvided(true); profile.setDefaultProfile(false); } 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 f0b76336a06..f6f81574756 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 @@ -118,7 +118,7 @@ - <% if !profile.provided? %> + <% if !(profile.provided?) %> <% form_tag(:action => 'rename', :id => profile.id) do -%> <%= hidden_field_tag 'rename_' + profile.id.to_s %> @@ -130,7 +130,7 @@ <% form_tag(:action => 'copy', :id => profile.id) do -%> <%= hidden_field_tag 'copy_' + profile.id.to_s %> - <% end + <% end %> diff --git a/sonar-server/src/test/java/org/sonar/server/rules/DeprecatedProfilesTest.java b/sonar-server/src/test/java/org/sonar/server/rules/DeprecatedProfilesTest.java index 1d94991ce8e..373f7369654 100644 --- a/sonar-server/src/test/java/org/sonar/server/rules/DeprecatedProfilesTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rules/DeprecatedProfilesTest.java @@ -31,8 +31,8 @@ public class DeprecatedProfilesTest { @Test public void testCreate() { DeprecatedProfiles.DefaultProfileDefinition def = DeprecatedProfiles.DefaultProfileDefinition.create("sonar way", "java"); - assertThat(def.getName(), is("sonar way")); - assertThat(def.getLanguage(), is("java")); + assertThat(def.createPrototype().getName(), is("sonar way")); + assertThat(def.createPrototype().getLanguage(), is("java")); } @Test -- cgit v1.2.3