From eace062d3e1490aefe9b6270c1a315470dda082a Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 2 Jul 2014 11:16:18 +0200 Subject: [PATCH] Fix quality flaws --- .../server/rule/index/RuleNormalizer.java | 78 +++++++++++-------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java b/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java index dc35e6e3a79..9186bc417af 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java @@ -38,6 +38,9 @@ import org.sonar.server.search.IndexField; import org.sonar.server.search.Indexable; import org.sonar.server.search.es.ListUpdate; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + import java.lang.reflect.Field; import java.util.*; @@ -111,7 +114,6 @@ public class RuleNormalizer extends BaseNormalizer { public static final IndexField _TAGS = addSearchable(IndexField.Type.STRING, "_tags"); public static final IndexField PARAMS = addEmbedded("params", RuleParamField.ALL_FIELDS); - public static final Set ALL_FIELDS = getAllFields(); private static final Set getAllFields() { @@ -138,12 +140,10 @@ public class RuleNormalizer extends BaseNormalizer { } } - public RuleNormalizer(DbClient db) { super(IndexDefinition.RULE, db); } - @Override public List normalize(RuleKey key) { DbSession dbSession = db.openSession(false); @@ -192,7 +192,7 @@ public class RuleNormalizer extends BaseNormalizer { update.put(RuleField.NOTE_CREATED_AT.field(), rule.getNoteCreatedAt()); update.put(RuleField.NOTE_UPDATED_AT.field(), rule.getNoteUpdatedAt()); - //TODO Legacy PARENT_ID in DTO should be parent_key + // TODO Legacy PARENT_ID in DTO should be parent_key Integer templateId = rule.getTemplateId(); if (templateId != null) { RuleDto templateRule = db.ruleDao().getById(session, templateId); @@ -202,7 +202,7 @@ public class RuleNormalizer extends BaseNormalizer { update.put(RuleField.TEMPLATE_KEY.field(), null); } - //TODO Legacy ID in DTO should be Key + // TODO Legacy ID in DTO should be Key update.put(RuleField.CHARACTERISTIC.field(), null); update.put(RuleField.SUB_CHARACTERISTIC.field(), null); update.put(RuleField.DEFAULT_CHARACTERISTIC.field(), null); @@ -213,12 +213,11 @@ public class RuleNormalizer extends BaseNormalizer { Integer defaultSubCharacteristicId = rule.getDefaultSubCharacteristicId(); if (defaultSubCharacteristicId != null) { - CharacteristicDto characteristic, subCharacteristic = null; - subCharacteristic = db.debtCharacteristicDao().selectById(defaultSubCharacteristicId, session); + CharacteristicDto subCharacteristic = db.debtCharacteristicDao().selectById(defaultSubCharacteristicId, session); if (subCharacteristic != null) { Integer characteristicId = subCharacteristic.getParentId(); if (characteristicId != null) { - characteristic = db.debtCharacteristicDao().selectById(characteristicId); + CharacteristicDto characteristic = db.debtCharacteristicDao().selectById(characteristicId); if (characteristic != null) { update.put(RuleField.DEFAULT_CHARACTERISTIC.field(), characteristic.getKey()); update.put(RuleField.DEFAULT_SUB_CHARACTERISTIC.field(), subCharacteristic.getKey()); @@ -233,14 +232,15 @@ public class RuleNormalizer extends BaseNormalizer { update.put(RuleField.CHARACTERISTIC.field(), DebtCharacteristic.NONE); update.put(RuleField.SUB_CHARACTERISTIC.field(), DebtCharacteristic.NONE); } else { - CharacteristicDto characteristic, subCharacteristic = null; - subCharacteristic = db.debtCharacteristicDao().selectById(subCharacteristicId, session); - Integer characteristicId = subCharacteristic.getParentId(); - if (characteristicId != null) { - characteristic = db.debtCharacteristicDao().selectById(characteristicId); - if (characteristic != null) { - update.put(RuleField.CHARACTERISTIC.field(), characteristic.getKey()); - update.put(RuleField.SUB_CHARACTERISTIC.field(), subCharacteristic.getKey()); + CharacteristicDto subCharacteristic = db.debtCharacteristicDao().selectById(subCharacteristicId, session); + if (subCharacteristic != null) { + Integer characteristicId = subCharacteristic.getParentId(); + if (characteristicId != null) { + CharacteristicDto characteristic = db.debtCharacteristicDao().selectById(characteristicId); + if (characteristic != null) { + update.put(RuleField.CHARACTERISTIC.field(), characteristic.getKey()); + update.put(RuleField.SUB_CHARACTERISTIC.field(), subCharacteristic.getKey()); + } } } } @@ -249,7 +249,6 @@ public class RuleNormalizer extends BaseNormalizer { update.put(RuleField.SUB_CHARACTERISTIC.field(), null); } - if (rule.getDefaultRemediationFunction() != null) { update.put(RuleField.DEFAULT_DEBT_FUNCTION_TYPE.field(), rule.getDefaultRemediationFunction()); update.put(RuleField.DEFAULT_DEBT_FUNCTION_COEFFICIENT.field(), rule.getDefaultRemediationCoefficient()); @@ -270,18 +269,15 @@ public class RuleNormalizer extends BaseNormalizer { update.put(RuleField.DEBT_FUNCTION_OFFSET.field(), null); } - update.put(RuleField.TAGS.field(), rule.getTags()); update.put(RuleField.SYSTEM_TAGS.field(), rule.getSystemTags()); update.put(RuleField._TAGS.field(), Sets.union(rule.getSystemTags(), rule.getTags())); - /** Upsert elements */ Map upsert = new HashMap(update); upsert.put(RuleField.KEY.field(), rule.getKey().toString()); upsert.put(RuleField.PARAMS.field(), new ArrayList()); - /** Creating updateRequest */ return ImmutableList.of(new UpdateRequest() .replicationType(ReplicationType.ASYNC) @@ -323,23 +319,37 @@ public class RuleNormalizer extends BaseNormalizer { newParam.put(RuleParamField.DEFAULT_VALUE.field(), param.getDefaultValue()); return ImmutableList.of(new UpdateRequest() - .id(key.toString()) - .script(ListUpdate.NAME) - .addScriptParam(ListUpdate.FIELD, RuleField.PARAMS.field()) - .addScriptParam(ListUpdate.VALUE, newParam) - .addScriptParam(ListUpdate.ID_FIELD, RuleParamField.NAME.field()) - .addScriptParam(ListUpdate.ID_VALUE, param.getName()) - ); + .id(key.toString()) + .script(ListUpdate.NAME) + .addScriptParam(ListUpdate.FIELD, RuleField.PARAMS.field()) + .addScriptParam(ListUpdate.VALUE, newParam) + .addScriptParam(ListUpdate.ID_FIELD, RuleParamField.NAME.field()) + .addScriptParam(ListUpdate.ID_VALUE, param.getName()) + ); } private List nestedDelete(RuleParamDto param, RuleKey key) { return ImmutableList.of(new UpdateRequest() - .id(key.toString()) - .script(ListUpdate.NAME) - .addScriptParam(ListUpdate.FIELD, RuleField.PARAMS.field()) - .addScriptParam(ListUpdate.VALUE, null) - .addScriptParam(ListUpdate.ID_FIELD, RuleParamField.NAME.field()) - .addScriptParam(ListUpdate.ID_VALUE, param.getName()) - ); + .id(key.toString()) + .script(ListUpdate.NAME) + .addScriptParam(ListUpdate.FIELD, RuleField.PARAMS.field()) + .addScriptParam(ListUpdate.VALUE, null) + .addScriptParam(ListUpdate.ID_FIELD, RuleParamField.NAME.field()) + .addScriptParam(ListUpdate.ID_VALUE, param.getName()) + ); + } + + @CheckForNull + private CharacteristicDto characteristic(@Nullable Integer subCharacteristicId, DbSession session) { + if (subCharacteristicId != null) { + CharacteristicDto subCharacteristic = db.debtCharacteristicDao().selectById(subCharacteristicId, session); + if (subCharacteristic != null) { + Integer characteristicId = subCharacteristic.getParentId(); + if (characteristicId != null) { + return db.debtCharacteristicDao().selectById(characteristicId); + } + } + } + return null; } } -- 2.39.5