Search in sources :

Example 1 with Association

use of org.qi4j.api.association.Association in project qi4j-sdk by Qi4j.

the class Qi4jPropertyAccessor method setProperty.

@Override
@SuppressWarnings("unchecked")
public final void setProperty(Map aContext, Object aTarget, Object aPropertyName, Object aPropertyValue) throws OgnlException {
    String fieldName = aPropertyName.toString();
    Object qi4jField = getQi4jField(aContext, aTarget, fieldName);
    if (qi4jField != null) {
        Class memberClass = qi4jField.getClass();
        if (Property.class.isAssignableFrom(memberClass)) {
            Property property = (Property) qi4jField;
            OgnlContext ognlContext = (OgnlContext) aContext;
            Class propertyType = (Class) api.propertyDescriptorFor(property).type();
            Object convertedValue = getConvertedType(ognlContext, aTarget, null, fieldName, aPropertyValue, propertyType);
            try {
                property.set(convertedValue);
            } catch (ConstraintViolationException e) {
                Collection<ConstraintViolation> violations = e.constraintViolations();
                handleConstraintViolation(aContext, aTarget, fieldName, convertedValue, violations);
            }
            return;
        } else if (Association.class.isAssignableFrom(memberClass)) {
            Association association = (Association) qi4jField;
            OgnlContext ognlContext = (OgnlContext) aContext;
            Class associationType = (Class) api.associationDescriptorFor(association).type();
            Object convertedValue = getConvertedType(ognlContext, aTarget, null, fieldName, aPropertyValue, associationType);
            if (convertedValue == OgnlRuntime.NoConversionPossible) {
                throw new OgnlException("Could not convert value to association type");
            }
            try {
                association.set(convertedValue);
            } catch (ConstraintViolationException e) {
                Collection<ConstraintViolation> violations = e.constraintViolations();
                handleConstraintViolation(aContext, aTarget, fieldName, aPropertyValue, violations);
            }
            return;
        } else if (ManyAssociation.class.isAssignableFrom(memberClass)) {
            throw new OgnlException("Setting many association [" + fieldName + "] is impossible.");
        }
    }
    super.setProperty(aContext, aTarget, aPropertyName, aPropertyValue);
}
Also used : ManyAssociation(org.qi4j.api.association.ManyAssociation) Association(org.qi4j.api.association.Association) ConstraintViolation(org.qi4j.api.constraint.ConstraintViolation) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) Collection(java.util.Collection) Property(org.qi4j.api.property.Property)

Example 2 with Association

use of org.qi4j.api.association.Association in project qi4j-sdk by Qi4j.

the class AssociationInstance method equals.

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    Association<?> that = (Association) o;
    // Unwrap if needed
    while (that instanceof AssociationWrapper) {
        that = ((AssociationWrapper) that).next();
    }
    // Descriptor equality
    AssociationInstance<?> thatInstance = (AssociationInstance) that;
    AssociationDescriptor thatDescriptor = (AssociationDescriptor) thatInstance.associationInfo();
    if (!associationInfo.equals(thatDescriptor)) {
        return false;
    }
    // State equality
    if (associationState.get() != null ? !associationState.get().equals(thatInstance.associationState.get()) : thatInstance.associationState.get() != null) {
        return false;
    }
    return true;
}
Also used : Association(org.qi4j.api.association.Association) AssociationWrapper(org.qi4j.api.association.AssociationWrapper) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor)

Example 3 with Association

use of org.qi4j.api.association.Association in project qi4j-sdk by Qi4j.

the class EntityInstance method removeAggregatedEntities.

private void removeAggregatedEntities(UnitOfWork unitOfWork) {
    // Calculate aggregated Entities
    AssociationStateDescriptor stateDescriptor = entityModel.state();
    Set<Object> aggregatedEntities = new HashSet<Object>();
    Iterable<? extends AssociationDescriptor> associations = stateDescriptor.associations();
    for (AssociationDescriptor association : associations) {
        if (association.isAggregated()) {
            Association assoc = state.associationFor(association.accessor());
            Object aggregatedEntity = assoc.get();
            if (aggregatedEntity != null) {
                aggregatedEntities.add(aggregatedEntity);
            }
        }
    }
    Iterable<? extends AssociationDescriptor> manyAssociations = stateDescriptor.manyAssociations();
    for (AssociationDescriptor association : manyAssociations) {
        if (association.isAggregated()) {
            ManyAssociation manyAssoc = state.manyAssociationFor(association.accessor());
            for (Object entity : manyAssoc) {
                aggregatedEntities.add(entity);
            }
        }
    }
    // Remove aggregated Entities
    for (Object aggregatedEntity : aggregatedEntities) {
        unitOfWork.remove(aggregatedEntity);
    }
}
Also used : ManyAssociation(org.qi4j.api.association.ManyAssociation) Association(org.qi4j.api.association.Association) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) ManyAssociation(org.qi4j.api.association.ManyAssociation) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor) HashSet(java.util.HashSet)

Example 4 with Association

use of org.qi4j.api.association.Association in project qi4j-sdk by Qi4j.

the class EntityStateInstance method associationFor.

@Override
public <T> Association<T> associationFor(AccessibleObject accessor) throws IllegalArgumentException {
    Map<AccessibleObject, Object> state = state();
    Association<T> association = (Association<T>) state.get(accessor);
    if (association == null) {
        final AssociationModel associationModel = stateModel.getAssociation(accessor);
        if (associationModel == null) {
            throw new IllegalArgumentException("No such association:" + accessor);
        }
        association = new AssociationInstance<T>(entityState instanceof BuilderEntityState ? associationModel.getBuilderInfo() : associationModel, entityFunction, new Property<EntityReference>() {

            @Override
            public EntityReference get() {
                return entityState.associationValueOf(associationModel.qualifiedName());
            }

            @Override
            public void set(EntityReference newValue) throws IllegalArgumentException, IllegalStateException {
                entityState.setAssociationValue(associationModel.qualifiedName(), newValue);
            }
        });
        state.put(accessor, association);
    }
    return association;
}
Also used : ManyAssociation(org.qi4j.api.association.ManyAssociation) Association(org.qi4j.api.association.Association) EntityReference(org.qi4j.api.entity.EntityReference) AccessibleObject(java.lang.reflect.AccessibleObject) AccessibleObject(java.lang.reflect.AccessibleObject) BuilderEntityState(org.qi4j.runtime.unitofwork.BuilderEntityState) Property(org.qi4j.api.property.Property) ManyAssociationModel(org.qi4j.runtime.association.ManyAssociationModel) AssociationModel(org.qi4j.runtime.association.AssociationModel)

Example 5 with Association

use of org.qi4j.api.association.Association in project qi4j-sdk by Qi4j.

the class StateInjectionProviderFactory method newInjectionProvider.

@Override
public InjectionProvider newInjectionProvider(Resolution resolution, DependencyModel dependencyModel) throws InvalidInjectionException {
    if (StateHolder.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State StateHolder properties;
        return new StateInjectionProvider();
    } else if (UnitOfWork.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        if (!(resolution.model() instanceof EntityDescriptor)) {
            throw new InvalidInjectionException("Only EntityComposites can be injected with '@State UnitOfWork'");
        }
        return new UnitOfWorkInjectionProvider();
    } else if (Property.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State Property<String> name;
        StateDescriptor descriptor;
        descriptor = ((StatefulCompositeDescriptor) resolution.model()).state();
        State annotation = (State) dependencyModel.injectionAnnotation();
        String name;
        if (annotation.value().equals("")) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        PropertyDescriptor propertyDescriptor = descriptor.findPropertyModelByName(name);
        // Check if property exists
        if (propertyDescriptor == null) {
            return null;
        }
        return new PropertyInjectionProvider(propertyDescriptor);
    } else if (Association.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State Association<MyEntity> name;
        AssociationStateDescriptor descriptor = ((EntityDescriptor) resolution.model()).state();
        State annotation = (State) dependencyModel.injectionAnnotation();
        String name;
        if (annotation.value().equals("")) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        AssociationDescriptor model = descriptor.getAssociationByName(name);
        // No such association found
        if (model == null) {
            return null;
        }
        return new AssociationInjectionProvider(model);
    } else if (ManyAssociation.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State ManyAssociation<MyEntity> name;
        AssociationStateDescriptor descriptor = ((EntityDescriptor) resolution.model()).state();
        State annotation = (State) dependencyModel.injectionAnnotation();
        String name;
        if (annotation.value().equals("")) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        AssociationDescriptor model = descriptor.getManyAssociationByName(name);
        // No such association found
        if (model == null) {
            return null;
        }
        return new ManyAssociationInjectionProvider(model);
    }
    throw new InjectionProviderException("Injected value has invalid type");
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) ManyAssociation(org.qi4j.api.association.ManyAssociation) AbstractAssociation(org.qi4j.api.association.AbstractAssociation) Association(org.qi4j.api.association.Association) State(org.qi4j.api.injection.scope.State) InvalidInjectionException(org.qi4j.bootstrap.InvalidInjectionException) StateDescriptor(org.qi4j.api.composite.StateDescriptor) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor)

Aggregations

Association (org.qi4j.api.association.Association)11 ManyAssociation (org.qi4j.api.association.ManyAssociation)9 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)3 Property (org.qi4j.api.property.Property)3 AssociationModel (org.qi4j.runtime.association.AssociationModel)3 ManyAssociationModel (org.qi4j.runtime.association.ManyAssociationModel)3 Annotation (java.lang.annotation.Annotation)2 AccessibleObject (java.lang.reflect.AccessibleObject)2 Member (java.lang.reflect.Member)2 AssociationStateDescriptor (org.qi4j.api.association.AssociationStateDescriptor)2 MetaInfo (org.qi4j.api.common.MetaInfo)2 Optional (org.qi4j.api.common.Optional)2 ValueConstraintsInstance (org.qi4j.runtime.composite.ValueConstraintsInstance)2 ValueConstraintsModel (org.qi4j.runtime.composite.ValueConstraintsModel)2 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 QName (javax.xml.namespace.QName)1 AegisType (org.apache.cxf.aegis.type.AegisType)1 MessageWriter (org.apache.cxf.aegis.xml.MessageWriter)1