Advertisement
Guest User

sfg

a guest
Dec 2nd, 2017
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.59 KB | None | 0 0
  1. @Override
  2.     public void updateOtherTitleContribution(
  3.             ContributionUpdate<OtherTitle> contribution,
  4.             Long contributionId,
  5.             Long userId
  6.     ) throws ResourceNotFoundException {
  7.         log.info("Called with contribution {}, contributionId {}, userId {}",
  8.                 contributionId, contributionId, userId);
  9.  
  10.         final UserEntity user = this.findUser(userId);
  11.         final ContributionEntity contributionEntity = this.findContribution(contributionId, user, MovieField.OTHER_TITLE);
  12.  
  13.         if(contribution.getElementsToAdd() != null) {
  14.             this.validIds(contributionEntity.getIdsToAdd(), contribution.getElementsToAdd().keySet());
  15.         }
  16.         if(contribution.getElementsToUpdate() != null) {
  17.             this.validIds(contributionEntity.getIdsToUpdate().keySet(), contribution.getElementsToUpdate().keySet());
  18.         }
  19.         if(contribution.getIdsToDelete() != null) {
  20.             this.validIds(contributionEntity.getIdsToDelete(), contribution.getIdsToDelete());
  21.         }
  22.  
  23.         // UPDATE
  24.  
  25.         if(contribution.getElementsToUpdate() != null) {
  26.             for (final Iterator<Map.Entry<Long, Long>> it = contributionEntity.getIdsToUpdate().entrySet().iterator(); it.hasNext(); )
  27.             {
  28.                 if (!contribution.getElementsToUpdate().containsKey(it.next().getKey())) {
  29.                     final MovieOtherTitle oldTitle = this.entityManager.find(MovieOtherTitle.class, it.next().getValue());
  30.                     final MovieOtherTitle newTitle = this.entityManager.find(MovieOtherTitle.class, it.next().getKey());
  31.  
  32.                     it.remove();
  33.                     this.entityManager.remove(newTitle);
  34.                     oldTitle.setReportedForUpdate(false);
  35.                 }
  36.             }
  37.  
  38.             contribution.getElementsToUpdate().forEach((key, value) -> {
  39.                 this.moviePersistenceService.updateOtherTitle(value, key);
  40.             });
  41.         } else  {
  42.             for (final Iterator<Map.Entry<Long, Long>> it = contributionEntity.getIdsToUpdate().entrySet().iterator(); it.hasNext(); )
  43.         {
  44.                 final MovieOtherTitle oldTitle = this.entityManager.find(MovieOtherTitle.class, it.next().getValue());
  45.                 final MovieOtherTitle newTitle = this.entityManager.find(MovieOtherTitle.class, it.next().getKey());
  46.  
  47.                 it.remove();
  48.                 this.entityManager.remove(newTitle);
  49.                 oldTitle.setReportedForUpdate(false);
  50.             }
  51.         }
  52.  
  53.         // DELETE
  54.  
  55.         if(contribution.getIdsToDelete() != null) {
  56.             final List<Long> idsToDelete = new ArrayList<>(contribution.getIdsToDelete());
  57.  
  58.             for (final Iterator<Long> it = contributionEntity.getIdsToDelete().iterator(); it.hasNext(); ) {
  59.                 final Long id = it.next();
  60.                 if (!idsToDelete.contains(id)) {
  61.                     it.remove();
  62.                 }
  63.             }
  64.         } else {
  65.             for (final Iterator<Long> it = contributionEntity.getIdsToDelete().iterator(); it.hasNext(); ) {
  66.                 it.remove();
  67.             }
  68.         }
  69.  
  70.          // ADD
  71.  
  72.         if(contribution.getElementsToAdd() != null) {
  73.             for (final Iterator<Long> it = contributionEntity.getIdsToAdd().iterator(); it.hasNext(); ) {
  74.                 final Long id = it.next();
  75.                 if (!contribution.getElementsToAdd().containsKey(id)) {
  76.                     it.remove();
  77.                     this.entityManager.remove(this.movieInfoRepository.findOne(id));
  78.                 }
  79.             }
  80.  
  81.             contribution.getElementsToAdd().forEach((key, value) -> {
  82.                 this.moviePersistenceService.updateOtherTitle(value, key);
  83.             });
  84.         } else {
  85.             for (final Iterator<Long> it = contributionEntity.getIdsToAdd().iterator(); it.hasNext(); ) {
  86.                 final Long id = it.next();
  87.                 it.remove();
  88.                 this.entityManager.remove(this.movieInfoRepository.findOne(id));
  89.             }
  90.         }
  91.  
  92.         // ADD
  93.  
  94.         if(contribution.getNewElementsToAdd() != null) {
  95.             contribution.getNewElementsToAdd()
  96.                     .forEach(otherTitle -> {
  97.                         final Long id = this.moviePersistenceService.createOtherTitle(otherTitle, contributionEntity.getMovie(), user);
  98.                         contributionEntity.getIdsToAdd().add(id);
  99.                     });
  100.         }
  101. //
  102.         contributionEntity.setSources(contribution.getSources());
  103.         Optional.ofNullable(contribution.getComment()).ifPresent(contributionEntity::setUserComment);
  104.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement