Search in sources :

Example 16 with AssociationDescriptor

use of org.qi4j.api.association.AssociationDescriptor 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().isEmpty()) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        PropertyDescriptor propertyDescriptor = descriptor.findPropertyModelByName(name);
        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().isEmpty()) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        AssociationDescriptor model = descriptor.getAssociationByName(name);
        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().isEmpty()) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        AssociationDescriptor model = descriptor.getManyAssociationByName(name);
        return new ManyAssociationInjectionProvider(model);
    } else if (NamedAssociation.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State NamedAssociation<MyEntity> name;
        AssociationStateDescriptor descriptor = ((EntityDescriptor) resolution.model()).state();
        State annotation = (State) dependencyModel.injectionAnnotation();
        String name;
        if (annotation.value().isEmpty()) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        AssociationDescriptor model = descriptor.getNamedAssociationByName(name);
        return new NamedAssociationInjectionProvider(model);
    }
    throw new InjectionProviderException("Injected value has invalid type");
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) NamedAssociation(org.qi4j.api.association.NamedAssociation) 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) NamedAssociation(org.qi4j.api.association.NamedAssociation) 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 17 with AssociationDescriptor

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

the class EntityStateInstance method checkConstraints.

public void checkConstraints() {
    for (PropertyDescriptor propertyDescriptor : stateModel.properties()) {
        ConstraintsCheck constraints = (ConstraintsCheck) propertyDescriptor;
        Property<Object> property = this.propertyFor(propertyDescriptor.accessor());
        constraints.checkConstraints(property.get());
    }
    for (AssociationDescriptor associationDescriptor : stateModel.associations()) {
        ConstraintsCheck constraints = (ConstraintsCheck) associationDescriptor;
        Association<Object> association = this.associationFor(associationDescriptor.accessor());
        constraints.checkConstraints(association.get());
    }
// TODO Should ManyAssociations be checked too?
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) ConstraintsCheck(org.qi4j.runtime.composite.ConstraintsCheck) AccessibleObject(java.lang.reflect.AccessibleObject) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor)

Example 18 with AssociationDescriptor

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

Example 19 with AssociationDescriptor

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

the class AssociationEqualityTest method givenValuesOfDifferentTypeAndSameStateWhenTestingAssociationDescriptorEqualityExpectNotEquals.

@Test
public void givenValuesOfDifferentTypeAndSameStateWhenTestingAssociationDescriptorEqualityExpectNotEquals() {
    UnitOfWork uow = module.newUnitOfWork();
    try {
        AnEntity anEntity = uow.newEntity(AnEntity.class);
        SomeWithAssociations some = buildSomeWithAssociation(anEntity);
        AssociationDescriptor someAssocDesc = qi4j.api().associationDescriptorFor(some.anEntity());
        AssociationDescriptor someManyAssocDesc = qi4j.api().associationDescriptorFor(some.manyEntities());
        OtherWithAssociations other = buildOtherWithAssociation(anEntity);
        AssociationDescriptor otherAssocDesc = qi4j.api().associationDescriptorFor(other.anEntity());
        AssociationDescriptor some2ManyAssocDesc = qi4j.api().associationDescriptorFor(other.manyEntities());
        assertThat("AssociationDescriptor not equal", someAssocDesc, not(equalTo(otherAssocDesc)));
        assertThat("AssociationDescriptor hashcode not equal", someAssocDesc.hashCode(), not(equalTo(otherAssocDesc.hashCode())));
        assertThat("ManyAssociationDescriptor not equal", someManyAssocDesc, not(equalTo(some2ManyAssocDesc)));
        assertThat("ManyAssociationDescriptor hashcode not equal", someManyAssocDesc.hashCode(), not(equalTo(some2ManyAssocDesc.hashCode())));
    } finally {
        uow.discard();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 20 with AssociationDescriptor

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

the class AssociationEqualityTest method givenValuesOfTheSameTypeAndDifferentStateWhenTestingAssociationDescriptorEqualityExpectEquals.

@Test
public void givenValuesOfTheSameTypeAndDifferentStateWhenTestingAssociationDescriptorEqualityExpectEquals() {
    UnitOfWork uow = module.newUnitOfWork();
    try {
        SomeWithAssociations some = buildSomeWithAssociation(uow.newEntity(AnEntity.class));
        AssociationDescriptor someAssocDesc = qi4j.api().associationDescriptorFor(some.anEntity());
        AssociationDescriptor someManyAssocDesc = qi4j.api().associationDescriptorFor(some.manyEntities());
        SomeWithAssociations some2 = buildSomeWithAssociation(uow.newEntity(AnEntity.class));
        AssociationDescriptor some2AssocDesc = qi4j.api().associationDescriptorFor(some2.anEntity());
        AssociationDescriptor some2ManyAssocDesc = qi4j.api().associationDescriptorFor(some2.manyEntities());
        assertThat("AssociationDescriptor equal", someAssocDesc, equalTo(some2AssocDesc));
        assertThat("AssociationDescriptor hashcode equal", someAssocDesc.hashCode(), equalTo(some2AssocDesc.hashCode()));
        assertThat("ManyAssociationDescriptor equal", someManyAssocDesc, equalTo(some2ManyAssocDesc));
        assertThat("ManyAssociationDescriptor hashcode equal", someManyAssocDesc.hashCode(), equalTo(some2ManyAssocDesc.hashCode()));
    } finally {
        uow.discard();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Aggregations

AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)28 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)15 EntityReference (org.qi4j.api.entity.EntityReference)13 Map (java.util.Map)8 QualifiedName (org.qi4j.api.common.QualifiedName)7 LinkedHashMap (java.util.LinkedHashMap)6 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)5 Test (org.junit.Test)4 URI (org.openrdf.model.URI)4 ValueFactory (org.openrdf.model.ValueFactory)4 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)4 Collection (java.util.Collection)3 AssociationStateDescriptor (org.qi4j.api.association.AssociationStateDescriptor)3 HashSet (java.util.HashSet)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2