use of org.openremote.model.attribute.AttributeMap in project openremote by openremote.
the class AssetStorageService method publishModificationEvents.
protected void publishModificationEvents(PersistenceEvent<Asset<?>> persistenceEvent) {
Asset<?> asset = persistenceEvent.getEntity();
switch(persistenceEvent.getCause()) {
case CREATE:
// Fully load the asset
Asset<?> loadedAsset = find(new AssetQuery().ids(asset.getId()));
if (loadedAsset == null) {
return;
}
if (LOG.isLoggable(Level.FINER)) {
LOG.finer("Asset created: " + loadedAsset.toStringAll());
} else {
LOG.fine("Asset created: " + loadedAsset);
}
clientEventService.publishEvent(new AssetEvent(AssetEvent.Cause.CREATE, loadedAsset, null));
// ));
break;
case UPDATE:
String[] updatedProperties = persistenceEvent.getPropertyNames();
boolean attributesChanged = Arrays.asList(updatedProperties).contains("attributes");
// String[] updatedProperties = Arrays.stream(persistenceEvent.getPropertyNames()).filter(propertyName -> {
// Object oldValue = persistenceEvent.getPreviousState(propertyName);
// Object newValue = persistenceEvent.getCurrentState(propertyName);
// return !Objects.deepEquals(oldValue, newValue);
// }).toArray(String[]::new);
// Fully load the asset
loadedAsset = find(new AssetQuery().ids(asset.getId()));
if (loadedAsset == null) {
return;
}
LOG.finer("Asset updated: " + persistenceEvent);
clientEventService.publishEvent(new AssetEvent(AssetEvent.Cause.UPDATE, loadedAsset, updatedProperties));
// Did any attributes change if so raise attribute events on the event bus
if (attributesChanged) {
AttributeMap oldAttributes = persistenceEvent.getPreviousState("attributes");
AttributeMap newAttributes = persistenceEvent.getCurrentState("attributes");
// Get removed attributes and raise an attribute event with deleted flag in attribute state
oldAttributes.stream().filter(oldAttribute -> newAttributes.stream().noneMatch(newAttribute -> oldAttribute.getName().equals(newAttribute.getName()))).forEach(obsoleteAttribute -> clientEventService.publishEvent(AttributeEvent.deletedAttribute(asset.getId(), obsoleteAttribute.getName())));
// Get new or modified attributes
getAddedOrModifiedAttributes(oldAttributes.values(), newAttributes.values()).forEach(newOrModifiedAttribute -> publishAttributeEvent(asset, newOrModifiedAttribute));
}
break;
case DELETE:
if (LOG.isLoggable(Level.FINER)) {
LOG.finer("Asset deleted: " + asset.toStringAll());
} else {
LOG.fine("Asset deleted: " + asset);
}
clientEventService.publishEvent(new AssetEvent(AssetEvent.Cause.DELETE, asset, null));
// Raise attribute event with deleted flag for each attribute
AttributeMap deletedAttributes = asset.getAttributes();
deletedAttributes.forEach(obsoleteAttribute -> clientEventService.publishEvent(AttributeEvent.deletedAttribute(asset.getId(), obsoleteAttribute.getName())));
break;
}
}
Aggregations