Search in sources :

Example 1 with AssociationStateDescriptor

use of org.qi4j.api.association.AssociationStateDescriptor 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);
    }
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) EntityComposite(org.qi4j.api.entity.EntityComposite) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder) ValueComposite(org.qi4j.api.value.ValueComposite) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor)

Example 2 with AssociationStateDescriptor

use of org.qi4j.api.association.AssociationStateDescriptor 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);
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder) ValueComposite(org.qi4j.api.value.ValueComposite) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor)

Example 3 with AssociationStateDescriptor

use of org.qi4j.api.association.AssociationStateDescriptor 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 AssociationStateDescriptor

use of org.qi4j.api.association.AssociationStateDescriptor 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)

Example 5 with AssociationStateDescriptor

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

the class Qi4jSPITest method validateState.

private void validateState(AssociationStateHolder state, EntityDescriptor entityDescriptor) {
    for (PropertyDescriptor propertyDescriptor : entityDescriptor.state().properties()) {
        Property<?> prop = state.propertyFor(propertyDescriptor.accessor());
        assertThat("Properties could be listed", prop, CoreMatchers.notNullValue());
    }
    AssociationStateDescriptor descriptor = entityDescriptor.state();
    for (AssociationDescriptor associationDescriptor : descriptor.associations()) {
        AbstractAssociation assoc = state.associationFor(associationDescriptor.accessor());
        assertThat("Assocs could be listed", assoc, CoreMatchers.notNullValue());
    }
}
Also used : AbstractAssociation(org.qi4j.api.association.AbstractAssociation) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor)

Aggregations

AssociationStateDescriptor (org.qi4j.api.association.AssociationStateDescriptor)5 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)3 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)3 AbstractAssociation (org.qi4j.api.association.AbstractAssociation)2 Association (org.qi4j.api.association.Association)2 AssociationStateHolder (org.qi4j.api.association.AssociationStateHolder)2 ManyAssociation (org.qi4j.api.association.ManyAssociation)2 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)2 ValueComposite (org.qi4j.api.value.ValueComposite)2 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)2 HashSet (java.util.HashSet)1 StateDescriptor (org.qi4j.api.composite.StateDescriptor)1 EntityComposite (org.qi4j.api.entity.EntityComposite)1 State (org.qi4j.api.injection.scope.State)1 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)1 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)1 InvalidInjectionException (org.qi4j.bootstrap.InvalidInjectionException)1