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.db.DbClient;
23 import org.sonar.db.DbSession;
24 import org.sonar.db.purge.IdUuidPair;
25 import org.sonar.server.computation.task.projectanalysis.component.Component;
26 import org.sonar.server.computation.task.projectanalysis.component.DbIdsRepository;
27 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
28 import org.sonar.server.computation.task.projectanalysis.component.DisabledComponentsHolder;
29 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
30 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
31 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
32 import org.sonar.server.computation.dbcleaner.ProjectCleaner;
33 import org.sonar.server.computation.task.step.ComputationStep;
35 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT;
36 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.VIEW;
37 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
38 import static org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit.reportMaxDepth;
40 public class PurgeDatastoresStep implements ComputationStep {
42 private final ProjectCleaner projectCleaner;
43 private final DbClient dbClient;
44 private final DbIdsRepository dbIdsRepository;
45 private final TreeRootHolder treeRootHolder;
46 private final SettingsRepository settingsRepository;
47 private final DisabledComponentsHolder disabledComponentsHolder;
49 public PurgeDatastoresStep(DbClient dbClient, ProjectCleaner projectCleaner, DbIdsRepository dbIdsRepository, TreeRootHolder treeRootHolder,
50 SettingsRepository settingsRepository, DisabledComponentsHolder disabledComponentsHolder) {
51 this.projectCleaner = projectCleaner;
52 this.dbClient = dbClient;
53 this.dbIdsRepository = dbIdsRepository;
54 this.treeRootHolder = treeRootHolder;
55 this.settingsRepository = settingsRepository;
56 this.disabledComponentsHolder = disabledComponentsHolder;
60 public void execute() {
61 new DepthTraversalTypeAwareCrawler(
62 new TypeAwareVisitorAdapter(reportMaxDepth(PROJECT).withViewsMaxDepth(VIEW), PRE_ORDER) {
64 public void visitProject(Component project) {
69 public void visitView(Component view) {
72 }).visit(treeRootHolder.getRoot());
75 private void execute(Component root) {
76 try (DbSession dbSession = dbClient.openSession(true)) {
77 IdUuidPair idUuidPair = new IdUuidPair(dbIdsRepository.getComponentId(root), root.getUuid());
78 projectCleaner.purge(dbSession, idUuidPair, settingsRepository.getSettings(root), disabledComponentsHolder.getUuids());
84 public String getDescription() {