From ed2cc5b1780246a3c48ede381d6918ab98a69b9c Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 26 Feb 2016 11:42:17 +0100 Subject: [PATCH] SONAR-7330 ActiveRuleCompleter is now using new ActiveRuleDao --- .../server/rule/ws/ActiveRuleCompleter.java | 8 +- .../db/qualityprofile/ActiveRuleDao.java | 96 +++++++++++++------ 2 files changed, 73 insertions(+), 31 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java index db51a5bd712..68fe9103c9b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java @@ -37,13 +37,13 @@ import org.sonar.api.rule.RuleKey; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.qualityprofile.ActiveRuleDto; import org.sonar.db.qualityprofile.ActiveRuleKey; import org.sonar.db.qualityprofile.ActiveRuleParamDto; import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.db.rule.RuleDto; -import org.sonar.server.db.DbClient; import org.sonar.server.qualityprofile.ActiveRule; import org.sonar.server.qualityprofile.QProfileLoader; import org.sonar.server.rule.index.RuleQuery; @@ -90,7 +90,7 @@ public class ActiveRuleCompleter { for (RuleDto rule : rules) { ActiveRule activeRule = loader.getActiveRule(ActiveRuleKey.of(profileKey, rule.getKey())); if (activeRule != null) { - Optional activeRuleDto = dbClient.activeRuleDao().selectByActiveRuleKey(dbSession, activeRule.key()); + Optional activeRuleDto = dbClient.activeRuleDao().selectByKey(dbSession, activeRule.key()); checkFoundWithOptional(activeRuleDto, "Active rule with key '%s' not found", activeRule.key().toString()); List activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRuleDto.get().getId()); ListMultimap activeRuleParamByActiveRuleKey = ArrayListMultimap.create(1, activeRuleParamDtos.size()); @@ -102,7 +102,7 @@ public class ActiveRuleCompleter { // Load details of all active rules for (RuleDto rule : rules) { List activeRules = loader.findActiveRulesByRule(rule.getKey()); - List activeRuleDtos = dbClient.activeRuleDao().selectByActiveRuleKeys(dbSession, Lists.transform(activeRules, ActiveRuleToKey.INSTANCE)); + List activeRuleDtos = dbClient.activeRuleDao().selectByKeys(dbSession, Lists.transform(activeRules, ActiveRuleToKey.INSTANCE)); Map activeRuleIdsByKey = new HashMap<>(); for (ActiveRuleDto activeRuleDto : activeRuleDtos) { activeRuleIdsByKey.put(activeRuleDto.getId(), activeRuleDto.getKey()); @@ -125,7 +125,7 @@ public class ActiveRuleCompleter { void completeShow(DbSession dbSession, RuleDto rule, ShowResponse.Builder response) { List activeRules = loader.findActiveRulesByRule(rule.getKey()); - List activeRuleDtos = dbClient.activeRuleDao().selectByActiveRuleKeys(dbSession, Lists.transform(activeRules, ActiveRuleToKey.INSTANCE)); + List activeRuleDtos = dbClient.activeRuleDao().selectByKeys(dbSession, Lists.transform(activeRules, ActiveRuleToKey.INSTANCE)); Map activeRuleIdsByKey = new HashMap<>(); for (ActiveRuleDto activeRuleDto : activeRuleDtos) { activeRuleIdsByKey.put(activeRuleDto.getId(), activeRuleDto.getKey()); diff --git a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java index d85a54b7a41..b236d22776c 100644 --- a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java +++ b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java @@ -19,11 +19,15 @@ */ package org.sonar.db.qualityprofile; +import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Preconditions; +import java.util.ArrayList; import java.util.List; import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; import org.sonar.db.Dao; +import org.sonar.db.DatabaseUtils; import org.sonar.db.DbSession; import org.sonar.db.RowNotFoundException; import org.sonar.db.rule.RuleDto; @@ -52,6 +56,14 @@ public class ActiveRuleDao implements Dao { throw new RowNotFoundException(String.format("Active rule with key '%s' does not exist", key)); } + public List selectByKeys(DbSession dbSession, List keys) { + List sqlKeys = new ArrayList<>(); + for (ActiveRuleKey key : keys) { + sqlKeys.add(new SqlActiveRuleKey(key)); + } + return DatabaseUtils.executeLargeInputs(sqlKeys, new KeyToDto(mapper(dbSession))); + } + public List selectByRule(DbSession dbSession, RuleDto rule) { Preconditions.checkNotNull(rule.getId(), RULE_IS_NOT_PERSISTED); return mapper(dbSession).selectByRuleId(rule.getId()); @@ -62,8 +74,8 @@ public class ActiveRuleDao implements Dao { return mapper(dbSession).selectAll(); } - public List selectAllParams(DbSession dbSession) { - return mapper(dbSession).selectAllParams(); + public List selectByProfileKey(DbSession session, String profileKey) { + return mapper(session).selectByProfileKey(profileKey); } public ActiveRuleDto insert(DbSession session, ActiveRuleDto item) { @@ -94,6 +106,35 @@ public class ActiveRuleDao implements Dao { * Nested DTO ActiveRuleParams */ + public List selectParamsByActiveRuleId(DbSession dbSession, Integer activeRuleId) { + return mapper(dbSession).selectParamsByActiveRuleId(activeRuleId); + } + + public List selectParamsByActiveRuleIds(final DbSession dbSession, List activeRuleIds) { + return DatabaseUtils.executeLargeInputs(activeRuleIds, new ParamIdToDto(mapper(dbSession))); + } + + public List selectParamsByActiveRuleKey(DbSession session, ActiveRuleKey key) { + Preconditions.checkNotNull(key, ACTIVE_RULE_KEY_CANNOT_BE_NULL); + ActiveRuleDto activeRule = selectOrFailByKey(session, key); + return mapper(session).selectParamsByActiveRuleId(activeRule.getId()); + } + + @CheckForNull + public ActiveRuleParamDto selectParamByKeyAndName(ActiveRuleKey key, String name, DbSession session) { + Preconditions.checkNotNull(key, ACTIVE_RULE_KEY_CANNOT_BE_NULL); + Preconditions.checkNotNull(name, PARAMETER_NAME_CANNOT_BE_NULL); + Optional activeRule = selectByKey(session, key); + if (activeRule.isPresent()) { + return mapper(session).selectParamByActiveRuleAndKey(activeRule.get().getId(), name); + } + return null; + } + + public List selectAllParams(DbSession dbSession) { + return mapper(dbSession).selectAllParams(); + } + public ActiveRuleParamDto insertParam(DbSession session, ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam) { Preconditions.checkArgument(activeRule.getId() != null, ACTIVE_RULE_IS_NOT_PERSISTED); Preconditions.checkArgument(activeRuleParam.getId() == null, ACTIVE_RULE_PARAM_IS_ALREADY_PERSISTED); @@ -127,31 +168,6 @@ public class ActiveRuleDao implements Dao { mapper(session).deleteParameter(activeRuleParam.getId()); } - public List selectByProfileKey(DbSession session, String profileKey) { - return mapper(session).selectByProfileKey(profileKey); - } - - /** - * Finder methods for ActiveRuleParams - */ - - public List selectParamsByActiveRuleKey(DbSession session, ActiveRuleKey key) { - Preconditions.checkNotNull(key, ACTIVE_RULE_KEY_CANNOT_BE_NULL); - ActiveRuleDto activeRule = selectOrFailByKey(session, key); - return mapper(session).selectParamsByActiveRuleId(activeRule.getId()); - } - - @CheckForNull - public ActiveRuleParamDto selectParamByKeyAndName(ActiveRuleKey key, String name, DbSession session) { - Preconditions.checkNotNull(key, ACTIVE_RULE_KEY_CANNOT_BE_NULL); - Preconditions.checkNotNull(name, PARAMETER_NAME_CANNOT_BE_NULL); - Optional activeRule = selectByKey(session, key); - if (activeRule.isPresent()) { - return mapper(session).selectParamByActiveRuleAndKey(activeRule.get().getId(), name); - } - return null; - } - public void deleteParamsByRuleParam(DbSession dbSession, RuleDto rule, String paramKey) { List activeRules = selectByRule(dbSession, rule); for (ActiveRuleDto activeRule : activeRules) { @@ -166,4 +182,30 @@ public class ActiveRuleDao implements Dao { private ActiveRuleMapper mapper(DbSession session) { return session.getMapper(ActiveRuleMapper.class); } + + private static class KeyToDto implements Function, List> { + private final ActiveRuleMapper mapper; + + private KeyToDto(ActiveRuleMapper mapper) { + this.mapper = mapper; + } + + @Override + public List apply(@Nonnull List partitionOfIds) { + return mapper.selectByKeys(partitionOfIds); + } + } + + private static class ParamIdToDto implements Function, List> { + private final ActiveRuleMapper mapper; + + private ParamIdToDto(ActiveRuleMapper mapper) { + this.mapper = mapper; + } + + @Override + public List apply(@Nonnull List partitionOfIds) { + return mapper.selectParamsByActiveRuleIds(partitionOfIds); + } + } } -- 2.39.5