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");
}
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?
}
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());
}
}
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();
}
}
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();
}
}
Aggregations