}
private void updateReferences( SessionCache cache,
Name propertyName,
SessionCache systemCache ) {
Property propertyWhichWasRemoved = null;
Property propertyWhichWasAdded = null;
// first try to determine if there's old reference property with the same name so that old references can be removed
boolean oldPropertyWasReference = false;
List<Reference> referencesToRemove = new ArrayList<>();
if (isPropertyModified(cache, propertyName) || isPropertyRemoved(propertyName)) {
// remove potential existing references
CachedNode persistedNode = nodeInWorkspace(session(cache));
Property oldProperty = persistedNode.getProperty(propertyName, cache);
if (oldProperty != null && oldProperty.isReference()) {
oldPropertyWasReference = true;
propertyWhichWasRemoved = oldProperty;
for (Object referenceObject : oldProperty.getValuesAsArray()) {
assert referenceObject instanceof Reference;
referencesToRemove.add((Reference)referenceObject);
}
}
}
// if the updated property is a reference, determine which are the references that need updating
boolean updatedPropertyIsReference = false;
List<Reference> referencesToAdd = new ArrayList<>();
Property property = changedProperties.get(propertyName);
if (property != null && property.isReference()) {
updatedPropertyIsReference = true;
propertyWhichWasAdded = property;
for (Object referenceObject : property.getValuesAsArray()) {
assert referenceObject instanceof Reference;
Reference updatedReference = (Reference)referenceObject;
if (referencesToRemove.contains(updatedReference)) {
// the reference is already present on a property with the same name, so this is a no-op for that reference
// therefore we remove it from the list of references that will be removed