You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseIssuesLoader.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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.
  10. *
  11. * SonarQube 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.
  15. *
  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.
  19. */
  20. package org.sonar.server.computation.issue;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import org.apache.ibatis.session.ResultContext;
  27. import org.apache.ibatis.session.ResultHandler;
  28. import org.sonar.api.rule.RuleKey;
  29. import org.sonar.api.rule.RuleStatus;
  30. import org.sonar.core.issue.DefaultIssue;
  31. import org.sonar.db.issue.IssueDto;
  32. import org.sonar.db.issue.IssueMapper;
  33. import org.sonar.db.DbSession;
  34. import org.sonar.db.MyBatis;
  35. import org.sonar.server.computation.batch.BatchReportReader;
  36. import org.sonar.server.computation.component.TreeRootHolder;
  37. import org.sonar.server.db.DbClient;
  38. import static com.google.common.collect.FluentIterable.from;
  39. import static org.sonar.core.rule.RuleKeyFunctions.stringToRuleKey;
  40. /**
  41. * Loads all the project open issues from database, including manual issues.
  42. *
  43. */
  44. public class BaseIssuesLoader {
  45. private final Set<RuleKey> activeRuleKeys;
  46. private final TreeRootHolder treeRootHolder;
  47. private final DbClient dbClient;
  48. private final RuleRepository ruleRepository;
  49. public BaseIssuesLoader(BatchReportReader reportReader, TreeRootHolder treeRootHolder,
  50. DbClient dbClient, RuleRepository ruleRepository) {
  51. this.activeRuleKeys = from(reportReader.readMetadata().getActiveRuleKeyList()).transform(stringToRuleKey()).toSet();
  52. this.treeRootHolder = treeRootHolder;
  53. this.dbClient = dbClient;
  54. this.ruleRepository = ruleRepository;
  55. }
  56. public List<DefaultIssue> loadForComponentUuid(String componentUuid) {
  57. DbSession session = dbClient.openSession(false);
  58. final List<DefaultIssue> result = new ArrayList<>();
  59. try {
  60. Map<String, String> params = ImmutableMap.of("componentUuid", componentUuid);
  61. session.select(IssueMapper.class.getName() + ".selectNonClosedByComponentUuid", params, new ResultHandler() {
  62. @Override
  63. public void handleResult(ResultContext resultContext) {
  64. DefaultIssue issue = ((IssueDto) resultContext.getResultObject()).toDefaultIssue();
  65. // TODO this field should be set outside this class
  66. if (!isActive(issue.ruleKey()) || ruleRepository.getByKey(issue.ruleKey()).getStatus() == RuleStatus.REMOVED) {
  67. issue.setOnDisabledRule(true);
  68. // TODO to be improved, why setOnDisabledRule(true) is not enough ?
  69. issue.setBeingClosed(true);
  70. }
  71. // FIXME
  72. issue.setSelectedAt(System.currentTimeMillis());
  73. result.add(issue);
  74. }
  75. });
  76. return result;
  77. } finally {
  78. MyBatis.closeQuietly(session);
  79. }
  80. }
  81. private boolean isActive(RuleKey ruleKey) {
  82. return ruleKey.isManual() || activeRuleKeys.contains(ruleKey);
  83. }
  84. /**
  85. * Uuids of all the components that have open issues on this project.
  86. */
  87. public Set<String> loadUuidsOfComponentsWithOpenIssues() {
  88. DbSession session = dbClient.openSession(false);
  89. try {
  90. return dbClient.issueDao().selectComponentUuidsOfOpenIssuesForProjectUuid(session, treeRootHolder.getRoot().getUuid());
  91. } finally {
  92. MyBatis.closeQuietly(session);
  93. }
  94. }
  95. }