]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 29 Feb 2016 08:27:19 +0000 (09:27 +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/es/EsUtils.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileReset.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexer.java
server/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java
server/sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java
server/sonar-server/src/main/java/org/sonar/server/rule/RuleService.java
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndexer.java
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleQuery.java
server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java

index ad081be93f4799247520dadbfaf8ad573d42e162..c506e7bea26a5a72d68988d35a33064cf32470bf 100644 (file)
@@ -91,61 +91,87 @@ public class EsUtils {
     return null;
   }
 
-  public static <D extends BaseDoc> Iterator<D> scroll(final EsClient esClient, final String scrollId, final Function<Map<String, Object>, D> docConverter) {
-    return new Iterator<D>() {
-      private final Queue<SearchHit> hits = new ArrayDeque<>();
+  public static <D extends BaseDoc> Iterator<D> scroll(EsClient esClient, String scrollId, Function<Map<String, Object>, D> docConverter) {
+    return new DocScrollIterator<>(esClient, scrollId, docConverter);
+  }
 
-      @Override
-      public boolean hasNext() {
-        if (hits.isEmpty()) {
-          SearchScrollRequestBuilder esRequest = esClient.prepareSearchScroll(scrollId)
-            .setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES));
-          Collections.addAll(hits, esRequest.get().getHits().getHits());
-        }
-        return !hits.isEmpty();
-      }
+  private static class DocScrollIterator<D extends BaseDoc> implements Iterator<D> {
 
-      @Override
-      public D next() {
-        if (!hasNext()) {
-          throw new NoSuchElementException();
-        }
-        return docConverter.apply(hits.poll().getSource());
+    private final EsClient esClient;
+    private final String scrollId;
+    private final Function<Map<String, Object>, D> docConverter;
+
+    private final Queue<SearchHit> hits = new ArrayDeque<>();
+
+    private DocScrollIterator(EsClient esClient, String scrollId, Function<Map<String, Object>, D> docConverter) {
+      this.esClient = esClient;
+      this.scrollId = scrollId;
+      this.docConverter = docConverter;
+    }
+
+    @Override
+    public boolean hasNext() {
+      if (hits.isEmpty()) {
+        SearchScrollRequestBuilder esRequest = esClient.prepareSearchScroll(scrollId)
+          .setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES));
+        Collections.addAll(hits, esRequest.get().getHits().getHits());
       }
+      return !hits.isEmpty();
+    }
 
-      @Override
-      public void remove() {
-        throw new UnsupportedOperationException("Cannot remove item when scrolling");
+    @Override
+    public D next() {
+      if (!hasNext()) {
+        throw new NoSuchElementException();
       }
-    };
+      return docConverter.apply(hits.poll().getSource());
+    }
+
+    @Override
+    public void remove() {
+      throw new UnsupportedOperationException("Cannot remove item when scrolling");
+    }
   }
 
-  public static <ID> Iterator<ID> scrollIds(final EsClient esClient, final String scrollId, final Function<String, ID> idConverter) {
-    return new Iterator<ID>() {
-      private final Queue<SearchHit> hits = new ArrayDeque<>();
+  public static <ID> Iterator<ID> scrollIds(EsClient esClient, String scrollId, Function<String, ID> idConverter) {
+    return new IdScrollIterator<>(esClient, scrollId, idConverter);
+  }
 
-      @Override
-      public boolean hasNext() {
-        if (hits.isEmpty()) {
-          SearchScrollRequestBuilder esRequest = esClient.prepareSearchScroll(scrollId)
-            .setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES));
-          Collections.addAll(hits, esRequest.get().getHits().getHits());
-        }
-        return !hits.isEmpty();
-      }
+  private static class IdScrollIterator<ID> implements Iterator<ID> {
 
-      @Override
-      public ID next() {
-        if (!hasNext()) {
-          throw new NoSuchElementException();
-        }
-        return idConverter.apply(hits.poll().getId());
+    private final EsClient esClient;
+    private final String scrollId;
+    private final Function<String, ID> idConverter;
+
+    private final Queue<SearchHit> hits = new ArrayDeque<>();
+
+    private IdScrollIterator(EsClient esClient, String scrollId, Function<String, ID> idConverter) {
+      this.esClient = esClient;
+      this.scrollId = scrollId;
+      this.idConverter = idConverter;
+    }
+
+    @Override
+    public boolean hasNext() {
+      if (hits.isEmpty()) {
+        SearchScrollRequestBuilder esRequest = esClient.prepareSearchScroll(scrollId)
+          .setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES));
+        Collections.addAll(hits, esRequest.get().getHits().getHits());
       }
+      return !hits.isEmpty();
+    }
 
-      @Override
-      public void remove() {
-        throw new UnsupportedOperationException("Cannot remove item when scrolling");
+    @Override
+    public ID next() {
+      if (!hasNext()) {
+        throw new NoSuchElementException();
       }
-    };
+      return idConverter.apply(hits.poll().getId());
+    }
+
+    @Override
+    public void remove() {
+      throw new UnsupportedOperationException("Cannot remove item when scrolling");
+    }
   }
 }
index beb198c2891f95034d2495eb54f5d686cccfefc5..5503ea0f5a2e4eee034d2a69a6422feb5531855d 100644 (file)
@@ -115,8 +115,7 @@ public class QProfileReset {
     DbSession dbSession = db.openSession(false);
     try {
       QualityProfileDto profile = factory.getOrCreate(dbSession, profileName);
-      BulkChangeResult result = doReset(dbSession, profile, activations);
-      return result;
+      return doReset(dbSession, profile, activations);
     } finally {
       dbSession.close();
     }
index e7f6e5aa3ca7e709cff45c226d8514f44709bd72..4337e9eaad290786fc3cc92f57992b22fc28fd6c 100644 (file)
@@ -73,7 +73,7 @@ public class ActiveRuleIndexer extends BaseIndexer {
     }
   }
 
-  private long doIndex(BulkIndexer bulk, Iterator<ActiveRuleDoc> activeRules) {
+  private static long doIndex(BulkIndexer bulk, Iterator<ActiveRuleDoc> activeRules) {
     bulk.start();
     long maxDate = 0L;
     while (activeRules.hasNext()) {
@@ -127,7 +127,7 @@ public class ActiveRuleIndexer extends BaseIndexer {
     return bulk;
   }
 
-  private IndexRequest newIndexRequest(ActiveRuleDoc doc) {
+  private static IndexRequest newIndexRequest(ActiveRuleDoc doc) {
     return new IndexRequest(INDEX, TYPE_ACTIVE_RULE, doc.key().toString())
       .parent(doc.key().ruleKey().toString())
       .routing(doc.key().ruleKey().repository())
index 91bf2feba743dc39b4f8a287dfa7375d5d22a4ab..1182058e3125acdca66d52a56cbd3090dd60844b 100644 (file)
@@ -181,7 +181,7 @@ public class DefaultRuleFinder implements RuleFinder {
       .setSeverity(severity != null ? RulePriority.valueOf(severity) : null)
       .setStatus(rule.getStatus().name())
       .setTags(rule.getTags().toArray(new String[rule.getTags().size()]))
-      .setId((rule.getId()));
+      .setId(rule.getId());
     if (description != null && descriptionFormat != null) {
       if (RuleDto.Format.HTML.equals(descriptionFormat)) {
         apiRule.setDescription(description);
index 64cc7365f96dc57a5fd9f30869d787af0808773d..22849e982440733ea49f9b64522c6973422f5611 100644 (file)
@@ -32,7 +32,6 @@ import org.sonar.db.DbSession;
 import org.sonar.db.MyBatis;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.rule.index.RuleIndexer;
 import org.sonar.server.user.UserSession;
 
index 75b4989141fef88993a321bb277d573aea7b0658..fec94c4e02bc453bdb27736254f83e4277184527 100644 (file)
  */
 package org.sonar.server.rule;
 
-import java.util.Collection;
-import java.util.List;
 import java.util.Set;
-import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.server.ServerSide;
@@ -30,8 +27,6 @@ import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.rule.index.RuleIndexDefinition;
 import org.sonar.server.rule.index.RuleQuery;
-import org.sonar.server.search.QueryContext;
-import org.sonar.server.search.Result;
 import org.sonar.server.user.UserSession;
 
 /**
@@ -41,45 +36,21 @@ import org.sonar.server.user.UserSession;
 public class RuleService {
 
   private final RuleIndex index;
-  private final RuleUpdater ruleUpdater;
   private final RuleCreator ruleCreator;
   private final RuleDeleter ruleDeleter;
   private final UserSession userSession;
 
-  public RuleService(RuleIndex index, RuleUpdater ruleUpdater, RuleCreator ruleCreator, RuleDeleter ruleDeleter, UserSession userSession) {
+  public RuleService(RuleIndex index, RuleCreator ruleCreator, RuleDeleter ruleDeleter, UserSession userSession) {
     this.index = index;
-    this.ruleUpdater = ruleUpdater;
     this.ruleCreator = ruleCreator;
     this.ruleDeleter = ruleDeleter;
     this.userSession = userSession;
   }
 
-  @CheckForNull
-  @Deprecated
-  public Rule getByKey(RuleKey key) {
-    throw new UnsupportedOperationException("Please use RuleDao");
-  }
-
-  @Deprecated
-  public List<Rule> getByKeys(Collection<RuleKey> keys) {
-    throw new UnsupportedOperationException("Please use RuleDao");
-  }
-
-  @Deprecated
-  public Rule getNonNullByKey(RuleKey key) {
-    throw new UnsupportedOperationException("Please use RuleDao");
-  }
-
   public RuleQuery newRuleQuery() {
     return new RuleQuery();
   }
 
-  public Result<Rule> search(RuleQuery query, QueryContext options) {
-    // TODO replace QueryContext by SearchOptions
-    // return index.search(query, options);
-    throw new UnsupportedOperationException("Wait for replacement of QueryContext by SearchOptions ");
-  }
-
   /**
    * List all tags, including system tags, defined on rules
    */
index 1d182f0722e06426d61d9482a7737ffbf1b9b1f8..2aa7e4cadc93f1561e1d8ec2b6a2f1c2e530c972 100644 (file)
@@ -69,6 +69,27 @@ import org.sonar.server.search.StickyFacetBuilder;
 
 import static org.sonar.server.es.EsUtils.SCROLL_TIME_IN_MINUTES;
 import static org.sonar.server.es.EsUtils.scrollIds;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_PROFILE_KEY;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_SEVERITY;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_ALL_TAGS;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_CREATED_AT;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_HTML_DESCRIPTION;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_INTERNAL_KEY;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_IS_TEMPLATE;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_KEY;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_KEY_AS_LIST;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_LANGUAGE;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_NAME;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_REPOSITORY;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_RULE_KEY;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_SEVERITY;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_STATUS;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_TEMPLATE_KEY;
+import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_UPDATED_AT;
+import static org.sonar.server.rule.index.RuleIndexDefinition.INDEX;
+import static org.sonar.server.rule.index.RuleIndexDefinition.TYPE_ACTIVE_RULE;
+import static org.sonar.server.rule.index.RuleIndexDefinition.TYPE_RULE;
 
 /**
  * The unique entry-point to interact with Elasticsearch index "rules".
@@ -107,8 +128,8 @@ public class RuleIndex extends BaseIndex {
 
   public SearchIdResult<RuleKey> search(RuleQuery query, SearchOptions options) {
     SearchRequestBuilder esSearch = getClient()
-      .prepareSearch(RuleIndexDefinition.INDEX)
-      .setTypes(RuleIndexDefinition.TYPE_RULE);
+      .prepareSearch(INDEX)
+      .setTypes(TYPE_RULE);
 
     QueryBuilder qb = buildQuery(query);
     Map<String, FilterBuilder> filters = buildFilters(query);
@@ -136,8 +157,8 @@ public class RuleIndex extends BaseIndex {
    */
   public Iterator<RuleKey> searchAll(RuleQuery query) {
     SearchRequestBuilder esSearch = getClient()
-      .prepareSearch(RuleIndexDefinition.INDEX)
-      .setTypes(RuleIndexDefinition.TYPE_RULE)
+      .prepareSearch(INDEX)
+      .setTypes(TYPE_RULE)
       .setSearchType(SearchType.SCAN)
       .setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES));
 
@@ -156,7 +177,7 @@ public class RuleIndex extends BaseIndex {
   }
 
   /* Build main query (search based) */
-  private QueryBuilder buildQuery(RuleQuery query) {
+  private static QueryBuilder buildQuery(RuleQuery query) {
 
     // No contextual query case
     String queryText = query.getQueryText();
@@ -170,29 +191,29 @@ public class RuleIndex extends BaseIndex {
 
     // Human readable type of querying
     qb.should(QueryBuilders.simpleQueryStringQuery(query.getQueryText())
-      .field(RuleIndexDefinition.FIELD_RULE_NAME + "." + BaseIndex.SEARCH_WORDS_SUFFIX, 20f)
-      .field(RuleIndexDefinition.FIELD_RULE_HTML_DESCRIPTION + "." + BaseIndex.SEARCH_WORDS_SUFFIX, 3f)
+      .field(FIELD_RULE_NAME + "." + BaseIndex.SEARCH_WORDS_SUFFIX, 20f)
+      .field(FIELD_RULE_HTML_DESCRIPTION + "." + BaseIndex.SEARCH_WORDS_SUFFIX, 3f)
       .defaultOperator(SimpleQueryStringBuilder.Operator.AND)
       ).boost(20f);
 
     // Match and partial Match queries
-    qb.should(this.termQuery(RuleIndexDefinition.FIELD_RULE_KEY, queryString, 15f));
-    qb.should(this.termQuery(RuleIndexDefinition.FIELD_RULE_KEY_AS_LIST, queryString, 35f));
-    qb.should(this.termQuery(RuleIndexDefinition.FIELD_RULE_LANGUAGE, queryString, 3f));
-    qb.should(this.termQuery(RuleIndexDefinition.FIELD_RULE_ALL_TAGS, queryString, 10f));
-    qb.should(this.termAnyQuery(RuleIndexDefinition.FIELD_RULE_ALL_TAGS, queryString, 1f));
+    qb.should(termQuery(FIELD_RULE_KEY, queryString, 15f));
+    qb.should(termQuery(FIELD_RULE_KEY_AS_LIST, queryString, 35f));
+    qb.should(termQuery(FIELD_RULE_LANGUAGE, queryString, 3f));
+    qb.should(termQuery(FIELD_RULE_ALL_TAGS, queryString, 10f));
+    qb.should(termAnyQuery(FIELD_RULE_ALL_TAGS, queryString, 1f));
 
     return qb;
   }
 
-  private QueryBuilder termQuery(String field, String query, float boost) {
+  private static QueryBuilder termQuery(String field, String query, float boost) {
     return QueryBuilders.multiMatchQuery(query,
       field, field + "." + IndexField.SEARCH_PARTIAL_SUFFIX)
       .operator(MatchQueryBuilder.Operator.AND)
       .boost(boost);
   }
 
-  private QueryBuilder termAnyQuery(String field, String query, float boost) {
+  private static QueryBuilder termAnyQuery(String field, String query, float boost) {
     return QueryBuilders.multiMatchQuery(query,
       field, field + "." + IndexField.SEARCH_PARTIAL_SUFFIX)
       .operator(MatchQueryBuilder.Operator.OR)
@@ -200,53 +221,53 @@ public class RuleIndex extends BaseIndex {
   }
 
   /* Build main filter (match based) */
-  private Map<String, FilterBuilder> buildFilters(RuleQuery query) {
+  private static Map<String, FilterBuilder> buildFilters(RuleQuery query) {
 
     Map<String, FilterBuilder> filters = new HashMap<>();
 
     /* Add enforced filter on rules that are REMOVED */
-    filters.put(RuleIndexDefinition.FIELD_RULE_STATUS,
+    filters.put(FIELD_RULE_STATUS,
       FilterBuilders.boolFilter().mustNot(
-        FilterBuilders.termFilter(RuleIndexDefinition.FIELD_RULE_STATUS,
+        FilterBuilders.termFilter(FIELD_RULE_STATUS,
           RuleStatus.REMOVED.toString())));
 
     if (!StringUtils.isEmpty(query.getInternalKey())) {
-      filters.put(RuleIndexDefinition.FIELD_RULE_INTERNAL_KEY,
-        FilterBuilders.termFilter(RuleIndexDefinition.FIELD_RULE_INTERNAL_KEY, query.getInternalKey()));
+      filters.put(FIELD_RULE_INTERNAL_KEY,
+        FilterBuilders.termFilter(FIELD_RULE_INTERNAL_KEY, query.getInternalKey()));
     }
 
     if (!StringUtils.isEmpty(query.getRuleKey())) {
-      filters.put(RuleIndexDefinition.FIELD_RULE_RULE_KEY,
-        FilterBuilders.termFilter(RuleIndexDefinition.FIELD_RULE_RULE_KEY, query.getRuleKey()));
+      filters.put(FIELD_RULE_RULE_KEY,
+        FilterBuilders.termFilter(FIELD_RULE_RULE_KEY, query.getRuleKey()));
     }
 
     if (!CollectionUtils.isEmpty(query.getLanguages())) {
-      filters.put(RuleIndexDefinition.FIELD_RULE_LANGUAGE,
-        FilterBuilders.termsFilter(RuleIndexDefinition.FIELD_RULE_LANGUAGE, query.getLanguages()));
+      filters.put(FIELD_RULE_LANGUAGE,
+        FilterBuilders.termsFilter(FIELD_RULE_LANGUAGE, query.getLanguages()));
     }
 
     if (!CollectionUtils.isEmpty(query.getRepositories())) {
-      filters.put(RuleIndexDefinition.FIELD_RULE_REPOSITORY,
-        FilterBuilders.termsFilter(RuleIndexDefinition.FIELD_RULE_REPOSITORY, query.getRepositories()));
+      filters.put(FIELD_RULE_REPOSITORY,
+        FilterBuilders.termsFilter(FIELD_RULE_REPOSITORY, query.getRepositories()));
     }
 
     if (!CollectionUtils.isEmpty(query.getSeverities())) {
-      filters.put(RuleIndexDefinition.FIELD_RULE_SEVERITY,
-        FilterBuilders.termsFilter(RuleIndexDefinition.FIELD_RULE_SEVERITY, query.getSeverities()));
+      filters.put(FIELD_RULE_SEVERITY,
+        FilterBuilders.termsFilter(FIELD_RULE_SEVERITY, query.getSeverities()));
     }
 
     if (!StringUtils.isEmpty(query.getKey())) {
-      filters.put(RuleIndexDefinition.FIELD_RULE_KEY,
-        FilterBuilders.termFilter(RuleIndexDefinition.FIELD_RULE_KEY, query.getKey()));
+      filters.put(FIELD_RULE_KEY,
+        FilterBuilders.termFilter(FIELD_RULE_KEY, query.getKey()));
     }
 
     if (!CollectionUtils.isEmpty(query.getTags())) {
-      filters.put(RuleIndexDefinition.FIELD_RULE_ALL_TAGS,
-        FilterBuilders.termsFilter(RuleIndexDefinition.FIELD_RULE_ALL_TAGS, query.getTags()));
+      filters.put(FIELD_RULE_ALL_TAGS,
+        FilterBuilders.termsFilter(FIELD_RULE_ALL_TAGS, query.getTags()));
     }
 
     if (query.getAvailableSinceLong() != null) {
-      filters.put("availableSince", FilterBuilders.rangeFilter(RuleIndexDefinition.FIELD_RULE_CREATED_AT)
+      filters.put("availableSince", FilterBuilders.rangeFilter(FIELD_RULE_CREATED_AT)
         .gte(query.getAvailableSinceLong()));
     }
 
@@ -256,27 +277,27 @@ public class RuleIndex extends BaseIndex {
       for (RuleStatus status : statusValues) {
         stringStatus.add(status.name());
       }
-      filters.put(RuleIndexDefinition.FIELD_RULE_STATUS,
-        FilterBuilders.termsFilter(RuleIndexDefinition.FIELD_RULE_STATUS, stringStatus));
+      filters.put(FIELD_RULE_STATUS,
+        FilterBuilders.termsFilter(FIELD_RULE_STATUS, stringStatus));
     }
 
     Boolean isTemplate = query.isTemplate();
     if (isTemplate != null) {
-      filters.put(RuleIndexDefinition.FIELD_RULE_IS_TEMPLATE,
-        FilterBuilders.termFilter(RuleIndexDefinition.FIELD_RULE_IS_TEMPLATE, Boolean.toString(isTemplate)));
+      filters.put(FIELD_RULE_IS_TEMPLATE,
+        FilterBuilders.termFilter(FIELD_RULE_IS_TEMPLATE, Boolean.toString(isTemplate)));
     }
 
     String template = query.templateKey();
     if (template != null) {
-      filters.put(RuleIndexDefinition.FIELD_RULE_TEMPLATE_KEY,
-        FilterBuilders.termFilter(RuleIndexDefinition.FIELD_RULE_TEMPLATE_KEY, template));
+      filters.put(FIELD_RULE_TEMPLATE_KEY,
+        FilterBuilders.termFilter(FIELD_RULE_TEMPLATE_KEY, template));
     }
 
     // ActiveRule Filter (profile and inheritance)
     BoolFilterBuilder childrenFilter = FilterBuilders.boolFilter();
-    addTermFilter(childrenFilter, RuleIndexDefinition.FIELD_ACTIVE_RULE_PROFILE_KEY, query.getQProfileKey());
-    addTermFilter(childrenFilter, RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE, query.getInheritance());
-    addTermFilter(childrenFilter, RuleIndexDefinition.FIELD_ACTIVE_RULE_SEVERITY, query.getActiveSeverities());
+    addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_PROFILE_KEY, query.getQProfileKey());
+    addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_INHERITANCE, query.getInheritance());
+    addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_SEVERITY, query.getActiveSeverities());
 
     // ChildQuery
     FilterBuilder childQuery;
@@ -289,19 +310,19 @@ public class RuleIndex extends BaseIndex {
     /** Implementation of activation query */
     if (Boolean.TRUE.equals(query.getActivation())) {
       filters.put("activation",
-        FilterBuilders.hasChildFilter(RuleIndexDefinition.TYPE_ACTIVE_RULE,
+        FilterBuilders.hasChildFilter(TYPE_ACTIVE_RULE,
           childQuery));
     } else if (Boolean.FALSE.equals(query.getActivation())) {
       filters.put("activation",
         FilterBuilders.boolFilter().mustNot(
-          FilterBuilders.hasChildFilter(RuleIndexDefinition.TYPE_ACTIVE_RULE,
+          FilterBuilders.hasChildFilter(TYPE_ACTIVE_RULE,
             childQuery)));
     }
 
     return filters;
   }
 
-  private BoolFilterBuilder addTermFilter(BoolFilterBuilder filter, String field, @Nullable Collection<String> values) {
+  private static BoolFilterBuilder addTermFilter(BoolFilterBuilder filter, String field, @Nullable Collection<String> values) {
     if (values != null && !values.isEmpty()) {
       BoolFilterBuilder valuesFilter = FilterBuilders.boolFilter();
       for (String value : values) {
@@ -313,14 +334,14 @@ public class RuleIndex extends BaseIndex {
     return filter;
   }
 
-  private BoolFilterBuilder addTermFilter(BoolFilterBuilder filter, String field, @Nullable String value) {
+  private static BoolFilterBuilder addTermFilter(BoolFilterBuilder filter, String field, @Nullable String value) {
     if (value != null && !value.isEmpty()) {
       filter.must(FilterBuilders.termFilter(field, value));
     }
     return filter;
   }
 
-  private Map<String, AggregationBuilder> getFacets(RuleQuery query, SearchOptions options, QueryBuilder queryBuilder, Map<String, FilterBuilder> filters) {
+  private static Map<String, AggregationBuilder> getFacets(RuleQuery query, SearchOptions options, QueryBuilder queryBuilder, Map<String, FilterBuilder> filters) {
     Map<String, AggregationBuilder> aggregations = new HashMap<>();
     StickyFacetBuilder stickyFacetBuilder = stickyFacetBuilder(queryBuilder, filters);
 
@@ -330,43 +351,43 @@ public class RuleIndex extends BaseIndex {
 
     if (options.getFacets().contains(FACET_SEVERITIES)) {
       aggregations.put(FACET_SEVERITIES,
-        stickyFacetBuilder.buildStickyFacet(RuleIndexDefinition.FIELD_RULE_SEVERITY, FACET_SEVERITIES, Severity.ALL.toArray()));
+        stickyFacetBuilder.buildStickyFacet(FIELD_RULE_SEVERITY, FACET_SEVERITIES, Severity.ALL.toArray()));
     }
 
     addActiveSeverityFacetIfNeeded(query, options, aggregations, stickyFacetBuilder);
     return aggregations;
   }
 
-  private void addDefaultFacets(RuleQuery query, SearchOptions options, Map<String, AggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
+  private static void addDefaultFacets(RuleQuery query, SearchOptions options, Map<String, AggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
     if (options.getFacets().contains(FACET_LANGUAGES) || options.getFacets().contains(FACET_OLD_DEFAULT)) {
       Collection<String> languages = query.getLanguages();
       aggregations.put(FACET_LANGUAGES,
-        stickyFacetBuilder.buildStickyFacet(RuleIndexDefinition.FIELD_RULE_LANGUAGE, FACET_LANGUAGES,
+        stickyFacetBuilder.buildStickyFacet(FIELD_RULE_LANGUAGE, FACET_LANGUAGES,
           languages == null ? new String[0] : languages.toArray()));
     }
     if (options.getFacets().contains(FACET_TAGS) || options.getFacets().contains(FACET_OLD_DEFAULT)) {
       Collection<String> tags = query.getTags();
       aggregations.put(FACET_TAGS,
-        stickyFacetBuilder.buildStickyFacet(RuleIndexDefinition.FIELD_RULE_ALL_TAGS, FACET_TAGS,
+        stickyFacetBuilder.buildStickyFacet(FIELD_RULE_ALL_TAGS, FACET_TAGS,
           tags == null ? new String[0] : tags.toArray()));
     }
     if (options.getFacets().contains("repositories") || options.getFacets().contains(FACET_OLD_DEFAULT)) {
       Collection<String> repositories = query.getRepositories();
       aggregations.put(FACET_REPOSITORIES,
-        stickyFacetBuilder.buildStickyFacet(RuleIndexDefinition.FIELD_RULE_REPOSITORY, FACET_REPOSITORIES,
+        stickyFacetBuilder.buildStickyFacet(FIELD_RULE_REPOSITORY, FACET_REPOSITORIES,
           repositories == null ? new String[0] : repositories.toArray()));
     }
   }
 
-  private void addStatusFacetIfNeeded(SearchOptions options, Map<String, AggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
+  private static void addStatusFacetIfNeeded(SearchOptions options, Map<String, AggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
     if (options.getFacets().contains(FACET_STATUSES)) {
-      BoolFilterBuilder facetFilter = stickyFacetBuilder.getStickyFacetFilter(RuleIndexDefinition.FIELD_RULE_STATUS);
+      BoolFilterBuilder facetFilter = stickyFacetBuilder.getStickyFacetFilter(FIELD_RULE_STATUS);
       AggregationBuilder statuses = AggregationBuilders.filter(FACET_STATUSES + "_filter")
         .filter(facetFilter)
         .subAggregation(
           AggregationBuilders
             .terms(FACET_STATUSES)
-            .field(RuleIndexDefinition.FIELD_RULE_STATUS)
+            .field(FIELD_RULE_STATUS)
             .include(Joiner.on('|').join(ALL_STATUSES_EXCEPT_REMOVED))
             .exclude(RuleStatus.REMOVED.toString())
             .size(ALL_STATUSES_EXCEPT_REMOVED.size()));
@@ -375,19 +396,19 @@ public class RuleIndex extends BaseIndex {
     }
   }
 
-  private void addActiveSeverityFacetIfNeeded(RuleQuery query, SearchOptions options, Map<String, AggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
+  private static void addActiveSeverityFacetIfNeeded(RuleQuery query, SearchOptions options, Map<String, AggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
     if (options.getFacets().contains(FACET_ACTIVE_SEVERITIES)) {
       // We are building a children aggregation on active rules
       // so the rule filter has to be used as parent filter for active rules
       // from which we remove filters that concern active rules ("activation")
       HasParentFilterBuilder ruleFilter = FilterBuilders.hasParentFilter(
-        RuleIndexDefinition.TYPE_RULE,
+        TYPE_RULE,
         stickyFacetBuilder.getStickyFacetFilter("activation"));
 
       // Rebuilding the active rule filter without severities
       BoolFilterBuilder childrenFilter = FilterBuilders.boolFilter();
-      this.addTermFilter(childrenFilter, RuleIndexDefinition.FIELD_ACTIVE_RULE_PROFILE_KEY, query.getQProfileKey());
-      this.addTermFilter(childrenFilter, RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE, query.getInheritance());
+      addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_PROFILE_KEY, query.getQProfileKey());
+      RuleIndex.addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_INHERITANCE, query.getInheritance());
       FilterBuilder activeRuleFilter;
       if (childrenFilter.hasClauses()) {
         activeRuleFilter = childrenFilter.must(ruleFilter);
@@ -396,13 +417,13 @@ public class RuleIndex extends BaseIndex {
       }
 
       AggregationBuilder activeSeverities = AggregationBuilders.children(FACET_ACTIVE_SEVERITIES + "_children")
-        .childType(RuleIndexDefinition.TYPE_ACTIVE_RULE)
+        .childType(TYPE_ACTIVE_RULE)
         .subAggregation(AggregationBuilders.filter(FACET_ACTIVE_SEVERITIES + "_filter")
           .filter(activeRuleFilter)
           .subAggregation(
             AggregationBuilders
               .terms(FACET_ACTIVE_SEVERITIES)
-              .field(RuleIndexDefinition.FIELD_ACTIVE_RULE_SEVERITY)
+              .field(FIELD_ACTIVE_RULE_SEVERITY)
               .include(Joiner.on('|').join(Severity.ALL))
               .size(Severity.ALL.size())));
 
@@ -410,11 +431,11 @@ public class RuleIndex extends BaseIndex {
     }
   }
 
-  private StickyFacetBuilder stickyFacetBuilder(QueryBuilder query, Map<String, FilterBuilder> filters) {
+  private static StickyFacetBuilder stickyFacetBuilder(QueryBuilder query, Map<String, FilterBuilder> filters) {
     return new StickyFacetBuilder(query, filters);
   }
 
-  private void setSorting(RuleQuery query, SearchRequestBuilder esSearch) {
+  private static void setSorting(RuleQuery query, SearchRequestBuilder esSearch) {
     /* integrate Query Sort */
     String queryText = query.getQueryText();
     if (query.getSortField() != null) {
@@ -428,21 +449,20 @@ public class RuleIndex extends BaseIndex {
     } else if (queryText != null && !queryText.isEmpty()) {
       esSearch.addSort(SortBuilders.scoreSort());
     } else {
-      esSearch.addSort(appendSortSuffixIfNeeded(RuleIndexDefinition.FIELD_RULE_UPDATED_AT), SortOrder.DESC);
+      esSearch.addSort(appendSortSuffixIfNeeded(FIELD_RULE_UPDATED_AT), SortOrder.DESC);
       // deterministic sort when exactly the same updated_at (same millisecond)
-      esSearch.addSort(appendSortSuffixIfNeeded(RuleIndexDefinition.FIELD_RULE_KEY), SortOrder.ASC);
+      esSearch.addSort(appendSortSuffixIfNeeded(FIELD_RULE_KEY), SortOrder.ASC);
     }
   }
 
-  public static String appendSortSuffixIfNeeded(String field) {
-      return field +
-        ((field.equals(RuleIndexDefinition.FIELD_RULE_NAME)
-        || field.equals(RuleIndexDefinition.FIELD_RULE_KEY))
-          ? "." + BaseIndex.SORT_SUFFIX
-          : "");
+  private static String appendSortSuffixIfNeeded(String field) {
+    return field +
+      ((field.equals(FIELD_RULE_NAME) || field.equals(FIELD_RULE_KEY))
+        ? ("." + BaseIndex.SORT_SUFFIX)
+        : "");
   }
 
-  private void setPagination(SearchOptions options, SearchRequestBuilder esSearch) {
+  private static void setPagination(SearchOptions options, SearchRequestBuilder esSearch) {
     esSearch.setFrom(options.getOffset());
     esSearch.setSize(options.getLimit());
   }
@@ -462,8 +482,8 @@ public class RuleIndex extends BaseIndex {
     if (query != null) {
       terms.include(".*" + query + ".*");
     }
-    SearchRequestBuilder request = this.getClient()
-      .prepareSearch(RuleIndexDefinition.INDEX)
+    SearchRequestBuilder request = getClient()
+      .prepareSearch(INDEX)
       .setQuery(QueryBuilders.matchAllQuery())
       .addAggregation(terms);
 
index 7592ac2557f1ec5c22974f0bc0302ef991fdd993..3cffbde568927e8fc3fa3ad7fe4666db546408f7 100644 (file)
@@ -62,7 +62,7 @@ public class RuleIndexer extends BaseIndexer {
     }
   }
 
-  private long doIndex(BulkIndexer bulk, Iterator<RuleDoc> rules) {
+  private static long doIndex(BulkIndexer bulk, Iterator<RuleDoc> rules) {
     bulk.start();
     long maxDate = 0L;
     while (rules.hasNext()) {
@@ -87,7 +87,7 @@ public class RuleIndexer extends BaseIndexer {
     return bulk;
   }
 
-  private IndexRequest newIndexRequest(RuleDoc rule) {
+  private static IndexRequest newIndexRequest(RuleDoc rule) {
     return new IndexRequest(INDEX, TYPE_RULE, rule.key().toString())
       .routing(rule.repository())
       .source(rule.getFields());
index 7ae51a9cac59c96c05cbf296a299104dcfd1d58d..e79761673ead1c490fa89bb8b5ecdb50d6d754bb 100644 (file)
@@ -21,7 +21,6 @@ package org.sonar.server.rule.index;
 
 import com.google.common.base.Preconditions;
 import java.util.Collection;
-import java.util.Date;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.sonar.api.rule.RuleStatus;
@@ -50,7 +49,6 @@ public class RuleQuery {
   private String internalKey;
   private String ruleKey;
 
-
   /**
    * TODO should not be public
    *
@@ -230,17 +228,6 @@ public class RuleQuery {
     return this;
   }
 
-  @Deprecated
-  public RuleQuery setAvailableSince(@Nullable Date d) {
-    this.availableSince = d.getTime();
-    return this;
-  }
-
-  @Deprecated
-  public Date getAvailableSince() {
-    return new Date(this.availableSince);
-  }
-
   public RuleQuery setAvailableSince(@Nullable Long l) {
     this.availableSince = l;
     return this;
index fdd7c5ff68320375073cd3878745c43c871af274..4cd8f84a599c54cbb22ba00720d34f1090343424 100644 (file)
@@ -344,7 +344,7 @@ public class SearchAction implements RulesWsAction {
     return searchQueryContext;
   }
 
-  private org.sonar.server.es.SearchOptions loadCommonContext(Request request) {
+  private static org.sonar.server.es.SearchOptions loadCommonContext(Request request) {
     int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE);
     org.sonar.server.es.SearchOptions context = new org.sonar.server.es.SearchOptions().addFields(request.paramAsStrings(Param.FIELDS));
     List<String> facets = request.paramAsStrings(Param.FACETS);