]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 - Added idField and idValue to native updateList script
authorStephane Gamard <stephane.gamard@searchbox.com>
Wed, 4 Jun 2014 09:50:20 +0000 (11:50 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Wed, 4 Jun 2014 12:30:20 +0000 (14:30 +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/es/ListUpdate.java

index 8058e8813eac2fc93f19b9d695c1c3959f5fdb60..21e38b22500a63f492a489f48c0030a0c88b843c 100644 (file)
@@ -46,20 +46,20 @@ public class ActiveRuleNormalizer extends BaseNormalizer<ActiveRuleDto, ActiveRu
 
   public static class ActiveRuleField extends Indexable {
 
-    public static IndexField KEY = addSortableAndSearchable(IndexField.Type.STRING,"key");
-    public static IndexField INHERITANCE = add(IndexField.Type.STRING,"inheritance");
-    public static IndexField PROFILE_KEY = add(IndexField.Type.STRING,"profile");
-    public static IndexField SEVERITY = add(IndexField.Type.STRING,"severity");
-    public static IndexField PARENT_KEY = add(IndexField.Type.STRING,"parentKey");
-    public static IndexField RULE_KEY = add(IndexField.Type.STRING,"ruleKey");
+    public static IndexField KEY = addSortableAndSearchable(IndexField.Type.STRING, "key");
+    public static IndexField INHERITANCE = add(IndexField.Type.STRING, "inheritance");
+    public static IndexField PROFILE_KEY = add(IndexField.Type.STRING, "profile");
+    public static IndexField SEVERITY = add(IndexField.Type.STRING, "severity");
+    public static IndexField PARENT_KEY = add(IndexField.Type.STRING, "parentKey");
+    public static IndexField RULE_KEY = add(IndexField.Type.STRING, "ruleKey");
     public static IndexField PARAMS = addEmbedded("params", ActiveRuleParamField.ALL_FIELDS);
 
     public static Set<IndexField> ALL_FIELDS = getAllFields();
 
-    private static Set<IndexField> getAllFields(){
+    private static Set<IndexField> getAllFields() {
       Set<IndexField> fields = new HashSet<IndexField>();
-      for(Field classField :ActiveRuleField.class.getDeclaredFields()){
-        if(classField.getType().isAssignableFrom(IndexField.class)){
+      for (Field classField : ActiveRuleField.class.getDeclaredFields()) {
+        if (classField.getType().isAssignableFrom(IndexField.class)) {
           //Modifier.isStatic(classField.getModifiers())
           try {
             fields.add(IndexField.class.cast(classField.get(null)));
@@ -74,15 +74,15 @@ public class ActiveRuleNormalizer extends BaseNormalizer<ActiveRuleDto, ActiveRu
   }
 
   public static class ActiveRuleParamField extends Indexable {
-    public static IndexField NAME = add(IndexField.Type.STRING,"name");
-    public static IndexField VALUE = add(IndexField.Type.STRING,"value");
+    public static IndexField NAME = add(IndexField.Type.STRING, "name");
+    public static IndexField VALUE = add(IndexField.Type.STRING, "value");
 
     public static Set<IndexField> ALL_FIELDS = getAllFields();
 
-    private static Set<IndexField> getAllFields(){
+    private static Set<IndexField> getAllFields() {
       Set<IndexField> fields = new HashSet<IndexField>();
-      for(Field classField :ActiveRuleParamField.class.getDeclaredFields()){
-        if(classField.getType().isAssignableFrom(IndexField.class)){
+      for (Field classField : ActiveRuleParamField.class.getDeclaredFields()) {
+        if (classField.getType().isAssignableFrom(IndexField.class)) {
           //Modifier.isStatic(classField.getModifiers())
           try {
             fields.add(IndexField.class.cast(classField.get(null)));
@@ -114,23 +114,6 @@ public class ActiveRuleNormalizer extends BaseNormalizer<ActiveRuleDto, ActiveRu
     return requests;
   }
 
-  public List<UpdateRequest> normalize(ActiveRuleParamDto param, ActiveRuleKey key) {
-    Preconditions.checkArgument(key != null, "Cannot normalize ActiveRuleParamDto for null key of ActiveRule");
-
-    Map<String, Object> newParam = new HashMap<String, Object>();
-    newParam.put("_id", param.getKey());
-    newParam.put(ActiveRuleParamField.NAME.field(), param.getKey());
-    newParam.put(ActiveRuleParamField.VALUE.field(), param.getValue());
-
-    return ImmutableList.of(new UpdateRequest()
-        .id(key.toString())
-        .script(ListUpdate.NAME)
-        .addScriptParam(ListUpdate.FIELD, ActiveRuleField.PARAMS.field())
-        .addScriptParam(ListUpdate.VALUE, newParam)
-        .addScriptParam(ListUpdate.ID, param.getKey())
-    );
-  }
-
   @Override
   public List<UpdateRequest> normalize(ActiveRuleDto activeRuleDto) {
     ActiveRuleKey key = activeRuleDto.getKey();
@@ -175,4 +158,21 @@ public class ActiveRuleNormalizer extends BaseNormalizer<ActiveRuleDto, ActiveRu
       .doc(newRule)
       .upsert(upsert));
   }
+
+  public List<UpdateRequest> normalize(ActiveRuleParamDto param, ActiveRuleKey key) {
+    Preconditions.checkArgument(key != null, "Cannot normalize ActiveRuleParamDto for null key of ActiveRule");
+
+    Map<String, Object> newParam = new HashMap<String, Object>();
+    newParam.put(ActiveRuleParamField.NAME.field(), param.getKey());
+    newParam.put(ActiveRuleParamField.VALUE.field(), param.getValue());
+
+    return ImmutableList.of(new UpdateRequest()
+        .id(key.toString())
+        .script(ListUpdate.NAME)
+        .addScriptParam(ListUpdate.FIELD, ActiveRuleField.PARAMS.field())
+        .addScriptParam(ListUpdate.VALUE, newParam)
+        .addScriptParam(ListUpdate.ID_FIELD, ActiveRuleParamField.NAME.field())
+        .addScriptParam(ListUpdate.ID_VALUE, param.getKey())
+    );
+  }
 }
index cc22e2e7c6229b1f1e44fd35baef2a5aaaddb92a..4931e4c9ad28aeae346389e2f16abcc6a4b8d8dc 100644 (file)
@@ -238,7 +238,6 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
   public List<UpdateRequest> normalize(RuleParamDto param, RuleKey key) {
 
     Map<String, Object> newParam = new HashMap<String, Object>();
-    newParam.put("_id", param.getName());
     newParam.put(RuleParamField.NAME.field(), param.getName());
     newParam.put(RuleParamField.TYPE.field(), param.getType());
     newParam.put(RuleParamField.DESCRIPTION.field(), param.getDescription());
@@ -249,7 +248,8 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
         .script(ListUpdate.NAME)
         .addScriptParam(ListUpdate.FIELD, RuleField.PARAMS.field())
         .addScriptParam(ListUpdate.VALUE, newParam)
-        .addScriptParam(ListUpdate.ID, param.getName())
+        .addScriptParam(ListUpdate.ID_FIELD, RuleParamField.NAME.field())
+        .addScriptParam(ListUpdate.ID_VALUE, param.getName())
     );
   }
 }
index e682d4d41d98762c1528d57bff1717d77166bddb..a47f4f1653771c963f60fa81cda0f54fbc4107d9 100644 (file)
@@ -36,19 +36,24 @@ public class ListUpdate extends AbstractExecutableScript {
 
   public static final String NAME = "listUpdate";
 
-  public static final String ID = "id";
+  public static final String ID_FIELD = "idField";
+  public static final String ID_VALUE = "idValue";
   public static final String FIELD = "field";
   public static final String VALUE = "value";
 
   public static class UpdateListScriptFactory implements NativeScriptFactory {
     @Override
     public ExecutableScript newScript(@Nullable Map<String, Object> params) {
-      String id = XContentMapValues.nodeStringValue(params.get(ID), null);
+      String idField = XContentMapValues.nodeStringValue(params.get(ID_FIELD), null);
+      String idValue = XContentMapValues.nodeStringValue(params.get(ID_VALUE), null);
       String field = XContentMapValues.nodeStringValue(params.get(FIELD), null);
       Map value = XContentMapValues.nodeMapValue(params.get(VALUE), "Update item");
 
-      if (id == null) {
-        throw new IllegalStateException("Missing '" + ID + "' parameter");
+      if (idField == null) {
+        throw new IllegalStateException("Missing '" + ID_FIELD + "' parameter");
+      }
+      if (idValue == null) {
+        throw new IllegalStateException("Missing '" + ID_VALUE + "' parameter");
       }
       if (field == null) {
         throw new IllegalStateException("Missing '" + FIELD + "' parameter");
@@ -57,19 +62,21 @@ public class ListUpdate extends AbstractExecutableScript {
         throw new IllegalStateException("Missing '" + VALUE + "' parameter");
       }
 
-      return new ListUpdate(id, field, value);
+      return new ListUpdate(idField, idValue, field, value);
     }
   }
 
 
-  private final String id;
+  private final String idField;
+  private final String idValue;
   private final String field;
   private final Map<String, Object> value;
 
   private Map<String, Object> ctx;
 
-  public ListUpdate(String id, String field, Map<String, Object> value) {
-    this.id = id;
+  public ListUpdate(String idField, String idValue, String field, Map<String, Object> value) {
+    this.idField = idField;
+    this.idValue = idValue;
     this.field = field;
     this.value = value;
   }
@@ -105,8 +112,9 @@ public class ListUpdate extends AbstractExecutableScript {
       // 3. field is a list
       Collection items = ((Collection) fieldValue);
       for (Object item : items) {
-        String idValue = XContentMapValues.nodeStringValue(item, null);
-        if (idValue != null && idValue.equals(id)) {
+        Map<String, Object> fields = (Map<String, Object>) item;
+        String itemIdValue = XContentMapValues.nodeStringValue(fields.get(idField), null);
+        if (itemIdValue != null && itemIdValue.equals(idValue)) {
           items.remove(item);
         }
       }