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.task.projectanalysis.step;
22 import java.util.List;
23 import javax.annotation.CheckForNull;
24 import javax.annotation.Nullable;
25 import org.sonar.api.utils.System2;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.component.SnapshotDto;
29 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
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.PathAwareCrawler;
33 import org.sonar.server.computation.task.projectanalysis.component.PathAwareVisitorAdapter;
34 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
35 import org.sonar.server.computation.task.projectanalysis.period.Period;
36 import org.sonar.server.computation.task.projectanalysis.period.PeriodsHolder;
37 import org.sonar.server.computation.task.step.ComputationStep;
42 public class PersistAnalysisStep implements ComputationStep {
44 private final System2 system2;
45 private final DbClient dbClient;
46 private final TreeRootHolder treeRootHolder;
47 private final AnalysisMetadataHolder analysisMetadataHolder;
48 private final PeriodsHolder periodsHolder;
50 public PersistAnalysisStep(System2 system2, DbClient dbClient, TreeRootHolder treeRootHolder,
51 AnalysisMetadataHolder analysisMetadataHolder, PeriodsHolder periodsHolder) {
52 this.system2 = system2;
53 this.dbClient = dbClient;
54 this.treeRootHolder = treeRootHolder;
55 this.analysisMetadataHolder = analysisMetadataHolder;
56 this.periodsHolder = periodsHolder;
60 public void execute() {
61 DbSession session = dbClient.openSession(false);
63 new PathAwareCrawler<>(
64 new PersistSnapshotsPathAwareVisitor(session, analysisMetadataHolder.getAnalysisDate()))
65 .visit(treeRootHolder.getRoot());
68 dbClient.closeSession(session);
72 private class PersistSnapshotsPathAwareVisitor extends PathAwareVisitorAdapter<SnapshotDtoHolder> {
74 private final DbSession dbSession;
75 private final long analysisDate;
77 public PersistSnapshotsPathAwareVisitor(DbSession dbSession, long analysisDate) {
78 super(CrawlerDepthLimit.ROOTS, Order.PRE_ORDER, SnapshotDtoHolderFactory.INSTANCE);
79 this.dbSession = dbSession;
80 this.analysisDate = analysisDate;
84 public void visitProject(Component project, Path<SnapshotDtoHolder> path) {
85 SnapshotDto snapshot = createAnalysis(analysisMetadataHolder.getUuid(), project, true);
86 updateSnapshotPeriods(snapshot);
87 persist(snapshot, dbSession);
91 public void visitView(Component view, Path<SnapshotDtoHolder> path) {
92 SnapshotDto snapshot = createAnalysis(analysisMetadataHolder.getUuid(), view, false);
93 updateSnapshotPeriods(snapshot);
94 persist(snapshot, dbSession);
97 private void updateSnapshotPeriods(SnapshotDto snapshotDto) {
98 List<Period> periods = periodsHolder.getPeriods();
99 if (periods.isEmpty()) {
102 Period period = periods.get(0);
103 snapshotDto.setPeriodMode(period.getMode());
104 snapshotDto.setPeriodParam(period.getModeParameter());
105 snapshotDto.setPeriodDate(period.getSnapshotDate());
108 private SnapshotDto createAnalysis(String snapshotUuid, Component component, boolean setVersion) {
109 String componentUuid = component.getUuid();
110 return new SnapshotDto()
111 .setUuid(snapshotUuid)
112 .setVersion(setVersion ? component.getReportAttributes().getVersion() : null)
113 .setComponentUuid(componentUuid)
115 .setStatus(SnapshotDto.STATUS_UNPROCESSED)
116 .setCreatedAt(analysisDate)
117 .setBuildDate(system2.now());
120 private void persist(SnapshotDto snapshotDto, DbSession dbSession) {
121 dbClient.snapshotDao().insert(dbSession, snapshotDto);
125 private static final class SnapshotDtoHolder {
127 private SnapshotDto snapshotDto;
130 public SnapshotDto getSnapshotDto() {
134 public void setSnapshotDto(@Nullable SnapshotDto snapshotDto) {
135 this.snapshotDto = snapshotDto;
140 * Factory of SnapshotDtoHolder.
142 * No need to create an instance for components of type FILE and PROJECT_VIEW, since they are the leaves of their
143 * respective trees, no one will consume the value of the holder, so we save on creating useless objects.
145 private static class SnapshotDtoHolderFactory extends PathAwareVisitorAdapter.SimpleStackElementFactory<SnapshotDtoHolder> {
146 public static final SnapshotDtoHolderFactory INSTANCE = new SnapshotDtoHolderFactory();
149 public SnapshotDtoHolder createForAny(Component component) {
150 return new SnapshotDtoHolder();
154 public SnapshotDtoHolder createForFile(Component file) {
159 public SnapshotDtoHolder createForProjectView(Component projectView) {
165 public String getDescription() {
166 return "Persist analysis";