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.Analysis;
28 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
29 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
30 import org.sonar.server.computation.task.projectanalysis.component.Component;
31 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
32 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
33 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
34 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
35 import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
36 import org.sonar.server.computation.task.projectanalysis.duplication.CrossProjectDuplicationStatusHolder;
37 import org.sonar.server.computation.task.step.ComputationStep;
39 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
41 import java.util.List;
43 import javax.annotation.Nullable;
46 * Persist cross project duplications text blocks into DUPLICATIONS_INDEX table
48 public class PersistCrossProjectDuplicationIndexStep implements ComputationStep {
50 private final DbClient dbClient;
51 private final TreeRootHolder treeRootHolder;
52 private final AnalysisMetadataHolder analysisMetadataHolder;
53 private final BatchReportReader reportReader;
54 private final CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder;
56 public PersistCrossProjectDuplicationIndexStep(CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder, DbClient dbClient,
57 TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder,
58 BatchReportReader reportReader) {
59 this.dbClient = dbClient;
60 this.treeRootHolder = treeRootHolder;
61 this.analysisMetadataHolder = analysisMetadataHolder;
62 this.reportReader = reportReader;
63 this.crossProjectDuplicationStatusHolder = crossProjectDuplicationStatusHolder;
67 public void execute() {
68 if (!crossProjectDuplicationStatusHolder.isEnabled()) {
72 try (DbSession dbSession = dbClient.openSession(true)) {
73 Component project = treeRootHolder.getRoot();
74 Analysis baseAnalysis = analysisMetadataHolder.getBaseAnalysis();
75 String lastAnalysysUuid = (baseAnalysis != null) ? baseAnalysis.getUuid() : null;
76 new DepthTraversalTypeAwareCrawler(new DuplicationVisitor(dbSession, analysisMetadataHolder.getUuid(), lastAnalysysUuid)).visit(project);
81 private class DuplicationVisitor extends TypeAwareVisitorAdapter {
83 private final DbSession session;
84 private final String analysisUuid;
85 private final String lastAnalysisUuid;
87 private DuplicationVisitor(DbSession session, String analysisUuid, @Nullable String lastAnalysisUuid) {
88 super(CrawlerDepthLimit.FILE, PRE_ORDER);
89 this.session = session;
90 this.analysisUuid = analysisUuid;
91 this.lastAnalysisUuid = lastAnalysisUuid;
95 public void visitFile(Component file) {
99 private void visitComponent(Component component) {
100 if (component.getStatus() == Status.SAME) {
101 readFromDb(component);
103 readFromReport(component);
107 private void readFromReport(Component component) {
109 CloseableIterator<ScannerReport.CpdTextBlock> blocks = reportReader.readCpdTextBlocks(component.getReportAttributes().getRef());
111 while (blocks.hasNext()) {
112 ScannerReport.CpdTextBlock block = blocks.next();
113 dbClient.duplicationDao().insert(
115 new DuplicationUnitDto()
116 .setHash(block.getHash())
117 .setStartLine(block.getStartLine())
118 .setEndLine(block.getEndLine())
119 .setIndexInFile(indexInFile)
120 .setAnalysisUuid(analysisUuid)
121 .setComponentUuid(component.getUuid()));
129 private void readFromDb(Component component) {
131 List<DuplicationUnitDto> units = dbClient.duplicationDao().selectComponent(session, component.getUuid(), lastAnalysisUuid);
132 for (DuplicationUnitDto unit : units) {
133 unit.setAnalysisUuid(analysisUuid);
134 unit.setIndexInFile(indexInFile);
135 dbClient.duplicationDao().insert(session, unit);
142 public String getDescription() {
143 return "Persist cross project duplications index";