Skip to content

Commit

Permalink
Adding Jackson code in order to allow importing of rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jgambarios authored and dsilvam committed Feb 22, 2020
1 parent 4f1a076 commit b69c38c
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 67 deletions.
Expand Up @@ -10,10 +10,15 @@
import com.dotmarketing.portlets.rules.model.ParameterModel;
import com.dotmarketing.portlets.rules.parameter.ParameterDefinition;
import com.dotmarketing.util.Logger;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.io.Serializable;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.elasticsearch.common.Nullable;

/**
* @author Geoff M. Granum
Expand All @@ -26,7 +31,20 @@ public abstract class RuleComponentDefinition<T extends RuleComponentInstance> i
private final Map<String, ParameterDefinition> parameterDefinitions;

protected RuleComponentDefinition(String i18nKey, ParameterDefinition... parameterDefinitions) {
this.id = this.getClass().getSimpleName();
this(null, i18nKey, parameterDefinitions);
}

/**
* @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding
* infrastructure
*/
protected RuleComponentDefinition(String id, String i18nKey,
ParameterDefinition... parameterDefinitions) {

if (null == id) {
id = this.getClass().getSimpleName();
}
this.id = id;
this.i18nKey = i18nKey;
Map<String, ParameterDefinition> defs = Maps.newLinkedHashMap();
for (ParameterDefinition def : parameterDefinitions) {
Expand Down Expand Up @@ -120,5 +138,41 @@ public String toLogString(){
}

public abstract boolean evaluate(HttpServletRequest request, HttpServletResponse response, T instance);
}

/**
* Utility type used to correctly read immutable object from JSON representation.
*
* @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding
* infrastructure
*/
@Deprecated
@JsonDeserialize
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
static protected final class Json {

@javax.annotation.Nullable
public String id;
@javax.annotation.Nullable
public String i18nKey;
@javax.annotation.Nullable
public Map<String, ParameterDefinition> parameterDefinitions;

@JsonProperty("id")
public void setId(@Nullable String id) {
this.id = id;
}

@JsonProperty("i18nKey")
public void setI18nKey(@Nullable String i18nKey) {
this.i18nKey = i18nKey;
}

@JsonProperty("parameterDefinitions")
public void setParameterDefinitions(
@Nullable Map<String, ParameterDefinition> parameterDefinitions) {
this.parameterDefinitions = parameterDefinitions;
}
}

}
Expand Up @@ -3,12 +3,50 @@
import com.dotmarketing.portlets.rules.RuleComponentDefinition;
import com.dotmarketing.portlets.rules.RuleComponentInstance;
import com.dotmarketing.portlets.rules.parameter.ParameterDefinition;
import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class Conditionlet<T extends RuleComponentInstance> extends RuleComponentDefinition<T> {

public static final String COMPARISON_KEY = "comparison";

/**
* @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding
* infrastructure
*/
protected Conditionlet(String id, String i18nKey, ParameterDefinition... parameterDefinitions) {
super(id, i18nKey, parameterDefinitions);
}

protected Conditionlet(String i18nKey, ParameterDefinition... parameterDefinitions) {
super(i18nKey, parameterDefinitions);
}
}

/**
* @param json A JSON-bindable data structure
* @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding
* infrastructure
*/
@Deprecated
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
static Conditionlet fromJson(RuleComponentDefinition.Json json) {

return new Conditionlet(json.id, json.i18nKey,
json.parameterDefinitions.values().toArray(new ParameterDefinition[]{})) {

@Override
public RuleComponentInstance instanceFrom(Map parameters) {
throw new UnsupportedOperationException();
}

@Override
public boolean evaluate(HttpServletRequest request, HttpServletResponse response,
RuleComponentInstance instance) {
throw new UnsupportedOperationException();
}
};
}

}
Expand Up @@ -6,7 +6,13 @@
import com.dotmarketing.portlets.rules.model.ParameterModel;
import com.dotmarketing.portlets.rules.parameter.display.Input;
import com.dotmarketing.portlets.rules.parameter.type.DataType;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.common.Nullable;

public class ParameterDefinition<T extends DataType> {

Expand Down Expand Up @@ -58,5 +64,67 @@ public Input<T> getInputType() {
public void checkValid(ParameterModel model) throws InvalidRuleParameterException, RuleEngineException {
this.inputType.checkValid(model.getValue());
}
}

/**
* Utility type used to correctly read immutable object from JSON representation.
*
* @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding
* infrastructure
*/
@Deprecated
@JsonDeserialize
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
static final class Json {

@javax.annotation.Nullable
String key;
@javax.annotation.Nullable
String i18nBaseKey;
@javax.annotation.Nullable
String defaultValue;
@javax.annotation.Nullable
Input inputType;
@javax.annotation.Nullable
int priority;

@JsonProperty("key")
public void setKey(@Nullable String key) {
this.key = key;
}

@JsonProperty("i18nBaseKey")
public void setI18nBaseKey(@Nullable String i18nBaseKey) {
this.i18nBaseKey = i18nBaseKey;
}

@JsonProperty("defaultValue")
public void setDefaultValue(@Nullable String defaultValue) {
this.defaultValue = defaultValue;
}

@JsonProperty("inputType")
public void setInputType(@Nullable Input inputType) {
this.inputType = inputType;
}

@JsonProperty("priority")
public void setPriority(@Nullable int priority) {
this.priority = priority;
}

}

/**
* @param json A JSON-bindable data structure
* @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding
* infrastructure
*/
@Deprecated
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
static ParameterDefinition fromJson(ParameterDefinition.Json json) {
return new ParameterDefinition(json.priority, json.key, json.i18nBaseKey, json.inputType,
json.defaultValue);
}

}
Expand Up @@ -2,6 +2,12 @@

import com.dotcms.rest.exception.InvalidRuleParameterException;
import com.dotmarketing.portlets.rules.parameter.type.DataType;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.elasticsearch.common.Nullable;

/**
* @author Geoff M. Granum
Expand Down Expand Up @@ -32,5 +38,45 @@ public T getDataType() {
public void checkValid(String value) throws InvalidRuleParameterException{
return;
}
}

/**
* Utility type used to correctly read immutable object from JSON representation.
*
* @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding
* infrastructure
*/
@Deprecated
@JsonDeserialize
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
static final class Json {

@javax.annotation.Nullable
String id;
@javax.annotation.Nullable
DataType dataType;

@JsonProperty("id")
public void setKey(@Nullable String id) {
this.id = id;
}

@JsonProperty("dataType")
public void setDataType(@Nullable DataType dataType) {
this.dataType = dataType;
}

}

/**
* @param json A JSON-bindable data structure
* @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding
* infrastructure
*/
@Deprecated
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
static Input fromJson(Input.Json json) {
return new Input(json.id, json.dataType);
}

}
Expand Up @@ -3,9 +3,15 @@
import com.dotcms.repackage.com.google.common.collect.ImmutableMap;
import com.dotcms.repackage.com.google.common.collect.Maps;
import com.dotmarketing.portlets.rules.parameter.type.constraint.TypeConstraint;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.Collections;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.common.Nullable;

/**
* @author Geoff M. Granum
Expand All @@ -23,6 +29,12 @@ public DataType(String id, String errorMessageKey) {
this.errorMessageKey = errorMessageKey;
}

public DataType(String id, String errorMessageKey, Map<String, TypeConstraint> restrictions) {
this.id = id;
this.errorMessageKey = errorMessageKey;
this.restrictions = restrictions;
}

public String getId() {
return id;
}
Expand Down Expand Up @@ -77,5 +89,57 @@ public DataType<T> restrict(TypeConstraint restriction) {
public Map<String, TypeConstraint> getConstraints() {
return ImmutableMap.copyOf(this.restrictions);
}
}


/**
* Utility type used to correctly read immutable object from JSON representation.
*
* @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding
* infrastructure
*/
@Deprecated
@JsonDeserialize
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
static final class Json {

@javax.annotation.Nullable
String id;
@javax.annotation.Nullable
String errorMessageKey;
@javax.annotation.Nullable
Map<String, TypeConstraint> restrictions;

@JsonProperty("id")
public void setKey(@Nullable String id) {
this.id = id;
}

@JsonProperty("errorMessageKey")
public void setDataType(@Nullable String errorMessageKey) {
this.errorMessageKey = errorMessageKey;
}

@JsonProperty("constraints")
public void setRestrictions(@Nullable Map<String, TypeConstraint> restrictions) {
this.restrictions = restrictions;
}

}

/**
* @param json A JSON-bindable data structure
* @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding
* infrastructure
*/
@Deprecated
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
static DataType fromJson(DataType.Json json) {
return new DataType(json.id, json.errorMessageKey, json.restrictions) {
@Override
public Object convert(String from) {
throw new UnsupportedOperationException();
}
};
}

}

0 comments on commit b69c38c

Please sign in to comment.