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.qualityprofile.ActiveRulesHolder;
33 public class ComponentIssuesLoader {
34 private final DbClient dbClient;
35 private final RuleRepository ruleRepository;
36 private final ActiveRulesHolder activeRulesHolder;
38 public ComponentIssuesLoader(DbClient dbClient, RuleRepository ruleRepository, ActiveRulesHolder activeRulesHolder) {
39 this.activeRulesHolder = activeRulesHolder;
40 this.dbClient = dbClient;
41 this.ruleRepository = ruleRepository;
44 public List<DefaultIssue> loadForComponentUuid(String componentUuid) {
45 try (DbSession dbSession = dbClient.openSession(false)) {
46 List<DefaultIssue> result = new ArrayList<>();
47 dbSession.getMapper(IssueMapper.class).scrollNonClosedByComponentUuid(componentUuid, resultContext -> {
48 DefaultIssue issue = (resultContext.getResultObject()).toDefaultIssue();
50 // TODO this field should be set outside this class
51 if (!isActive(issue.ruleKey()) || ruleRepository.getByKey(issue.ruleKey()).getStatus() == RuleStatus.REMOVED) {
52 issue.setOnDisabledRule(true);
53 // TODO to be improved, why setOnDisabledRule(true) is not enough ?
54 issue.setBeingClosed(true);
57 issue.setSelectedAt(System.currentTimeMillis());
64 private boolean isActive(RuleKey ruleKey) {
65 return activeRulesHolder.get(ruleKey).isPresent();