if (actualNewProperty != null) {
reversePropertyAccessor.setValue(actualNewProperty, proxy);
}
} else if (reversePropertyDescriptor instanceof ICollectionPropertyDescriptor) {
// It's a one-to-many relationship
ICollectionAccessor reversePropertyAccessor = accessorFactory
.createCollectionPropertyAccessor(
reversePropertyDescriptor.getName(),
((IReferencePropertyDescriptor<?>) propertyDescriptor)
.getReferencedDescriptor().getComponentContract(),
((ICollectionPropertyDescriptor<?>) reversePropertyDescriptor)
.getCollectionDescriptor().getElementDescriptor()
.getComponentContract());
if (reversePropertyAccessor instanceof IModelDescriptorAware) {
((IModelDescriptorAware) reversePropertyAccessor)
.setModelDescriptor(reversePropertyDescriptor);
}
if (oldProperty != null) {
reversePropertyAccessor.removeFromValue(oldProperty, proxy);
}
if (actualNewProperty != null) {
reversePropertyAccessor.addToValue(actualNewProperty, proxy);
}
}
}
} else if (propertyDescriptor instanceof ICollectionPropertyDescriptor) {
// It's a 'many' relation end
Collection<Object> oldPropertyElementsToRemove = new HashSet<Object>();
Collection<Object> newPropertyElementsToAdd = new HashSet<Object>();
Collection<Object> propertyElementsToKeep = new HashSet<Object>();
if (oldProperty != null) {
oldPropertyElementsToRemove.addAll((Collection<?>) oldProperty);
propertyElementsToKeep.addAll((Collection<?>) oldProperty);
}
if (actualNewProperty != null) {
newPropertyElementsToAdd.addAll((Collection<?>) actualNewProperty);
}
propertyElementsToKeep.retainAll(newPropertyElementsToAdd);
oldPropertyElementsToRemove.removeAll(propertyElementsToKeep);
newPropertyElementsToAdd.removeAll(propertyElementsToKeep);
ICollectionAccessor propertyAccessor = accessorFactory
.createCollectionPropertyAccessor(propertyDescriptor.getName(),
componentDescriptor.getComponentContract(),
((ICollectionPropertyDescriptor<?>) propertyDescriptor)
.getCollectionDescriptor().getElementDescriptor()
.getComponentContract());
for (Object element : oldPropertyElementsToRemove) {
propertyAccessor.removeFromValue(proxy, element);
}
for (Object element : newPropertyElementsToAdd) {
propertyAccessor.addToValue(proxy, element);
}
// if the property is a list we may restore the element order and be
// careful not to miss one...
if (actualNewProperty instanceof List) {
Collection<Object> currentProperty = (Collection<Object>) oldProperty;