3 * Copyright (C) 2009-2019 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.ce.task.projectanalysis.step;
22 import org.sonar.api.utils.System2;
23 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
24 import org.sonar.ce.task.projectanalysis.component.Component;
25 import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
26 import org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
27 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
28 import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
29 import org.sonar.ce.task.projectanalysis.period.Period;
30 import org.sonar.ce.task.projectanalysis.period.PeriodHolder;
31 import org.sonar.ce.task.step.ComputationStep;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.component.SnapshotDto;
36 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
41 public class PersistAnalysisStep implements ComputationStep {
43 private final System2 system2;
44 private final DbClient dbClient;
45 private final TreeRootHolder treeRootHolder;
46 private final AnalysisMetadataHolder analysisMetadataHolder;
47 private final PeriodHolder periodHolder;
49 public PersistAnalysisStep(System2 system2, DbClient dbClient, TreeRootHolder treeRootHolder,
50 AnalysisMetadataHolder analysisMetadataHolder, PeriodHolder periodHolder) {
51 this.system2 = system2;
52 this.dbClient = dbClient;
53 this.treeRootHolder = treeRootHolder;
54 this.analysisMetadataHolder = analysisMetadataHolder;
55 this.periodHolder = periodHolder;
59 public void execute(ComputationStep.Context context) {
60 try (DbSession dbSession = dbClient.openSession(false)) {
61 new DepthTraversalTypeAwareCrawler(
62 new PersistSnapshotsPathAwareVisitor(dbSession, analysisMetadataHolder.getAnalysisDate()))
63 .visit(treeRootHolder.getRoot());
68 private class PersistSnapshotsPathAwareVisitor extends TypeAwareVisitorAdapter {
70 private final DbSession dbSession;
71 private final long analysisDate;
73 public PersistSnapshotsPathAwareVisitor(DbSession dbSession, long analysisDate) {
74 super(CrawlerDepthLimit.ROOTS, Order.PRE_ORDER);
75 this.dbSession = dbSession;
76 this.analysisDate = analysisDate;
80 public void visitProject(Component project) {
81 SnapshotDto snapshot = createAnalysis(analysisMetadataHolder.getUuid(), project);
82 updateSnapshotPeriods(snapshot);
83 persist(snapshot, dbSession);
87 public void visitView(Component view) {
88 SnapshotDto snapshot = createAnalysis(analysisMetadataHolder.getUuid(), view);
89 updateSnapshotPeriods(snapshot);
90 persist(snapshot, dbSession);
93 private void updateSnapshotPeriods(SnapshotDto snapshotDto) {
94 if (!periodHolder.hasPeriod()) {
97 Period period = periodHolder.getPeriod();
98 snapshotDto.setPeriodMode(period.getMode());
99 snapshotDto.setPeriodParam(period.getModeParameter());
100 snapshotDto.setPeriodDate(period.getSnapshotDate());
103 private SnapshotDto createAnalysis(String snapshotUuid, Component component) {
104 String componentUuid = component.getUuid();
105 String projectVersion = component.getType() == PROJECT ? component.getProjectAttributes().getProjectVersion() : null;
106 String buildString = component.getType() == PROJECT ? component.getProjectAttributes().getBuildString().orElse(null) : null;
107 return new SnapshotDto()
108 .setUuid(snapshotUuid)
109 .setProjectVersion(projectVersion)
110 .setBuildString(buildString)
111 .setComponentUuid(componentUuid)
113 .setStatus(SnapshotDto.STATUS_UNPROCESSED)
114 .setCreatedAt(analysisDate)
115 .setBuildDate(system2.now());
118 private void persist(SnapshotDto snapshotDto, DbSession dbSession) {
119 dbClient.snapshotDao().insert(dbSession, snapshotDto);
124 public String getDescription() {
125 return "Persist analysis";