Search in sources :

Example 1 with ValueDescriptor

use of org.qi4j.api.value.ValueDescriptor in project qi4j-sdk by Qi4j.

the class ValueSerializerAdapter method serializeValueComposite.

private void serializeValueComposite(Options options, Object object, OutputType output, boolean rootPass) throws Exception {
    CompositeInstance valueInstance = Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map((ValueComposite) object);
    ValueDescriptor descriptor = (ValueDescriptor) valueInstance.descriptor();
    AssociationStateHolder state = (AssociationStateHolder) valueInstance.state();
    onObjectStart(output);
    if (options.getBoolean(Options.INCLUDE_TYPE_INFO) && !rootPass) {
        onFieldStart(output, "_type");
        onValueStart(output);
        onValue(output, first(descriptor.valueType().types()).getName());
        onValueEnd(output);
        onFieldEnd(output);
    }
    for (PropertyDescriptor persistentProperty : descriptor.valueType().properties()) {
        Property<?> property = state.propertyFor(persistentProperty.accessor());
        onFieldStart(output, persistentProperty.qualifiedName().name());
        onValueStart(output);
        doSerialize(options, property.get(), output, false);
        onValueEnd(output);
        onFieldEnd(output);
    }
    for (AssociationDescriptor associationDescriptor : descriptor.valueType().associations()) {
        Association<?> association = state.associationFor(associationDescriptor.accessor());
        Object instance = association.get();
        onFieldStart(output, associationDescriptor.qualifiedName().name());
        onValueStart(output);
        if (instance == null) {
            onValue(output, null);
        } else {
            onValue(output, ((Identity) instance).identity().get());
        }
        onValueEnd(output);
        onFieldEnd(output);
    }
    for (AssociationDescriptor associationDescriptor : descriptor.valueType().manyAssociations()) {
        ManyAssociation<?> manyAssociation = state.manyAssociationFor(associationDescriptor.accessor());
        onFieldStart(output, associationDescriptor.qualifiedName().name());
        onValueStart(output);
        onArrayStart(output);
        for (Object instance : manyAssociation) {
            onValueStart(output);
            onValue(output, ((Identity) instance).identity().get());
            onValueEnd(output);
        }
        onArrayEnd(output);
        onValueEnd(output);
        onFieldEnd(output);
    }
    for (AssociationDescriptor associationDescriptor : descriptor.valueType().namedAssociations()) {
        NamedAssociation<?> namedAssociation = state.namedAssociationFor(associationDescriptor.accessor());
        onFieldStart(output, associationDescriptor.qualifiedName().name());
        onValueStart(output);
        onObjectStart(output);
        for (String name : namedAssociation) {
            onFieldStart(output, name);
            onValueStart(output);
            onValue(output, ((Identity) namedAssociation.get(name)).identity().get());
            onValueEnd(output);
            onFieldEnd(output);
        }
        onObjectEnd(output);
        onValueEnd(output);
        onFieldEnd(output);
    }
    onObjectEnd(output);
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) CompositeInstance(org.qi4j.api.composite.CompositeInstance) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) Identity(org.qi4j.api.entity.Identity)

Example 2 with ValueDescriptor

use of org.qi4j.api.value.ValueDescriptor in project qi4j-sdk by Qi4j.

the class ValueToEntityMixin method update.

@Override
public void update(Object entity, Object value) throws NoSuchEntityException {
    EntityComposite eComposite = (EntityComposite) entity;
    ValueComposite vComposite = (ValueComposite) value;
    EntityDescriptor eDesc = spi.entityDescriptorFor(eComposite);
    AssociationStateHolder eState = spi.stateOf(eComposite);
    AssociationStateDescriptor eStateDesc = eDesc.state();
    ValueDescriptor vDesc = spi.valueDescriptorFor(vComposite);
    AssociationStateHolder vState = spi.stateOf(vComposite);
    AssociationStateDescriptor vStateDesc = vDesc.state();
    Unqualified unqualified = vDesc.metaInfo(Unqualified.class);
    if (unqualified == null || !unqualified.value()) {
        doQualifiedUpdate(eState, eStateDesc, vState, vStateDesc);
    } else {
        doUnQualifiedUpdate(eState, eStateDesc, vState, vStateDesc);
    }
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) EntityComposite(org.qi4j.api.entity.EntityComposite) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder) ValueComposite(org.qi4j.api.value.ValueComposite) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor)

Example 3 with ValueDescriptor

use of org.qi4j.api.value.ValueDescriptor in project qi4j-sdk by Qi4j.

the class ValueToEntityMixin method doConversion.

private <T> EntityBuilder<?> doConversion(Class<T> entityType, String identity, Object value) {
    EntityDescriptor eDesc = module.entityDescriptor(entityType.getName());
    if (eDesc == null) {
        throw new EntityTypeNotFoundException(entityType.getName());
    }
    ValueComposite vComposite = (ValueComposite) value;
    ValueDescriptor vDesc = spi.valueDescriptorFor(vComposite);
    AssociationStateHolder vState = spi.stateOf(vComposite);
    AssociationStateDescriptor vStateDesc = vDesc.state();
    Unqualified unqualified = vDesc.metaInfo(Unqualified.class);
    if (unqualified == null || !unqualified.value()) {
        return doQualifiedConversion(entityType, identity, vState, vStateDesc);
    }
    return doUnqualifiedConversion(entityType, identity, vState, vStateDesc);
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder) ValueComposite(org.qi4j.api.value.ValueComposite) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor)

Example 4 with ValueDescriptor

use of org.qi4j.api.value.ValueDescriptor 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 5 with ValueDescriptor

use of org.qi4j.api.value.ValueDescriptor 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)

Aggregations

ValueDescriptor (org.qi4j.api.value.ValueDescriptor)22 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)10 Test (org.junit.Test)6 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)6 AssociationStateHolder (org.qi4j.api.association.AssociationStateHolder)4 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)4 ValueComposite (org.qi4j.api.value.ValueComposite)4 IOException (java.io.IOException)3 Writer (java.io.Writer)3 Identity (org.qi4j.api.entity.Identity)3 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)3 AnotherSome (org.qi4j.runtime.property.PropertyEqualityTest.AnotherSome)3 Some (org.qi4j.runtime.property.PropertyEqualityTest.Some)3 TemplateException (freemarker.template.TemplateException)2 HashMap (java.util.HashMap)2 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)2 AssociationStateDescriptor (org.qi4j.api.association.AssociationStateDescriptor)2 QualifiedName (org.qi4j.api.common.QualifiedName)2 CompositeDescriptor (org.qi4j.api.composite.CompositeDescriptor)2 CompositeInstance (org.qi4j.api.composite.CompositeInstance)2