3 * Copyright (C) 2009-2020 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.component.ws;
22 import com.google.common.collect.Maps;
23 import java.util.Collections;
24 import java.util.List;
26 import javax.annotation.CheckForNull;
27 import javax.annotation.Nullable;
28 import org.apache.commons.lang.BooleanUtils;
29 import org.sonar.api.measures.Metric;
30 import org.sonar.api.utils.text.JsonWriter;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.component.ComponentDto;
34 import org.sonar.db.measure.LiveMeasureDto;
35 import org.sonar.db.metric.MetricDto;
36 import org.sonar.db.property.PropertyDto;
37 import org.sonar.db.property.PropertyQuery;
38 import org.sonar.server.user.UserSession;
40 import static java.util.Arrays.asList;
41 import static java.util.Collections.unmodifiableList;
42 import static org.sonar.api.measures.CoreMetrics.COVERAGE;
43 import static org.sonar.api.measures.CoreMetrics.COVERAGE_KEY;
44 import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY;
45 import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY_KEY;
46 import static org.sonar.api.measures.CoreMetrics.LINES;
47 import static org.sonar.api.measures.CoreMetrics.LINES_KEY;
48 import static org.sonar.api.measures.CoreMetrics.TESTS;
49 import static org.sonar.api.measures.CoreMetrics.TESTS_KEY;
50 import static org.sonar.api.measures.CoreMetrics.VIOLATIONS;
51 import static org.sonar.api.measures.CoreMetrics.VIOLATIONS_KEY;
53 public class ComponentViewerJsonWriter {
54 private static final List<String> METRIC_KEYS = unmodifiableList(asList(
58 DUPLICATED_LINES_DENSITY_KEY,
61 private final DbClient dbClient;
63 public ComponentViewerJsonWriter(DbClient dbClient) {
64 this.dbClient = dbClient;
67 public void writeComponentWithoutFav(JsonWriter json, ComponentDto component, DbSession session, boolean includeSubProject) {
68 json.prop("key", component.getKey());
69 json.prop("uuid", component.uuid());
70 json.prop("path", component.path());
71 json.prop("name", component.name());
72 json.prop("longName", component.longName());
73 json.prop("q", component.qualifier());
75 ComponentDto project = dbClient.componentDao().selectOrFailByUuid(session, component.projectUuid());
77 if (includeSubProject) {
78 ComponentDto parentModule = retrieveParentModuleIfNotCurrentComponent(component, session);
80 // Do not display parent module if parent module and project are the same
81 boolean displayParentModule = parentModule != null && !parentModule.uuid().equals(project.uuid());
82 json.prop("subProject", displayParentModule ? parentModule.getKey() : null);
83 json.prop("subProjectName", displayParentModule ? parentModule.longName() : null);
85 json.prop("project", project.getKey());
86 json.prop("projectName", project.longName());
87 String branch = project.getBranch();
89 json.prop("branch", branch);
91 String pullRequest = project.getPullRequest();
92 if (pullRequest != null) {
93 json.prop("pullRequest", pullRequest);
97 public void writeComponent(JsonWriter json, ComponentDto component, UserSession userSession, DbSession session) {
98 writeComponentWithoutFav(json, component, session, true);
100 List<PropertyDto> propertyDtos = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
102 .setComponentUuid(component.uuid())
103 .setUserUuid(userSession.getUuid())
106 boolean isFavourite = propertyDtos.size() == 1;
107 json.prop("fav", isFavourite);
110 public void writeMeasures(JsonWriter json, ComponentDto component, DbSession session) {
111 Map<String, LiveMeasureDto> measuresByMetricKey = loadMeasuresGroupedByMetricKey(component, session);
113 json.name("measures").beginObject();
114 json.prop("lines", formatMeasure(measuresByMetricKey, LINES));
115 json.prop("coverage", formatMeasure(measuresByMetricKey, COVERAGE));
116 json.prop("duplicationDensity", formatMeasure(measuresByMetricKey, DUPLICATED_LINES_DENSITY));
117 json.prop("issues", formatMeasure(measuresByMetricKey, VIOLATIONS));
118 json.prop("tests", formatMeasure(measuresByMetricKey, TESTS));
122 private Map<String, LiveMeasureDto> loadMeasuresGroupedByMetricKey(ComponentDto component, DbSession dbSession) {
123 List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, METRIC_KEYS);
124 Map<String, MetricDto> metricsByUuid = Maps.uniqueIndex(metrics, MetricDto::getUuid);
125 List<LiveMeasureDto> measures = dbClient.liveMeasureDao()
126 .selectByComponentUuidsAndMetricUuids(dbSession, Collections.singletonList(component.uuid()), metricsByUuid.keySet());
127 return Maps.uniqueIndex(measures, m -> metricsByUuid.get(m.getMetricUuid()).getKey());
131 private static String formatMeasure(Map<String, LiveMeasureDto> measuresByMetricKey, Metric metric) {
132 LiveMeasureDto measure = measuresByMetricKey.get(metric.getKey());
133 return formatMeasure(measure, metric);
136 private static String formatMeasure(@Nullable LiveMeasureDto measure, Metric metric) {
137 if (measure == null) {
140 Double value = getDoubleValue(measure, metric);
142 return Double.toString(value);
148 private static Double getDoubleValue(LiveMeasureDto measure, Metric metric) {
149 Double value = measure.getValue();
150 if (BooleanUtils.isTrue(metric.isOptimizedBestValue()) && value == null) {
151 value = metric.getBestValue();
157 private ComponentDto retrieveParentModuleIfNotCurrentComponent(ComponentDto componentDto, DbSession session) {
158 final String moduleUuid = componentDto.moduleUuid();
159 if (moduleUuid == null || componentDto.uuid().equals(moduleUuid)) {
162 return dbClient.componentDao().selectOrFailByUuid(session, moduleUuid);