@Override public InformationSource writeReadoutsToSource(InformationSource source, List<Sensor> sensors) { final InformationSource src = sourceService.getWithSensors(source.getId()); addNewSensors(sensors, src); sourceService.save(src); associateReadoutsWithPersistentSensors(src, sensors); sourceService.save(src); return source; }
/** * Adds new readouts to persistent sensors and saves those sensors with their new readouts, * cascading. * * @param source * @param sensors */ private void associateReadoutsWithPersistentSensors( InformationSource source, List<Sensor> sensors) { sensors.forEach( s -> { source .getSensors() .stream() .filter(sensor -> sensor.isActive()) .forEach( sensor -> { if (equalsButNotTheSameInstance(s, sensor)) { addReadoutsToSensor(sensor, s.getReadouts()); } }); }); }
private void addNewSensors(List<Sensor> sensors, final InformationSource src) { sensors.forEach( s -> { src.addSensor(s); }); }