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.

PersistDuplicationsStep.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.step;
  21. import java.util.List;
  22. import org.apache.commons.lang.StringEscapeUtils;
  23. import org.sonar.api.measures.CoreMetrics;
  24. import org.sonar.batch.protocol.output.BatchReport;
  25. import org.sonar.batch.protocol.output.BatchReport.Range;
  26. import org.sonar.db.measure.MeasureDto;
  27. import org.sonar.db.metric.MetricDto;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.MyBatis;
  30. import org.sonar.server.computation.batch.BatchReportReader;
  31. import org.sonar.server.computation.component.Component;
  32. import org.sonar.server.computation.component.DbIdsRepository;
  33. import org.sonar.server.computation.component.DepthTraversalTypeAwareVisitor;
  34. import org.sonar.server.computation.component.TreeRootHolder;
  35. import org.sonar.server.db.DbClient;
  36. import static org.sonar.server.computation.component.ComponentVisitor.Order.PRE_ORDER;
  37. /**
  38. * Persist duplications into
  39. */
  40. public class PersistDuplicationsStep implements ComputationStep {
  41. private final DbClient dbClient;
  42. private final DbIdsRepository dbIdsRepository;
  43. private final TreeRootHolder treeRootHolder;
  44. private final BatchReportReader reportReader;
  45. public PersistDuplicationsStep(DbClient dbClient, DbIdsRepository dbIdsRepository, TreeRootHolder treeRootHolder, BatchReportReader reportReader) {
  46. this.dbClient = dbClient;
  47. this.dbIdsRepository = dbIdsRepository;
  48. this.treeRootHolder = treeRootHolder;
  49. this.reportReader = reportReader;
  50. }
  51. @Override
  52. public void execute() {
  53. DbSession session = dbClient.openSession(true);
  54. try {
  55. MetricDto duplicationMetric = dbClient.metricDao().selectByKey(session, CoreMetrics.DUPLICATIONS_DATA_KEY);
  56. new DuplicationVisitor(session, duplicationMetric).visit(treeRootHolder.getRoot());
  57. session.commit();
  58. } finally {
  59. MyBatis.closeQuietly(session);
  60. }
  61. }
  62. private class DuplicationVisitor extends DepthTraversalTypeAwareVisitor {
  63. private final DbSession session;
  64. private final MetricDto duplicationMetric;
  65. private DuplicationVisitor(DbSession session, MetricDto duplicationMetric) {
  66. super(Component.Type.FILE, PRE_ORDER);
  67. this.session = session;
  68. this.duplicationMetric = duplicationMetric;
  69. }
  70. @Override
  71. public void visitFile(Component file) {
  72. visitComponent(file);
  73. }
  74. private void visitComponent(Component component) {
  75. List<BatchReport.Duplication> duplications = reportReader.readComponentDuplications(component.getRef());
  76. if (!duplications.isEmpty()) {
  77. saveDuplications( component, duplications);
  78. }
  79. }
  80. private void saveDuplications(Component component, List<BatchReport.Duplication> duplications) {
  81. String duplicationXml = createXmlDuplications(component.getKey(), duplications);
  82. MeasureDto measureDto = new MeasureDto()
  83. .setMetricId(duplicationMetric.getId())
  84. .setData(duplicationXml)
  85. .setComponentId(dbIdsRepository.getComponentId(component))
  86. .setSnapshotId(dbIdsRepository.getSnapshotId(component));
  87. dbClient.measureDao().insert(session, measureDto);
  88. }
  89. private String createXmlDuplications(String componentKey, Iterable<BatchReport.Duplication> duplications) {
  90. StringBuilder xml = new StringBuilder();
  91. xml.append("<duplications>");
  92. for (BatchReport.Duplication duplication : duplications) {
  93. xml.append("<g>");
  94. appendDuplication(xml, componentKey, duplication.getOriginPosition());
  95. for (BatchReport.Duplicate duplicationBlock : duplication.getDuplicateList()) {
  96. processDuplicationBlock(xml, duplicationBlock, componentKey);
  97. }
  98. xml.append("</g>");
  99. }
  100. xml.append("</duplications>");
  101. return xml.toString();
  102. }
  103. private void processDuplicationBlock(StringBuilder xml, BatchReport.Duplicate duplicate, String componentKey) {
  104. if (duplicate.hasOtherFileKey()) {
  105. // componentKey is only set for cross project duplications
  106. String crossProjectComponentKey = duplicate.getOtherFileKey();
  107. appendDuplication(xml, crossProjectComponentKey, duplicate);
  108. } else {
  109. if (duplicate.hasOtherFileRef()) {
  110. // Duplication is on a different file
  111. appendDuplication(xml, treeRootHolder.getComponentByRef(duplicate.getOtherFileRef()).getKey(), duplicate);
  112. } else {
  113. // Duplication is on a the same file
  114. appendDuplication(xml, componentKey, duplicate);
  115. }
  116. }
  117. }
  118. private void appendDuplication(StringBuilder xml, String componentKey, BatchReport.Duplicate duplicate) {
  119. appendDuplication(xml, componentKey, duplicate.getRange());
  120. }
  121. private void appendDuplication(StringBuilder xml, String componentKey, Range range) {
  122. int length = range.getEndLine() - range.getStartLine() + 1;
  123. xml.append("<b s=\"").append(range.getStartLine())
  124. .append("\" l=\"").append(length)
  125. .append("\" r=\"").append(StringEscapeUtils.escapeXml(componentKey))
  126. .append("\"/>");
  127. }
  128. }
  129. @Override
  130. public String getDescription() {
  131. return "Persist duplications";
  132. }
  133. }