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.

SearchProjectPermissionsData.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.permission.ws;
  21. import com.google.common.collect.FluentIterable;
  22. import com.google.common.collect.Iterables;
  23. import com.google.common.collect.Ordering;
  24. import com.google.common.collect.Table;
  25. import java.util.List;
  26. import java.util.Set;
  27. import org.sonar.api.utils.Paging;
  28. import org.sonar.db.component.ComponentDto;
  29. import static com.google.common.base.MoreObjects.firstNonNull;
  30. import static com.google.common.base.Preconditions.checkState;
  31. import static com.google.common.collect.ImmutableList.copyOf;
  32. import static com.google.common.collect.ImmutableTable.copyOf;
  33. class SearchProjectPermissionsData {
  34. private final List<ComponentDto> rootComponents;
  35. private final Paging paging;
  36. private final Table<String, String, Integer> userCountByProjectUuidAndPermission;
  37. private final Table<String, String, Integer> groupCountByProjectUuidAndPermission;
  38. private SearchProjectPermissionsData(Builder builder) {
  39. this.rootComponents = copyOf(builder.projects);
  40. this.paging = builder.paging;
  41. this.userCountByProjectUuidAndPermission = copyOf(builder.userCountByProjectUuidAndPermission);
  42. this.groupCountByProjectUuidAndPermission = copyOf(builder.groupCountByProjectUuidAndPermission);
  43. }
  44. static Builder newBuilder() {
  45. return new Builder();
  46. }
  47. List<ComponentDto> rootComponents() {
  48. return rootComponents;
  49. }
  50. Paging paging() {
  51. return paging;
  52. }
  53. int userCount(String rootComponentUuid, String permission) {
  54. return firstNonNull(userCountByProjectUuidAndPermission.get(rootComponentUuid, permission), 0);
  55. }
  56. int groupCount(String rootComponentUuid, String permission) {
  57. return firstNonNull(groupCountByProjectUuidAndPermission.get(rootComponentUuid, permission), 0);
  58. }
  59. Set<String> permissions(String rootComponentUuid) {
  60. return FluentIterable.from(
  61. Iterables.concat(
  62. userCountByProjectUuidAndPermission.row(rootComponentUuid).keySet(),
  63. groupCountByProjectUuidAndPermission.row(rootComponentUuid).keySet()))
  64. .toSortedSet(Ordering.natural());
  65. }
  66. static class Builder {
  67. private List<ComponentDto> projects;
  68. private Paging paging;
  69. private Table<String, String, Integer> userCountByProjectUuidAndPermission;
  70. private Table<String, String, Integer> groupCountByProjectUuidAndPermission;
  71. private Builder() {
  72. // prevents instantiation outside main class
  73. }
  74. SearchProjectPermissionsData build() {
  75. checkState(projects != null);
  76. checkState(userCountByProjectUuidAndPermission != null);
  77. checkState(groupCountByProjectUuidAndPermission != null);
  78. return new SearchProjectPermissionsData(this);
  79. }
  80. Builder rootComponents(List<ComponentDto> projects) {
  81. this.projects = projects;
  82. return this;
  83. }
  84. Builder paging(Paging paging) {
  85. this.paging = paging;
  86. return this;
  87. }
  88. Builder userCountByProjectIdAndPermission(Table<String, String, Integer> userCountByProjectIdAndPermission) {
  89. this.userCountByProjectUuidAndPermission = userCountByProjectIdAndPermission;
  90. return this;
  91. }
  92. Builder groupCountByProjectIdAndPermission(Table<String, String, Integer> groupCountByProjectIdAndPermission) {
  93. this.groupCountByProjectUuidAndPermission = groupCountByProjectIdAndPermission;
  94. return this;
  95. }
  96. }
  97. }