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)));
}
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)));
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();
.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())
+ );
+ }
}
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");
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;
}
// 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);
}
}