]> source.dussan.org Git - sonarqube.git/blob
f0be9ded75dfe929a0ca5dcd9d166b018b9968c9
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
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.task.projectanalysis.step;
21
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;
36
37 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
38
39 /**
40  * Persist cross project duplications text blocks into DUPLICATIONS_INDEX table
41  */
42 public class PersistCrossProjectDuplicationIndexStep implements ComputationStep {
43
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;
49
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;
58   }
59
60   @Override
61   public void execute() {
62     if (!crossProjectDuplicationStatusHolder.isEnabled()) {
63       return;
64     }
65
66     try (DbSession dbSession = dbClient.openSession(true)) {
67       Component project = treeRootHolder.getRoot();
68       new DepthTraversalTypeAwareCrawler(new DuplicationVisitor(dbSession, analysisMetadataHolder.getUuid())).visit(project);
69       dbSession.commit();
70     }
71   }
72
73   private class DuplicationVisitor extends TypeAwareVisitorAdapter {
74
75     private final DbSession session;
76     private final String analysisUuid;
77
78     private DuplicationVisitor(DbSession session, String analysisUuid) {
79       super(CrawlerDepthLimit.FILE, PRE_ORDER);
80       this.session = session;
81       this.analysisUuid = analysisUuid;
82     }
83
84     @Override
85     public void visitFile(Component file) {
86       visitComponent(file);
87     }
88
89     private void visitComponent(Component component) {
90       readFromReport(component);
91     }
92
93     private void readFromReport(Component component) {
94       int indexInFile = 0;
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(
99             session,
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()));
107           indexInFile++;
108         }
109       }
110     }
111
112   }
113
114   @Override
115   public String getDescription() {
116     return "Persist cross project duplications index";
117   }
118
119 }