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 java.util.ArrayList;
24 import java.util.List;
25 import java.util.concurrent.atomic.AtomicInteger;
26 import java.util.stream.IntStream;
27 import org.sonar.api.notifications.Notification;
29 import static com.google.common.base.Preconditions.checkState;
30 import static java.util.Objects.requireNonNull;
31 import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotificationSender.BUILT_IN_QUALITY_PROFILES;
33 public class BuiltInQualityProfilesNotification {
35 private static final String NUMBER_OF_PROFILES = "numberOfProfiles";
36 private static final String PROFILE_NAME = ".profileName";
37 private static final String LANGUAGE = ".language";
38 private final List<Profile> profiles = new ArrayList<>();
40 public BuiltInQualityProfilesNotification addProfile(Profile profile) {
41 profiles.add(profile);
45 public Notification serialize() {
46 Notification notification = new Notification(BUILT_IN_QUALITY_PROFILES);
47 notification.setFieldValue(NUMBER_OF_PROFILES, String.valueOf(profiles.size()));
48 AtomicInteger count = new AtomicInteger();
49 profiles.forEach(profile -> {
50 int index = count.getAndIncrement();
51 notification.setFieldValue(index + ".profileName", profile.getProfileName());
52 notification.setFieldValue(index + ".language", profile.getLanguage());
57 public static BuiltInQualityProfilesNotification parse(Notification notification) {
58 checkState(BUILT_IN_QUALITY_PROFILES.equals(notification.getType()),
59 "Expected notification of type %s but got %s", BUILT_IN_QUALITY_PROFILES, notification.getType());
60 BuiltInQualityProfilesNotification notif = new BuiltInQualityProfilesNotification();
61 String numberOfProfilesText = notification.getFieldValue(NUMBER_OF_PROFILES);
62 checkState(numberOfProfilesText != null, "Could not read the built-in quality profile notification");
63 Integer numberOfProfiles = Integer.valueOf(numberOfProfilesText);
64 IntStream.rangeClosed(0, numberOfProfiles - 1)
65 .mapToObj(index -> new Profile(
66 requireNonNull(notification.getFieldValue(index + PROFILE_NAME)),
67 requireNonNull(notification.getFieldValue(index + LANGUAGE))))
68 .forEach(notif::addProfile);
72 public List<Profile> getProfiles() {
76 public static class Profile {
77 private final String profileName;
78 private final String language;
80 public Profile(String profileName, String language) {
81 this.profileName = profileName;
82 this.language = language;
85 public String getProfileName() {
89 public String getLanguage() {