
com.xlrit.gears.engine.meta.BaseField Maven / Gradle / Ivy
package com.xlrit.gears.engine.meta;
import com.xlrit.gears.base.execution.Execution;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xlrit.gears.base.meta.Calculated;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
public abstract class BaseField implements PathEval {
protected final Property property;
protected final String name;
protected final String label;
protected final boolean calculated;
protected BaseField(Property property, String name, String label) {
this.property = checkNotNull(property);
this.name = checkNotNull(name);
this.label = checkNotNull(label);
this.calculated = property.isAnnotationPresent(Calculated.class);
}
public final String getName() {
return name;
}
public final String getLabel() {
return label;
}
public final String getPropertyName() {
return property.getName();
}
public final boolean isReadOnly() {
return property.isReadOnly();
}
public final boolean isCalculated() {
return calculated;
}
public final boolean isEntityReference() {
return getTypeInfo().isEntityReference();
}
public abstract TypeInfo getTypeInfo();
public final Object getValue(Object instance) {
checkNotNull(instance, "Argument 'instance' is required");
return property.getValue(instance);
}
public final void setValue(Object instance, Object value) {
property.setValue(instance, value);
}
public final void setValueBidi(Object instance, Object value) {
property.setValueBidi(instance, value);
}
/**
* Sets the value bidirectionally if possible, otherwise sets value normally
* @param instance Entity instance on which to set the value.
* @param value Value to set. Should be an entity.
*/
public final void trySetValueBidi(Object instance, Object value) {
if (property.isBidirectional()) property.setValueBidi(instance, value);
else property.setValue(instance, value);
}
public JsonNode serialize(ObjectMapper objectMapper, Object instance, PrintOptions printOptions) {
return getTypeInfo().serialize(instance, objectMapper, printOptions);
}
public Object deserialize(ObjectMapper objectMapper, JsonNode node) {
return getTypeInfo().deserialize(node, objectMapper);
}
@Override
public R evaluate(String path, Execution execution, FieldFunction f) {
throw new UnsupportedOperationException("Cannot evaluate path '" + path + "' in " + this);
}
public EntityInfo> getAssociatedEntityInfo() {
checkState(this.isEntityReference(), "must be an association");
return asEntityInfo(getTypeInfo());
}
private EntityInfo> asEntityInfo(TypeInfo typeInfo) {
if (typeInfo instanceof EntityInfo) {
return (EntityInfo>) typeInfo;
}
if (typeInfo instanceof MultipleInfo) {
return asEntityInfo(((MultipleInfo) typeInfo).getElementType());
}
throw new UnsupportedOperationException("Cannot coerce as EntityInfo: " + typeInfo);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy