Search in sources :

Example 1 with CompositeInstance

use of org.qi4j.api.composite.CompositeInstance 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 CompositeInstance

use of org.qi4j.api.composite.CompositeInstance in project qi4j-sdk by Qi4j.

the class MixinModel method newInstance.

public Object newInstance(InjectionContext injectionContext) {
    Object mixin;
    CompositeInstance compositeInstance = injectionContext.compositeInstance();
    mixin = constructorsModel.newInstance(injectionContext);
    if (FragmentClassLoader.isGenerated(instantiationClass)) {
        try {
            instantiationClass.getDeclaredField("_instance").set(mixin, injectionContext.compositeInstance());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    injectedFieldsModel.inject(injectionContext, mixin);
    injectedMethodsModel.inject(injectionContext, mixin);
    if (mixin instanceof Initializable) {
        try {
            ((Initializable) mixin).initialize();
        } catch (InitializationException e) {
            List<Class<?>> compositeType = toList(compositeInstance.types());
            String message = "Unable to initialize " + mixinClass + " in composite " + compositeType;
            throw new ConstructionException(message, e);
        }
    }
    return mixin;
}
Also used : Initializable(org.qi4j.api.mixin.Initializable) CompositeInstance(org.qi4j.api.composite.CompositeInstance) Iterables.toList(org.qi4j.functional.Iterables.toList) List(java.util.List) InitializationException(org.qi4j.api.mixin.InitializationException) ConstructionException(org.qi4j.api.common.ConstructionException)

Example 3 with CompositeInstance

use of org.qi4j.api.composite.CompositeInstance in project qi4j-sdk by Qi4j.

the class ManyAssociationFunction method map.

@Override
public ManyAssociation<T> map(Composite entity) {
    try {
        Object target = entity;
        if (traversedAssociation != null) {
            target = traversedAssociation.map(entity).get();
        }
        if (traversedManyAssociation != null) {
            throw new IllegalArgumentException("Cannot traverse ManyAssociations");
        }
        CompositeInstance handler = (CompositeInstance) Proxy.getInvocationHandler(target);
        return ((AssociationStateHolder) handler.state()).manyAssociationFor(accessor);
    } catch (IllegalArgumentException e) {
        throw e;
    } catch (Throwable e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : CompositeInstance(org.qi4j.api.composite.CompositeInstance) AccessibleObject(java.lang.reflect.AccessibleObject) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder)

Example 4 with CompositeInstance

use of org.qi4j.api.composite.CompositeInstance in project qi4j-sdk by Qi4j.

the class TransientBuilderInstance method newInstance.

@Override
public T newInstance() throws ConstructionException {
    // Set correct info's (immutable) on the state
    for (PropertyDescriptor propertyDescriptor : model.model().state().properties()) {
        ((PropertyInstance<Object>) state.propertyFor(propertyDescriptor.accessor())).setPropertyInfo((PropertyInfo) propertyDescriptor);
    }
    model.model().checkConstraints(state);
    CompositeInstance compositeInstance = model.model().newInstance(model.module(), uses, state);
    return compositeInstance.<T>proxy();
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) CompositeInstance(org.qi4j.api.composite.CompositeInstance) PropertyInstance(org.qi4j.runtime.property.PropertyInstance)

Example 5 with CompositeInstance

use of org.qi4j.api.composite.CompositeInstance in project qi4j-sdk by Qi4j.

the class NamedAssociationFunction method map.

@Override
public NamedAssociation<T> map(Composite entity) {
    try {
        Object target = entity;
        if (traversedAssociation != null) {
            target = traversedAssociation.map(entity).get();
        }
        if (traversedManyAssociation != null) {
            throw new IllegalArgumentException("Cannot traverse ManyAssociations");
        }
        if (traversedNamedAssociation != null) {
            throw new IllegalArgumentException("Cannot traverse NamedAssociations");
        }
        CompositeInstance handler = (CompositeInstance) Proxy.getInvocationHandler(target);
        return ((AssociationStateHolder) handler.state()).namedAssociationFor(accessor);
    } catch (IllegalArgumentException e) {
        throw e;
    } catch (Throwable e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : CompositeInstance(org.qi4j.api.composite.CompositeInstance) AccessibleObject(java.lang.reflect.AccessibleObject) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder)

Aggregations

CompositeInstance (org.qi4j.api.composite.CompositeInstance)8 AssociationStateHolder (org.qi4j.api.association.AssociationStateHolder)5 AccessibleObject (java.lang.reflect.AccessibleObject)4 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)3 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)2 Identity (org.qi4j.api.entity.Identity)2 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)2 List (java.util.List)1 Association (org.qi4j.api.association.Association)1 ConstructionException (org.qi4j.api.common.ConstructionException)1 Initializable (org.qi4j.api.mixin.Initializable)1 InitializationException (org.qi4j.api.mixin.InitializationException)1 Iterables.toList (org.qi4j.functional.Iterables.toList)1 PropertyInstance (org.qi4j.runtime.property.PropertyInstance)1