You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SearchMyProjectsData.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.server.project.ws;
  21. import com.google.common.collect.ImmutableListMultimap;
  22. import com.google.common.collect.ImmutableMap;
  23. import com.google.common.collect.ListMultimap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Optional;
  27. import java.util.stream.Collectors;
  28. import org.sonar.db.component.ComponentDto;
  29. import org.sonar.db.component.ProjectLinkDto;
  30. import org.sonar.db.component.SnapshotDto;
  31. import org.sonar.db.measure.LiveMeasureDto;
  32. import static com.google.common.collect.ImmutableList.copyOf;
  33. import static java.util.Objects.requireNonNull;
  34. import static java.util.function.Function.identity;
  35. import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
  36. class SearchMyProjectsData {
  37. private final List<ComponentDto> projects;
  38. private final ListMultimap<String, ProjectLinkDto> projectLinksByProjectUuid;
  39. private final Map<String, SnapshotDto> snapshotsByComponentUuid;
  40. private final Map<String, String> qualityGateStatuses;
  41. private final int totalNbOfProject;
  42. private SearchMyProjectsData(Builder builder) {
  43. this.projects = copyOf(builder.projects);
  44. this.projectLinksByProjectUuid = buildProjectLinks(builder.projectLinks);
  45. this.snapshotsByComponentUuid =builder.snapshots.stream().collect(uniqueIndex(SnapshotDto::getComponentUuid, identity()));
  46. this.qualityGateStatuses = buildQualityGateStatuses(builder.qualityGates);
  47. this.totalNbOfProject = builder.totalNbOfProjects;
  48. }
  49. static Builder builder() {
  50. return new Builder();
  51. }
  52. List<ComponentDto> projects() {
  53. return projects;
  54. }
  55. List<ProjectLinkDto> projectLinksFor(String projectUuid) {
  56. return projectLinksByProjectUuid.get(projectUuid);
  57. }
  58. Optional<SnapshotDto> lastSnapshot(String componentUuid) {
  59. return Optional.ofNullable(snapshotsByComponentUuid.get(componentUuid));
  60. }
  61. Optional<String> qualityGateStatusFor(String componentUuid) {
  62. return Optional.ofNullable(qualityGateStatuses.get(componentUuid));
  63. }
  64. int totalNbOfProjects() {
  65. return totalNbOfProject;
  66. }
  67. private static ListMultimap<String, ProjectLinkDto> buildProjectLinks(List<ProjectLinkDto> dtos) {
  68. ImmutableListMultimap.Builder<String, ProjectLinkDto> projectLinks = ImmutableListMultimap.builder();
  69. dtos.forEach(projectLink -> projectLinks.put(projectLink.getProjectUuid(), projectLink));
  70. return projectLinks.build();
  71. }
  72. private static Map<String, String> buildQualityGateStatuses(List<LiveMeasureDto> measures) {
  73. return ImmutableMap.copyOf(measures.stream()
  74. .collect(Collectors.toMap(LiveMeasureDto::getComponentUuid, LiveMeasureDto::getDataAsString)));
  75. }
  76. static class Builder {
  77. private List<ComponentDto> projects;
  78. private List<ProjectLinkDto> projectLinks;
  79. private List<SnapshotDto> snapshots;
  80. private List<LiveMeasureDto> qualityGates;
  81. private Integer totalNbOfProjects;
  82. private Builder() {
  83. // enforce method constructor
  84. }
  85. Builder setProjects(List<ComponentDto> projects) {
  86. this.projects = projects;
  87. return this;
  88. }
  89. public Builder setProjectLinks(List<ProjectLinkDto> projectLinks) {
  90. this.projectLinks = projectLinks;
  91. return this;
  92. }
  93. public Builder setSnapshots(List<SnapshotDto> snapshots) {
  94. this.snapshots = snapshots;
  95. return this;
  96. }
  97. public Builder setQualityGates(List<LiveMeasureDto> qGateStatuses) {
  98. this.qualityGates = qGateStatuses;
  99. return this;
  100. }
  101. public Builder setTotalNbOfProjects(Integer totalNbOfProjects) {
  102. this.totalNbOfProjects = totalNbOfProjects;
  103. return this;
  104. }
  105. SearchMyProjectsData build() {
  106. requireNonNull(projects);
  107. requireNonNull(projectLinks);
  108. requireNonNull(snapshots);
  109. requireNonNull(qualityGates);
  110. requireNonNull(totalNbOfProjects);
  111. return new SearchMyProjectsData(this);
  112. }
  113. }
  114. }