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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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 org.sonar.api.utils.DateUtils.formatDateTime;
  35. class SearchMyProjectsData {
  36. private final List<ComponentDto> projects;
  37. private final ListMultimap<String, ProjectLinkDto> projectLinksByProjectUuid;
  38. private final Map<String, String> lastAnalysisDates;
  39. private final Map<String, String> qualityGateStatuses;
  40. private final int totalNbOfProject;
  41. private SearchMyProjectsData(Builder builder) {
  42. this.projects = copyOf(builder.projects);
  43. this.projectLinksByProjectUuid = buildProjectLinks(builder.projectLinks);
  44. this.lastAnalysisDates = buildAnalysisDates(builder.snapshots);
  45. this.qualityGateStatuses = buildQualityGateStatuses(builder.qualityGates);
  46. this.totalNbOfProject = builder.totalNbOfProjects;
  47. }
  48. static Builder builder() {
  49. return new Builder();
  50. }
  51. List<ComponentDto> projects() {
  52. return projects;
  53. }
  54. List<ProjectLinkDto> projectLinksFor(String projectUuid) {
  55. return projectLinksByProjectUuid.get(projectUuid);
  56. }
  57. Optional<String> lastAnalysisDateFor(String componentUuid) {
  58. return Optional.ofNullable(lastAnalysisDates.get(componentUuid));
  59. }
  60. Optional<String> qualityGateStatusFor(String componentUuid) {
  61. return Optional.ofNullable(qualityGateStatuses.get(componentUuid));
  62. }
  63. int totalNbOfProjects() {
  64. return totalNbOfProject;
  65. }
  66. private static ListMultimap<String, ProjectLinkDto> buildProjectLinks(List<ProjectLinkDto> dtos) {
  67. ImmutableListMultimap.Builder<String, ProjectLinkDto> projectLinks = ImmutableListMultimap.builder();
  68. dtos.forEach(projectLink -> projectLinks.put(projectLink.getProjectUuid(), projectLink));
  69. return projectLinks.build();
  70. }
  71. private static Map<String, String> buildAnalysisDates(List<SnapshotDto> snapshots) {
  72. return ImmutableMap.copyOf(snapshots.stream().collect(Collectors.toMap(
  73. SnapshotDto::getComponentUuid,
  74. snapshot -> formatDateTime(snapshot.getCreatedAt()))));
  75. }
  76. private static Map<String, String> buildQualityGateStatuses(List<LiveMeasureDto> measures) {
  77. return ImmutableMap.copyOf(measures.stream()
  78. .collect(Collectors.toMap(LiveMeasureDto::getComponentUuid, LiveMeasureDto::getDataAsString)));
  79. }
  80. static class Builder {
  81. private List<ComponentDto> projects;
  82. private List<ProjectLinkDto> projectLinks;
  83. private List<SnapshotDto> snapshots;
  84. private List<LiveMeasureDto> qualityGates;
  85. private Integer totalNbOfProjects;
  86. private Builder() {
  87. // enforce method constructor
  88. }
  89. Builder setProjects(List<ComponentDto> projects) {
  90. this.projects = projects;
  91. return this;
  92. }
  93. public Builder setProjectLinks(List<ProjectLinkDto> projectLinks) {
  94. this.projectLinks = projectLinks;
  95. return this;
  96. }
  97. public Builder setSnapshots(List<SnapshotDto> snapshots) {
  98. this.snapshots = snapshots;
  99. return this;
  100. }
  101. public Builder setQualityGates(List<LiveMeasureDto> qGateStatuses) {
  102. this.qualityGates = qGateStatuses;
  103. return this;
  104. }
  105. public Builder setTotalNbOfProjects(Integer totalNbOfProjects) {
  106. this.totalNbOfProjects = totalNbOfProjects;
  107. return this;
  108. }
  109. SearchMyProjectsData build() {
  110. requireNonNull(projects);
  111. requireNonNull(projectLinks);
  112. requireNonNull(snapshots);
  113. requireNonNull(qualityGates);
  114. requireNonNull(totalNbOfProjects);
  115. return new SearchMyProjectsData(this);
  116. }
  117. }
  118. }