3 * Copyright (C) 2009-2021 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.getCopyResourceUuid());
41 if (referenceComponent != null) {
42 wsComponent.setRefId(referenceComponent.uuid());
43 wsComponent.setRefKey(referenceComponent.getDbKey());
46 Measures.Measure.Builder measureBuilder = Measures.Measure.newBuilder();
47 for (Map.Entry<MetricDto, LiveMeasureDto> entry : measuresByMetric.entrySet()) {
48 MeasureDtoToWsMeasure.updateMeasureBuilder(measureBuilder, entry.getKey(), entry.getValue());
49 wsComponent.addMeasures(measureBuilder);
50 measureBuilder.clear();
56 static Component.Builder componentDtoToWsComponent(ComponentDto component) {
57 Component.Builder wsComponent = Component.newBuilder()
58 .setId(component.uuid())
59 .setKey(component.getKey())
60 .setName(component.name())
61 .setQualifier(component.qualifier());
62 ofNullable(component.getBranch()).ifPresent(wsComponent::setBranch);
63 ofNullable(component.getPullRequest()).ifPresent(wsComponent::setPullRequest);
64 ofNullable(component.path()).ifPresent(wsComponent::setPath);
65 ofNullable(component.description()).ifPresent(wsComponent::setDescription);
66 ofNullable(component.language()).ifPresent(wsComponent::setLanguage);