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 com.google.common.annotations.VisibleForTesting;
23 import java.util.List;
24 import org.apache.commons.lang.math.NumberUtils;
25 import org.sonar.db.DbClient;
26 import org.sonar.db.DbSession;
27 import org.sonar.db.measure.custom.CustomMeasureDto;
28 import org.sonar.server.computation.task.projectanalysis.component.Component;
29 import org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor;
30 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
31 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
32 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
33 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
34 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
35 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepository;
36 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
37 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
38 import org.sonar.server.computation.task.step.ComputationStep;
40 public class CustomMeasuresCopyStep implements ComputationStep {
42 private final TreeRootHolder treeRootHolder;
43 private final DbClient dbClient;
44 private final MetricRepository metricRepository;
45 private final MeasureRepository measureRepository;
47 public CustomMeasuresCopyStep(TreeRootHolder treeRootHolder, DbClient dbClient,
48 MetricRepository metricRepository, MeasureRepository measureRepository) {
49 this.treeRootHolder = treeRootHolder;
50 this.dbClient = dbClient;
51 this.metricRepository = metricRepository;
52 this.measureRepository = measureRepository;
56 public void execute() {
57 new DepthTraversalTypeAwareCrawler(
58 new TypeAwareVisitorAdapter(CrawlerDepthLimit.LEAVES, ComponentVisitor.Order.PRE_ORDER) {
60 public void visitAny(Component component) {
63 }).visit(treeRootHolder.getRoot());
66 private void copy(Component component) {
67 for (CustomMeasureDto dto : loadCustomMeasures(component)) {
68 Metric metric = metricRepository.getById(dto.getMetricId());
69 // else metric is not found and an exception is raised
70 Measure measure = dtoToMeasure(dto, metric);
71 measureRepository.add(component, metric, measure);
75 private List<CustomMeasureDto> loadCustomMeasures(Component component) {
76 try (DbSession session = dbClient.openSession(false)) {
77 return dbClient.customMeasureDao().selectByComponentUuid(session, component.getUuid());
82 static Measure dtoToMeasure(CustomMeasureDto dto, Metric metric) {
83 switch (metric.getType()) {
86 return Measure.newMeasureBuilder().create((int) dto.getValue());
89 return Measure.newMeasureBuilder().create((long) dto.getValue());
92 return Measure.newMeasureBuilder().create(dto.getValue(), metric.getDecimalScale());
94 return Measure.newMeasureBuilder().create(NumberUtils.compare(dto.getValue(), 1.0) == 0);
96 return Measure.newMeasureBuilder().create(Measure.Level.valueOf(dto.getTextValue()));
100 return Measure.newMeasureBuilder().create(dto.getTextValue());
102 throw new IllegalArgumentException(String.format("Custom measures do not support the metric type [%s]", metric.getType()));
107 public String getDescription() {
108 return "Copy custom measures";