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.

SearchTemplatesData.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.template;
  21. import com.google.common.collect.Table;
  22. import java.util.List;
  23. import org.sonar.db.permission.template.PermissionTemplateDto;
  24. import org.sonar.server.common.permission.DefaultTemplatesResolver.ResolvedDefaultTemplates;
  25. import static com.google.common.base.MoreObjects.firstNonNull;
  26. import static com.google.common.base.Preconditions.checkState;
  27. import static com.google.common.collect.ImmutableList.copyOf;
  28. import static com.google.common.collect.ImmutableTable.copyOf;
  29. class SearchTemplatesData {
  30. private final List<PermissionTemplateDto> templates;
  31. private final ResolvedDefaultTemplates defaultTemplates;
  32. private final Table<String, String, Integer> userCountByTemplateUuidAndPermission;
  33. private final Table<String, String, Integer> groupCountByTemplateUuidAndPermission;
  34. private final Table<String, String, Boolean> withProjectCreatorByTemplateUuidAndPermission;
  35. private SearchTemplatesData(Builder builder) {
  36. this.templates = copyOf(builder.templates);
  37. this.defaultTemplates = builder.defaultTemplates;
  38. this.userCountByTemplateUuidAndPermission = copyOf(builder.userCountByTemplateUuidAndPermission);
  39. this.groupCountByTemplateUuidAndPermission = copyOf(builder.groupCountByTemplateUuidAndPermission);
  40. this.withProjectCreatorByTemplateUuidAndPermission = copyOf(builder.withProjectCreatorByTemplateUuidAndPermission);
  41. }
  42. public static Builder builder() {
  43. return new Builder();
  44. }
  45. public List<PermissionTemplateDto> templates() {
  46. return templates;
  47. }
  48. public ResolvedDefaultTemplates defaultTemplates() {
  49. return defaultTemplates;
  50. }
  51. public int userCount(String templateUuid, String permission) {
  52. return firstNonNull(userCountByTemplateUuidAndPermission.get(templateUuid, permission), 0);
  53. }
  54. public int groupCount(String templateUuid, String permission) {
  55. return firstNonNull(groupCountByTemplateUuidAndPermission.get(templateUuid, permission), 0);
  56. }
  57. public boolean withProjectCreator(String templateUuid, String permission) {
  58. return firstNonNull(withProjectCreatorByTemplateUuidAndPermission.get(templateUuid, permission), false);
  59. }
  60. public static class Builder {
  61. private List<PermissionTemplateDto> templates;
  62. private ResolvedDefaultTemplates defaultTemplates;
  63. private Table<String, String, Integer> userCountByTemplateUuidAndPermission;
  64. private Table<String, String, Integer> groupCountByTemplateUuidAndPermission;
  65. private Table<String, String, Boolean> withProjectCreatorByTemplateUuidAndPermission;
  66. private Builder() {
  67. // prevents instantiation outside main class
  68. }
  69. public SearchTemplatesData build() {
  70. checkState(templates != null);
  71. checkState(defaultTemplates != null);
  72. checkState(userCountByTemplateUuidAndPermission != null);
  73. checkState(groupCountByTemplateUuidAndPermission != null);
  74. checkState(withProjectCreatorByTemplateUuidAndPermission != null);
  75. return new SearchTemplatesData(this);
  76. }
  77. public Builder templates(List<PermissionTemplateDto> templates) {
  78. this.templates = templates;
  79. return this;
  80. }
  81. public Builder defaultTemplates(ResolvedDefaultTemplates defaultTemplates) {
  82. this.defaultTemplates = defaultTemplates;
  83. return this;
  84. }
  85. public Builder userCountByTemplateUuidAndPermission(Table<String, String, Integer> userCountByTemplateUuidAndPermission) {
  86. this.userCountByTemplateUuidAndPermission = userCountByTemplateUuidAndPermission;
  87. return this;
  88. }
  89. public Builder groupCountByTemplateUuidAndPermission(Table<String, String, Integer> groupCountByTemplateUuidAndPermission) {
  90. this.groupCountByTemplateUuidAndPermission = groupCountByTemplateUuidAndPermission;
  91. return this;
  92. }
  93. public Builder withProjectCreatorByTemplateUuidAndPermission(Table<String, String, Boolean> withProjectCreatorByTemplateUuidAndPermission) {
  94. this.withProjectCreatorByTemplateUuidAndPermission = withProjectCreatorByTemplateUuidAndPermission;
  95. return this;
  96. }
  97. }
  98. }