package org.sonar.server.activity.index;
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
import org.elasticsearch.action.support.replication.ReplicationType;
import org.elasticsearch.action.update.UpdateRequest;
import org.sonar.api.utils.KeyValueFormat;
import org.sonar.server.search.IndexField;
import org.sonar.server.search.Indexable;
-import java.lang.reflect.Field;
-import java.util.*;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
/**
* @since 4.4
*/
public class ActivityNormalizer extends BaseNormalizer<ActivityDto, String> {
-
public static final class LogFields extends Indexable {
public static final IndexField KEY = add(IndexField.Type.STRING, "key");
public static final IndexField LOGIN = addSearchable(IndexField.Type.STRING, "login");
public static final IndexField DETAILS = addSearchable(IndexField.Type.OBJECT, "details");
public static final IndexField MESSAGE = addSearchable(IndexField.Type.STRING, "message");
-
- public static Set<IndexField> ALL_FIELDS = getAllFields();
-
- private static Set<IndexField> getAllFields() {
- Set<IndexField> fields = new HashSet<IndexField>();
- for (Field classField : LogFields.class.getDeclaredFields()) {
- if (classField.getType().isAssignableFrom(IndexField.class)) {
- try {
- fields.add(IndexField.class.cast(classField.get(null)));
- } catch (IllegalAccessException e) {
- throw new IllegalStateException("Could not access Field '" + classField.getName() + "'", e);
- }
- }
- }
- return fields;
- }
+ public static final Set<IndexField> ALL_FIELDS = ImmutableSet.of(KEY, TYPE, ACTION, CREATED_AT, UPDATED_AT, LOGIN, DETAILS, MESSAGE);
}
public ActivityNormalizer(DbClient db) {
logDoc.put(LogFields.DETAILS.field(), KeyValueFormat.parse(dto.getData()));
- /* Creating updateRequest */
+ /* Creating updateRequest */
return ImmutableList.of(new UpdateRequest()
.id(dto.getKey())
.replicationType(ReplicationType.ASYNC)
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
import org.elasticsearch.action.update.UpdateRequest;
import org.sonar.core.issue.db.IssueAuthorizationDto;
import org.sonar.server.db.DbClient;
import org.sonar.server.search.IndexField;
import org.sonar.server.search.Indexable;
-import java.lang.reflect.Field;
-import java.util.*;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
public class IssueAuthorizationNormalizer extends BaseNormalizer<IssueAuthorizationDto, String> {
public static final IndexField USERS = add(IndexField.Type.STRING, "users");
public static final IndexField UPDATED_AT = add(IndexField.Type.DATE, BaseNormalizer.UPDATED_AT_FIELD);
- public static final Set<IndexField> ALL_FIELDS = getAllFields();
-
- private static Set<IndexField> getAllFields() {
- Set<IndexField> fields = new HashSet<IndexField>();
- for (Field classField : IssueAuthorizationField.class.getDeclaredFields()) {
- if (classField.getType().isAssignableFrom(IndexField.class)) {
- try {
- fields.add(IndexField.class.cast(classField.get(null)));
- } catch (IllegalAccessException e) {
- throw new IllegalStateException("Could not access Field '" + classField.getName() + "'", e);
- }
- }
- }
- return fields;
- }
+ public static final Set<IndexField> ALL_FIELDS = ImmutableSet.of(PROJECT, PERMISSION, GROUPS, USERS, UPDATED_AT);
}
@Override
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
import org.elasticsearch.action.update.UpdateRequest;
import org.sonar.api.rule.Severity;
import org.sonar.core.issue.db.IssueDto;
import org.sonar.server.search.IndexField;
import org.sonar.server.search.Indexable;
-import java.lang.reflect.Field;
-import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public static final IndexField RULE_KEY = add(IndexField.Type.STRING, "ruleKey");
public static final IndexField FILE_PATH = addSortable(IndexField.Type.STRING, "filePath");
- public static final Set<IndexField> ALL_FIELDS = getAllFields();
-
- private static final Set<IndexField> getAllFields() {
- Set<IndexField> fields = new HashSet<IndexField>();
- for (Field classField : IssueField.class.getDeclaredFields()) {
- if (classField.getType().isAssignableFrom(IndexField.class)) {
- try {
- fields.add(IndexField.class.cast(classField.get(null)));
- } catch (IllegalAccessException e) {
- throw new IllegalStateException("Could not access Field '" + classField.getName() + "'", e);
- }
- }
- }
- return fields;
- }
+ public static final Set<IndexField> ALL_FIELDS = ImmutableSet.of(KEY, CREATED_AT, UPDATED_AT, PROJECT, COMPONENT,
+ MODULE, MODULE_PATH, ACTION_PLAN, ASSIGNEE, ATTRIBUTES, AUTHOR_LOGIN, DEBT, EFFORT, ISSUE_CREATED_AT,
+ ISSUE_UPDATED_AT, ISSUE_CLOSE_DATE, LINE, MESSAGE, RESOLUTION, REPORTER, STATUS, SEVERITY, SEVERITY_VALUE,
+ LANGUAGE, RULE_KEY, FILE_PATH);
}
@Override
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
import org.elasticsearch.action.support.replication.ReplicationType;
import org.elasticsearch.action.update.UpdateRequest;
import org.sonar.core.persistence.DbSession;
import org.sonar.server.search.IndexField;
import org.sonar.server.search.Indexable;
-import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
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 = getAllFields();
-
- private static Set<IndexField> getAllFields() {
- Set<IndexField> fields = new HashSet<IndexField>();
- for (Field classField : ActiveRuleField.class.getDeclaredFields()) {
- if (classField.getType().isAssignableFrom(IndexField.class)) {
- try {
- fields.add(IndexField.class.cast(classField.get(null)));
- } catch (IllegalAccessException e) {
- throw new IllegalStateException("Can not introspect active rule fields", e);
- }
- }
- }
- return fields;
- }
+ 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 = getAllFields();
-
- private static Set<IndexField> getAllFields() {
- Set<IndexField> fields = new HashSet<IndexField>();
- for (Field classField : ActiveRuleParamField.class.getDeclaredFields()) {
- if (classField.getType().isAssignableFrom(IndexField.class)) {
- try {
- fields.add(IndexField.class.cast(classField.get(null)));
- } catch (IllegalAccessException e) {
- throw new IllegalStateException("Can not introspect active rule param fields", e);
- }
- }
- }
- return fields;
- }
+ public static final Set<IndexField> ALL_FIELDS = ImmutableSet.of(NAME, VALUE);
}
public ActiveRuleNormalizer(DbClient db) {
.doc(newRule)
.upsert(getUpsertFor(ActiveRuleField.ALL_FIELDS, newRule)));
- //Get the RuleParameters
+ // Get the RuleParameters
for (ActiveRuleParamDto param : db.activeRuleDao().findParamsByActiveRuleKey(session, key)) {
requests.addAll(normalizeNested(param, key));
}
newParam.put(ActiveRuleParamField.VALUE.field(), param.getValue());
return ImmutableList.of(new UpdateRequest()
- .replicationType(ReplicationType.ASYNC)
- .routing(key.ruleKey().toString())
- .id(key.toString())
- .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, ActiveRuleField.PARAMS.field())
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, newParam)
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, ActiveRuleParamField.NAME.field())
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getKey())
- );
+ .replicationType(ReplicationType.ASYNC)
+ .routing(key.ruleKey().toString())
+ .id(key.toString())
+ .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, ActiveRuleField.PARAMS.field())
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, newParam)
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, ActiveRuleParamField.NAME.field())
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getKey())
+ );
}
private List<UpdateRequest> nestedDelete(ActiveRuleParamDto param, ActiveRuleKey key) {
return ImmutableList.of(new UpdateRequest()
- .replicationType(ReplicationType.ASYNC)
- .routing(key.ruleKey().toString())
- .id(key.toString())
- .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, ActiveRuleField.PARAMS.field())
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, null)
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, ActiveRuleParamField.NAME.field())
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getKey())
- );
+ .replicationType(ReplicationType.ASYNC)
+ .routing(key.ruleKey().toString())
+ .id(key.toString())
+ .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, ActiveRuleField.PARAMS.field())
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, null)
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, ActiveRuleParamField.NAME.field())
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getKey())
+ );
}
}
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 org.elasticsearch.action.update.UpdateRequest;
import org.sonar.api.rule.RuleKey;
public static final IndexField DESCRIPTION = addSearchable(IndexField.Type.TEXT, "description");
public static final IndexField DEFAULT_VALUE = add(IndexField.Type.STRING, "defaultValue");
- public static Set<IndexField> ALL_FIELDS = getAllFields();
-
- private static final Set<IndexField> getAllFields() {
- Set<IndexField> fields = new HashSet<IndexField>();
- for (Field classField : RuleParamField.class.getDeclaredFields()) {
- if (classField.getType().isAssignableFrom(IndexField.class)) {
- try {
- fields.add(IndexField.class.cast(classField.get(null)));
- } catch (IllegalAccessException e) {
- throw new IllegalStateException("Could not access Field '" + classField.getName() + "'", e);
- }
- }
- }
- return fields;
- }
+ public static final Set<IndexField> ALL_FIELDS = ImmutableSet.of(NAME, TYPE, DESCRIPTION, DEFAULT_VALUE);
}
public static final class RuleField extends Indexable {
public static final IndexField ALL_TAGS = addSearchable(IndexField.Type.STRING, "allTags");
public static final IndexField PARAMS = addEmbedded("params", RuleParamField.ALL_FIELDS);
- public static final Set<IndexField> ALL_FIELDS = getAllFields();
-
- private static final Set<IndexField> getAllFields() {
- Set<IndexField> fields = new HashSet<IndexField>();
- for (Field classField : RuleField.class.getDeclaredFields()) {
- if (classField.getType().isAssignableFrom(IndexField.class)) {
- try {
- fields.add(IndexField.class.cast(classField.get(null)));
- } catch (IllegalAccessException e) {
- throw new IllegalStateException("Could not access Field '" + classField.getName() + "'", e);
- }
- }
- }
- return fields;
- }
+ public static final Set<IndexField> ALL_FIELDS = ImmutableSet.of(ID, KEY, _KEY, REPOSITORY, RULE_KEY, NAME, CREATED_AT,
+ UPDATED_AT, HTML_DESCRIPTION, MARKDOWN_DESCRIPTION, SEVERITY, STATUS, FIX_DESCRIPTION,
+ LANGUAGE, TAGS, SYSTEM_TAGS, INTERNAL_KEY, IS_TEMPLATE, TEMPLATE_KEY, DEFAULT_DEBT_FUNCTION_TYPE,
+ DEFAULT_DEBT_FUNCTION_COEFFICIENT, DEFAULT_DEBT_FUNCTION_OFFSET, DEBT_FUNCTION_TYPE, DEBT_FUNCTION_COEFFICIENT,
+ DEBT_FUNCTION_OFFSET, DEFAULT_CHARACTERISTIC, DEFAULT_SUB_CHARACTERISTIC, CHARACTERISTIC, SUB_CHARACTERISTIC,
+ NOTE, NOTE_LOGIN, NOTE_CREATED_AT, NOTE_UPDATED_AT, ALL_TAGS, PARAMS);
+ /**
+ * Warning - O(n) complexity
+ */
public static IndexField of(String fieldName) {
for (IndexField field : ALL_FIELDS) {
if (field.field().equals(fieldName)) {
newParam.put(RuleParamField.DEFAULT_VALUE.field(), param.getDefaultValue());
return ImmutableList.of(new UpdateRequest()
- .id(key.toString())
- .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, RuleField.PARAMS.field())
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, newParam)
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, RuleParamField.NAME.field())
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getName())
- );
+ .id(key.toString())
+ .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, RuleField.PARAMS.field())
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, newParam)
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, RuleParamField.NAME.field())
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getName())
+ );
}
private List<UpdateRequest> nestedDelete(RuleParamDto param, RuleKey key) {
return ImmutableList.of(new UpdateRequest()
- .id(key.toString())
- .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, RuleField.PARAMS.field())
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, null)
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, RuleParamField.NAME.field())
- .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getName())
- );
+ .id(key.toString())
+ .script(ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME)
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, RuleField.PARAMS.field())
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, null)
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, RuleParamField.NAME.field())
+ .addScriptParam(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, param.getName())
+ );
}
}
import org.apache.commons.lang.builder.ToStringStyle;
import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
public class Indexable {
- private static final Set<IndexField> ALL_FIELDS = new HashSet<IndexField>();
-
- public static IndexField add(IndexField.Type type, String field){
- IndexField indexField = new IndexField(type, field);
- ALL_FIELDS.add(indexField);
- return indexField;
+ public static IndexField add(IndexField.Type type, String field) {
+ return new IndexField(type, field);
}
public static IndexField addEmbedded(String field, Collection<IndexField> nestedFields) {
- IndexField indexField = new IndexField(IndexField.Type.OBJECT, field, nestedFields);
- ALL_FIELDS.add(indexField);
- return indexField;
+ return new IndexField(IndexField.Type.OBJECT, field, nestedFields);
}
-
- public static IndexField addSearchable(IndexField.Type type, String field){
- IndexField indexField = new IndexField(type, field)
- .setSearchable(true);
- ALL_FIELDS.add(indexField);
- return indexField;
+ public static IndexField addSearchable(IndexField.Type type, String field) {
+ return new IndexField(type, field).setSearchable(true);
}
-
public static IndexField addSortableAndSearchable(IndexField.Type type, String field) {
- IndexField indexField = new IndexField(type, field)
- .setSearchable(true)
- .setSortable(true);
- ALL_FIELDS.add(indexField);
- return indexField;
+ return new IndexField(type, field).setSearchable(true).setSortable(true);
}
- public static IndexField addSortable(IndexField.Type type, String field){
- IndexField indexField = new IndexField(type, field)
- .setSortable(true);
- ALL_FIELDS.add(indexField);
- return indexField;
+ public static IndexField addSortable(IndexField.Type type, String field) {
+ return new IndexField(type, field).setSortable(true);
}
@Override
return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
-