3 * Copyright (C) 2009-2023 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.measure.ws;
23 import javax.annotation.Nullable;
24 import org.sonar.db.component.ComponentDto;
25 import org.sonar.db.measure.LiveMeasureDto;
26 import org.sonar.db.metric.MetricDto;
27 import org.sonarqube.ws.Measures;
28 import org.sonarqube.ws.Measures.Component;
30 import static java.util.Optional.ofNullable;
32 class ComponentDtoToWsComponent {
33 private ComponentDtoToWsComponent() {
34 // static methods only
37 static Component.Builder componentDtoToWsComponent(ComponentDto component, Map<MetricDto, LiveMeasureDto> measuresByMetric,
38 Map<String, ComponentDto> referenceComponentsByUuid, @Nullable String branch,
39 @Nullable String pullRequest) {
40 Component.Builder wsComponent = componentDtoToWsComponent(component, branch, pullRequest);
42 ComponentDto referenceComponent = referenceComponentsByUuid.get(component.getCopyComponentUuid());
43 if (referenceComponent != null) {
44 wsComponent.setRefKey(referenceComponent.getKey());
47 Measures.Measure.Builder measureBuilder = Measures.Measure.newBuilder();
48 for (Map.Entry<MetricDto, LiveMeasureDto> entry : measuresByMetric.entrySet()) {
49 MeasureDtoToWsMeasure.updateMeasureBuilder(measureBuilder, entry.getKey(), entry.getValue());
50 wsComponent.addMeasures(measureBuilder);
51 measureBuilder.clear();
57 static Component.Builder componentDtoToWsComponent(ComponentDto component, @Nullable String branch, @Nullable String pullRequest) {
58 Component.Builder wsComponent = Component.newBuilder()
59 .setKey(ComponentDto.removeBranchAndPullRequestFromKey(component.getKey()))
60 .setName(component.name())
61 .setQualifier(component.qualifier());
62 ofNullable(branch).ifPresent(wsComponent::setBranch);
63 ofNullable(pullRequest).ifPresent(wsComponent::setPullRequest);
64 ofNullable(component.path()).ifPresent(wsComponent::setPath);
65 ofNullable(component.description()).ifPresent(wsComponent::setDescription);
66 ofNullable(component.language()).ifPresent(wsComponent::setLanguage);