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.

ProjectAlmSettingDao.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.alm.setting;
  21. import java.util.List;
  22. import java.util.Locale;
  23. import java.util.Optional;
  24. import java.util.Set;
  25. import org.sonar.api.utils.System2;
  26. import org.sonar.core.util.UuidFactory;
  27. import org.sonar.db.Dao;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.audit.AuditPersister;
  30. import org.sonar.db.audit.model.DevOpsPlatformSettingNewValue;
  31. import org.sonar.db.project.ProjectDto;
  32. import static org.sonar.db.DatabaseUtils.executeLargeInputs;
  33. public class ProjectAlmSettingDao implements Dao {
  34. private final System2 system2;
  35. private final UuidFactory uuidFactory;
  36. private final AuditPersister auditPersister;
  37. public ProjectAlmSettingDao(System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) {
  38. this.system2 = system2;
  39. this.uuidFactory = uuidFactory;
  40. this.auditPersister = auditPersister;
  41. }
  42. public void insertOrUpdate(DbSession dbSession, ProjectAlmSettingDto projectAlmSettingDto, String key, String projectName, String projectKey) {
  43. String uuid = uuidFactory.create();
  44. long now = system2.now();
  45. ProjectAlmSettingMapper mapper = getMapper(dbSession);
  46. boolean isUpdate = true;
  47. if (mapper.update(projectAlmSettingDto, now) == 0) {
  48. mapper.insert(projectAlmSettingDto, uuid, now);
  49. projectAlmSettingDto.setUuid(uuid);
  50. projectAlmSettingDto.setCreatedAt(now);
  51. isUpdate = false;
  52. }
  53. projectAlmSettingDto.setUpdatedAt(now);
  54. DevOpsPlatformSettingNewValue value = new DevOpsPlatformSettingNewValue(projectAlmSettingDto, key, projectName, projectKey);
  55. if (isUpdate) {
  56. auditPersister.updateDevOpsPlatformSetting(dbSession, value);
  57. } else {
  58. auditPersister.addDevOpsPlatformSetting(dbSession, value);
  59. }
  60. }
  61. public void deleteByProject(DbSession dbSession, ProjectDto project) {
  62. int deletedRows = getMapper(dbSession).deleteByProjectUuid(project.getUuid());
  63. if (deletedRows > 0) {
  64. auditPersister.deleteDevOpsPlatformSetting(dbSession, new DevOpsPlatformSettingNewValue(project));
  65. }
  66. }
  67. public void deleteByAlmSetting(DbSession dbSession, AlmSettingDto almSetting) {
  68. getMapper(dbSession).deleteByAlmSettingUuid(almSetting.getUuid());
  69. }
  70. public int countByAlmSetting(DbSession dbSession, AlmSettingDto almSetting) {
  71. return getMapper(dbSession).countByAlmSettingUuid(almSetting.getUuid());
  72. }
  73. public Optional<ProjectAlmSettingDto> selectByProject(DbSession dbSession, ProjectDto project) {
  74. return selectByProject(dbSession, project.getUuid());
  75. }
  76. public Optional<ProjectAlmSettingDto> selectByProject(DbSession dbSession, String projectUuid) {
  77. return Optional.ofNullable(getMapper(dbSession).selectByProjectUuid(projectUuid));
  78. }
  79. private static ProjectAlmSettingMapper getMapper(DbSession dbSession) {
  80. return dbSession.getMapper(ProjectAlmSettingMapper.class);
  81. }
  82. public List<ProjectAlmSettingDto> selectByAlmSettingAndSlugs(DbSession dbSession, AlmSettingDto almSettingDto, Set<String> almSlugs) {
  83. return executeLargeInputs(almSlugs, slugs -> getMapper(dbSession).selectByAlmSettingAndSlugs(almSettingDto.getUuid(), slugs));
  84. }
  85. public List<ProjectAlmSettingDto> selectByAlmSettingAndRepos(DbSession dbSession, AlmSettingDto almSettingDto, Set<String> almRepos) {
  86. return executeLargeInputs(almRepos, repos -> getMapper(dbSession).selectByAlmSettingAndRepos(almSettingDto.getUuid(), repos));
  87. }
  88. public List<ProjectAlmSettingDto> selectByAlm(DbSession dbSession, ALM alm) {
  89. return getMapper(dbSession).selectByAlm(alm.getId().toLowerCase(Locale.ROOT));
  90. }
  91. public List<ProjectAlmSettingDto> selectByProjectUuidsAndAlm(DbSession dbSession, Set<String> projectUuids, ALM alm) {
  92. return getMapper(dbSession).selectByProjectUuidsAndAlm(projectUuids, alm.getId().toLowerCase(Locale.ROOT));
  93. }
  94. public List<ProjectAlmKeyAndProject> selectAlmTypeAndUrlByProject(DbSession dbSession) {
  95. return getMapper(dbSession).selectAlmTypeAndUrlByProject();
  96. }
  97. }