]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7330 ActiveRuleCompleter is now using new ActiveRuleDao
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 26 Feb 2016 10:42:17 +0000 (11:42 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 29 Feb 2016 12:26:54 +0000 (13:26 +0100)
server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java
sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java

index db51a5bd71271d8996caf78f2c481c8fb0c4057a..68fe9103c9ba267484af7648425bdb021325207a 100644 (file)
@@ -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> activeRuleDto = dbClient.activeRuleDao().selectByActiveRuleKey(dbSession, activeRule.key());
+          Optional<ActiveRuleDto> activeRuleDto = dbClient.activeRuleDao().selectByKey(dbSession, activeRule.key());
           checkFoundWithOptional(activeRuleDto, "Active rule with key '%s' not found", activeRule.key().toString());
           List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRuleDto.get().getId());
           ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamByActiveRuleKey = ArrayListMultimap.create(1, activeRuleParamDtos.size());
@@ -102,7 +102,7 @@ public class ActiveRuleCompleter {
       // Load details of all active rules
       for (RuleDto rule : rules) {
         List<ActiveRule> activeRules = loader.findActiveRulesByRule(rule.getKey());
-        List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByActiveRuleKeys(dbSession, Lists.transform(activeRules, ActiveRuleToKey.INSTANCE));
+        List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByKeys(dbSession, Lists.transform(activeRules, ActiveRuleToKey.INSTANCE));
         Map<Integer, ActiveRuleKey> 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<ActiveRule> activeRules = loader.findActiveRulesByRule(rule.getKey());
-    List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByActiveRuleKeys(dbSession, Lists.transform(activeRules, ActiveRuleToKey.INSTANCE));
+    List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByKeys(dbSession, Lists.transform(activeRules, ActiveRuleToKey.INSTANCE));
     Map<Integer, ActiveRuleKey> activeRuleIdsByKey = new HashMap<>();
     for (ActiveRuleDto activeRuleDto : activeRuleDtos) {
       activeRuleIdsByKey.put(activeRuleDto.getId(), activeRuleDto.getKey());
index d85a54b7a41fc96b40c337e65f4ed09d4eaafb6a..b236d22776cbc1d22c6e344be821858abd1674a2 100644 (file)
  */
 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<ActiveRuleDto> selectByKeys(DbSession dbSession, List<ActiveRuleKey> keys) {
+    List<SqlActiveRuleKey> sqlKeys = new ArrayList<>();
+    for (ActiveRuleKey key : keys) {
+      sqlKeys.add(new SqlActiveRuleKey(key));
+    }
+    return DatabaseUtils.executeLargeInputs(sqlKeys, new KeyToDto(mapper(dbSession)));
+  }
+
   public List<ActiveRuleDto> 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<ActiveRuleParamDto> selectAllParams(DbSession dbSession) {
-    return mapper(dbSession).selectAllParams();
+  public List<ActiveRuleDto> 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<ActiveRuleParamDto> selectParamsByActiveRuleId(DbSession dbSession, Integer activeRuleId) {
+    return mapper(dbSession).selectParamsByActiveRuleId(activeRuleId);
+  }
+
+  public List<ActiveRuleParamDto> selectParamsByActiveRuleIds(final DbSession dbSession, List<Integer> activeRuleIds) {
+    return DatabaseUtils.executeLargeInputs(activeRuleIds, new ParamIdToDto(mapper(dbSession)));
+  }
+
+  public List<ActiveRuleParamDto> 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<ActiveRuleDto> activeRule = selectByKey(session, key);
+    if (activeRule.isPresent()) {
+      return mapper(session).selectParamByActiveRuleAndKey(activeRule.get().getId(), name);
+    }
+    return null;
+  }
+
+  public List<ActiveRuleParamDto> 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<ActiveRuleDto> selectByProfileKey(DbSession session, String profileKey) {
-    return mapper(session).selectByProfileKey(profileKey);
-  }
-
-  /**
-   * Finder methods for ActiveRuleParams
-   */
-
-  public List<ActiveRuleParamDto> 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<ActiveRuleDto> 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<ActiveRuleDto> 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<SqlActiveRuleKey>, List<ActiveRuleDto>> {
+    private final ActiveRuleMapper mapper;
+
+    private KeyToDto(ActiveRuleMapper mapper) {
+      this.mapper = mapper;
+    }
+
+    @Override
+    public List<ActiveRuleDto> apply(@Nonnull List<SqlActiveRuleKey> partitionOfIds) {
+      return mapper.selectByKeys(partitionOfIds);
+    }
+  }
+
+  private static class ParamIdToDto implements Function<List<Integer>, List<ActiveRuleParamDto>> {
+    private final ActiveRuleMapper mapper;
+
+    private ParamIdToDto(ActiveRuleMapper mapper) {
+      this.mapper = mapper;
+    }
+
+    @Override
+    public List<ActiveRuleParamDto> apply(@Nonnull List<Integer> partitionOfIds) {
+      return mapper.selectParamsByActiveRuleIds(partitionOfIds);
+    }
+  }
 }