You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BuiltInQPChangeNotificationBuilder.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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. package org.sonar.server.qualityprofile;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.concurrent.atomic.AtomicInteger;
  24. import java.util.stream.IntStream;
  25. import org.sonar.api.notifications.Notification;
  26. import static com.google.common.base.Preconditions.checkState;
  27. import static java.lang.Integer.parseInt;
  28. import static java.lang.Long.parseLong;
  29. import static java.lang.String.format;
  30. import static java.util.Objects.requireNonNull;
  31. public class BuiltInQPChangeNotificationBuilder {
  32. private static final String NUMBER_OF_PROFILES = "numberOfProfiles";
  33. private static final String PROFILE_NAME = ".profileName";
  34. private static final String LANGUAGE_KEY = ".languageKey";
  35. private static final String LANGUAGE_NAME = ".languageName";
  36. private static final String NEW_RULES = ".newRules";
  37. private static final String UPDATED_RULES = ".updatedRules";
  38. private static final String REMOVED_RULES = ".removedRules";
  39. private static final String START_DATE = ".startDate";
  40. private static final String END_DATE = ".endDate";
  41. private final List<Profile> profiles = new ArrayList<>();
  42. public BuiltInQPChangeNotificationBuilder addProfile(Profile profile) {
  43. profiles.add(profile);
  44. return this;
  45. }
  46. public BuiltInQPChangeNotification build() {
  47. BuiltInQPChangeNotification notification = new BuiltInQPChangeNotification();
  48. notification.setFieldValue(NUMBER_OF_PROFILES, String.valueOf(profiles.size()));
  49. AtomicInteger count = new AtomicInteger();
  50. profiles.forEach(profile -> {
  51. int index = count.getAndIncrement();
  52. notification.setFieldValue(index + PROFILE_NAME, profile.getProfileName());
  53. notification.setFieldValue(index + LANGUAGE_KEY, profile.getLanguageKey());
  54. notification.setFieldValue(index + LANGUAGE_NAME, profile.getLanguageName());
  55. notification.setFieldValue(index + NEW_RULES, String.valueOf(profile.getNewRules()));
  56. notification.setFieldValue(index + UPDATED_RULES, String.valueOf(profile.getUpdatedRules()));
  57. notification.setFieldValue(index + REMOVED_RULES, String.valueOf(profile.getRemovedRules()));
  58. notification.setFieldValue(index + START_DATE, String.valueOf(profile.getStartDate()));
  59. notification.setFieldValue(index + END_DATE, String.valueOf(profile.getEndDate()));
  60. });
  61. return notification;
  62. }
  63. public static BuiltInQPChangeNotificationBuilder parse(Notification notification) {
  64. checkState(BuiltInQPChangeNotification.TYPE.equals(notification.getType()),
  65. "Expected notification of type %s but got %s", BuiltInQPChangeNotification.TYPE, notification.getType());
  66. BuiltInQPChangeNotificationBuilder notif = new BuiltInQPChangeNotificationBuilder();
  67. String numberOfProfilesText = notification.getFieldValue(NUMBER_OF_PROFILES);
  68. checkState(numberOfProfilesText != null, "Could not read the built-in quality profile notification");
  69. Integer numberOfProfiles = Integer.valueOf(numberOfProfilesText);
  70. IntStream.rangeClosed(0, numberOfProfiles - 1)
  71. .mapToObj(index -> Profile.newBuilder()
  72. .setProfileName(getNonNullFieldValue(notification, index + PROFILE_NAME))
  73. .setLanguageKey(getNonNullFieldValue(notification, index + LANGUAGE_KEY))
  74. .setLanguageName(getNonNullFieldValue(notification, index + LANGUAGE_NAME))
  75. .setNewRules(parseInt(getNonNullFieldValue(notification, index + NEW_RULES)))
  76. .setUpdatedRules(parseInt(getNonNullFieldValue(notification, index + UPDATED_RULES)))
  77. .setRemovedRules(parseInt(getNonNullFieldValue(notification, index + REMOVED_RULES)))
  78. .setStartDate(parseLong(getNonNullFieldValue(notification, index + START_DATE)))
  79. .setEndDate(parseLong(getNonNullFieldValue(notification, index + END_DATE)))
  80. .build())
  81. .forEach(notif::addProfile);
  82. return notif;
  83. }
  84. private static String getNonNullFieldValue(Notification notification, String key) {
  85. String value = notification.getFieldValue(key);
  86. return requireNonNull(value, format("Notification field '%s' is null", key));
  87. }
  88. public List<Profile> getProfiles() {
  89. return profiles;
  90. }
  91. public static class Profile {
  92. private final String profileName;
  93. private final String languageKey;
  94. private final String languageName;
  95. private final int newRules;
  96. private final int updatedRules;
  97. private final int removedRules;
  98. private final long startDate;
  99. private final long endDate;
  100. public Profile(Builder builder) {
  101. this.profileName = builder.profileName;
  102. this.languageKey = builder.languageKey;
  103. this.languageName = builder.languageName;
  104. this.newRules = builder.newRules;
  105. this.updatedRules = builder.updatedRules;
  106. this.removedRules = builder.removedRules;
  107. this.startDate = builder.startDate;
  108. this.endDate = builder.endDate;
  109. }
  110. public String getProfileName() {
  111. return profileName;
  112. }
  113. public String getLanguageKey() {
  114. return languageKey;
  115. }
  116. public String getLanguageName() {
  117. return languageName;
  118. }
  119. public int getNewRules() {
  120. return newRules;
  121. }
  122. public int getUpdatedRules() {
  123. return updatedRules;
  124. }
  125. public int getRemovedRules() {
  126. return removedRules;
  127. }
  128. public long getStartDate() {
  129. return startDate;
  130. }
  131. public long getEndDate() {
  132. return endDate;
  133. }
  134. public static Builder newBuilder() {
  135. return new Builder();
  136. }
  137. public static class Builder {
  138. private String profileName;
  139. private String languageKey;
  140. private String languageName;
  141. private int newRules;
  142. private int updatedRules;
  143. private int removedRules;
  144. private long startDate;
  145. private long endDate;
  146. private Builder() {
  147. }
  148. public Builder setLanguageKey(String languageKey) {
  149. this.languageKey = requireNonNull(languageKey, "languageKEy should not be null");
  150. return this;
  151. }
  152. public Builder setLanguageName(String languageName) {
  153. this.languageName = requireNonNull(languageName, "languageName should not be null");
  154. return this;
  155. }
  156. public Builder setProfileName(String profileName) {
  157. this.profileName = requireNonNull(profileName, "profileName should not be null");
  158. return this;
  159. }
  160. public Builder setNewRules(int newRules) {
  161. checkState(newRules >= 0, "newRules should not be negative");
  162. this.newRules = newRules;
  163. return this;
  164. }
  165. public Builder setUpdatedRules(int updatedRules) {
  166. checkState(updatedRules >= 0, "updatedRules should not be negative");
  167. this.updatedRules = updatedRules;
  168. return this;
  169. }
  170. public Builder setRemovedRules(int removedRules) {
  171. checkState(removedRules >= 0, "removedRules should not be negative");
  172. this.removedRules = removedRules;
  173. return this;
  174. }
  175. public Builder setStartDate(long startDate) {
  176. this.startDate = startDate;
  177. return this;
  178. }
  179. public Builder setEndDate(long endDate) {
  180. this.endDate = endDate;
  181. return this;
  182. }
  183. public Profile build() {
  184. return new Profile(this);
  185. }
  186. }
  187. }
  188. }