Search in sources :

Example 6 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)

Example 7 with CompositeInstance

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

the class PropertyFunction method map.

@Override
public Property<T> map(Composite entity) {
    try {
        Object target = entity;
        if (traversedProperty != null) {
            Property<?> property = traversedProperty.map(entity);
            if (property == null) {
                return null;
            }
            target = property.get();
        } else if (traversedAssociation != null) {
            Association<?> association = traversedAssociation.map(entity);
            if (association == null) {
                return null;
            }
            target = association.get();
        } else if (traversedManyAssociation != null) {
            throw new IllegalArgumentException("Cannot evaluate a ManyAssociation");
        } else if (traversedNamedAssociation != null) {
            throw new IllegalArgumentException("Cannot evaluate a NamedAssociation");
        }
        if (target == null) {
            return null;
        }
        CompositeInstance handler = (CompositeInstance) Proxy.getInvocationHandler(target);
        return handler.state().propertyFor(accessor);
    } catch (IllegalArgumentException e) {
        throw e;
    } catch (Throwable e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : Association(org.qi4j.api.association.Association) CompositeInstance(org.qi4j.api.composite.CompositeInstance) AccessibleObject(java.lang.reflect.AccessibleObject)

Aggregations

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