3 * Copyright (C) 2009-2017 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.
21 package org.sonar.server.qualityprofile;
23 import com.google.common.collect.Multimap;
24 import java.util.Collection;
25 import org.sonar.api.resources.Languages;
26 import org.sonar.server.notification.NotificationManager;
27 import org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.Profile;
29 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED;
30 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED;
31 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.UPDATED;
33 public class BuiltInQualityProfilesNotificationSender {
35 static final String BUILT_IN_QUALITY_PROFILES = "built-in-quality-profiles";
37 private final NotificationManager notificationManager;
38 private final Languages languages;
40 public BuiltInQualityProfilesNotificationSender(NotificationManager notificationManager, Languages languages) {
41 this.notificationManager = notificationManager;
42 this.languages = languages;
45 void send(Multimap<QProfileName, ActiveRuleChange> changedProfiles) {
46 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification();
47 changedProfiles.keySet().stream()
48 .map(changedProfile -> {
49 String profileName = changedProfile.getName();
50 String languageName = languages.get(changedProfile.getLanguage()).getName();
51 Collection<ActiveRuleChange> activeRuleChanges = changedProfiles.get(changedProfile);
52 int newRules = (int) activeRuleChanges.stream().map(ActiveRuleChange::getType).filter(ACTIVATED::equals).count();
53 int updatedRules = (int) activeRuleChanges.stream().map(ActiveRuleChange::getType).filter(UPDATED::equals).count();
54 int removedRules = (int) activeRuleChanges.stream().map(ActiveRuleChange::getType).filter(DEACTIVATED::equals).count();
55 return Profile.newBuilder(profileName, languageName)
56 .setNewRules(newRules)
57 .setUpdatedRules(updatedRules)
58 .setRemovedRules(removedRules)
61 .forEach(notification::addProfile);
62 notificationManager.scheduleForSending(notification.serialize());