]> source.dussan.org Git - sonarqube.git/blob
f260563094c713941cd286199526d2bbd3cae6ae
[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.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;
34
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;
39
40 public class PurgeDatastoresStep implements ComputationStep {
41
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;
48
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;
57   }
58
59   @Override
60   public void execute() {
61     new DepthTraversalTypeAwareCrawler(
62       new TypeAwareVisitorAdapter(reportMaxDepth(PROJECT).withViewsMaxDepth(VIEW), PRE_ORDER) {
63         @Override
64         public void visitProject(Component project) {
65           execute(project);
66         }
67
68         @Override
69         public void visitView(Component view) {
70           execute(view);
71         }
72       }).visit(treeRootHolder.getRoot());
73   }
74
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());
79       dbSession.commit();
80     }
81   }
82
83   @Override
84   public String getDescription() {
85     return "Purge db";
86   }
87 }