]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7330 Cleanup Normalizer
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 26 Feb 2016 14:29:41 +0000 (15:29 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 29 Feb 2016 12:26:54 +0000 (13:26 +0100)
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleNormalizer.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleDoc.java
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionMediumTest.java

index 7f19739bc102c670f617cf29a31afb8ef9ff143c..fecd7deff196a248083733865f947f70e75b0e3d 100644 (file)
@@ -43,10 +43,8 @@ import org.sonar.server.platform.ServerImpl;
 import org.sonar.server.platform.ServerSettings;
 import org.sonar.server.platform.TempFolderProvider;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
-import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer;
 import org.sonar.server.ruby.PlatformRackBridge;
 import org.sonar.server.rule.index.RuleIndex;
-import org.sonar.server.rule.index.RuleNormalizer;
 import org.sonar.server.search.EsSearchModule;
 import org.sonar.server.search.IndexQueue;
 import org.sonar.server.user.ThreadLocalUserSession;
@@ -104,8 +102,6 @@ public class PlatformLevel1 extends PlatformLevel {
       // rules/qprofiles
       RuleIndex.class,
       ActiveRuleIndex.class,
-      RuleNormalizer.class,
-      ActiveRuleNormalizer.class,
 
       // issues
       IssueIndex.class,
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleNormalizer.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleNormalizer.java
deleted file mode 100644 (file)
index df27cd9..0000000
+++ /dev/null
@@ -1,180 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.qualityprofile.index;
-
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import org.elasticsearch.action.update.UpdateRequest;
-import org.sonar.db.DbSession;
-import org.sonar.db.qualityprofile.ActiveRuleDto;
-import org.sonar.db.qualityprofile.ActiveRuleKey;
-import org.sonar.db.qualityprofile.ActiveRuleParamDto;
-import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.process.ProcessProperties;
-import org.sonar.server.db.DbClient;
-import org.sonar.server.qualityprofile.ActiveRule;
-import org.sonar.server.search.BaseNormalizer;
-import org.sonar.server.search.IndexField;
-import org.sonar.server.search.Indexable;
-
-public class ActiveRuleNormalizer extends BaseNormalizer<ActiveRuleDto, ActiveRuleKey> {
-
-  public static class ActiveRuleField extends Indexable {
-
-    public static final IndexField KEY = addSortableAndSearchable(IndexField.Type.STRING, "key");
-    public static final IndexField INHERITANCE = add(IndexField.Type.STRING, "inheritance");
-    public static final IndexField PROFILE_KEY = add(IndexField.Type.STRING, "profile");
-    public static final IndexField SEVERITY = add(IndexField.Type.STRING, "severity");
-    public static final IndexField PARENT_KEY = add(IndexField.Type.STRING, "parentKey");
-    public static final IndexField RULE_KEY = add(IndexField.Type.STRING, "ruleKey");
-    public static final IndexField PARAMS = addEmbedded("params", ActiveRuleParamField.ALL_FIELDS);
-
-    public static final IndexField CREATED_AT = addSortable(IndexField.Type.DATE, "createdAt");
-    public static final IndexField UPDATED_AT = addSortable(IndexField.Type.DATE, UPDATED_AT_FIELD);
-
-    public static final Set<IndexField> ALL_FIELDS = ImmutableSet.of(KEY, INHERITANCE, PROFILE_KEY, SEVERITY, PARENT_KEY, RULE_KEY, PARAMS, CREATED_AT, UPDATED_AT);
-
-  }
-
-  public static class ActiveRuleParamField extends Indexable {
-    public static final IndexField NAME = add(IndexField.Type.STRING, "name");
-    public static final IndexField VALUE = add(IndexField.Type.STRING, "value");
-    public static final Set<IndexField> ALL_FIELDS = ImmutableSet.of(NAME, VALUE);
-  }
-
-  public ActiveRuleNormalizer(DbClient db) {
-    super(db);
-  }
-
-  @Override
-  public List<UpdateRequest> normalize(ActiveRuleDto activeRuleDto) {
-
-    List<UpdateRequest> requests = new ArrayList<>();
-
-    ActiveRuleKey key = activeRuleDto.getKey();
-    Preconditions.checkArgument(key != null, "Cannot normalize ActiveRuleDto with null key");
-
-    Map<String, Object> newRule = new HashMap<>();
-    newRule.put("_parent", key.ruleKey().toString());
-    newRule.put(ActiveRuleField.RULE_KEY.field(), key.ruleKey().toString());
-    newRule.put(ActiveRuleField.KEY.field(), key.toString());
-    newRule.put(ActiveRuleField.INHERITANCE.field(),
-      (activeRuleDto.getInheritance() != null) ?
-        activeRuleDto.getInheritance() :
-        ActiveRule.Inheritance.NONE.name());
-    newRule.put(ActiveRuleField.SEVERITY.field(), activeRuleDto.getSeverityString());
-    newRule.put(ActiveRuleField.KEY.field(), key.toString());
-
-    newRule.put(ActiveRuleField.CREATED_AT.field(), activeRuleDto.getCreatedAt());
-    newRule.put(ActiveRuleField.UPDATED_AT.field(), activeRuleDto.getUpdatedAt());
-
-    DbSession session = db.openSession(false);
-    try {
-      // TODO because DTO uses legacy ID pattern
-      QualityProfileDto profile = db.qualityProfileDao().selectById(session, activeRuleDto.getProfileId());
-      if (profile == null) {
-        throw new IllegalStateException("Profile is null : " + activeRuleDto.getProfileId());
-      }
-      newRule.put(ActiveRuleField.PROFILE_KEY.field(), profile.getKey());
-
-      // TODO this should be generated by RegisterRule and modified in DTO.
-      String parentKey = null;
-      Integer parentId = activeRuleDto.getParentId();
-      if (parentId != null) {
-
-      }
-
-      /* Creating updateRequest */
-      requests.add(new UpdateRequest()
-        .id(key.toString())
-        .routing(key.ruleKey().toString())
-        .parent(key.ruleKey().toString())
-        .doc(newRule)
-        .upsert(getUpsertFor(ActiveRuleField.ALL_FIELDS, newRule)));
-
-      // Get the RuleParameters
-      for (ActiveRuleParamDto param : db.activeRuleDao().selectParamsByActiveRuleKey(session, key)) {
-        requests.addAll(normalizeNested(param, key));
-      }
-
-      newRule.put(ActiveRuleField.PARENT_KEY.field(), parentKey);
-    } finally {
-      session.close();
-    }
-
-    return requests;
-  }
-
-  @Override
-  public List<UpdateRequest> normalizeNested(Object object, ActiveRuleKey key) {
-    Preconditions.checkNotNull(key);
-    if (object.getClass().isAssignableFrom(ActiveRuleParamDto.class)) {
-      return nestedUpdate((ActiveRuleParamDto) object, key);
-    } else {
-      throw new IllegalStateException("Cannot normalize object of type '" + object.getClass() + "' in current context");
-    }
-  }
-
-  @Override
-  public List<UpdateRequest> deleteNested(Object object, ActiveRuleKey key) {
-    Preconditions.checkNotNull(key);
-    if (object.getClass().isAssignableFrom(ActiveRuleParamDto.class)) {
-      return nestedDelete((ActiveRuleParamDto) object, key);
-    } else {
-      throw new IllegalStateException("Cannot normalize object of type '" + object.getClass() + "' in current context");
-    }
-  }
-
-  private static List<UpdateRequest> nestedUpdate(ActiveRuleParamDto param, ActiveRuleKey key) {
-    Preconditions.checkNotNull(key);
-
-    Map<String, Object> newParam = new HashMap<>();
-    newParam.put(ActiveRuleParamField.NAME.field(), param.getKey());
-    newParam.put(ActiveRuleParamField.VALUE.field(), param.getValue());
-
-    return ImmutableList.of(new UpdateRequest()
-      .routing(key.ruleKey().toString())
-      .id(key.toString())
-      .script(ProcessProperties.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, ActiveRuleField.PARAMS.field())
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, newParam)
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, ActiveRuleParamField.NAME.field())
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getKey())
-      );
-  }
-
-  private static List<UpdateRequest> nestedDelete(ActiveRuleParamDto param, ActiveRuleKey key) {
-    return ImmutableList.of(new UpdateRequest()
-      .routing(key.ruleKey().toString())
-      .id(key.toString())
-      .script(ProcessProperties.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, ActiveRuleField.PARAMS.field())
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, null)
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, ActiveRuleParamField.NAME.field())
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getKey())
-      );
-  }
-}
index 7dbaa7f185ab05596c26435bfa54ddc3f93e6d24..fe3376a163ee22ba6988dd627c83d9a36841e585 100644 (file)
 package org.sonar.server.rule.index;
 
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Predicate;
-import com.google.common.collect.Iterables;
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
-import org.apache.commons.lang.BooleanUtils;
 import org.apache.commons.lang.builder.ReflectionToStringBuilder;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
-import org.sonar.api.server.debt.DebtRemediationFunction;
-import org.sonar.api.server.rule.RuleParamType;
-import org.sonar.server.rule.Rule;
-import org.sonar.server.rule.RuleParam;
 import org.sonar.server.search.BaseDoc;
-import org.sonar.server.search.IndexUtils;
 
 /**
  * Implementation of Rule based on an Elasticsearch document
  */
-public class RuleDoc extends BaseDoc implements Rule {
+public class RuleDoc extends BaseDoc {
 
   public static final String MANUAL_REPOSITORY = "manual";
 
@@ -51,15 +41,6 @@ public class RuleDoc extends BaseDoc implements Rule {
     super(fields);
   }
 
-  /**
-   * @deprecated Only use for sqale backward compat. Use key() instead.
-   */
-  @Deprecated
-  public Integer id() {
-    return getField(RuleNormalizer.RuleField.ID.field());
-  }
-
-  @Override
   public RuleKey key() {
     return RuleKey.parse(this.<String>getField(RuleIndexDefinition.FIELD_RULE_KEY));
   }
@@ -99,7 +80,6 @@ public class RuleDoc extends BaseDoc implements Rule {
     return this;
   }
 
-  @Override
   @CheckForNull
   public String internalKey() {
     return getNullableField(RuleIndexDefinition.FIELD_RULE_INTERNAL_KEY);
@@ -110,7 +90,6 @@ public class RuleDoc extends BaseDoc implements Rule {
     return this;
   }
 
-  @Override
   @CheckForNull
   public String language() {
     return getNullableField(RuleIndexDefinition.FIELD_RULE_LANGUAGE);
@@ -121,7 +100,6 @@ public class RuleDoc extends BaseDoc implements Rule {
     return this;
   }
 
-  @Override
   public String name() {
     return getField(RuleIndexDefinition.FIELD_RULE_NAME);
   }
@@ -131,7 +109,6 @@ public class RuleDoc extends BaseDoc implements Rule {
     return this;
   }
 
-  @Override
   @CheckForNull
   public String htmlDescription() {
     return getNullableField(RuleIndexDefinition.FIELD_RULE_HTML_DESCRIPTION);
@@ -142,19 +119,6 @@ public class RuleDoc extends BaseDoc implements Rule {
     return this;
   }
 
-  @Override
-  @CheckForNull
-  public String markdownDescription() {
-    return getNullableField(RuleNormalizer.RuleField.MARKDOWN_DESCRIPTION.field());
-  }
-
-  @Override
-  @CheckForNull
-  public String effortToFixDescription() {
-    return getNullableField(RuleNormalizer.RuleField.FIX_DESCRIPTION.field());
-  }
-
-  @Override
   @CheckForNull
   public String severity() {
     return (String) getNullableField(RuleIndexDefinition.FIELD_RULE_SEVERITY);
@@ -165,7 +129,6 @@ public class RuleDoc extends BaseDoc implements Rule {
     return this;
   }
 
-  @Override
   @CheckForNull
   public RuleStatus status() {
     return RuleStatus.valueOf((String) getField(RuleIndexDefinition.FIELD_RULE_STATUS));
@@ -176,20 +139,9 @@ public class RuleDoc extends BaseDoc implements Rule {
     return this;
   }
 
-  @Override
-  public boolean isTemplate() {
-    return (Boolean) getField(RuleIndexDefinition.FIELD_RULE_IS_TEMPLATE);
-  }
-
-  public RuleDoc setIsTemplate(@Nullable Boolean b) {
-    setField(RuleIndexDefinition.FIELD_RULE_IS_TEMPLATE, b);
-    return this;
-  }
-
-  @Override
   @CheckForNull
   public RuleKey templateKey() {
-    String templateKey = getNullableField(RuleNormalizer.RuleField.TEMPLATE_KEY.field());
+    String templateKey = getNullableField(RuleIndexDefinition.FIELD_RULE_TEMPLATE_KEY);
     return templateKey != null ? RuleKey.parse(templateKey) : null;
   }
 
@@ -198,14 +150,13 @@ public class RuleDoc extends BaseDoc implements Rule {
     return this;
   }
 
-  @Override
-  public List<String> tags() {
-    return (List<String>) getField(RuleNormalizer.RuleField.TAGS.field());
+  public boolean isTemplate() {
+    return (Boolean) getField(RuleIndexDefinition.FIELD_RULE_IS_TEMPLATE);
   }
 
-  @Override
-  public List<String> systemTags() {
-    return (List<String>) getField(RuleNormalizer.RuleField.SYSTEM_TAGS.field());
+  public RuleDoc setIsTemplate(@Nullable Boolean b) {
+    setField(RuleIndexDefinition.FIELD_RULE_IS_TEMPLATE, b);
+    return this;
   }
 
   public Collection<String> allTags() {
@@ -217,135 +168,6 @@ public class RuleDoc extends BaseDoc implements Rule {
     return this;
   }
 
-  @Override
-  public List<RuleParam> params() {
-    List<RuleParam> params = new ArrayList<>();
-    final List<Map<String, Object>> esParams = getNullableField(RuleNormalizer.RuleField.PARAMS.field());
-    if (esParams != null) {
-      for (final Map<String, Object> esParam : esParams) {
-        params.add(new RuleParam() {
-          @Override
-          public String key() {
-            return (String) esParam.get(RuleNormalizer.RuleParamField.NAME.field());
-          }
-
-          @Override
-          public String description() {
-            return (String) esParam.get(RuleNormalizer.RuleParamField.DESCRIPTION.field());
-          }
-
-          @Override
-          public String defaultValue() {
-            return (String) esParam.get(RuleNormalizer.RuleParamField.DEFAULT_VALUE.field());
-          }
-
-          @Override
-          public RuleParamType type() {
-            return RuleParamType.parse((String) esParam.get(RuleNormalizer.RuleParamField.TYPE.field()));
-          }
-        });
-      }
-    }
-    return params;
-  }
-
-  @CheckForNull
-  @Override
-  public RuleParam param(String key) {
-    return Iterables.find(params(), new RuleParamMatchKey(key), null);
-  }
-
-  @Override
-  public boolean debtOverloaded() {
-    return BooleanUtils.isTrue(isDebtRemediationFunctionOverridden());
-  }
-
-  @Override
-  @CheckForNull
-  public DebtRemediationFunction debtRemediationFunction() {
-    final String function = getNullableField(RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE.field());
-    if (function == null || function.isEmpty()) {
-      return null;
-    } else {
-      return new DebtRemediationFunction() {
-        @Override
-        public Type type() {
-          return Type.valueOf(function.toUpperCase());
-        }
-
-        @Override
-        public String coefficient() {
-          return (String) getNullableField(RuleNormalizer.RuleField.DEBT_FUNCTION_COEFFICIENT.field());
-        }
-
-        @Override
-        public String offset() {
-          return (String) getNullableField(RuleNormalizer.RuleField.DEBT_FUNCTION_OFFSET.field());
-        }
-      };
-    }
-  }
-
-  @Override
-  @CheckForNull
-  public DebtRemediationFunction defaultDebtRemediationFunction() {
-    final String function = getNullableField(RuleNormalizer.RuleField.DEFAULT_DEBT_FUNCTION_TYPE.field());
-    if (function == null || function.isEmpty()) {
-      return null;
-    } else {
-      return new DebtRemediationFunction() {
-        @Override
-        public Type type() {
-          return Type.valueOf(function.toUpperCase());
-        }
-
-        @Override
-        public String coefficient() {
-          return (String) getNullableField(RuleNormalizer.RuleField.DEFAULT_DEBT_FUNCTION_COEFFICIENT.field());
-        }
-
-        @Override
-        public String offset() {
-          return (String) getNullableField(RuleNormalizer.RuleField.DEFAULT_DEBT_FUNCTION_OFFSET.field());
-        }
-      };
-    }
-  }
-
-  @CheckForNull
-  public Boolean isDebtRemediationFunctionOverridden() {
-    return (Boolean) getNullableField(RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE_OVERLOADED.field());
-  }
-
-  @Override
-  @CheckForNull
-  public String markdownNote() {
-    return getNullableField(RuleNormalizer.RuleField.NOTE.field());
-  }
-
-  @Override
-  @CheckForNull
-  public String noteLogin() {
-    return (String) getNullableField(RuleNormalizer.RuleField.NOTE_LOGIN.field());
-  }
-
-  @Override
-  @CheckForNull
-  public Date noteCreatedAt() {
-    return IndexUtils.parseDateTime((String) getNullableField(RuleNormalizer.RuleField.NOTE_CREATED_AT.field()));
-  }
-
-  @Override
-  @CheckForNull
-  public Date noteUpdatedAt() {
-    return IndexUtils.parseDateTime((String) getNullableField(RuleNormalizer.RuleField.NOTE_UPDATED_AT.field()));
-  }
-
-  @Override
-  public Date createdAt() {
-    return IndexUtils.parseDateTime((String) getNullableField(RuleNormalizer.RuleField.CREATED_AT.field()));
-  }
-
   @CheckForNull
   public Long createdAtAsLong() {
     return (Long) getField(RuleIndexDefinition.FIELD_RULE_CREATED_AT);
@@ -356,11 +178,6 @@ public class RuleDoc extends BaseDoc implements Rule {
     return this;
   }
 
-  @Override
-  public Date updatedAt() {
-    return IndexUtils.parseDateTime((String) getNullableField(RuleNormalizer.RuleField.UPDATED_AT.field()));
-  }
-
   @CheckForNull
   public Long updatedAtAtAsLong() {
     return (Long) getField(RuleIndexDefinition.FIELD_RULE_UPDATED_AT);
@@ -371,26 +188,9 @@ public class RuleDoc extends BaseDoc implements Rule {
     return this;
   }
 
-  @Override
-  public boolean isManual() {
-    return getField(RuleNormalizer.RuleField.REPOSITORY.field()).equals(MANUAL_REPOSITORY);
-  }
-
   @Override
   public String toString() {
     return ReflectionToStringBuilder.toString(this);
   }
 
-  private static class RuleParamMatchKey implements Predicate<RuleParam> {
-    private final String key;
-
-    public RuleParamMatchKey(String key) {
-      this.key = key;
-    }
-
-    @Override
-    public boolean apply(@Nullable RuleParam input) {
-      return input != null && input.key().equals(key);
-    }
-  }
 }
index 1573785d7a128fcbb57bbd1299466f981b0d8979..442e04a65497f416fd5b02d81529921644dd0a15 100644 (file)
  */
 package org.sonar.server.rule.index;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Sets;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
 import java.util.Set;
-import org.elasticsearch.action.update.UpdateRequest;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.RuleStatus;
-import org.sonar.db.DbSession;
-import org.sonar.db.rule.RuleDto;
-import org.sonar.db.rule.RuleParamDto;
-import org.sonar.markdown.Markdown;
-import org.sonar.process.ProcessProperties;
-import org.sonar.server.db.DbClient;
-import org.sonar.server.search.BaseNormalizer;
 import org.sonar.server.search.IndexField;
 import org.sonar.server.search.Indexable;
 
-public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
+/**
+ * Only used by RuleMapping and RuleMapper, should be removed
+ */
+@Deprecated
+public class RuleNormalizer {
+
+  public static final String UPDATED_AT_FIELD = "updatedAt";
 
   public static final class RuleParamField extends Indexable {
 
@@ -127,160 +115,4 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
     }
   }
 
-  public RuleNormalizer(DbClient db) {
-    super(db);
-  }
-
-  @Override
-  public List<UpdateRequest> normalize(RuleDto rule) {
-
-    List<UpdateRequest> requests = new ArrayList<>();
-
-    DbSession session = db.openSession(false);
-    try {
-
-      /** Update Fields */
-      Map<String, Object> update = new HashMap<>();
-
-      update.put(RuleField.ID.field(), rule.getId());
-
-      update.put(RuleField.KEY.field(), rule.getKey().toString());
-      update.put(RuleField._KEY.field(), ImmutableList.of(rule.getKey().repository(), rule.getKey().rule()));
-
-      update.put(RuleField.REPOSITORY.field(), rule.getRepositoryKey());
-      update.put(RuleField.RULE_KEY.field(), rule.getRuleKey());
-      update.put(RuleField.NAME.field(), rule.getName());
-      update.put(RuleField.CREATED_AT.field(), rule.getCreatedAt());
-      update.put(RuleField.UPDATED_AT.field(), rule.getUpdatedAt());
-
-      if (RuleDto.Format.HTML.equals(rule.getDescriptionFormat())) {
-        update.put(RuleField.HTML_DESCRIPTION.field(), rule.getDescription());
-        update.put(RuleField.MARKDOWN_DESCRIPTION.field(), null);
-      } else {
-        update.put(RuleField.HTML_DESCRIPTION.field(), rule.getDescription() == null ? null : Markdown.convertToHtml(rule.getDescription()));
-        update.put(RuleField.MARKDOWN_DESCRIPTION.field(), rule.getDescription());
-      }
-
-      update.put(RuleField.FIX_DESCRIPTION.field(), rule.getEffortToFixDescription());
-      update.put(RuleField.SEVERITY.field(), rule.getSeverityString());
-
-      RuleStatus status = rule.getStatus();
-      update.put(RuleField.STATUS.field(), status != null ? rule.getStatus().name() : null);
-
-      update.put(RuleField.LANGUAGE.field(), rule.getLanguage());
-      update.put(RuleField.INTERNAL_KEY.field(), rule.getConfigKey());
-      update.put(RuleField.IS_TEMPLATE.field(), rule.isTemplate());
-
-      update.put(RuleField.NOTE.field(), rule.getNoteData());
-      update.put(RuleField.NOTE_LOGIN.field(), rule.getNoteUserLogin());
-      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
-      Integer templateId = rule.getTemplateId();
-      String templateKeyFieldValue = null;
-      if (templateId != null) {
-        Optional<RuleDto> templateRule = db.ruleDao().selectById(templateId, session);
-        if (templateRule.isPresent()) {
-          RuleKey templateKey = templateRule.get().getKey();
-          templateKeyFieldValue = templateKey != null ? templateKey.toString() : null;
-        }
-      }
-      update.put(RuleField.TEMPLATE_KEY.field(), templateKeyFieldValue);
-
-      if (rule.getDefaultRemediationFunction() != null) {
-        update.put(RuleField.DEFAULT_DEBT_FUNCTION_TYPE.field(), rule.getDefaultRemediationFunction());
-        update.put(RuleField.DEFAULT_DEBT_FUNCTION_COEFFICIENT.field(), rule.getDefaultRemediationCoefficient());
-        update.put(RuleField.DEFAULT_DEBT_FUNCTION_OFFSET.field(), rule.getDefaultRemediationOffset());
-      } else {
-        update.put(RuleField.DEFAULT_DEBT_FUNCTION_TYPE.field(), null);
-        update.put(RuleField.DEFAULT_DEBT_FUNCTION_COEFFICIENT.field(), null);
-        update.put(RuleField.DEFAULT_DEBT_FUNCTION_OFFSET.field(), null);
-      }
-
-      if (rule.getRemediationFunction() != null) {
-        update.put(RuleField.DEBT_FUNCTION_TYPE.field(), rule.getRemediationFunction());
-        update.put(RuleField.DEBT_FUNCTION_COEFFICIENT.field(), rule.getRemediationCoefficient());
-        update.put(RuleField.DEBT_FUNCTION_OFFSET.field(), rule.getRemediationOffset());
-        update.put(RuleField.DEBT_FUNCTION_TYPE_OVERLOADED.field(), true);
-      } else {
-        update.put(RuleField.DEBT_FUNCTION_TYPE.field(), rule.getDefaultRemediationFunction());
-        update.put(RuleField.DEBT_FUNCTION_COEFFICIENT.field(), rule.getDefaultRemediationCoefficient());
-        update.put(RuleField.DEBT_FUNCTION_OFFSET.field(), rule.getDefaultRemediationOffset());
-        update.put(RuleField.DEBT_FUNCTION_TYPE_OVERLOADED.field(), false);
-      }
-
-      update.put(RuleField.TAGS.field(), rule.getTags());
-      update.put(RuleField.SYSTEM_TAGS.field(), rule.getSystemTags());
-      update.put(RuleField.ALL_TAGS.field(), Sets.union(rule.getSystemTags(), rule.getTags()));
-
-      /** Upsert elements */
-      Map<String, Object> upsert = getUpsertFor(RuleField.ALL_FIELDS, update);
-      upsert.put(RuleField.KEY.field(), rule.getKey().toString());
-
-      /** Creating updateRequest */
-      requests.add(new UpdateRequest()
-        .id(rule.getKey().toString())
-        .doc(update)
-        .upsert(upsert));
-
-      for (RuleParamDto param : db.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey())) {
-        requests.addAll(normalizeNested(param, rule.getKey()));
-      }
-
-    } finally {
-      session.close();
-    }
-
-    return requests;
-  }
-
-  @Override
-  public List<UpdateRequest> normalizeNested(Object object, RuleKey key) {
-    Preconditions.checkNotNull(key, "key of rule must be set");
-    if (object.getClass().isAssignableFrom(RuleParamDto.class)) {
-      return nestedUpdate((RuleParamDto) object, key);
-    } else {
-      throw new IllegalStateException("Cannot normalize object of type '" + object.getClass() + "' in current context");
-    }
-  }
-
-  @Override
-  public List<UpdateRequest> deleteNested(Object object, RuleKey key) {
-    Preconditions.checkNotNull(key, "key of Rule must be set");
-    if (object.getClass().isAssignableFrom(RuleParamDto.class)) {
-      return nestedDelete((RuleParamDto) object, key);
-    } else {
-      throw new IllegalStateException("Cannot normalize object of type '" + object.getClass() + "' in current context");
-    }
-  }
-
-  private List<UpdateRequest> nestedUpdate(RuleParamDto param, RuleKey key) {
-    Map<String, Object> newParam = new HashMap<>();
-    newParam.put(RuleParamField.NAME.field(), param.getName());
-    newParam.put(RuleParamField.TYPE.field(), param.getType());
-    newParam.put(RuleParamField.DESCRIPTION.field(), param.getDescription());
-    newParam.put(RuleParamField.DEFAULT_VALUE.field(), param.getDefaultValue());
-
-    return ImmutableList.of(new UpdateRequest()
-      .id(key.toString())
-      .script(ProcessProperties.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, RuleField.PARAMS.field())
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, newParam)
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, RuleParamField.NAME.field())
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getName())
-      );
-  }
-
-  private List<UpdateRequest> nestedDelete(RuleParamDto param, RuleKey key) {
-    return ImmutableList.of(new UpdateRequest()
-      .id(key.toString())
-      .script(ProcessProperties.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, RuleField.PARAMS.field())
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, null)
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, RuleParamField.NAME.field())
-      .addScriptParam(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getName())
-      );
-  }
-
 }
index 63dddf763f2382137cb0681b30978d824668c752..0d8cf59ff96be8ea9553755c32428e993bbad1be 100644 (file)
@@ -53,7 +53,7 @@ import org.sonar.server.activity.ActivityService;
 import org.sonar.server.es.SearchOptions;
 import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
-import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer;
+import org.sonar.server.rule.index.RuleIndexDefinition;
 import org.sonar.server.rule.index.RuleIndexer;
 import org.sonar.server.search.FacetValue;
 import org.sonar.server.search.Result;
@@ -166,8 +166,8 @@ public class QProfileServiceMediumTest {
 
     assertThat(stats.size()).isEqualTo(2);
     assertThat(stats.get(XOO_P1_KEY).size()).isEqualTo(3);
-    assertThat(stats.get(XOO_P1_KEY).get(ActiveRuleNormalizer.ActiveRuleField.SEVERITY.field()).size()).isEqualTo(1);
-    assertThat(stats.get(XOO_P1_KEY).get(ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.field()).size()).isEqualTo(1);
+    assertThat(stats.get(XOO_P1_KEY).get(RuleIndexDefinition.FIELD_ACTIVE_RULE_SEVERITY).size()).isEqualTo(1);
+    assertThat(stats.get(XOO_P1_KEY).get(RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE).size()).isEqualTo(1);
     assertThat(stats.get(XOO_P1_KEY).get("countActiveRules").size()).isEqualTo(1);
   }
 
@@ -247,8 +247,8 @@ public class QProfileServiceMediumTest {
 
     // We need an actual rule in DB to test RuleName in Activity
     // TODO ???
-    //db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1);
-    //dbSession.commit();
+    // db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1);
+    // dbSession.commit();
 
     tester.get(ActivityService.class).save(
       ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1))
index a50e3f9564347b720c7c33b5ecb2a6474b6caa46..5e7eb7b719d219d6380675d35280ef1327709efc 100644 (file)
@@ -49,7 +49,6 @@ import org.sonar.server.qualityprofile.QProfileTesting;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
 import org.sonar.server.rule.index.RuleIndexDefinition;
 import org.sonar.server.rule.index.RuleIndexer;
-import org.sonar.server.rule.index.RuleNormalizer;
 import org.sonar.server.tester.ServerTester;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsTester;
@@ -536,7 +535,7 @@ public class SearchActionMediumTest {
     // 2. Sort Name DESC
     request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD);
     request.setParam(WebService.Param.FIELDS, "");
-    request.setParam(WebService.Param.SORT, RuleNormalizer.RuleField.NAME.field());
+    request.setParam(WebService.Param.SORT, RuleIndexDefinition.FIELD_RULE_NAME);
     request.setParam(WebService.Param.ASCENDING, "false");
 
     result = request.execute();