]> source.dussan.org Git - sonarqube.git/blob
2a44cf87bce9ebb52527801c87987ebb4042c01b
[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.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;
38
39 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
40
41 import java.util.List;
42
43 import javax.annotation.Nullable;
44
45 /**
46  * Persist cross project duplications text blocks into DUPLICATIONS_INDEX table
47  */
48 public class PersistCrossProjectDuplicationIndexStep implements ComputationStep {
49
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;
55
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;
64   }
65
66   @Override
67   public void execute() {
68     if (!crossProjectDuplicationStatusHolder.isEnabled()) {
69       return;
70     }
71
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);
77       dbSession.commit();
78     }
79   }
80
81   private class DuplicationVisitor extends TypeAwareVisitorAdapter {
82
83     private final DbSession session;
84     private final String analysisUuid;
85     private final String lastAnalysisUuid;
86
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;
92     }
93
94     @Override
95     public void visitFile(Component file) {
96       visitComponent(file);
97     }
98
99     private void visitComponent(Component component) {
100       if (component.getStatus() == Status.SAME) {
101         readFromDb(component);
102       } else {
103         readFromReport(component);
104       }
105     }
106
107     private void readFromReport(Component component) {
108       int indexInFile = 0;
109       CloseableIterator<ScannerReport.CpdTextBlock> blocks = reportReader.readCpdTextBlocks(component.getReportAttributes().getRef());
110       try {
111         while (blocks.hasNext()) {
112           ScannerReport.CpdTextBlock block = blocks.next();
113           dbClient.duplicationDao().insert(
114             session,
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()));
122           indexInFile++;
123         }
124       } finally {
125         blocks.close();
126       }
127     }
128
129     private void readFromDb(Component component) {
130       int indexInFile = 0;
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);
136         indexInFile++;
137       }
138     }
139   }
140
141   @Override
142   public String getDescription() {
143     return "Persist cross project duplications index";
144   }
145
146 }