]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 2 Jul 2014 09:16:18 +0000 (11:16 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 2 Jul 2014 09:16:18 +0000 (11:16 +0200)
sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java

index dc35e6e3a79da55279c9da85e66ebbaeeafaa7c0..9186bc417af7c8de08b5ceef0ea5eba45b31a039 100644 (file)
@@ -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<RuleDto, RuleKey> {
     public static final IndexField _TAGS = addSearchable(IndexField.Type.STRING, "_tags");
     public static final IndexField PARAMS = addEmbedded("params", RuleParamField.ALL_FIELDS);
 
-
     public static final Set<IndexField> ALL_FIELDS = getAllFields();
 
     private static final Set<IndexField> getAllFields() {
@@ -138,12 +140,10 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
     }
   }
 
-
   public RuleNormalizer(DbClient db) {
     super(IndexDefinition.RULE, db);
   }
 
-
   @Override
   public List<UpdateRequest> normalize(RuleKey key) {
     DbSession dbSession = db.openSession(false);
@@ -192,7 +192,7 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
       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<RuleDto, RuleKey> {
         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<RuleDto, RuleKey> {
 
       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<RuleDto, RuleKey> {
           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<RuleDto, RuleKey> {
         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<RuleDto, RuleKey> {
         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<String, Object> upsert = new HashMap<String, Object>(update);
       upsert.put(RuleField.KEY.field(), rule.getKey().toString());
       upsert.put(RuleField.PARAMS.field(), new ArrayList<String>());
 
-
       /** Creating updateRequest */
       return ImmutableList.of(new UpdateRequest()
         .replicationType(ReplicationType.ASYNC)
@@ -323,23 +319,37 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
     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<UpdateRequest> 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;
   }
 }