3 * Copyright (C) 2009-2022 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.Preconditions;
23 import java.util.Objects;
24 import java.util.Optional;
25 import org.sonar.api.rules.RuleType;
26 import org.sonar.api.utils.System2;
27 import org.sonar.core.util.UuidFactory;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.rule.RuleDao;
31 import org.sonar.db.rule.RuleDto;
32 import org.sonar.db.rule.RuleMetadataDto;
33 import org.sonar.server.rule.index.RuleIndexer;
35 import static java.util.Objects.requireNonNull;
36 import static org.apache.commons.lang.StringUtils.substring;
37 import static org.sonar.api.rule.RuleStatus.READY;
38 import static org.sonar.db.rule.RuleDto.Scope.ALL;
40 public class AdHocRuleCreator {
42 private static final int MAX_LENGTH_AD_HOC_NAME = 200;
43 private static final int MAX_LENGTH_AD_HOC_DESC = 16_777_215;
44 private final DbClient dbClient;
45 private final System2 system2;
46 private final RuleIndexer ruleIndexer;
47 private final UuidFactory uuidFactory;
49 public AdHocRuleCreator(DbClient dbClient, System2 system2, RuleIndexer ruleIndexer, UuidFactory uuidFactory) {
50 this.dbClient = dbClient;
51 this.system2 = system2;
52 this.ruleIndexer = ruleIndexer;
53 this.uuidFactory = uuidFactory;
57 * Persists a new add hoc rule in the DB and indexes it.
58 * @return the rule that was inserted in the DB, which <b>includes the generated ID</b>.
60 public RuleDto persistAndIndex(DbSession dbSession, NewAdHocRule adHoc) {
61 RuleDao dao = dbClient.ruleDao();
62 RuleMetadataDto metadata;
63 long now = system2.now();
65 RuleDto ruleDtoToUpdate = findOrCreateRuleDto(dbSession, adHoc, dao, now);
67 metadata = ruleDtoToUpdate.getMetadata();
68 if (adHoc.hasDetails()) {
69 boolean changed = false;
70 if (!Objects.equals(metadata.getAdHocName(), adHoc.getName())) {
71 metadata.setAdHocName(substring(adHoc.getName(), 0, MAX_LENGTH_AD_HOC_NAME));
74 if (!Objects.equals(metadata.getAdHocDescription(), adHoc.getDescription())) {
75 metadata.setAdHocDescription(substring(adHoc.getDescription(), 0, MAX_LENGTH_AD_HOC_DESC));
78 if (!Objects.equals(metadata.getAdHocSeverity(), adHoc.getSeverity())) {
79 metadata.setAdHocSeverity(adHoc.getSeverity());
82 RuleType ruleType = requireNonNull(adHoc.getRuleType(), "Rule type should not be null");
83 if (!Objects.equals(metadata.getAdHocType(), ruleType.getDbConstant())) {
84 metadata.setAdHocType(ruleType);
88 ruleDtoToUpdate.setUpdatedAt(now);
89 dao.update(dbSession, ruleDtoToUpdate);
94 RuleDto ruleDto = dao.selectOrFailByKey(dbSession, adHoc.getKey());
95 ruleIndexer.commitAndIndex(dbSession, ruleDto.getUuid());
99 private RuleDto findOrCreateRuleDto(DbSession dbSession, NewAdHocRule adHoc, RuleDao dao, long now) {
100 Optional<RuleDto> existingRuleDtoOpt = dbClient.ruleDao().selectByKey(dbSession, adHoc.getKey());
101 if (existingRuleDtoOpt.isEmpty()) {
102 RuleDto ruleDto = new RuleDto()
103 .setUuid(uuidFactory.create())
104 .setRuleKey(adHoc.getKey())
107 .setName(adHoc.getEngineId() + ":" + adHoc.getRuleId())
112 dao.insert(dbSession, ruleDto);
115 RuleDto ruleDto = existingRuleDtoOpt.get();
116 Preconditions.checkState(ruleDto.isExternal() && ruleDto.isAdHoc());