use of eu.esdihumboldt.hale.common.core.io.ValueList in project hale by halestudio.
the class Message method applyFromValue.
/**
* Apply from a given {@link Value}, if the value represents a
* {@link Message}.
*
* @param value the value to interpret as {@link Message}
* @return this for chaining
*/
@Nullable
public Message applyFromValue(Value value) {
ValueProperties props = value.as(ValueProperties.class);
if (props != null) {
String text = props.get("text").as(String.class);
if (text != null && !text.isEmpty()) {
//
setText(text).setAuthor(//
props.getSafe("author").as(String.class)).setFormat(//
props.getSafe("format").as(String.class)).setCategory(//
props.getSafe("category").as(String.class)).setDismissed(//
props.getSafe("dismissed").as(Boolean.class, false)).setCustomPayload(props.get("payload"));
ValueList tags = props.getSafe("tags").as(ValueList.class);
this.tags.clear();
if (tags != null) {
for (Value tag : tags) {
String tagString = tag.as(String.class);
if (tagString != null) {
addTag(tagString);
}
}
}
ValueList comments = props.getSafe("comments").as(ValueList.class);
this.comments.clear();
if (comments != null) {
for (Value commentVal : comments) {
Optional<Comment> comment = Comment.fromValue(commentVal);
comment.ifPresent(c -> addComment(c));
}
}
}
}
return this;
}
use of eu.esdihumboldt.hale.common.core.io.ValueList in project hale by halestudio.
the class Message method toProperties.
/**
* Convert to a {@link Value}.
*
* @return the value representation of the comment
*/
public ValueProperties toProperties() {
ValueProperties props = new ValueProperties();
if (author != null) {
props.put("author", Value.of(author));
}
if (text != null) {
props.put("text", Value.of(text));
}
if (format != null) {
props.put("format", Value.of(format));
}
if (category != null) {
props.put("category", Value.of(category));
}
if (dismissed) {
props.put("dismissed", Value.of(dismissed));
}
if (customPayload != null) {
props.put("payload", customPayload);
}
if (!tags.isEmpty()) {
ValueList tagList = new ValueList(tags.stream().map(tag -> Value.of(tag)).collect(Collectors.toList()));
props.put("tags", tagList.toValue());
}
if (!comments.isEmpty()) {
ValueList commentList = new ValueList(comments.stream().map(comment -> Value.of(comment)).collect(Collectors.toList()));
props.put("comments", commentList.toValue());
}
return props;
}
use of eu.esdihumboldt.hale.common.core.io.ValueList in project hale by halestudio.
the class SQLArrayFactory method store.
@Override
public Value store(SQLArray constraint, TypeReferenceBuilder typeIndex) throws Exception {
ValueProperties props = new ValueProperties();
if (constraint.isArray()) {
// store element class
Class<?> elementType = constraint.getElementType();
if (elementType != null) {
props.put(NAME_ELEMENT_CLASS, Value.of(elementType.getName()));
}
// store element database type name
String typeName = constraint.getElementTypeName();
if (typeName != null) {
props.put(NAME_ELEMENT_TYPE_NAME, Value.of(typeName));
}
// store dimension
props.put(NAME_DIMENSION, Value.of(constraint.getDimension()));
// store array dimension sizes
List<Integer> sizes = constraint.getSizes();
if (sizes != null && !sizes.isEmpty()) {
ValueList sizeList = new ValueList(Collections2.transform(sizes, new Function<Integer, Value>() {
@Override
public Value apply(Integer input) {
return Value.of(input);
}
}));
props.put(NAME_SIZES, Value.complex(sizeList));
}
return props.toValue();
} else {
return null;
}
}
use of eu.esdihumboldt.hale.common.core.io.ValueList in project hale by halestudio.
the class SQLArrayFactory method restore.
@Override
public SQLArray restore(Value value, Definition<?> definition, TypeResolver typeIndex, ClassResolver resolver) throws Exception {
ValueProperties props = value.as(ValueProperties.class);
if (props != null) {
// read element class
Class<?> elementType = null;
String className = props.getSafe(NAME_ELEMENT_CLASS).as(String.class);
if (className != null) {
elementType = resolver.loadClass(className);
}
// read element database type name
String elementTypeName = props.getSafe(NAME_ELEMENT_TYPE_NAME).as(String.class);
// read dimension
int dimension = props.getSafe(NAME_DIMENSION).as(Integer.class, 0);
// read array dimension sizes
int[] sizes = null;
ValueList sizeList = props.getSafe(NAME_SIZES).as(ValueList.class);
if (sizeList != null) {
sizes = new int[sizeList.size()];
int index = 0;
for (Value size : sizeList) {
sizes[index] = size.as(Integer.class, 0);
index++;
}
}
return new SQLArray(elementType, elementTypeName, dimension, sizes);
}
return SQLArray.NO_ARRAY;
}
use of eu.esdihumboldt.hale.common.core.io.ValueList in project hale by halestudio.
the class XmlElementsFactory method restore.
@Override
public XmlElements restore(Value value, Definition<?> definition, TypeResolver typeResolver, ClassResolver classResolver) throws Exception {
XmlElements result = new XmlElements();
ValueList list = value.as(ValueList.class);
if (list != null) {
for (Value val : list) {
XmlElement element = valueToElement(val, definition, typeResolver);
if (element != null) {
result.addElement(element);
}
}
}
return result;
}
Aggregations