use of org.qi4j.api.association.Association in project qi4j-sdk by Qi4j.
the class Qi4jPropertyAccessor method setProperty.
@Override
@SuppressWarnings("unchecked")
public final void setProperty(Map aContext, Object aTarget, Object aPropertyName, Object aPropertyValue) throws OgnlException {
String fieldName = aPropertyName.toString();
Object qi4jField = getQi4jField(aContext, aTarget, fieldName);
if (qi4jField != null) {
Class memberClass = qi4jField.getClass();
if (Property.class.isAssignableFrom(memberClass)) {
Property property = (Property) qi4jField;
OgnlContext ognlContext = (OgnlContext) aContext;
Class propertyType = (Class) api.propertyDescriptorFor(property).type();
Object convertedValue = getConvertedType(ognlContext, aTarget, null, fieldName, aPropertyValue, propertyType);
try {
property.set(convertedValue);
} catch (ConstraintViolationException e) {
Collection<ConstraintViolation> violations = e.constraintViolations();
handleConstraintViolation(aContext, aTarget, fieldName, convertedValue, violations);
}
return;
} else if (Association.class.isAssignableFrom(memberClass)) {
Association association = (Association) qi4jField;
OgnlContext ognlContext = (OgnlContext) aContext;
Class associationType = (Class) api.associationDescriptorFor(association).type();
Object convertedValue = getConvertedType(ognlContext, aTarget, null, fieldName, aPropertyValue, associationType);
if (convertedValue == OgnlRuntime.NoConversionPossible) {
throw new OgnlException("Could not convert value to association type");
}
try {
association.set(convertedValue);
} catch (ConstraintViolationException e) {
Collection<ConstraintViolation> violations = e.constraintViolations();
handleConstraintViolation(aContext, aTarget, fieldName, aPropertyValue, violations);
}
return;
} else if (ManyAssociation.class.isAssignableFrom(memberClass)) {
throw new OgnlException("Setting many association [" + fieldName + "] is impossible.");
}
}
super.setProperty(aContext, aTarget, aPropertyName, aPropertyValue);
}
use of org.qi4j.api.association.Association in project qi4j-sdk by Qi4j.
the class AssociationInstance method equals.
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Association<?> that = (Association) o;
// Unwrap if needed
while (that instanceof AssociationWrapper) {
that = ((AssociationWrapper) that).next();
}
// Descriptor equality
AssociationInstance<?> thatInstance = (AssociationInstance) that;
AssociationDescriptor thatDescriptor = (AssociationDescriptor) thatInstance.associationInfo();
if (!associationInfo.equals(thatDescriptor)) {
return false;
}
// State equality
if (associationState.get() != null ? !associationState.get().equals(thatInstance.associationState.get()) : thatInstance.associationState.get() != null) {
return false;
}
return true;
}
use of org.qi4j.api.association.Association 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.Association in project qi4j-sdk by Qi4j.
the class EntityStateInstance method associationFor.
@Override
public <T> Association<T> associationFor(AccessibleObject accessor) throws IllegalArgumentException {
Map<AccessibleObject, Object> state = state();
Association<T> association = (Association<T>) state.get(accessor);
if (association == null) {
final AssociationModel associationModel = stateModel.getAssociation(accessor);
if (associationModel == null) {
throw new IllegalArgumentException("No such association:" + accessor);
}
association = new AssociationInstance<T>(entityState instanceof BuilderEntityState ? associationModel.getBuilderInfo() : associationModel, entityFunction, new Property<EntityReference>() {
@Override
public EntityReference get() {
return entityState.associationValueOf(associationModel.qualifiedName());
}
@Override
public void set(EntityReference newValue) throws IllegalArgumentException, IllegalStateException {
entityState.setAssociationValue(associationModel.qualifiedName(), newValue);
}
});
state.put(accessor, association);
}
return association;
}
use of org.qi4j.api.association.Association 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");
}
Aggregations