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);
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations