Search in sources :

Example 1 with ValueCompositeType

use of org.qi4j.api.type.ValueCompositeType in project qi4j-sdk by Qi4j.

the class ValueDeserializerAdapter method deserializeNodeValueComposite.

private <T> T deserializeNodeValueComposite(ValueType valueType, InputNodeType inputNode) throws Exception {
    ValueCompositeType valueCompositeType = (ValueCompositeType) valueType;
    Class<?> valueBuilderType = first(valueCompositeType.types());
    String typeInfo = this.<String>getObjectFieldValue(inputNode, "_type", this.<String>buildDeserializeInputNodeFunction(new ValueType(String.class)));
    TREE_PARSING_LOG.trace("In deserializeNodeValueComposite(), getObjectFieldValue( {} ) returned '{}'", inputNode, typeInfo);
    if (typeInfo != null) {
        ValueDescriptor valueDescriptor = valuesModule().valueDescriptor(typeInfo);
        if (valueDescriptor == null) {
            throw new ValueSerializationException("Specified value type could not be resolved: " + typeInfo);
        }
        valueCompositeType = valueDescriptor.valueType();
        valueBuilderType = Class.forName(typeInfo);
        if (!valueType.equals(valueCompositeType)) {
            TREE_PARSING_LOG.debug("Overriding {} with {} as defined in _type field.", valueType, valueCompositeType);
        }
    }
    return deserializeValueComposite(valueCompositeType, valueBuilderType, inputNode);
}
Also used : ValueType(org.qi4j.api.type.ValueType) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Example 2 with ValueCompositeType

use of org.qi4j.api.type.ValueCompositeType in project qi4j-sdk by Qi4j.

the class ValueDeserializerAdapter method deserializeNodeGuessed.

@SuppressWarnings("unchecked")
private <T> T deserializeNodeGuessed(ValueType valueType, InputNodeType inputNode) throws Exception {
    if (isObjectValue(inputNode)) {
        // Attempt ValueComposite deserialization
        ValueCompositeType valueCompositeType;
        if (// with _type info
        objectHasField(inputNode, "_type")) {
            String typeInfo = this.<String>getObjectFieldValue(inputNode, "_type", this.<String>buildDeserializeInputNodeFunction(new ValueType(String.class)));
            TREE_PARSING_LOG.trace("In deserializeNodeGuessed(), getObjectFieldValue( {} ) returned '{}'", inputNode, typeInfo);
            ValueDescriptor valueDescriptor = valuesModule().valueDescriptor(typeInfo);
            if (valueDescriptor == null) {
                throw new ValueSerializationException("Specified value type could not be resolved: " + typeInfo);
            }
            valueCompositeType = valueDescriptor.valueType();
            TREE_PARSING_LOG.debug("Overriding {} with {} as defined in _type field.", valueType, valueCompositeType);
        } else // without _type info
        {
            ValueDescriptor valueDescriptor = valuesModule().valueDescriptor(first(valueType.types()).getName());
            if (valueDescriptor == null) {
                throw new ValueSerializationException("Don't know how to deserialize " + inputNode);
            }
            valueCompositeType = valueDescriptor.valueType();
            TREE_PARSING_LOG.debug("Overriding {} with {} as found in available ValueComposites.", valueType, valueCompositeType);
        }
        Class<?> valueBuilderType = first(valueCompositeType.types());
        return deserializeValueComposite(valueCompositeType, valueBuilderType, inputNode);
    }
    // Last resort : base64 java deserialization
    return (T) deserializeBase64Serialized(inputNode);
}
Also used : ValueType(org.qi4j.api.type.ValueType) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Example 3 with ValueCompositeType

use of org.qi4j.api.type.ValueCompositeType in project qi4j-sdk by Qi4j.

the class GaeEntityState method initializeValueTypes.

private HashMap<QualifiedName, ValueType> initializeValueTypes(EntityDescriptor descriptor) {
    HashMap<QualifiedName, ValueType> result = new HashMap<QualifiedName, ValueType>();
    for (PropertyDescriptor persistent : descriptor.state().properties()) {
        if (persistent.valueType() instanceof ValueCompositeType) {
            QualifiedName name = persistent.qualifiedName();
            result.put(name, persistent.valueType());
        }
    }
    return result;
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) HashMap(java.util.HashMap) ValueType(org.qi4j.api.type.ValueType) QualifiedName(org.qi4j.api.common.QualifiedName) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Example 4 with ValueCompositeType

use of org.qi4j.api.type.ValueCompositeType in project qi4j-sdk by Qi4j.

the class AbstractCollectionSerializationTest method givenListOfValueCompositesAndNullElementWhenSerializingAndDeserializingExpectEquals.

@Test
public void givenListOfValueCompositesAndNullElementWhenSerializingAndDeserializingExpectEquals() throws Exception {
    String output = valueSerialization.serialize(valueCompositesList());
    ValueCompositeType valueType = module.valueDescriptor(SomeValue.class.getName()).valueType();
    CollectionType collectionType = new CollectionType(List.class, valueType);
    List<SomeValue> value = valueSerialization.deserialize(collectionType, output);
    assertEquals(valueCompositesList(), value);
}
Also used : CollectionType(org.qi4j.api.type.CollectionType) ValueCompositeType(org.qi4j.api.type.ValueCompositeType) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 5 with ValueCompositeType

use of org.qi4j.api.type.ValueCompositeType in project qi4j-sdk by Qi4j.

the class EntityStateSerializer method serializeValueComposite.

private void serializeValueComposite(Resource subject, URI predicate, ValueComposite value, ValueType valueType, Graph graph, String baseUri, boolean includeNonQueryable) {
    final ValueFactory valueFactory = graph.getValueFactory();
    BNode collection = valueFactory.createBNode();
    graph.add(subject, predicate, collection);
    for (PropertyDescriptor persistentProperty : ((ValueCompositeType) valueType).properties()) {
        Object propertyValue = Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map((Composite) value).state().propertyFor(persistentProperty.accessor()).get();
        if (propertyValue == null) {
            // Skip null values
            continue;
        }
        ValueType type = persistentProperty.valueType();
        if (type instanceof ValueCompositeType) {
            URI pred = valueFactory.createURI(baseUri, persistentProperty.qualifiedName().name());
            serializeValueComposite(collection, pred, (ValueComposite) propertyValue, type, graph, baseUri + persistentProperty.qualifiedName().name() + "/", includeNonQueryable);
        } else {
            serializeProperty(persistentProperty, propertyValue, collection, graph, includeNonQueryable);
        }
    }
}
Also used : BNode(org.openrdf.model.BNode) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) ValueComposite(org.qi4j.api.value.ValueComposite) Composite(org.qi4j.api.composite.Composite) ValueType(org.qi4j.api.type.ValueType) ValueFactory(org.openrdf.model.ValueFactory) URI(org.openrdf.model.URI) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Aggregations

ValueCompositeType (org.qi4j.api.type.ValueCompositeType)11 ValueType (org.qi4j.api.type.ValueType)7 CollectionType (org.qi4j.api.type.CollectionType)5 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4 List (java.util.List)4 Map (java.util.Map)4 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)4 MapType (org.qi4j.api.type.MapType)4 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)4 HashMap (java.util.HashMap)3 QualifiedName (org.qi4j.api.common.QualifiedName)3 ValueComposite (org.qi4j.api.value.ValueComposite)3 Collection (java.util.Collection)2 LinkedHashSet (java.util.LinkedHashSet)2 Set (java.util.Set)2 Preferences (java.util.prefs.Preferences)2 URI (org.openrdf.model.URI)2 ValueFactory (org.openrdf.model.ValueFactory)2 EntityReference (org.qi4j.api.entity.EntityReference)2