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.lang.Integer.parseInt;
31 import static java.lang.String.format;
32 import static java.util.Objects.requireNonNull;
33 import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotificationSender.BUILT_IN_QUALITY_PROFILES;
35 public class BuiltInQualityProfilesNotification {
37 private static final String NUMBER_OF_PROFILES = "numberOfProfiles";
38 private static final String PROFILE_NAME = ".profileName";
39 private static final String LANGUAGE_KEY = ".languageKey";
40 private static final String LANGUAGE_NAME = ".languageName";
41 private static final String NEW_RULES = ".newRules";
42 private static final String UPDATED_RULES = ".updatedRules";
43 private static final String REMOVED_RULES = ".removedRules";
45 private final List<Profile> profiles = new ArrayList<>();
47 public BuiltInQualityProfilesNotification addProfile(Profile profile) {
48 profiles.add(profile);
52 public Notification serialize() {
53 Notification notification = new Notification(BUILT_IN_QUALITY_PROFILES);
54 notification.setFieldValue(NUMBER_OF_PROFILES, String.valueOf(profiles.size()));
55 AtomicInteger count = new AtomicInteger();
56 profiles.forEach(profile -> {
57 int index = count.getAndIncrement();
58 notification.setFieldValue(index + PROFILE_NAME, profile.getProfileName());
59 notification.setFieldValue(index + LANGUAGE_KEY, profile.getLanguageKey());
60 notification.setFieldValue(index + LANGUAGE_NAME, profile.getLanguageName());
61 notification.setFieldValue(index + NEW_RULES, String.valueOf(profile.getNewRules()));
62 notification.setFieldValue(index + UPDATED_RULES, String.valueOf(profile.getUpdatedRules()));
63 notification.setFieldValue(index + REMOVED_RULES, String.valueOf(profile.getRemovedRules()));
68 public static BuiltInQualityProfilesNotification parse(Notification notification) {
69 checkState(BUILT_IN_QUALITY_PROFILES.equals(notification.getType()),
70 "Expected notification of type %s but got %s", BUILT_IN_QUALITY_PROFILES, notification.getType());
71 BuiltInQualityProfilesNotification notif = new BuiltInQualityProfilesNotification();
72 String numberOfProfilesText = notification.getFieldValue(NUMBER_OF_PROFILES);
73 checkState(numberOfProfilesText != null, "Could not read the built-in quality profile notification");
74 Integer numberOfProfiles = Integer.valueOf(numberOfProfilesText);
75 IntStream.rangeClosed(0, numberOfProfiles - 1)
76 .mapToObj(index -> Profile.newBuilder()
77 .setProfileName(getNonNullFieldValue(notification, index + PROFILE_NAME))
78 .setLanguageKey(getNonNullFieldValue(notification, index + LANGUAGE_KEY))
79 .setLanguageName(getNonNullFieldValue(notification, index + LANGUAGE_NAME))
80 .setNewRules(parseInt(getNonNullFieldValue(notification, index + NEW_RULES)))
81 .setUpdatedRules(parseInt(getNonNullFieldValue(notification, index + UPDATED_RULES)))
82 .setRemovedRules(parseInt(getNonNullFieldValue(notification, index + REMOVED_RULES)))
84 .forEach(notif::addProfile);
88 private static String getNonNullFieldValue(Notification notification, String key) {
89 String value = notification.getFieldValue(key);
90 return requireNonNull(value, format("Notification field '%s' is null", key));
93 public List<Profile> getProfiles() {
97 public static class Profile {
98 private final String profileName;
99 private final String languageKey;
100 private final String languageName;
101 private final int newRules;
102 private final int updatedRules;
103 private final int removedRules;
105 public Profile(Builder builder) {
106 this.profileName = builder.profileName;
107 this.languageKey = builder.languageKey;
108 this.languageName = builder.languageName;
109 this.newRules = builder.newRules;
110 this.updatedRules = builder.updatedRules;
111 this.removedRules = builder.removedRules;
114 public String getProfileName() {
118 public String getLanguageKey() {
122 public String getLanguageName() {
126 public int getNewRules() {
130 public int getUpdatedRules() {
134 public int getRemovedRules() {
138 public static Builder newBuilder() {
139 return new Builder();
142 public static class Builder {
143 private String profileName;
144 private String languageKey;
145 private String languageName;
146 private int newRules;
147 private int updatedRules;
148 private int removedRules;
153 public Builder setLanguageKey(String languageKey) {
154 this.languageKey = requireNonNull(languageKey, "languageKEy should not be null");
158 public Builder setLanguageName(String languageName) {
159 this.languageName = requireNonNull(languageName, "languageName should not be null");
163 public Builder setProfileName(String profileName) {
164 this.profileName = requireNonNull(profileName, "profileName should not be null");
168 public Builder setNewRules(int newRules) {
169 checkState(newRules >= 0, "newRules should not be negative");
170 this.newRules = newRules;
174 public Builder setUpdatedRules(int updatedRules) {
175 checkState(updatedRules >= 0, "updatedRules should not be negative");
176 this.updatedRules = updatedRules;
180 public Builder setRemovedRules(int removedRules) {
181 checkState(removedRules >= 0, "removedRules should not be negative");
182 this.removedRules = removedRules;
186 public Profile build() {
187 return new Profile(this);