Search in sources :

Example 1 with State

use of org.qi4j.api.injection.scope.State 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

AbstractAssociation (org.qi4j.api.association.AbstractAssociation)1 Association (org.qi4j.api.association.Association)1 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)1 AssociationStateDescriptor (org.qi4j.api.association.AssociationStateDescriptor)1 ManyAssociation (org.qi4j.api.association.ManyAssociation)1 StateDescriptor (org.qi4j.api.composite.StateDescriptor)1 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)1 State (org.qi4j.api.injection.scope.State)1 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)1 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)1 InvalidInjectionException (org.qi4j.bootstrap.InvalidInjectionException)1