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.

OrganizationDao.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.db.organization;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Optional;
  24. import java.util.Set;
  25. import org.sonar.api.utils.System2;
  26. import org.sonar.db.Dao;
  27. import org.sonar.db.DbSession;
  28. import org.sonar.db.Pagination;
  29. import org.sonar.db.component.BranchType;
  30. import org.sonar.db.permission.template.DefaultTemplates;
  31. import org.sonar.db.property.InternalPropertiesDao;
  32. import org.sonar.db.user.GroupDto;
  33. import static com.google.common.base.Preconditions.checkState;
  34. import static java.util.Objects.requireNonNull;
  35. import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
  36. import static org.sonar.db.DatabaseUtils.executeLargeInputs;
  37. import static org.sonar.db.DatabaseUtils.executeLargeUpdates;
  38. public class OrganizationDao implements Dao {
  39. /**
  40. * The UUID of the default organization.
  41. * Can't be null unless SQ is strongly corrupted.
  42. */
  43. public static final String DEFAULT_ORGANIZATION = "organization.default";
  44. private final System2 system2;
  45. private final InternalPropertiesDao internalPropertiesDao;
  46. public OrganizationDao(System2 system2, InternalPropertiesDao internalPropertiesDao) {
  47. this.system2 = system2;
  48. this.internalPropertiesDao = internalPropertiesDao;
  49. }
  50. public void insert(DbSession dbSession, OrganizationDto organization, boolean newProjectPrivate) {
  51. checkDto(organization);
  52. long now = system2.now();
  53. organization.setCreatedAt(now);
  54. organization.setUpdatedAt(now);
  55. getMapper(dbSession).insert(organization, newProjectPrivate);
  56. }
  57. // TODO remove after getting rid of organization code
  58. public OrganizationDto getDefaultOrganization(DbSession dbSession) {
  59. Optional<String> uuid = internalPropertiesDao.selectByKey(dbSession, DEFAULT_ORGANIZATION);
  60. checkState(uuid.isPresent() && !uuid.get().isEmpty(), "No Default organization uuid configured");
  61. return getMapper(dbSession).selectByUuid(uuid.get());
  62. }
  63. public int countByQuery(DbSession dbSession, OrganizationQuery organizationQuery) {
  64. requireNonNull(organizationQuery, "organizationQuery can't be null");
  65. return getMapper(dbSession).countByQuery(organizationQuery);
  66. }
  67. public List<OrganizationDto> selectByQuery(DbSession dbSession, OrganizationQuery organizationQuery, Pagination pagination) {
  68. requireNonNull(organizationQuery, "organizationQuery can't be null");
  69. return getMapper(dbSession).selectByQuery(organizationQuery, pagination);
  70. }
  71. public Optional<OrganizationDto> selectByUuid(DbSession dbSession, String uuid) {
  72. checkUuid(uuid);
  73. return Optional.ofNullable(getMapper(dbSession).selectByUuid(uuid));
  74. }
  75. public Optional<OrganizationDto> selectByKey(DbSession dbSession, String key) {
  76. requireNonNull(key, "key can't be null");
  77. return Optional.ofNullable(getMapper(dbSession).selectByKey(key));
  78. }
  79. public List<OrganizationDto> selectByUuids(DbSession dbSession, Set<String> organizationUuids) {
  80. return executeLargeInputs(organizationUuids, getMapper(dbSession)::selectByUuids);
  81. }
  82. public List<OrganizationDto> selectByPermission(DbSession dbSession, String userUuid, String permission) {
  83. return getMapper(dbSession).selectByPermission(userUuid, permission);
  84. }
  85. public List<String> selectAllUuids(DbSession dbSession) {
  86. return getMapper(dbSession).selectAllUuids();
  87. }
  88. /**
  89. * Retrieve the default template of the specified organization if:
  90. * <ol>
  91. * <li>the specified organization exists</li>
  92. * <li>the project default permission template is defined</li>
  93. * </ol>
  94. */
  95. public Optional<DefaultTemplates> getDefaultTemplates(DbSession dbSession, String organizationUuid) {
  96. checkUuid(organizationUuid);
  97. return Optional.ofNullable(getMapper(dbSession).selectDefaultTemplatesByUuid(organizationUuid));
  98. }
  99. public void setDefaultTemplates(DbSession dbSession, String uuid, DefaultTemplates defaultTemplates) {
  100. checkUuid(uuid);
  101. checkDefaultTemplates(defaultTemplates);
  102. long now = system2.now();
  103. getMapper(dbSession).updateDefaultTemplates(uuid, defaultTemplates, now);
  104. }
  105. public Optional<String> getDefaultGroupUuid(DbSession dbSession, String organizationUuid) {
  106. checkUuid(organizationUuid);
  107. return Optional.ofNullable(getMapper(dbSession).selectDefaultGroupUuidByUuid(organizationUuid));
  108. }
  109. public void setDefaultGroupUuid(DbSession dbSession, String uuid, GroupDto defaultGroup) {
  110. checkUuid(uuid);
  111. String defaultGroupUuid = requireNonNull(defaultGroup, "Default group cannot be null").getUuid();
  112. getMapper(dbSession).updateDefaultGroupUuid(uuid, requireNonNull(defaultGroupUuid, "Default group uuid cannot be null"), system2.now());
  113. }
  114. public int update(DbSession dbSession, OrganizationDto organization) {
  115. checkDto(organization);
  116. organization.setUpdatedAt(system2.now());
  117. return getMapper(dbSession).update(organization);
  118. }
  119. public int deleteByUuid(DbSession dbSession, String uuid) {
  120. return getMapper(dbSession).deleteByUuid(uuid);
  121. }
  122. public List<OrganizationWithNclocDto> selectOrganizationsWithNcloc(DbSession dbSession, List<String> organizationUuids) {
  123. List<OrganizationWithNclocDto> result = new ArrayList<>();
  124. executeLargeUpdates(organizationUuids, chunk -> result.addAll(getMapper(dbSession).selectOrganizationsWithNcloc(NCLOC_KEY, chunk, BranchType.BRANCH)));
  125. return result;
  126. }
  127. private static void checkDto(OrganizationDto organization) {
  128. requireNonNull(organization, "OrganizationDto can't be null");
  129. }
  130. private static OrganizationMapper getMapper(DbSession dbSession) {
  131. return dbSession.getMapper(OrganizationMapper.class);
  132. }
  133. private static void checkUuid(String uuid) {
  134. requireNonNull(uuid, "uuid can't be null");
  135. }
  136. private static void checkDefaultTemplates(DefaultTemplates defaultTemplates) {
  137. requireNonNull(defaultTemplates, "defaultTemplates can't be null");
  138. requireNonNull(defaultTemplates.getProjectUuid());
  139. }
  140. }