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.

ActiveRuleChange.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 com.google.common.base.MoreObjects;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import javax.annotation.CheckForNull;
  25. import javax.annotation.Nullable;
  26. import org.apache.commons.lang.StringUtils;
  27. import org.sonar.db.qualityprofile.ActiveRuleDto;
  28. import org.sonar.db.qualityprofile.ActiveRuleKey;
  29. import org.sonar.db.qualityprofile.QProfileChangeDto;
  30. import org.sonar.db.rule.RuleDefinitionDto;
  31. public class ActiveRuleChange {
  32. private ActiveRuleDto activeRule;
  33. public enum Type {
  34. ACTIVATED, DEACTIVATED, UPDATED
  35. }
  36. private final Type type;
  37. private final ActiveRuleKey key;
  38. private final int ruleId;
  39. private String severity = null;
  40. private ActiveRuleInheritance inheritance = null;
  41. private final Map<String, String> parameters = new HashMap<>();
  42. public ActiveRuleChange(Type type, ActiveRuleDto activeRule, RuleDefinitionDto ruleDefinition) {
  43. this.type = type;
  44. this.key = activeRule.getKey();
  45. this.ruleId = ruleDefinition.getId();
  46. this.activeRule = activeRule;
  47. }
  48. public ActiveRuleChange(Type type, ActiveRuleKey key, RuleDefinitionDto ruleDefinition) {
  49. this.type = type;
  50. this.key = key;
  51. this.ruleId = ruleDefinition.getId();
  52. }
  53. public ActiveRuleKey getKey() {
  54. return key;
  55. }
  56. public int getRuleId() {
  57. return ruleId;
  58. }
  59. public Type getType() {
  60. return type;
  61. }
  62. @CheckForNull
  63. public String getSeverity() {
  64. return severity;
  65. }
  66. public ActiveRuleChange setSeverity(@Nullable String s) {
  67. this.severity = s;
  68. return this;
  69. }
  70. public ActiveRuleChange setInheritance(@Nullable ActiveRuleInheritance i) {
  71. this.inheritance = i;
  72. return this;
  73. }
  74. @CheckForNull
  75. public ActiveRuleInheritance getInheritance() {
  76. return inheritance;
  77. }
  78. public Map<String, String> getParameters() {
  79. return parameters;
  80. }
  81. public ActiveRuleChange setParameter(String key, @Nullable String value) {
  82. parameters.put(key, value);
  83. return this;
  84. }
  85. public ActiveRuleChange setParameters(Map<String, String> m) {
  86. parameters.clear();
  87. parameters.putAll(m);
  88. return this;
  89. }
  90. @CheckForNull
  91. public ActiveRuleDto getActiveRule() {
  92. return activeRule;
  93. }
  94. public ActiveRuleChange setActiveRule(@Nullable ActiveRuleDto activeRule) {
  95. this.activeRule = activeRule;
  96. return this;
  97. }
  98. public QProfileChangeDto toDto(@Nullable String userUuid) {
  99. QProfileChangeDto dto = new QProfileChangeDto();
  100. dto.setChangeType(type.name());
  101. dto.setRulesProfileUuid(getKey().getRuleProfileUuid());
  102. dto.setUserUuid(userUuid);
  103. Map<String, String> data = new HashMap<>();
  104. data.put("ruleId", String.valueOf(getRuleId()));
  105. parameters.entrySet().stream()
  106. .filter(param -> !param.getKey().isEmpty())
  107. .forEach(param -> data.put("param_" + param.getKey(), param.getValue()));
  108. if (StringUtils.isNotEmpty(severity)) {
  109. data.put("severity", severity);
  110. }
  111. if (inheritance != null) {
  112. data.put("inheritance", inheritance.name());
  113. }
  114. dto.setData(data);
  115. return dto;
  116. }
  117. @Override
  118. public String toString() {
  119. return MoreObjects.toStringHelper(this)
  120. .add("type", type)
  121. .add("key", key)
  122. .add("ruleId", ruleId)
  123. .add("severity", severity)
  124. .add("inheritance", inheritance)
  125. .add("parameters", parameters)
  126. .toString();
  127. }
  128. }