Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ProjectAlmSettingDao.java 4.8KB

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