com.memority.toolkit.rule.api.RuleConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of toolkit-rule-api Show documentation
Show all versions of toolkit-rule-api Show documentation
This artifact provides the API classes that are necessary to implement the contracts of Memority configuration Rules.
/*
* Copyright (c) 2016-2023 Memority. All Rights Reserved.
*
* This file is part of Memority Toolkit API , a Memority project.
*
* This file is released under the Memority Public Artifacts End-User License Agreement,
* see
* Unauthorized copying of this file, via any medium is strictly prohibited.
*/
package com.memority.toolkit.rule.api;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.DatabindContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
import com.fasterxml.jackson.databind.type.TypeFactory;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.xml.bind.annotation.XmlType;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* A RuleConfiguration contains information that are passed to a ConfigurableRule.
* It is specifically used for Java implementation of rules to gives more parameters
* which are used to apply the rule.
*/
// Note: this json type info configuration is used only when the rule configuration is not deserialized
// through another type info configuration in the parent (enclosing) type, such as in RuleDefinition
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.PROPERTY, property = "@classLabel")
@JsonTypeIdResolver(RuleConfiguration.RuleConfigurationTypeIdResolver.class)
@XmlType(name = XmlConstants.NAME_TYPE_RULECONFIGURATION)
public abstract class RuleConfiguration implements Serializable {
/**
* See CTD-348. We must implement equals and hashCode so that Hibernate can detect configuration changes and
* generate the corresponding DB update statement. Otherwise Hibernate won't update the configuration.
*/
@Override
public boolean equals(Object obj) {
throw new UnsupportedOperationException("equals must be implemented by subclasses!");
}
@Override
public int hashCode() {
throw new UnsupportedOperationException("hashCode must be implemented by subclasses!");
}
/**
* Empty Configuration used to handle corner case of deserialization. Will never be actually resolved/instantiated.
* See CTD-7500
*/
@Data
@EqualsAndHashCode(callSuper = false)
public static class EmptyRuleConfiguration extends RuleConfiguration {
}
public static class Registry {
private static final Map> labelToClass = new HashMap<>();
private static final Map, String> classToLabel = new HashMap<>();
public static void registerType(JavaRule javaRule, Class extends RuleConfiguration> clazz) {
for (String label:javaRule.label()) {
labelToClass.put(label, clazz);
}
classToLabel.put(clazz, javaRule.label()[0]);
}
public static Class extends RuleConfiguration> classForLabel(String label) {
return labelToClass.get(label);
}
public static String labelForClass(Class extends RuleConfiguration> clazz) {
return classToLabel.get(clazz);
}
}
static class RuleConfigurationTypeIdResolver implements TypeIdResolver {
@Override
public void init(JavaType javaType) {
// No op
}
@Override
@SuppressWarnings("unchecked")
public String idFromValue(Object o) {
if (o == null) {
return null;
}
return Registry.labelForClass((Class extends RuleConfiguration>) o.getClass());
}
@Override
public String idFromValueAndType(Object o, Class> aClass) {
if(EmptyRuleConfiguration.class.equals(aClass)) {
return null;
}
return idFromValue(o);
}
@Override
public String idFromBaseType() {
return null;
}
@Override
public JavaType typeFromId(DatabindContext databindContext, String s) throws IOException {
return TypeFactory.defaultInstance().constructType(Registry.classForLabel(s));
}
@Override
public String getDescForKnownTypeIds() {
return null;
}
@Override
public JsonTypeInfo.Id getMechanism() {
return JsonTypeInfo.Id.CUSTOM;
}
}
}