3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.qualityprofile;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
27 import java.util.stream.Stream;
28 import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
29 import org.sonar.core.util.stream.MoreCollectors;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.qualityprofile.ActiveRuleDto;
33 import org.sonar.db.qualityprofile.RulesProfileDto;
34 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
36 import static org.sonar.core.util.stream.MoreCollectors.toSet;
38 public class BuiltInQProfileUpdateImpl implements BuiltInQProfileUpdate {
40 private final DbClient dbClient;
41 private final RuleActivator ruleActivator;
42 private final ActiveRuleIndexer activeRuleIndexer;
44 public BuiltInQProfileUpdateImpl(DbClient dbClient, RuleActivator ruleActivator, ActiveRuleIndexer activeRuleIndexer) {
45 this.dbClient = dbClient;
46 this.ruleActivator = ruleActivator;
47 this.activeRuleIndexer = activeRuleIndexer;
50 public List<ActiveRuleChange> update(DbSession dbSession, BuiltInQProfile builtInDefinition, RulesProfileDto initialRuleProfile) {
51 // Keep reference to all the activated rules before update
52 Set<Integer> deactivatedRuleIds = dbClient.activeRuleDao().selectByRuleProfile(dbSession, initialRuleProfile)
54 .map(ActiveRuleDto::getRuleId)
55 .collect(MoreCollectors.toHashSet());
57 // all rules, including those which are removed from built-in profile
58 Set<Integer> ruleIds = Stream.concat(
59 deactivatedRuleIds.stream(),
60 builtInDefinition.getActiveRules().stream().map(BuiltInQProfile.ActiveRule::getRuleId))
63 Collection<RuleActivation> activations = new ArrayList<>();
64 for (BuiltInQProfile.ActiveRule ar : builtInDefinition.getActiveRules()) {
65 RuleActivation activation = convert(ar);
66 activations.add(activation);
67 deactivatedRuleIds.remove(activation.getRuleId());
70 RuleActivationContext context = ruleActivator.createContextForBuiltInProfile(dbSession, initialRuleProfile, ruleIds);
71 List<ActiveRuleChange> changes = new ArrayList<>();
72 for (RuleActivation activation : activations) {
73 changes.addAll(ruleActivator.activate(dbSession, activation, context));
76 // these rules are no longer part of the built-in profile
77 deactivatedRuleIds.forEach(ruleKey -> changes.addAll(ruleActivator.deactivate(dbSession, context, ruleKey, false)));
79 activeRuleIndexer.commitAndIndex(dbSession, changes);
83 private static RuleActivation convert(BuiltInQProfile.ActiveRule ar) {
84 Map<String, String> params = ar.getBuiltIn().overriddenParams().stream()
85 .collect(MoreCollectors.uniqueIndex(BuiltInQualityProfilesDefinition.OverriddenParam::key, BuiltInQualityProfilesDefinition.OverriddenParam::overriddenValue));
86 return RuleActivation.create(ar.getRuleId(), ar.getBuiltIn().overriddenSeverity(), params);