Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void updateOtherTitleContribution(
- ContributionUpdate<OtherTitle> 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<Map.Entry<Long, Long>> 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<Map.Entry<Long, Long>> 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<Long> idsToDelete = new ArrayList<>(contribution.getIdsToDelete());
- for (final Iterator<Long> it = contributionEntity.getIdsToDelete().iterator(); it.hasNext(); ) {
- final Long id = it.next();
- if (!idsToDelete.contains(id)) {
- it.remove();
- }
- }
- } else {
- for (final Iterator<Long> it = contributionEntity.getIdsToDelete().iterator(); it.hasNext(); ) {
- it.remove();
- }
- }
- // ADD
- if(contribution.getElementsToAdd() != null) {
- for (final Iterator<Long> 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<Long> 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement