private void updateUdiEventFromDto(UdiEvent originalUdiEvent, UdiEventDto udiEventDto) { // should not change but you never know, let's allow it for now originalUdiEvent.setPeriod(udiEventDto.getPeriod()); originalUdiEvent.setUdiEventType( UdiEventTransformer.transformToModel(udiEventDto.getUdiEventType())); // may change originalUdiEvent.setStartDtu(udiEventDto.getStartDtu()); originalUdiEvent.setEndDtu(udiEventDto.getEndDtu()); originalUdiEvent.setStartAfterDtu(udiEventDto.getStartAfterDtu()); originalUdiEvent.setFinishBeforeDtu(udiEventDto.getFinishBeforeDtu()); originalUdiEvent.setDeviceSelector(udiEventDto.getDeviceSelector()); Map<String, DeviceCapability> dbCapabilitiesPerId = originalUdiEvent .getDeviceCapabilities() .stream() .collect(Collectors.toMap(DeviceCapability::getId, Function.identity())); LOGGER.debug("Updated udi event with id [{}]", originalUdiEvent.getId()); // update of create udiEventDto .getDeviceCapabilities() .stream() .forEach( deviceCapabilityDto -> { if (!dbCapabilitiesPerId.containsKey(deviceCapabilityDto.getId())) { createDeviceCapability(deviceCapabilityDto, originalUdiEvent); } else { updateDeviceCapability( dbCapabilitiesPerId.get(deviceCapabilityDto.getId()), deviceCapabilityDto); } }); // remove capabilities that are not given back in the updated id list Set<String> updatedCapabilitiesId = udiEventDto .getDeviceCapabilities() .stream() .map(DeviceCapabilityDto::getId) .collect(Collectors.toSet()); dbCapabilitiesPerId .values() .stream() .filter(deviceCapability -> !updatedCapabilitiesId.contains(deviceCapability.getId())) .forEach( deviceCapability -> { LOGGER.debug( "Deleted device capability with id [{}] for udi event with id [{}]", deviceCapability.getId(), deviceCapability.getUdiEvent().getId()); originalUdiEvent.getDeviceCapabilities().remove(deviceCapability); }); }
private void createUdiEventFromDto(UdiEventDto udiEventDto, LocalDate period) { Udi udi = udiRepository.findByEndpoint(udiEventDto.getUdiEndpoint(), period); UdiEvent udiEvent = UdiEventTransformer.transformToModel(udiEventDto, udi); udiEventRepository.persist(udiEvent); LOGGER.debug("Created new udi event with id [{}]", udiEvent.getId()); udiEventDto .getDeviceCapabilities() .forEach(deviceCapabilityDto -> createDeviceCapability(deviceCapabilityDto, udiEvent)); }