]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 Getting rid of Normalizer's Reflexive call
authorStephane Gamard <stephane.gamard@searchbox.com>
Wed, 4 Jun 2014 12:37:17 +0000 (14:37 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Wed, 4 Jun 2014 12:37:17 +0000 (14:37 +0200)
sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleNormalizer.java
sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java
sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java
sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java

index b333278d14f7fb98f77908f7d93fd4030c71b534..b160eb817dc8ca91e62dfafa509b0207bf0b9851 100644 (file)
@@ -159,6 +159,14 @@ public class ActiveRuleNormalizer extends BaseNormalizer<ActiveRuleDto, ActiveRu
       .upsert(upsert));
   }
 
+  @Override
+  public List<UpdateRequest> normalize(Object object, Object key) {
+    Preconditions.checkArgument(object.getClass().isAssignableFrom(ActiveRuleParamDto.class), "Cannot Normalize class");
+    Preconditions.checkArgument(key.getClass().isAssignableFrom(ActiveRuleKey.class), "key is not an ActiveRuleKey");
+
+    return normalize((ActiveRuleParamDto)object, (ActiveRuleKey)key);
+  }
+
   public List<UpdateRequest> normalize(ActiveRuleParamDto param, ActiveRuleKey key) {
     Preconditions.checkArgument(key != null, "Cannot normalize ActiveRuleParamDto for null key of ActiveRule");
 
index 4931e4c9ad28aeae346389e2f16abcc6a4b8d8dc..5766113b0d06a72bf30fba6d5e6f600dfa3538c5 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.rule.index;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Sets;
 import org.elasticsearch.action.update.UpdateRequest;
@@ -132,6 +133,7 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
     super(IndexDefinition.RULE, db);
   }
 
+
   @Override
   public List<UpdateRequest> normalize(RuleKey key) {
     DbSession dbSession = db.openSession(false);
@@ -235,7 +237,15 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
     }
   }
 
-  public List<UpdateRequest> normalize(RuleParamDto param, RuleKey key) {
+  @Override
+  public List<UpdateRequest> normalize(Object object, Object key) {
+    Preconditions.checkArgument(object.getClass().isAssignableFrom(RuleParamDto.class), "Cannot Normalize class");
+    Preconditions.checkArgument(key.getClass().isAssignableFrom(RuleKey.class), "key is not an ActiveRuleKey");
+
+    return normalize((RuleParamDto)object, (RuleKey)key);
+  }
+
+  private List<UpdateRequest> normalize(RuleParamDto param, RuleKey key) {
 
     Map<String, Object> newParam = new HashMap<String, Object>();
     newParam.put(RuleParamField.NAME.field(), param.getName());
index 23410d3371d394678e6f78619289ed9d8eb474ed..d419c718c954269e1e31cf3631ed8b13c79c4c9a 100644 (file)
@@ -198,7 +198,7 @@ public abstract class BaseIndex<D, E extends Dto<K>, K extends Serializable>
 
   protected abstract Map mapKey();
 
-  protected Map mapDomain(){
+  protected Map mapDomain() {
     Map<String, Object> mapping = new HashMap<String, Object>();
     mapping.put("dynamic", false);
     mapping.put("_id", mapKey());
@@ -236,7 +236,7 @@ public abstract class BaseIndex<D, E extends Dto<K>, K extends Serializable>
     mapping.put("type", "nested");
     Map<String, Object> mappings = new HashMap<String, Object>();
     for (IndexField nestedField : field.nestedFields()) {
-      if(nestedField != null) {
+      if (nestedField != null) {
         mappings.put(nestedField.field(), mapField(nestedField));
       }
     }
@@ -363,12 +363,7 @@ public abstract class BaseIndex<D, E extends Dto<K>, K extends Serializable>
 
   @Override
   public void upsert(Object obj, K key) throws Exception {
-    if (this.normalizer.canNormalize(obj.getClass(), key.getClass())) {
-      this.updateDocument(this.normalizer.normalizeOther(obj, key), key);
-    } else {
-      throw new IllegalStateException("Index " + this.getIndexName() +
-        " cannot execute UPDATE for class: " + obj.getClass());
-    }
+    this.updateDocument(this.normalizer.normalize(obj, key), key);
   }
 
   @Override
@@ -403,12 +398,7 @@ public abstract class BaseIndex<D, E extends Dto<K>, K extends Serializable>
 
   @Override
   public void delete(Object obj, K key) throws Exception {
-    if (this.normalizer.canNormalize(obj.getClass(), key.getClass())) {
-      //TODO don't really know what to do here for the moment...
-    } else {
-      throw new IllegalStateException("Index " + this.getIndexName() +
-        " cannot execute DELETE for class: " + obj.getClass());
-    }
+    throw new IllegalStateException("Cannot delete nested Object from ES. Should be using Update");
   }
 
   @Override
index 3cbbf6716d367c2ed9ce8a9f8c4e7bbc3465e0ea..5486f4a88e0aff807e77498a38850351ad33a80b 100644 (file)
@@ -24,7 +24,6 @@ import org.sonar.core.persistence.Dto;
 import org.sonar.server.db.DbClient;
 
 import java.io.Serializable;
-import java.util.List;
 
 public abstract class BaseNormalizer<E extends Dto<K>, K extends Serializable> {
 
@@ -36,23 +35,7 @@ public abstract class BaseNormalizer<E extends Dto<K>, K extends Serializable> {
     this.definition = definition;
   }
 
-  public boolean canNormalize(Class<?> objectClass, Class<?> keyClass) {
-    try {
-      return this.getClass().getMethod("normalize", objectClass, keyClass) != null;
-    } catch (NoSuchMethodException e) {
-      return false;
-    }
-  }
-
-  public List<UpdateRequest> normalizeOther(Object object, Object key) {
-    try {
-      return (List<UpdateRequest>) this.getClass()
-        .getMethod("normalize", object.getClass(), key.getClass())
-        .invoke(this, object, key);
-    } catch (Exception e) {
-      throw new IllegalStateException("Could not invoke Normalizer Method", e);
-    }
-  }
+  public abstract java.util.List<UpdateRequest> normalize(Object object, Object key);
 
   public abstract java.util.List<UpdateRequest> normalize(K key);