3 * Copyright (C) 2009-2023 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.
20 package org.sonar.ce.task.projectanalysis.issue;
22 import com.google.common.base.MoreObjects;
24 import java.util.Optional;
26 import java.util.stream.Collectors;
27 import javax.annotation.CheckForNull;
28 import javax.annotation.Nullable;
29 import javax.annotation.concurrent.Immutable;
30 import org.sonar.api.issue.impact.Severity;
31 import org.sonar.api.issue.impact.SoftwareQuality;
32 import org.sonar.api.rule.RuleKey;
33 import org.sonar.api.rule.RuleStatus;
34 import org.sonar.api.rules.CleanCodeAttribute;
35 import org.sonar.api.rules.RuleType;
36 import org.sonar.api.server.debt.DebtRemediationFunction;
37 import org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction;
38 import org.sonar.db.issue.ImpactDto;
39 import org.sonar.db.rule.RuleDescriptionSectionDto;
40 import org.sonar.db.rule.RuleDto;
42 import static com.google.common.collect.Sets.union;
45 public class RuleImpl implements Rule {
47 private final String uuid;
48 private final RuleKey key;
49 private final String name;
50 private final String language;
51 private final RuleStatus status;
52 private final Set<String> tags;
53 private final DebtRemediationFunction remediationFunction;
54 private final RuleType type;
55 private final String pluginKey;
56 private final boolean isExternal;
57 private final boolean isAdHoc;
58 private final String defaultRuleDescription;
59 private final String severity;
60 private final Set<String> securityStandards;
61 private final Map<SoftwareQuality, Severity> defaultImpacts;
62 private final CleanCodeAttribute cleanCodeAttribute;
64 public RuleImpl(RuleDto dto) {
65 this.uuid = dto.getUuid();
66 this.key = dto.getKey();
67 this.name = dto.getName();
68 this.language = dto.getLanguage();
69 this.status = dto.getStatus();
70 this.tags = union(dto.getSystemTags(), dto.getTags());
71 this.remediationFunction = effectiveRemediationFunction(dto);
72 this.type = RuleType.valueOfNullable(dto.getType());
73 this.pluginKey = dto.getPluginKey();
74 this.isExternal = dto.isExternal();
75 this.isAdHoc = dto.isAdHoc();
76 this.defaultRuleDescription = getNonNullDefaultRuleDescription(dto);
77 this.severity = Optional.ofNullable(dto.getSeverityString()).orElse(dto.getAdHocSeverity());
78 this.securityStandards = dto.getSecurityStandards();
79 this.defaultImpacts = dto.getDefaultImpacts()
80 .stream().collect(Collectors.toMap(ImpactDto::getSoftwareQuality, ImpactDto::getSeverity));
81 this.cleanCodeAttribute = dto.getCleanCodeAttribute();
84 private static String getNonNullDefaultRuleDescription(RuleDto dto) {
85 return Optional.ofNullable(dto.getDefaultRuleDescriptionSection())
86 .map(RuleDescriptionSectionDto::getContent)
91 public String getUuid() {
96 public RuleKey getKey() {
101 public String getName() {
107 public String getLanguage() {
112 public RuleStatus getStatus() {
117 public Set<String> getTags() {
122 public DebtRemediationFunction getRemediationFunction() {
123 return remediationFunction;
127 public RuleType getType() {
133 public String getPluginKey() {
137 public String getDefaultRuleDescription() {
138 return defaultRuleDescription;
142 public String getSeverity() {
147 public Set<String> getSecurityStandards() {
148 return securityStandards;
152 public Map<SoftwareQuality, Severity> getDefaultImpacts() {
153 return defaultImpacts;
158 public CleanCodeAttribute cleanCodeAttribute() {
159 return cleanCodeAttribute;
163 public boolean equals(@Nullable Object o) {
167 if (o == null || getClass() != o.getClass()) {
170 RuleImpl rule = (RuleImpl) o;
171 return key.equals(rule.key);
175 public int hashCode() {
176 return key.hashCode();
180 public String toString() {
181 return MoreObjects.toStringHelper(this)
185 .add("language", language)
186 .add("status", status)
188 .add("pluginKey", pluginKey)
193 private static DebtRemediationFunction effectiveRemediationFunction(RuleDto dto) {
194 String fn = dto.getRemediationFunction();
196 return new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.valueOf(fn), dto.getRemediationGapMultiplier(), dto.getRemediationBaseEffort());
198 String defaultFn = dto.getDefRemediationFunction();
199 if (defaultFn != null) {
200 return new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.valueOf(defaultFn), dto.getDefRemediationGapMultiplier(), dto.getDefRemediationBaseEffort());
206 public boolean isAdHoc() {
211 public boolean isExternal() {