Search in sources :

Example 16 with ValueList

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;
}
Also used : ValueProperties(eu.esdihumboldt.hale.common.core.io.ValueProperties) ValueList(eu.esdihumboldt.hale.common.core.io.ValueList) Value(eu.esdihumboldt.hale.common.core.io.Value) Nullable(javax.annotation.Nullable)

Example 17 with ValueList

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;
}
Also used : ValueProperties(eu.esdihumboldt.hale.common.core.io.ValueProperties) ValueList(eu.esdihumboldt.hale.common.core.io.ValueList)

Example 18 with ValueList

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;
    }
}
Also used : Function(com.google.common.base.Function) ValueProperties(eu.esdihumboldt.hale.common.core.io.ValueProperties) ValueList(eu.esdihumboldt.hale.common.core.io.ValueList)

Example 19 with ValueList

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;
}
Also used : ValueProperties(eu.esdihumboldt.hale.common.core.io.ValueProperties) ValueList(eu.esdihumboldt.hale.common.core.io.ValueList) Value(eu.esdihumboldt.hale.common.core.io.Value) SQLArray(eu.esdihumboldt.hale.io.jdbc.constraints.SQLArray)

Example 20 with ValueList

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;
}
Also used : XmlElements(eu.esdihumboldt.hale.io.xsd.constraint.XmlElements) ValueList(eu.esdihumboldt.hale.common.core.io.ValueList) Value(eu.esdihumboldt.hale.common.core.io.Value) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement)

Aggregations

ValueList (eu.esdihumboldt.hale.common.core.io.ValueList)24 Value (eu.esdihumboldt.hale.common.core.io.Value)12 ValueProperties (eu.esdihumboldt.hale.common.core.io.ValueProperties)8 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 Element (org.w3c.dom.Element)4 QName (javax.xml.namespace.QName)3 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)2 CustomTypeContentAssociation (eu.esdihumboldt.hale.io.xsd.anytype.CustomTypeContentAssociation)2 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2 Function (com.google.common.base.Function)1 Definition (eu.esdihumboldt.hale.common.schema.model.Definition)1 ClassResolver (eu.esdihumboldt.hale.common.schema.model.constraint.factory.ClassResolver)1 TypeReferenceBuilder (eu.esdihumboldt.hale.common.schema.model.constraint.factory.TypeReferenceBuilder)1 TypeResolver (eu.esdihumboldt.hale.common.schema.model.constraint.factory.TypeResolver)1 ValueConstraintFactory (eu.esdihumboldt.hale.common.schema.model.constraint.factory.ValueConstraintFactory)1 CodeListAssociation (eu.esdihumboldt.hale.common.schema.model.constraint.property.CodeListAssociation)1 Reference (eu.esdihumboldt.hale.common.schema.model.constraint.property.Reference)1