use of org.openremote.container.persistence.PersistenceEvent in project openremote by openremote.
the class AgentService method processAgentChange.
/**
* Looks for new, modified and obsolete protocol configurations and links / unlinks any associated attributes
*/
protected void processAgentChange(Asset agent, PersistenceEvent persistenceEvent) {
LOG.finest("Processing agent persistence event: " + persistenceEvent.getCause());
switch(persistenceEvent.getCause()) {
case INSERT:
addReplaceAgent(agent);
linkProtocolConfigurations(agent.getAttributesStream().filter(ProtocolConfiguration::isProtocolConfiguration));
break;
case UPDATE:
addReplaceAgent(agent);
// Check if any protocol config attributes have been added/removed or modified
int attributesIndex = Arrays.asList(persistenceEvent.getPropertyNames()).indexOf("attributes");
if (attributesIndex < 0) {
return;
}
// Attributes have possibly changed so need to compare old and new state to determine
// which protocol configs are affected
List<AssetAttribute> oldProtocolConfigurations = attributesFromJson((ObjectValue) persistenceEvent.getPreviousState()[attributesIndex], agent.getId()).filter(ProtocolConfiguration::isProtocolConfiguration).collect(Collectors.toList());
List<AssetAttribute> newProtocolConfigurations = attributesFromJson((ObjectValue) persistenceEvent.getCurrentState()[attributesIndex], agent.getId()).filter(ProtocolConfiguration::isProtocolConfiguration).collect(Collectors.toList());
// Compare protocol configurations by JSON value
// Unlink protocols that are in oldConfigs but not in newConfigs
unlinkProtocolConfigurations(oldProtocolConfigurations.stream().filter(oldProtocolAttribute -> newProtocolConfigurations.stream().noneMatch(oldProtocolAttribute::equals)));
// Link protocols that are in newConfigs but not in oldConfigs
linkProtocolConfigurations(newProtocolConfigurations.stream().filter(newProtocolAttribute -> oldProtocolConfigurations.stream().noneMatch(newProtocolAttribute::equals)));
break;
case DELETE:
removeAgent(agent);
// Unlink any attributes that have an agent link to this agent
unlinkProtocolConfigurations(agent.getAttributesStream().filter(ProtocolConfiguration::isProtocolConfiguration));
break;
}
}
Aggregations