From e20c23f822908cad7ddb1038322c5a395b51f49c Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 1 Jul 2014 17:37:11 +0200 Subject: [PATCH] Fix quality flaws --- .../server/issue/ws/IssueSearchAction.java | 2 +- .../measure/persistence/MeasureDao.java | 10 +- .../org/sonar/server/rule/RuleUpdater.java | 99 ++++++++++--------- .../org/sonar/server/rule/db/RuleDao.java | 2 +- 4 files changed, 63 insertions(+), 50 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueSearchAction.java b/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueSearchAction.java index 8ee8705b275..00435cb28d9 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueSearchAction.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueSearchAction.java @@ -191,7 +191,7 @@ public class IssueSearchAction implements RequestHandler { private void checkFormatParameter(Request request) { String format = request.param("format"); - if (!Strings.isNullOrEmpty(format) && !format.equals("json")){ + if (!Strings.isNullOrEmpty(format) && !"json".equals(format)){ throw new BadRequestException("Only json format is supported."); } } diff --git a/sonar-server/src/main/java/org/sonar/server/measure/persistence/MeasureDao.java b/sonar-server/src/main/java/org/sonar/server/measure/persistence/MeasureDao.java index 2f30fe0fbba..ef7663576ef 100644 --- a/sonar-server/src/main/java/org/sonar/server/measure/persistence/MeasureDao.java +++ b/sonar-server/src/main/java/org/sonar/server/measure/persistence/MeasureDao.java @@ -71,21 +71,25 @@ public class MeasureDao extends BaseDao i @Override protected MeasureDto doInsert(DbSession session, MeasureDto item) { - throw new IllegalStateException("Not implemented yet"); + throw notImplemented(); } @Override protected MeasureDto doUpdate(DbSession session, MeasureDto item) { - throw new IllegalStateException("Not implemented yet"); + throw notImplemented(); } @Override protected void doDeleteByKey(DbSession session, MeasureKey key) { - throw new IllegalStateException("Not implemented yet"); + throw notImplemented(); } @Override public void synchronizeAfter(DbSession session, Date date) { + throw notImplemented(); + } + + private static RuntimeException notImplemented(){ throw new IllegalStateException("Not implemented yet"); } } diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java b/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java index b74ce3b2725..93fe4fbe9a3 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java @@ -254,11 +254,10 @@ public class RuleUpdater implements ServerComponent { .isEquals(); } - private void updateParameters(DbSession dbSession, RuleUpdate update, Context context) { if (update.isChangeParameters() && update.isCustomRule()) { RuleDto customRule = context.rule; - RuleDto templateRule = dbClient.ruleDao().getById(dbSession, customRule.getTemplateId()); + RuleDto templateRule = dbClient.ruleDao().getTemplate(customRule, dbSession); List templateRuleParams = dbClient.ruleDao().findRuleParamsByRuleKey(dbSession, templateRule.getKey()); List paramKeys = new ArrayList(); @@ -273,57 +272,67 @@ public class RuleUpdater implements ServerComponent { } // Browse custom rule parameters to update or delete them - for (RuleParamDto ruleParamDto : dbClient.ruleDao().findRuleParamsByRuleKey(dbSession, update.getRuleKey())) { - String key = ruleParamDto.getName(); - String value = update.parameter(key); - if (!Strings.isNullOrEmpty(value)) { - // Update rule param - ruleParamDto.setDefaultValue(value); - dbClient.ruleDao().updateRuleParam(dbSession, customRule, ruleParamDto); + deleteOrUpdateParameters(dbSession, update, customRule, paramKeys, activeRules, activeRuleParams); - // Update linked active rule params - for (ActiveRuleDto activeRuleDto : activeRules.get(customRule)) { - for (ActiveRuleParamDto activeRuleParamDto : activeRuleParams.get(activeRuleDto)) { - if (activeRuleParamDto.getKey().equals(key)) { - dbClient.activeRuleDao().updateParam(dbSession, activeRuleDto, activeRuleParamDto.setValue(value)); - } + // Browse template rule parameters to create new parameters + createNewParameters(dbSession, update, customRule, templateRuleParams, paramKeys, activeRules); + } + } + + private void deleteOrUpdateParameters(DbSession dbSession, RuleUpdate update, RuleDto customRule, List paramKeys, + Multimap activeRules, Multimap activeRuleParams){ + for (RuleParamDto ruleParamDto : dbClient.ruleDao().findRuleParamsByRuleKey(dbSession, update.getRuleKey())) { + String key = ruleParamDto.getName(); + String value = update.parameter(key); + if (!Strings.isNullOrEmpty(value)) { + // Update rule param + ruleParamDto.setDefaultValue(value); + dbClient.ruleDao().updateRuleParam(dbSession, customRule, ruleParamDto); + + // Update linked active rule params + for (ActiveRuleDto activeRuleDto : activeRules.get(customRule)) { + for (ActiveRuleParamDto activeRuleParamDto : activeRuleParams.get(activeRuleDto)) { + if (activeRuleParamDto.getKey().equals(key)) { + dbClient.activeRuleDao().updateParam(dbSession, activeRuleDto, activeRuleParamDto.setValue(value)); } } - } else { - // Delete rule param - dbClient.ruleDao().removeRuleParam(dbSession, customRule, ruleParamDto); - - // Delete linked active rule params - for (ActiveRuleDto activeRuleDto : activeRules.get(customRule)) { - for (ActiveRuleParamDto activeRuleParamDto : activeRuleParams.get(activeRuleDto)) { - if (activeRuleParamDto.getKey().equals(key)) { - dbClient.activeRuleDao().deleteParam(dbSession, activeRuleDto, activeRuleParamDto); - } + } + } else { + // Delete rule param + dbClient.ruleDao().removeRuleParam(dbSession, customRule, ruleParamDto); + + // Delete linked active rule params + for (ActiveRuleDto activeRuleDto : activeRules.get(customRule)) { + for (ActiveRuleParamDto activeRuleParamDto : activeRuleParams.get(activeRuleDto)) { + if (activeRuleParamDto.getKey().equals(key)) { + dbClient.activeRuleDao().deleteParam(dbSession, activeRuleDto, activeRuleParamDto); } } } - paramKeys.add(key); } + paramKeys.add(key); + } + } - // Browse template rule parameters to create new parameters - for (RuleParamDto templateRuleParam : templateRuleParams) { - String key = templateRuleParam.getName(); - if (!paramKeys.contains(key)) { - String value = update.parameter(key); - if (!Strings.isNullOrEmpty(value)) { - - // Create new param - RuleParamDto paramDto = RuleParamDto.createFor(customRule) - .setName(key) - .setDescription(templateRuleParam.getDescription()) - .setDefaultValue(value) - .setType(templateRuleParam.getType()); - dbClient.ruleDao().addRuleParam(dbSession, customRule, paramDto); - - // Create new active rule param - for (ActiveRuleDto activeRuleDto : activeRules.get(customRule)) { - dbClient.activeRuleDao().addParam(dbSession, activeRuleDto, ActiveRuleParamDto.createFor(paramDto).setValue(value)); - } + private void createNewParameters(DbSession dbSession, RuleUpdate update, RuleDto customRule, List templateRuleParams, List paramKeys, + Multimap activeRules){ + for (RuleParamDto templateRuleParam : templateRuleParams) { + String key = templateRuleParam.getName(); + if (!paramKeys.contains(key)) { + String value = update.parameter(key); + if (!Strings.isNullOrEmpty(value)) { + + // Create new param + RuleParamDto paramDto = RuleParamDto.createFor(customRule) + .setName(key) + .setDescription(templateRuleParam.getDescription()) + .setDefaultValue(value) + .setType(templateRuleParam.getType()); + dbClient.ruleDao().addRuleParam(dbSession, customRule, paramDto); + + // Create new active rule param + for (ActiveRuleDto activeRuleDto : activeRules.get(customRule)) { + dbClient.activeRuleDao().addParam(dbSession, activeRuleDto, ActiveRuleParamDto.createFor(paramDto).setValue(value)); } } } diff --git a/sonar-server/src/main/java/org/sonar/server/rule/db/RuleDao.java b/sonar-server/src/main/java/org/sonar/server/rule/db/RuleDao.java index edc294c2e6f..c7cd45ef14a 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/db/RuleDao.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/db/RuleDao.java @@ -35,6 +35,7 @@ import org.sonar.server.search.action.IndexAction; import org.sonar.server.search.action.KeyIndexAction; import javax.annotation.CheckForNull; + import java.sql.Timestamp; import java.util.Date; import java.util.List; @@ -92,7 +93,6 @@ public class RuleDao extends BaseDao { return mapper(session).selectById(rule.getTemplateId()); } - @Override public void synchronizeAfter(final DbSession session, Date date) { session.select("selectKeysOfRulesUpdatedSince", new Timestamp(date.getTime()), new ResultHandler() { -- 2.39.5