]> source.dussan.org Git - sonarqube.git/blob
8d57e9bf5cde19064c15d60d23af1aa30c79694f
[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 java.util.List;
23 import javax.annotation.Nullable;
24 import org.sonar.core.util.CloseableIterator;
25 import org.sonar.db.DbClient;
26 import org.sonar.db.DbSession;
27 import org.sonar.db.duplication.DuplicationUnitDto;
28 import org.sonar.scanner.protocol.output.ScannerReport;
29 import org.sonar.server.computation.task.projectanalysis.analysis.Analysis;
30 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
31 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
32 import org.sonar.server.computation.task.projectanalysis.component.Component;
33 import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
34 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
35 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
36 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
37 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
38 import org.sonar.server.computation.task.projectanalysis.duplication.CrossProjectDuplicationStatusHolder;
39 import org.sonar.server.computation.task.step.ComputationStep;
40
41 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
42
43 /**
44  * Persist cross project duplications text blocks into DUPLICATIONS_INDEX table
45  */
46 public class PersistCrossProjectDuplicationIndexStep implements ComputationStep {
47
48   private final DbClient dbClient;
49   private final TreeRootHolder treeRootHolder;
50   private final AnalysisMetadataHolder analysisMetadataHolder;
51   private final BatchReportReader reportReader;
52   private final CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder;
53
54   public PersistCrossProjectDuplicationIndexStep(CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder, DbClient dbClient,
55     TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder,
56     BatchReportReader reportReader) {
57     this.dbClient = dbClient;
58     this.treeRootHolder = treeRootHolder;
59     this.analysisMetadataHolder = analysisMetadataHolder;
60     this.reportReader = reportReader;
61     this.crossProjectDuplicationStatusHolder = crossProjectDuplicationStatusHolder;
62   }
63
64   @Override
65   public void execute() {
66     if (!crossProjectDuplicationStatusHolder.isEnabled()) {
67       return;
68     }
69
70     try (DbSession dbSession = dbClient.openSession(true)) {
71       Component project = treeRootHolder.getRoot();
72       Analysis baseAnalysis = analysisMetadataHolder.getBaseAnalysis();
73       String lastAnalysysUuid = (baseAnalysis != null) ? baseAnalysis.getUuid() : null;
74       new DepthTraversalTypeAwareCrawler(new DuplicationVisitor(dbSession, analysisMetadataHolder.getUuid(), lastAnalysysUuid)).visit(project);
75       dbSession.commit();
76     }
77   }
78
79   private class DuplicationVisitor extends TypeAwareVisitorAdapter {
80
81     private final DbSession session;
82     private final String analysisUuid;
83     private final String lastAnalysisUuid;
84
85     private DuplicationVisitor(DbSession session, String analysisUuid, @Nullable String lastAnalysisUuid) {
86       super(CrawlerDepthLimit.FILE, PRE_ORDER);
87       this.session = session;
88       this.analysisUuid = analysisUuid;
89       this.lastAnalysisUuid = lastAnalysisUuid;
90     }
91
92     @Override
93     public void visitFile(Component file) {
94       visitComponent(file);
95     }
96
97     private void visitComponent(Component component) {
98       if (analysisMetadataHolder.isIncrementalAnalysis() && component.getStatus() == Status.SAME) {
99         readFromDb(component);
100       } else {
101         readFromReport(component);
102       }
103     }
104
105     private void readFromReport(Component component) {
106       int indexInFile = 0;
107       try (CloseableIterator<ScannerReport.CpdTextBlock> blocks = reportReader.readCpdTextBlocks(component.getReportAttributes().getRef())) {
108         while (blocks.hasNext()) {
109           ScannerReport.CpdTextBlock block = blocks.next();
110           dbClient.duplicationDao().insert(
111             session,
112             new DuplicationUnitDto()
113               .setHash(block.getHash())
114               .setStartLine(block.getStartLine())
115               .setEndLine(block.getEndLine())
116               .setIndexInFile(indexInFile)
117               .setAnalysisUuid(analysisUuid)
118               .setComponentUuid(component.getUuid()));
119           indexInFile++;
120         }
121       }
122     }
123
124     private void readFromDb(Component component) {
125       int indexInFile = 0;
126       List<DuplicationUnitDto> units = dbClient.duplicationDao().selectComponent(session, component.getUuid(), lastAnalysisUuid);
127       for (DuplicationUnitDto unit : units) {
128         unit.setAnalysisUuid(analysisUuid);
129         unit.setIndexInFile(indexInFile);
130         dbClient.duplicationDao().insert(session, unit);
131         indexInFile++;
132       }
133     }
134   }
135
136   @Override
137   public String getDescription() {
138     return "Persist cross project duplications index";
139   }
140
141 }