3 * Copyright (C) 2009-2024 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;
23 import org.sonar.ce.task.projectanalysis.component.Component;
24 import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
25 import org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
26 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
27 import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
28 import org.sonar.ce.task.projectanalysis.measure.Measure;
29 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
30 import org.sonar.ce.task.projectanalysis.measure.MeasureToMeasureDto;
31 import org.sonar.ce.task.projectanalysis.metric.Metric;
32 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
33 import org.sonar.ce.task.step.ComputationStep;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
36 import org.sonar.db.measure.MeasureDao;
37 import org.sonar.db.measure.MeasureDto;
39 import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
41 public class PersistMeasuresStep implements ComputationStep {
43 private final DbClient dbClient;
44 private final MetricRepository metricRepository;
45 private final MeasureToMeasureDto measureToMeasureDto;
46 private final TreeRootHolder treeRootHolder;
47 private final MeasureRepository measureRepository;
49 public PersistMeasuresStep(DbClient dbClient, MetricRepository metricRepository, MeasureToMeasureDto measureToMeasureDto, TreeRootHolder treeRootHolder,
50 MeasureRepository measureRepository) {
51 this.dbClient = dbClient;
52 this.metricRepository = metricRepository;
53 this.measureToMeasureDto = measureToMeasureDto;
54 this.treeRootHolder = treeRootHolder;
55 this.measureRepository = measureRepository;
59 public String getDescription() {
60 return "Persist measures";
64 public void execute(ComputationStep.Context context) {
65 try (DbSession dbSession = dbClient.openSession(true)) {
66 MeasureVisitor visitor = new MeasureVisitor(dbSession);
67 new DepthTraversalTypeAwareCrawler(visitor).visit(treeRootHolder.getRoot());
69 context.getStatistics().add("inserts", visitor.inserts);
73 private class MeasureVisitor extends TypeAwareVisitorAdapter {
74 private final DbSession session;
75 private int inserts = 0;
77 private MeasureVisitor(DbSession session) {
78 super(CrawlerDepthLimit.LEAVES, PRE_ORDER);
79 this.session = session;
83 public void visitProject(Component project) {
84 persistMeasures(project);
88 public void visitView(Component view) {
89 persistMeasures(view);
93 public void visitSubView(Component subView) {
94 persistMeasures(subView);
98 public void visitProjectView(Component projectView) {
99 // measures of project copies are never read. No need to persist them.
102 private void persistMeasures(Component component) {
103 Map<String, Measure> measures = measureRepository.getRawMeasures(component);
104 MeasureDao measureDao = dbClient.measureDao();
106 for (Map.Entry<String, Measure> e : measures.entrySet()) {
107 Measure measure = e.getValue();
108 if (measure.isEmpty()) {
111 String metricKey = e.getKey();
112 Metric metric = metricRepository.getByKey(metricKey);
113 if (!metric.isDeleteHistoricalData()) {
114 MeasureDto measureDto = measureToMeasureDto.toMeasureDto(measure, metric, component);
115 measureDao.insert(session, measureDto);