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.measure;
22 import javax.annotation.CheckForNull;
23 import javax.annotation.Nonnull;
24 import org.sonar.db.measure.MeasureDto;
25 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
26 import org.sonar.server.computation.task.projectanalysis.component.Component;
27 import org.sonar.server.computation.task.projectanalysis.component.DbIdsRepository;
28 import org.sonar.server.computation.task.projectanalysis.component.Developer;
29 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
31 public class MeasureToMeasureDto {
33 private final DbIdsRepository dbIdsRepository;
34 private final AnalysisMetadataHolder analysisMetadataHolder;
36 public MeasureToMeasureDto(DbIdsRepository dbIdsRepository, AnalysisMetadataHolder analysisMetadataHolder) {
37 this.dbIdsRepository = dbIdsRepository;
38 this.analysisMetadataHolder = analysisMetadataHolder;
42 public MeasureDto toMeasureDto(Measure measure, Metric metric, Component component) {
43 MeasureDto out = new MeasureDto();
44 out.setMetricId(metric.getId());
45 out.setComponentUuid(component.getUuid());
46 out.setAnalysisUuid(analysisMetadataHolder.getUuid());
47 if (measure.hasVariations()) {
48 setVariation(out, measure.getVariations());
50 if (measure.hasQualityGateStatus()) {
51 setAlert(out, measure.getQualityGateStatus());
53 Developer developer = measure.getDeveloper();
54 if (developer != null) {
55 out.setDeveloperId(dbIdsRepository.getDeveloperId(developer));
57 out.setDescription(measure.getDescription());
58 out.setValue(valueAsDouble(measure));
59 out.setData(data(measure));
63 private static void setVariation(MeasureDto measureDto, MeasureVariations variations) {
64 measureDto.setVariation(variations.hasVariation1() ? variations.getVariation1() : null);
67 private static void setAlert(MeasureDto measureDto, QualityGateStatus qualityGateStatus) {
68 measureDto.setAlertStatus(qualityGateStatus.getStatus().name());
69 measureDto.setAlertText(qualityGateStatus.getText());
72 private static String data(Measure in) {
73 switch (in.getValueType()) {
81 return in.getStringValue();
83 return in.getLevelValue().name();
90 * return the numerical value as a double. It's the type used in db.
91 * Returns null if no numerical value found
94 private static Double valueAsDouble(Measure measure) {
95 switch (measure.getValueType()) {
97 return measure.getBooleanValue() ? 1.0d : 0.0d;
99 return (double) measure.getIntValue();
101 return (double) measure.getLongValue();
103 return measure.getDoubleValue();