3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.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.batch.BatchReportReader;
28 import org.sonar.server.computation.component.Component;
29 import org.sonar.server.computation.component.CrawlerDepthLimit;
30 import org.sonar.server.computation.component.DbIdsRepository;
31 import org.sonar.server.computation.component.DepthTraversalTypeAwareCrawler;
32 import org.sonar.server.computation.component.TreeRootHolder;
33 import org.sonar.server.computation.component.TypeAwareVisitorAdapter;
34 import org.sonar.server.computation.duplication.CrossProjectDuplicationStatusHolder;
36 import static org.sonar.server.computation.component.ComponentVisitor.Order.PRE_ORDER;
39 * Persist cross project duplications text blocks into DUPLICATIONS_INDEX table
41 public class PersistCrossProjectDuplicationIndexStep implements ComputationStep {
43 private final DbClient dbClient;
44 private final TreeRootHolder treeRootHolder;
45 private final BatchReportReader reportReader;
46 private final DbIdsRepository dbIdsRepository;
47 private final CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder;
49 public PersistCrossProjectDuplicationIndexStep(DbClient dbClient, DbIdsRepository dbIdsRepository, TreeRootHolder treeRootHolder, BatchReportReader reportReader,
50 CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder) {
51 this.dbClient = dbClient;
52 this.treeRootHolder = treeRootHolder;
53 this.reportReader = reportReader;
54 this.dbIdsRepository = dbIdsRepository;
55 this.crossProjectDuplicationStatusHolder = crossProjectDuplicationStatusHolder;
59 public void execute() {
60 DbSession session = dbClient.openSession(true);
62 if (crossProjectDuplicationStatusHolder.isEnabled()) {
63 Component project = treeRootHolder.getRoot();
64 long projectSnapshotId = dbIdsRepository.getSnapshotId(project);
65 new DepthTraversalTypeAwareCrawler(new DuplicationVisitor(session, projectSnapshotId)).visit(project);
69 dbClient.closeSession(session);
73 private class DuplicationVisitor extends TypeAwareVisitorAdapter {
75 private final DbSession session;
76 private final long projectSnapshotId;
78 private DuplicationVisitor(DbSession session, long projectSnapshotId) {
79 super(CrawlerDepthLimit.FILE, PRE_ORDER);
80 this.session = session;
81 this.projectSnapshotId = projectSnapshotId;
85 public void visitFile(Component file) {
89 private void visitComponent(Component component) {
91 CloseableIterator<ScannerReport.CpdTextBlock> blocks = reportReader.readCpdTextBlocks(component.getReportAttributes().getRef());
93 while (blocks.hasNext()) {
94 ScannerReport.CpdTextBlock block = blocks.next();
95 dbClient.duplicationDao().insert(
97 new DuplicationUnitDto()
98 .setHash(block.getHash())
99 .setStartLine(block.getStartLine())
100 .setEndLine(block.getEndLine())
101 .setIndexInFile(indexInFile)
102 .setSnapshotId(dbIdsRepository.getSnapshotId(component))
103 .setProjectSnapshotId(projectSnapshotId)
114 public String getDescription() {
115 return "Persist cross project duplications index";