3 * Copyright (C) 2009-2022 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 org.sonar.db.component.ComponentDto;
24 import org.sonar.db.measure.LiveMeasureDto;
25 import org.sonar.db.metric.MetricDto;
26 import org.sonarqube.ws.Measures;
27 import org.sonarqube.ws.Measures.Component;
29 import static java.util.Optional.ofNullable;
31 class ComponentDtoToWsComponent {
32 private ComponentDtoToWsComponent() {
33 // static methods only
36 static Component.Builder componentDtoToWsComponent(ComponentDto component, Map<MetricDto, LiveMeasureDto> measuresByMetric,
37 Map<String, ComponentDto> referenceComponentsByUuid) {
38 Component.Builder wsComponent = componentDtoToWsComponent(component);
40 ComponentDto referenceComponent = referenceComponentsByUuid.get(component.getCopyComponentUuid());
41 if (referenceComponent != null) {
42 wsComponent.setRefKey(referenceComponent.getKey());
45 Measures.Measure.Builder measureBuilder = Measures.Measure.newBuilder();
46 for (Map.Entry<MetricDto, LiveMeasureDto> entry : measuresByMetric.entrySet()) {
47 MeasureDtoToWsMeasure.updateMeasureBuilder(measureBuilder, entry.getKey(), entry.getValue());
48 wsComponent.addMeasures(measureBuilder);
49 measureBuilder.clear();
55 static Component.Builder componentDtoToWsComponent(ComponentDto component) {
56 Component.Builder wsComponent = Component.newBuilder()
57 .setKey(component.getKey())
58 .setName(component.name())
59 .setQualifier(component.qualifier());
60 ofNullable(component.getBranch()).ifPresent(wsComponent::setBranch);
61 ofNullable(component.getPullRequest()).ifPresent(wsComponent::setPullRequest);
62 ofNullable(component.path()).ifPresent(wsComponent::setPath);
63 ofNullable(component.description()).ifPresent(wsComponent::setDescription);
64 ofNullable(component.language()).ifPresent(wsComponent::setLanguage);