/** * Updates, creates or deletes UDI events using a given list of UDI Event DTOs. * * @param period {@link LocalDate} period for which UDI Events will be created/updated/deleted. * @param updatedUdiEvents {@link List} of {@link UdiEventDto}. */ public void updateUdiEvents(LocalDate period, List<UdiEventDto> updatedUdiEvents) { LOGGER.debug("Updating udi events for period [{}]", period); // fetch existing udi events Map<String, UdiEvent> udiEventsPerId = udiEventRepository .findUdiEventsForPeriod(period) .stream() .collect(Collectors.toMap(UdiEvent::getId, Function.identity())); // 1. Update or create UdiEvents for (UdiEventDto udiEventDto : updatedUdiEvents) { if (!udiEventsPerId.containsKey(udiEventDto.getId())) { createUdiEventFromDto(udiEventDto, period); } else { updateUdiEventFromDto(udiEventsPerId.get(udiEventDto.getId()), udiEventDto); } } // 2. Delete udi events not present in the list anymore Set<String> updatedIds = updatedUdiEvents.stream().map(UdiEventDto::getId).collect(Collectors.toSet()); udiEventsPerId .entrySet() .stream() .filter(udiEventPerId -> !updatedIds.contains(udiEventPerId.getKey())) .forEach( udiEventPerId -> { LOGGER.debug("Deleted udi event with id [{}]", udiEventPerId.getKey()); udiEventRepository.delete(udiEventPerId.getValue()); }); }
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)); }