]> source.dussan.org Git - sonarqube.git/blob
9ed6c7c150a7c43ce0350251564bc5b046bc565b
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 package org.sonar.server.qualityprofile;
22
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;
29
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;
33
34 public class BuiltInQualityProfilesNotificationSender {
35
36   static final String BUILT_IN_QUALITY_PROFILES = "built-in-quality-profiles";
37
38   private final NotificationManager notificationManager;
39   private final Languages languages;
40
41   public BuiltInQualityProfilesNotificationSender(NotificationManager notificationManager, Languages languages) {
42     this.notificationManager = notificationManager;
43     this.languages = languages;
44   }
45
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)
63           .build();
64       })
65       .forEach(notification::addProfile);
66     notificationManager.scheduleForSending(notification.serialize());
67   }
68 }