@Override public void updateOtherTitleContribution( ContributionUpdate contribution, Long contributionId, Long userId ) throws ResourceNotFoundException { log.info("Called with contribution {}, contributionId {}, userId {}", contributionId, contributionId, userId); final UserEntity user = this.findUser(userId); final ContributionEntity contributionEntity = this.findContribution(contributionId, user, MovieField.OTHER_TITLE); if(contribution.getElementsToAdd() != null) { this.validIds(contributionEntity.getIdsToAdd(), contribution.getElementsToAdd().keySet()); } if(contribution.getElementsToUpdate() != null) { this.validIds(contributionEntity.getIdsToUpdate().keySet(), contribution.getElementsToUpdate().keySet()); } if(contribution.getIdsToDelete() != null) { this.validIds(contributionEntity.getIdsToDelete(), contribution.getIdsToDelete()); } // UPDATE if(contribution.getElementsToUpdate() != null) { for (final Iterator> it = contributionEntity.getIdsToUpdate().entrySet().iterator(); it.hasNext(); ) { if (!contribution.getElementsToUpdate().containsKey(it.next().getKey())) { final MovieOtherTitle oldTitle = this.entityManager.find(MovieOtherTitle.class, it.next().getValue()); final MovieOtherTitle newTitle = this.entityManager.find(MovieOtherTitle.class, it.next().getKey()); it.remove(); this.entityManager.remove(newTitle); oldTitle.setReportedForUpdate(false); } } contribution.getElementsToUpdate().forEach((key, value) -> { this.moviePersistenceService.updateOtherTitle(value, key); }); } else { for (final Iterator> it = contributionEntity.getIdsToUpdate().entrySet().iterator(); it.hasNext(); ) { final MovieOtherTitle oldTitle = this.entityManager.find(MovieOtherTitle.class, it.next().getValue()); final MovieOtherTitle newTitle = this.entityManager.find(MovieOtherTitle.class, it.next().getKey()); it.remove(); this.entityManager.remove(newTitle); oldTitle.setReportedForUpdate(false); } } // DELETE if(contribution.getIdsToDelete() != null) { final List idsToDelete = new ArrayList<>(contribution.getIdsToDelete()); for (final Iterator it = contributionEntity.getIdsToDelete().iterator(); it.hasNext(); ) { final Long id = it.next(); if (!idsToDelete.contains(id)) { it.remove(); } } } else { for (final Iterator it = contributionEntity.getIdsToDelete().iterator(); it.hasNext(); ) { it.remove(); } } // ADD if(contribution.getElementsToAdd() != null) { for (final Iterator it = contributionEntity.getIdsToAdd().iterator(); it.hasNext(); ) { final Long id = it.next(); if (!contribution.getElementsToAdd().containsKey(id)) { it.remove(); this.entityManager.remove(this.movieInfoRepository.findOne(id)); } } contribution.getElementsToAdd().forEach((key, value) -> { this.moviePersistenceService.updateOtherTitle(value, key); }); } else { for (final Iterator it = contributionEntity.getIdsToAdd().iterator(); it.hasNext(); ) { final Long id = it.next(); it.remove(); this.entityManager.remove(this.movieInfoRepository.findOne(id)); } } // ADD if(contribution.getNewElementsToAdd() != null) { contribution.getNewElementsToAdd() .forEach(otherTitle -> { final Long id = this.moviePersistenceService.createOtherTitle(otherTitle, contributionEntity.getMovie(), user); contributionEntity.getIdsToAdd().add(id); }); } // contributionEntity.setSources(contribution.getSources()); Optional.ofNullable(contribution.getComment()).ifPresent(contributionEntity::setUserComment); }