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.Language;
26 import org.sonar.api.resources.Languages;
27 import org.sonar.server.notification.NotificationManager;
28 import org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.Profile;
30 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED;
31 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED;
32 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.UPDATED;
34 public class BuiltInQualityProfilesNotificationSender {
36 static final String BUILT_IN_QUALITY_PROFILES = "built-in-quality-profiles";
38 private final NotificationManager notificationManager;
39 private final Languages languages;
41 public BuiltInQualityProfilesNotificationSender(NotificationManager notificationManager, Languages languages) {
42 this.notificationManager = notificationManager;
43 this.languages = languages;
46 void send(Multimap<QProfileName, ActiveRuleChange> changedProfiles) {
47 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification();
48 changedProfiles.keySet().stream()
49 .map(changedProfile -> {
50 String profileName = changedProfile.getName();
51 Language language = languages.get(changedProfile.getLanguage());
52 Collection<ActiveRuleChange> activeRuleChanges = changedProfiles.get(changedProfile);
53 int newRules = (int) activeRuleChanges.stream().map(ActiveRuleChange::getType).filter(ACTIVATED::equals).count();
54 int updatedRules = (int) activeRuleChanges.stream().map(ActiveRuleChange::getType).filter(UPDATED::equals).count();
55 int removedRules = (int) activeRuleChanges.stream().map(ActiveRuleChange::getType).filter(DEACTIVATED::equals).count();
56 return Profile.newBuilder()
57 .setProfileName(profileName)
58 .setLanguageKey(language.getKey())
59 .setLanguageName(language.getName())
60 .setNewRules(newRules)
61 .setUpdatedRules(updatedRules)
62 .setRemovedRules(removedRules)
65 .forEach(notification::addProfile);
66 notificationManager.scheduleForSending(notification.serialize());