this.field = field;
}
@Override
public Object setValue(final GraphBacked<PropertyContainer> entity, final Object newVal) {
final PropertyContainer propertyContainer = entity.getPersistentState();
PrefixedDynamicProperties dynamicProperties;
if (newVal instanceof ManagedPrefixedDynamicProperties) {
// newVal is already a managed container
dynamicProperties = (ManagedPrefixedDynamicProperties<?>) newVal;
}
else {
// newVal is not a managed prefixed container and therefore contains
// pure key/values that must be converted to a prefixed form
dynamicProperties = new PrefixedDynamicProperties(propertyNamePrefix);
DynamicProperties newPropertiesVal = (DynamicProperties)newVal;
for(String key : newPropertiesVal.getPropertyKeys()) {
dynamicProperties.setProperty(key, newPropertiesVal.getProperty(key));
}
}
Set<String> dynamicProps = dynamicProperties.getPrefixedPropertyKeys();
Set<String> nodeProps = new HashSet<String>();
IteratorUtil.addToCollection(propertyContainer.getPropertyKeys(), nodeProps);
// Get the properties that are not present in the DynamicProperties container anymore
// by removing all present keys from the actual node properties.
for (String prop : dynamicProps) {
nodeProps.remove(prop);
}
// nodeProps now contains the properties that are present on the node, but not in the DynamicProperties -
// in other words: properties that have been removed. Remove them from the node as well.
for(String removedKey : nodeProps) {
if (dynamicProperties.isPrefixedKey(removedKey)) {
propertyContainer.removeProperty(removedKey);
}
}
// Add all properties to the propertyContainer
for (String key : dynamicProps) {
propertyContainer.setProperty(key, dynamicProperties.getPrefixedProperty(key));
}
return newVal;
}