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.

QProfileEditGroupsDao.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.qualityprofile;
  21. import java.util.Collection;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import java.util.stream.Collectors;
  25. import org.sonar.api.utils.System2;
  26. import org.sonar.db.Dao;
  27. import org.sonar.db.DatabaseUtils;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.Pagination;
  30. import org.sonar.db.audit.AuditPersister;
  31. import org.sonar.db.audit.model.GroupEditorNewValue;
  32. import org.sonar.db.user.GroupDto;
  33. import org.sonar.db.user.SearchGroupMembershipDto;
  34. import static org.sonar.db.DatabaseUtils.executeLargeInputs;
  35. import static org.sonar.db.DatabaseUtils.executeLargeUpdates;
  36. public class QProfileEditGroupsDao implements Dao {
  37. private final System2 system2;
  38. private final AuditPersister auditPersister;
  39. public QProfileEditGroupsDao(System2 system2, AuditPersister auditPersister) {
  40. this.system2 = system2;
  41. this.auditPersister = auditPersister;
  42. }
  43. public boolean exists(DbSession dbSession, QProfileDto profile, GroupDto group) {
  44. return exists(dbSession, profile, Collections.singletonList(group));
  45. }
  46. public boolean exists(DbSession dbSession, QProfileDto profile, Collection<GroupDto> groups) {
  47. return !executeLargeInputs(groups.stream().map(GroupDto::getUuid).toList(), partition -> mapper(dbSession).selectByQProfileAndGroups(profile.getKee(), partition))
  48. .isEmpty();
  49. }
  50. public int countByQuery(DbSession dbSession, SearchQualityProfilePermissionQuery query) {
  51. return mapper(dbSession).countByQuery(query);
  52. }
  53. public List<SearchGroupMembershipDto> selectByQuery(DbSession dbSession, SearchQualityProfilePermissionQuery query, Pagination pagination) {
  54. return mapper(dbSession).selectByQuery(query, pagination);
  55. }
  56. public List<String> selectQProfileUuidsByGroups(DbSession dbSession, Collection<GroupDto> groups) {
  57. return DatabaseUtils.executeLargeInputs(groups.stream().map(GroupDto::getUuid).toList(),
  58. g -> mapper(dbSession).selectQProfileUuidsByGroups(g));
  59. }
  60. public void insert(DbSession dbSession, QProfileEditGroupsDto dto, String qualityProfileName, String groupName) {
  61. mapper(dbSession).insert(dto, system2.now());
  62. auditPersister.addQualityProfileEditor(dbSession, new GroupEditorNewValue(dto, qualityProfileName, groupName));
  63. }
  64. public void deleteByQProfileAndGroup(DbSession dbSession, QProfileDto profile, GroupDto group) {
  65. int deletedRows = mapper(dbSession).delete(profile.getKee(), group.getUuid());
  66. if (deletedRows > 0) {
  67. auditPersister.deleteQualityProfileEditor(dbSession, new GroupEditorNewValue(profile, group));
  68. }
  69. }
  70. public void deleteByQProfiles(DbSession dbSession, List<QProfileDto> qProfiles) {
  71. executeLargeUpdates(qProfiles,
  72. partitionedProfiles ->
  73. {
  74. int deletedRows = mapper(dbSession).deleteByQProfiles(partitionedProfiles
  75. .stream()
  76. .map(QProfileDto::getKee)
  77. .toList());
  78. if (deletedRows > 0) {
  79. partitionedProfiles.forEach(p -> auditPersister.deleteQualityProfileEditor(dbSession, new GroupEditorNewValue(p)));
  80. }
  81. });
  82. }
  83. public void deleteByGroup(DbSession dbSession, GroupDto group) {
  84. int deletedRows = mapper(dbSession).deleteByGroup(group.getUuid());
  85. if (deletedRows > 0) {
  86. auditPersister.deleteQualityProfileEditor(dbSession, new GroupEditorNewValue(group));
  87. }
  88. }
  89. private static QProfileEditGroupsMapper mapper(DbSession dbSession) {
  90. return dbSession.getMapper(QProfileEditGroupsMapper.class);
  91. }
  92. }