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.

ActiveRuleDao.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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 com.google.common.base.Preconditions;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Optional;
  26. import java.util.function.Consumer;
  27. import org.sonar.core.util.UuidFactory;
  28. import org.sonar.db.Dao;
  29. import org.sonar.db.DatabaseUtils;
  30. import org.sonar.db.DbSession;
  31. import org.sonar.db.organization.OrganizationDto;
  32. import org.sonar.db.rule.RuleParamDto;
  33. import static com.google.common.base.Preconditions.checkArgument;
  34. import static java.util.Collections.emptyList;
  35. import static org.sonar.db.DatabaseUtils.executeLargeInputs;
  36. import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput;
  37. import static org.sonar.db.KeyLongValue.toMap;
  38. public class ActiveRuleDao implements Dao {
  39. private static final String QUALITY_PROFILE_IS_NOT_PERSISTED = "Quality profile is not persisted (missing id)";
  40. private static final String RULE_IS_NOT_PERSISTED = "Rule is not persisted";
  41. private static final String RULE_PARAM_IS_NOT_PERSISTED = "Rule param is not persisted";
  42. private static final String ACTIVE_RULE_IS_NOT_PERSISTED = "ActiveRule is not persisted";
  43. private static final String ACTIVE_RULE_IS_ALREADY_PERSISTED = "ActiveRule is already persisted";
  44. private static final String ACTIVE_RULE_PARAM_IS_NOT_PERSISTED = "ActiveRuleParam is not persisted";
  45. private static final String ACTIVE_RULE_PARAM_IS_ALREADY_PERSISTED = "ActiveRuleParam is already persisted";
  46. private final UuidFactory uuidFactory;
  47. public ActiveRuleDao(UuidFactory uuidFactory) {
  48. this.uuidFactory = uuidFactory;
  49. }
  50. public Optional<ActiveRuleDto> selectByKey(DbSession dbSession, ActiveRuleKey key) {
  51. return Optional.ofNullable(mapper(dbSession).selectByKey(key.getRuleProfileUuid(), key.getRuleKey().repository(), key.getRuleKey().rule()));
  52. }
  53. public List<OrgActiveRuleDto> selectByRuleUuid(DbSession dbSession, OrganizationDto organization, String ruleUuid) {
  54. return mapper(dbSession).selectByRuleUuid(organization.getUuid(), ruleUuid);
  55. }
  56. public List<ActiveRuleDto> selectByRuleUuidOfAllOrganizations(DbSession dbSession, String ruleUuid) {
  57. return mapper(dbSession).selectByRuleUuidOfAllOrganizations(ruleUuid);
  58. }
  59. public List<OrgActiveRuleDto> selectByRuleUuids(DbSession dbSession, OrganizationDto organization, List<String> uuids) {
  60. return executeLargeInputs(uuids, chunk -> mapper(dbSession).selectByRuleUuids(organization.getUuid(), chunk));
  61. }
  62. /**
  63. * Active rule on removed rule are NOT returned
  64. */
  65. public List<OrgActiveRuleDto> selectByProfileUuid(DbSession dbSession, String uuid) {
  66. return mapper(dbSession).selectByProfileUuid(uuid);
  67. }
  68. public List<OrgActiveRuleDto> selectByTypeAndProfileUuids(DbSession dbSession, List<Integer> types, List<String> uuids) {
  69. return executeLargeInputs(uuids, chunk -> mapper(dbSession).selectByTypeAndProfileUuids(types, chunk));
  70. }
  71. public List<OrgActiveRuleDto> selectByProfile(DbSession dbSession, QProfileDto profile) {
  72. return selectByProfileUuid(dbSession, profile.getKee());
  73. }
  74. public List<ActiveRuleDto> selectByRuleProfile(DbSession dbSession, RulesProfileDto ruleProfileDto) {
  75. return mapper(dbSession).selectByRuleProfileUuid(ruleProfileDto.getUuid());
  76. }
  77. public Collection<ActiveRuleDto> selectByRulesAndRuleProfileUuids(DbSession dbSession, Collection<String> ruleUuids, Collection<String> ruleProfileUuids) {
  78. if (ruleUuids.isEmpty() || ruleProfileUuids.isEmpty()) {
  79. return emptyList();
  80. }
  81. ActiveRuleMapper mapper = mapper(dbSession);
  82. return executeLargeInputs(ruleUuids,
  83. ruleUuidsChunk -> executeLargeInputs(ruleProfileUuids, chunk -> mapper.selectByRuleUuidsAndRuleProfileUuids(ruleUuidsChunk, chunk)));
  84. }
  85. public ActiveRuleDto insert(DbSession dbSession, ActiveRuleDto item) {
  86. checkArgument(item.getProfileUuid() != null, QUALITY_PROFILE_IS_NOT_PERSISTED);
  87. checkArgument(item.getRuleUuid() != null, RULE_IS_NOT_PERSISTED);
  88. checkArgument(item.getUuid() == null, ACTIVE_RULE_IS_ALREADY_PERSISTED);
  89. item.setUuid(uuidFactory.create());
  90. mapper(dbSession).insert(item);
  91. return item;
  92. }
  93. public ActiveRuleDto update(DbSession dbSession, ActiveRuleDto item) {
  94. checkArgument(item.getProfileUuid() != null, QUALITY_PROFILE_IS_NOT_PERSISTED);
  95. checkArgument(item.getRuleUuid() != null, ActiveRuleDao.RULE_IS_NOT_PERSISTED);
  96. checkArgument(item.getUuid() != null, ACTIVE_RULE_IS_NOT_PERSISTED);
  97. mapper(dbSession).update(item);
  98. return item;
  99. }
  100. public Optional<ActiveRuleDto> delete(DbSession dbSession, ActiveRuleKey key) {
  101. Optional<ActiveRuleDto> activeRule = selectByKey(dbSession, key);
  102. if (activeRule.isPresent()) {
  103. mapper(dbSession).deleteParameters(activeRule.get().getUuid());
  104. mapper(dbSession).delete(activeRule.get().getUuid());
  105. }
  106. return activeRule;
  107. }
  108. public void deleteByRuleProfileUuids(DbSession dbSession, Collection<String> rulesProfileUuids) {
  109. ActiveRuleMapper mapper = mapper(dbSession);
  110. DatabaseUtils.executeLargeUpdates(rulesProfileUuids, mapper::deleteByRuleProfileUuids);
  111. }
  112. public void deleteByUuids(DbSession dbSession, List<String> activeRuleUuids) {
  113. ActiveRuleMapper mapper = mapper(dbSession);
  114. DatabaseUtils.executeLargeUpdates(activeRuleUuids, mapper::deleteByUuids);
  115. }
  116. public void deleteParametersByRuleProfileUuids(DbSession dbSession, Collection<String> rulesProfileUuids) {
  117. ActiveRuleMapper mapper = mapper(dbSession);
  118. DatabaseUtils.executeLargeUpdates(rulesProfileUuids, mapper::deleteParametersByRuleProfileUuids);
  119. }
  120. /**
  121. * Nested DTO ActiveRuleParams
  122. */
  123. public List<ActiveRuleParamDto> selectParamsByActiveRuleUuid(DbSession dbSession, String activeRuleUuid) {
  124. return mapper(dbSession).selectParamsByActiveRuleUuid(activeRuleUuid);
  125. }
  126. public List<ActiveRuleParamDto> selectParamsByActiveRuleUuids(final DbSession dbSession, List<String> activeRuleUuids) {
  127. return executeLargeInputs(activeRuleUuids, mapper(dbSession)::selectParamsByActiveRuleUuids);
  128. }
  129. public ActiveRuleParamDto insertParam(DbSession dbSession, ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam) {
  130. checkArgument(activeRule.getUuid() != null, ACTIVE_RULE_IS_NOT_PERSISTED);
  131. checkArgument(activeRuleParam.getUuid() == null, ACTIVE_RULE_PARAM_IS_ALREADY_PERSISTED);
  132. Preconditions.checkNotNull(activeRuleParam.getRulesParameterUuid(), RULE_PARAM_IS_NOT_PERSISTED);
  133. activeRuleParam.setActiveRuleUuid(activeRule.getUuid());
  134. activeRuleParam.setUuid(uuidFactory.create());
  135. mapper(dbSession).insertParameter(activeRuleParam);
  136. return activeRuleParam;
  137. }
  138. public void updateParam(DbSession dbSession, ActiveRuleParamDto activeRuleParam) {
  139. Preconditions.checkNotNull(activeRuleParam.getUuid(), ACTIVE_RULE_PARAM_IS_NOT_PERSISTED);
  140. mapper(dbSession).updateParameter(activeRuleParam);
  141. }
  142. public void deleteParam(DbSession dbSession, ActiveRuleParamDto activeRuleParam) {
  143. Preconditions.checkNotNull(activeRuleParam.getUuid(), ACTIVE_RULE_PARAM_IS_NOT_PERSISTED);
  144. deleteParamByUuid(dbSession, activeRuleParam.getUuid());
  145. }
  146. public void deleteParamByUuid(DbSession dbSession, String uuid) {
  147. mapper(dbSession).deleteParameter(uuid);
  148. }
  149. public void deleteParamsByRuleParamOfAllOrganizations(DbSession dbSession, RuleParamDto param) {
  150. List<ActiveRuleDto> activeRules = selectByRuleUuidOfAllOrganizations(dbSession, param.getRuleUuid());
  151. for (ActiveRuleDto activeRule : activeRules) {
  152. for (ActiveRuleParamDto activeParam : selectParamsByActiveRuleUuid(dbSession, activeRule.getUuid())) {
  153. if (activeParam.getKey().equals(param.getName())) {
  154. deleteParam(dbSession, activeParam);
  155. }
  156. }
  157. }
  158. }
  159. public void deleteParamsByActiveRuleUuids(DbSession dbSession, List<String> activeRuleUuids) {
  160. ActiveRuleMapper mapper = mapper(dbSession);
  161. DatabaseUtils.executeLargeUpdates(activeRuleUuids, mapper::deleteParamsByActiveRuleUuids);
  162. }
  163. public Map<String, Long> countActiveRulesByQuery(DbSession dbSession, ActiveRuleCountQuery query) {
  164. return toMap(executeLargeInputs(query.getProfileUuids(),
  165. partition -> mapper(dbSession).countActiveRulesByQuery(query.getOrganization().getUuid(), partition, query.getRuleStatus(), query.getInheritance())));
  166. }
  167. public void scrollAllForIndexing(DbSession dbSession, Consumer<IndexedActiveRuleDto> consumer) {
  168. mapper(dbSession).scrollAllForIndexing(context -> {
  169. IndexedActiveRuleDto dto = context.getResultObject();
  170. consumer.accept(dto);
  171. });
  172. }
  173. public void scrollByUuidsForIndexing(DbSession dbSession, Collection<String> uuids, Consumer<IndexedActiveRuleDto> consumer) {
  174. ActiveRuleMapper mapper = mapper(dbSession);
  175. executeLargeInputsWithoutOutput(uuids,
  176. pageOfIds -> mapper
  177. .scrollByUuidsForIndexing(pageOfIds, context -> {
  178. IndexedActiveRuleDto dto = context.getResultObject();
  179. consumer.accept(dto);
  180. }));
  181. }
  182. public void scrollByRuleProfileForIndexing(DbSession dbSession, String ruleProfileUuid, Consumer<IndexedActiveRuleDto> consumer) {
  183. mapper(dbSession).scrollByRuleProfileUuidForIndexing(ruleProfileUuid, context -> {
  184. IndexedActiveRuleDto dto = context.getResultObject();
  185. consumer.accept(dto);
  186. });
  187. }
  188. private static ActiveRuleMapper mapper(DbSession dbSession) {
  189. return dbSession.getMapper(ActiveRuleMapper.class);
  190. }
  191. }