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.

QProfileEditUsersDao.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.List;
  22. import java.util.stream.Collectors;
  23. import org.sonar.api.utils.System2;
  24. import org.sonar.db.Dao;
  25. import org.sonar.db.DbSession;
  26. import org.sonar.db.Pagination;
  27. import org.sonar.db.audit.AuditPersister;
  28. import org.sonar.db.audit.model.UserEditorNewValue;
  29. import org.sonar.db.user.SearchUserMembershipDto;
  30. import org.sonar.db.user.UserDto;
  31. import static org.sonar.db.DatabaseUtils.executeLargeUpdates;
  32. public class QProfileEditUsersDao implements Dao {
  33. private final System2 system2;
  34. private final AuditPersister auditPersister;
  35. public QProfileEditUsersDao(System2 system2, AuditPersister auditPersister) {
  36. this.system2 = system2;
  37. this.auditPersister = auditPersister;
  38. }
  39. public boolean exists(DbSession dbSession, QProfileDto profile, UserDto user) {
  40. return mapper(dbSession).selectByQProfileAndUser(profile.getKee(), user.getUuid()) != null;
  41. }
  42. public int countByQuery(DbSession dbSession, SearchQualityProfilePermissionQuery query) {
  43. return mapper(dbSession).countByQuery(query);
  44. }
  45. public List<SearchUserMembershipDto> selectByQuery(DbSession dbSession, SearchQualityProfilePermissionQuery query, Pagination pagination) {
  46. return mapper(dbSession).selectByQuery(query, pagination);
  47. }
  48. public List<String> selectQProfileUuidsByUser(DbSession dbSession, UserDto userDto) {
  49. return mapper(dbSession).selectQProfileUuidsByUser(userDto.getUuid());
  50. }
  51. public void insert(DbSession dbSession, QProfileEditUsersDto dto, String qualityProfileName, String userLogin) {
  52. mapper(dbSession).insert(dto, system2.now());
  53. auditPersister.addQualityProfileEditor(dbSession, new UserEditorNewValue(dto, qualityProfileName, userLogin));
  54. }
  55. public void deleteByQProfileAndUser(DbSession dbSession, QProfileDto profile, UserDto user) {
  56. int deletedRows = mapper(dbSession).delete(profile.getKee(), user.getUuid());
  57. if (deletedRows > 0) {
  58. auditPersister.deleteQualityProfileEditor(dbSession, new UserEditorNewValue(profile, user));
  59. }
  60. }
  61. public void deleteByQProfiles(DbSession dbSession, List<QProfileDto> qProfiles) {
  62. executeLargeUpdates(qProfiles,
  63. partitionedProfiles ->
  64. {
  65. int deletedRows = mapper(dbSession).deleteByQProfiles(partitionedProfiles
  66. .stream()
  67. .map(QProfileDto::getKee)
  68. .toList());
  69. if (deletedRows > 0) {
  70. partitionedProfiles.forEach(p -> auditPersister.deleteQualityProfileEditor(dbSession, new UserEditorNewValue(p)));
  71. }
  72. });
  73. }
  74. public void deleteByUser(DbSession dbSession, UserDto user) {
  75. int deletedRows = mapper(dbSession).deleteByUser(user.getUuid());
  76. if (deletedRows > 0) {
  77. auditPersister.deleteQualityProfileEditor(dbSession, new UserEditorNewValue(user));
  78. }
  79. }
  80. private static QProfileEditUsersMapper mapper(DbSession dbSession) {
  81. return dbSession.getMapper(QProfileEditUsersMapper.class);
  82. }
  83. }