diff options
author | Fabrice Bellingard <bellingard@gmail.com> | 2012-03-21 10:21:08 +0100 |
---|---|---|
committer | Fabrice Bellingard <bellingard@gmail.com> | 2012-03-21 13:55:24 +0100 |
commit | cafd2f8ff7f99ed218bc263f7a942828c5949454 (patch) | |
tree | 57eec437e33daae17d0af398ae84814b4f3d03da /sonar-server/src/main/java | |
parent | 513aa53c77d7516457b26a2a87573cc30dab1879 (diff) | |
download | sonarqube-cafd2f8ff7f99ed218bc263f7a942828c5949454.tar.gz sonarqube-cafd2f8ff7f99ed218bc263f7a942828c5949454.zip |
SONAR-2977 Fix issue when new profile added and set to default
If in a new version, a plugin adds a new profile and sets this one as
the default profile, then the language gets 2 defaults profiles.
=> Fixed.
Diffstat (limited to 'sonar-server/src/main/java')
-rw-r--r-- | sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java | 27 |
1 files changed, 21 insertions, 6 deletions
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 7ed797f2ead..b9a803b49d8 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 @@ -20,6 +20,8 @@ package org.sonar.server.startup; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; + import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,7 +35,9 @@ import org.sonar.jpa.session.DatabaseSessionFactory; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.List; +import java.util.Set; public final class RegisterProvidedProfiles { @@ -81,7 +85,7 @@ public final class RegisterProvidedProfiles { return result; } - void cleanProvidedProfiles(List<RulesProfile> profiles, DatabaseSession session) { + private void cleanProvidedProfiles(List<RulesProfile> profiles, DatabaseSession session) { TimeProfiler profiler = new TimeProfiler().start("Clean provided profiles"); List<RulesProfile> existingProfiles = session.getResults(RulesProfile.class, "provided", true); for (RulesProfile existingProfile : existingProfiles) { @@ -105,10 +109,11 @@ public final class RegisterProvidedProfiles { profiler.stop(); } - void saveProvidedProfiles(List<RulesProfile> profiles, DatabaseSession session) { + private void saveProvidedProfiles(List<RulesProfile> profiles, DatabaseSession session) { + Collection<String> languagesWithDefaultProfile = findLanguagesWithDefaultProfile(session); for (RulesProfile profile : profiles) { TimeProfiler profiler = new TimeProfiler().start("Save profile " + profile); - RulesProfile persistedProfile = findOrCreate(profile, session); + RulesProfile persistedProfile = findOrCreate(profile, session, languagesWithDefaultProfile.contains(profile.getLanguage())); for (ActiveRule activeRule : profile.getActiveRules()) { Rule rule = getPersistedRule(activeRule); @@ -124,10 +129,18 @@ public final class RegisterProvidedProfiles { session.saveWithoutFlush(persistedProfile); profiler.stop(); } + } + private Collection<String> findLanguagesWithDefaultProfile(DatabaseSession session) { + Set<String> languagesWithDefaultProfile = Sets.newHashSet(); + List<RulesProfile> defaultProfiles = session.getResults(RulesProfile.class, "defaultProfile", true); + for (RulesProfile defaultProfile : defaultProfiles) { + languagesWithDefaultProfile.add(defaultProfile.getLanguage()); + } + return languagesWithDefaultProfile; } - Rule getPersistedRule(ActiveRule activeRule) { + private Rule getPersistedRule(ActiveRule activeRule) { Rule rule = activeRule.getRule(); if (rule != null && rule.getId() == null) { if (rule.getKey() != null) { @@ -140,12 +153,14 @@ public final class RegisterProvidedProfiles { return rule; } - private RulesProfile findOrCreate(RulesProfile profile, DatabaseSession session) { + private RulesProfile findOrCreate(RulesProfile profile, DatabaseSession session, boolean defaultProfileAlreadyExist) { RulesProfile persistedProfile = session.getSingleResult(RulesProfile.class, "name", profile.getName(), "language", profile.getLanguage()); if (persistedProfile == null) { persistedProfile = RulesProfile.create(profile.getName(), profile.getLanguage()); - persistedProfile.setDefaultProfile(profile.getDefaultProfile()); persistedProfile.setProvided(true); + if (!defaultProfileAlreadyExist) { + persistedProfile.setDefaultProfile(profile.getDefaultProfile()); + } } return persistedProfile; } |