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.
20 package org.sonar.server.computation.task.projectanalysis.issue;
22 import java.util.ArrayList;
23 import java.util.List;
25 import org.sonar.api.rule.RuleKey;
26 import org.sonar.api.rule.RuleStatus;
27 import org.sonar.core.issue.DefaultIssue;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.issue.IssueMapper;
31 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
32 import org.sonar.server.computation.task.projectanalysis.qualityprofile.ActiveRulesHolder;
35 * Loads all the project open issues from database, including manual issues.
38 public class BaseIssuesLoader {
40 private final TreeRootHolder treeRootHolder;
41 private final DbClient dbClient;
42 private final RuleRepository ruleRepository;
43 private final ActiveRulesHolder activeRulesHolder;
45 public BaseIssuesLoader(TreeRootHolder treeRootHolder,
46 DbClient dbClient, RuleRepository ruleRepository, ActiveRulesHolder activeRulesHolder) {
47 this.activeRulesHolder = activeRulesHolder;
48 this.treeRootHolder = treeRootHolder;
49 this.dbClient = dbClient;
50 this.ruleRepository = ruleRepository;
53 public List<DefaultIssue> loadForComponentUuid(String componentUuid) {
54 try (DbSession dbSession = dbClient.openSession(false)) {
55 List<DefaultIssue> result = new ArrayList<>();
56 dbSession.getMapper(IssueMapper.class).selectNonClosedByComponentUuid(componentUuid, resultContext -> {
57 DefaultIssue issue = (resultContext.getResultObject()).toDefaultIssue();
59 // TODO this field should be set outside this class
60 if (!isActive(issue.ruleKey()) || ruleRepository.getByKey(issue.ruleKey()).getStatus() == RuleStatus.REMOVED) {
61 issue.setOnDisabledRule(true);
62 // TODO to be improved, why setOnDisabledRule(true) is not enough ?
63 issue.setBeingClosed(true);
66 issue.setSelectedAt(System.currentTimeMillis());
73 private boolean isActive(RuleKey ruleKey) {
74 return activeRulesHolder.get(ruleKey).isPresent();
78 * Uuids of all the components that have open issues on this project.
80 public Set<String> loadUuidsOfComponentsWithOpenIssues() {
81 try (DbSession dbSession = dbClient.openSession(false)) {
82 return dbClient.issueDao().selectComponentUuidsOfOpenIssuesForProjectUuid(dbSession, treeRootHolder.getRoot().getUuid());