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.step;
22 import org.sonar.core.util.CloseableIterator;
23 import org.sonar.db.DbClient;
24 import org.sonar.db.DbSession;
25 import org.sonar.db.duplication.DuplicationUnitDto;
26 import org.sonar.scanner.protocol.output.ScannerReport;
27 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
28 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
29 import org.sonar.server.computation.task.projectanalysis.component.Component;
30 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
31 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
32 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
33 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
34 import org.sonar.server.computation.task.projectanalysis.duplication.CrossProjectDuplicationStatusHolder;
35 import org.sonar.server.computation.task.step.ComputationStep;
37 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
40 * Persist cross project duplications text blocks into DUPLICATIONS_INDEX table
42 public class PersistCrossProjectDuplicationIndexStep implements ComputationStep {
44 private final DbClient dbClient;
45 private final TreeRootHolder treeRootHolder;
46 private final AnalysisMetadataHolder analysisMetadataHolder;
47 private final BatchReportReader reportReader;
48 private final CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder;
50 public PersistCrossProjectDuplicationIndexStep(CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder, DbClient dbClient,
51 TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder,
52 BatchReportReader reportReader) {
53 this.dbClient = dbClient;
54 this.treeRootHolder = treeRootHolder;
55 this.analysisMetadataHolder = analysisMetadataHolder;
56 this.reportReader = reportReader;
57 this.crossProjectDuplicationStatusHolder = crossProjectDuplicationStatusHolder;
61 public void execute() {
62 if (!crossProjectDuplicationStatusHolder.isEnabled()) {
66 try (DbSession dbSession = dbClient.openSession(true)) {
67 Component project = treeRootHolder.getRoot();
68 new DepthTraversalTypeAwareCrawler(new DuplicationVisitor(dbSession, analysisMetadataHolder.getUuid())).visit(project);
73 private class DuplicationVisitor extends TypeAwareVisitorAdapter {
75 private final DbSession session;
76 private final String analysisUuid;
78 private DuplicationVisitor(DbSession session, String analysisUuid) {
79 super(CrawlerDepthLimit.FILE, PRE_ORDER);
80 this.session = session;
81 this.analysisUuid = analysisUuid;
85 public void visitFile(Component file) {
89 private void visitComponent(Component component) {
90 readFromReport(component);
93 private void readFromReport(Component component) {
95 try (CloseableIterator<ScannerReport.CpdTextBlock> blocks = reportReader.readCpdTextBlocks(component.getReportAttributes().getRef())) {
96 while (blocks.hasNext()) {
97 ScannerReport.CpdTextBlock block = blocks.next();
98 dbClient.duplicationDao().insert(
100 new DuplicationUnitDto()
101 .setHash(block.getHash())
102 .setStartLine(block.getStartLine())
103 .setEndLine(block.getEndLine())
104 .setIndexInFile(indexInFile)
105 .setAnalysisUuid(analysisUuid)
106 .setComponentUuid(component.getUuid()));
115 public String getDescription() {
116 return "Persist cross project duplications index";