123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- /*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- package org.sonar.server.computation.issue;
-
- import com.google.common.collect.ImmutableMap;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import org.apache.ibatis.session.ResultContext;
- import org.apache.ibatis.session.ResultHandler;
- import org.sonar.api.rule.RuleKey;
- import org.sonar.api.rule.RuleStatus;
- import org.sonar.core.issue.DefaultIssue;
- import org.sonar.db.issue.IssueDto;
- import org.sonar.db.issue.IssueMapper;
- import org.sonar.db.DbSession;
- import org.sonar.db.MyBatis;
- import org.sonar.server.computation.batch.BatchReportReader;
- import org.sonar.server.computation.component.TreeRootHolder;
- import org.sonar.server.db.DbClient;
-
- import static com.google.common.collect.FluentIterable.from;
- import static org.sonar.core.rule.RuleKeyFunctions.stringToRuleKey;
-
- /**
- * Loads all the project open issues from database, including manual issues.
- *
- */
- public class BaseIssuesLoader {
-
- private final Set<RuleKey> activeRuleKeys;
- private final TreeRootHolder treeRootHolder;
- private final DbClient dbClient;
- private final RuleRepository ruleRepository;
-
- public BaseIssuesLoader(BatchReportReader reportReader, TreeRootHolder treeRootHolder,
- DbClient dbClient, RuleRepository ruleRepository) {
- this.activeRuleKeys = from(reportReader.readMetadata().getActiveRuleKeyList()).transform(stringToRuleKey()).toSet();
- this.treeRootHolder = treeRootHolder;
- this.dbClient = dbClient;
- this.ruleRepository = ruleRepository;
- }
-
- public List<DefaultIssue> loadForComponentUuid(String componentUuid) {
- DbSession session = dbClient.openSession(false);
- final List<DefaultIssue> result = new ArrayList<>();
- try {
- Map<String, String> params = ImmutableMap.of("componentUuid", componentUuid);
- session.select(IssueMapper.class.getName() + ".selectNonClosedByComponentUuid", params, new ResultHandler() {
- @Override
- public void handleResult(ResultContext resultContext) {
- DefaultIssue issue = ((IssueDto) resultContext.getResultObject()).toDefaultIssue();
-
- // TODO this field should be set outside this class
- if (!isActive(issue.ruleKey()) || ruleRepository.getByKey(issue.ruleKey()).getStatus() == RuleStatus.REMOVED) {
- issue.setOnDisabledRule(true);
- // TODO to be improved, why setOnDisabledRule(true) is not enough ?
- issue.setBeingClosed(true);
- }
- // FIXME
- issue.setSelectedAt(System.currentTimeMillis());
- result.add(issue);
- }
- });
- return result;
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- private boolean isActive(RuleKey ruleKey) {
- return ruleKey.isManual() || activeRuleKeys.contains(ruleKey);
- }
-
- /**
- * Uuids of all the components that have open issues on this project.
- */
- public Set<String> loadUuidsOfComponentsWithOpenIssues() {
- DbSession session = dbClient.openSession(false);
- try {
- return dbClient.issueDao().selectComponentUuidsOfOpenIssuesForProjectUuid(session, treeRootHolder.getRoot().getUuid());
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
- }
|