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