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 = ".language";
40 private static final String NEW_RULES = ".newRules";
41 private static final String UPDATED_RULES = ".updatedRules";
42 private static final String REMOVED_RULES = ".removedRules";
44 private final List<Profile> profiles = new ArrayList<>();
46 public BuiltInQualityProfilesNotification addProfile(Profile profile) {
47 profiles.add(profile);
51 public Notification serialize() {
52 Notification notification = new Notification(BUILT_IN_QUALITY_PROFILES);
53 notification.setFieldValue(NUMBER_OF_PROFILES, String.valueOf(profiles.size()));
54 AtomicInteger count = new AtomicInteger();
55 profiles.forEach(profile -> {
56 int index = count.getAndIncrement();
57 notification.setFieldValue(index + PROFILE_NAME, profile.getProfileName());
58 notification.setFieldValue(index + LANGUAGE, profile.getLanguage());
59 notification.setFieldValue(index + NEW_RULES, String.valueOf(profile.getNewRules()));
60 notification.setFieldValue(index + UPDATED_RULES, String.valueOf(profile.getUpdatedRules()));
61 notification.setFieldValue(index + REMOVED_RULES, String.valueOf(profile.getRemovedRules()));
66 public static BuiltInQualityProfilesNotification parse(Notification notification) {
67 checkState(BUILT_IN_QUALITY_PROFILES.equals(notification.getType()),
68 "Expected notification of type %s but got %s", BUILT_IN_QUALITY_PROFILES, notification.getType());
69 BuiltInQualityProfilesNotification notif = new BuiltInQualityProfilesNotification();
70 String numberOfProfilesText = notification.getFieldValue(NUMBER_OF_PROFILES);
71 checkState(numberOfProfilesText != null, "Could not read the built-in quality profile notification");
72 Integer numberOfProfiles = Integer.valueOf(numberOfProfilesText);
73 IntStream.rangeClosed(0, numberOfProfiles - 1)
74 .mapToObj(index -> Profile.newBuilder(
75 getNonNullFieldValue(notification, index + PROFILE_NAME),
76 getNonNullFieldValue(notification, index + LANGUAGE))
77 .setNewRules(parseInt(getNonNullFieldValue(notification, index + NEW_RULES)))
78 .setUpdatedRules(parseInt(getNonNullFieldValue(notification, index + UPDATED_RULES)))
79 .setRemovedRules(parseInt(getNonNullFieldValue(notification, index + REMOVED_RULES)))
81 .forEach(notif::addProfile);
85 private static String getNonNullFieldValue(Notification notification, String key) {
86 String value = notification.getFieldValue(key);
87 return requireNonNull(value, format("Notification field '%s' is null", key));
90 public List<Profile> getProfiles() {
94 public static class Profile {
95 private final String profileName;
96 private final String language;
97 private final int newRules;
98 private final int updatedRules;
99 private final int removedRules;
101 public Profile(Builder builder) {
102 this.profileName = builder.profileName;
103 this.language = builder.language;
104 this.newRules = builder.newRules;
105 this.updatedRules = builder.updatedRules;
106 this.removedRules = builder.removedRules;
109 public String getProfileName() {
113 public String getLanguage() {
117 public int getNewRules() {
121 public int getUpdatedRules() {
125 public int getRemovedRules() {
129 public static Builder newBuilder(String profileName, String language) {
130 return new Builder(profileName, language);
133 public static class Builder {
134 private final String profileName;
135 private final String language;
136 private int newRules;
137 private int updatedRules;
138 private int removedRules;
140 private Builder(String profileName, String language) {
141 this.profileName = requireNonNull(profileName, "profileName should not be null");
142 this.language = requireNonNull(language, "language should not be null");
145 public Builder setNewRules(int newRules) {
146 checkState(newRules >= 0, "newRules should not be negative");
147 this.newRules = newRules;
151 public Builder setUpdatedRules(int updatedRules) {
152 checkState(updatedRules >= 0, "updatedRules should not be negative");
153 this.updatedRules = updatedRules;
157 public Builder setRemovedRules(int removedRules) {
158 checkState(removedRules >= 0, "removedRules should not be negative");
159 this.removedRules = removedRules;
163 public Profile build() {
164 return new Profile(this);