Advertisement
Guest User

sa

a guest
Jan 15th, 2018
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. public
  2.     ResponseEntity<Void> createPhotoContribution(
  3.             @ApiParam(value = "The movie ID", required = true)
  4.             @PathVariable("id") final Long id,
  5.             @ApiParam(value = "Elements to be added")
  6.             @RequestPart(required = false) List<MultipartFile> elementsToAdd,
  7.             @ApiParam(value = "Element IDs to be updated")
  8.             @RequestParam(required = false) Set<Long> idsToUpdate,
  9.             @ApiParam(value = "Element to be updated")
  10.             @RequestPart(required = false) List<MultipartFile> elementsToUpdate,
  11.             @ApiParam(value = "Element IDs to be deleted")
  12.             @RequestParam(required = false) Set<Long> idsToDelete,
  13.             @ApiParam(value = "Sources of information(elements)", required = true)
  14.             @RequestParam final Set<String> sources,
  15.             @ApiParam(value = "Comment from the user")
  16.             @RequestParam(required = false) final String comment
  17.     ) throws IOException {
  18.         log.info("Called with id {}, elementsToAdd {}, idsToUpdate {}, elementsToUpdate{}, idsToDelete {}," +
  19.                         " sources {}, comment {}",
  20.                 id, elementsToAdd, idsToUpdate, elementsToUpdate, idsToDelete, sources, comment);
  21.  
  22.         if(elementsToAdd == null) { elementsToAdd = new ArrayList<>(); }
  23.         if(idsToUpdate == null) { idsToUpdate = new HashSet<>(); }
  24.         if(elementsToUpdate == null) { elementsToUpdate = new ArrayList<>(); }
  25.         if(idsToDelete == null) { idsToDelete = new HashSet<>(); }
  26.  
  27.         final Set<Long> allIds = Stream.concat(idsToUpdate.stream(), idsToDelete.stream()).collect(Collectors.toSet());
  28.         for(final Long idd : allIds) {
  29.             if(!this.movieSearchService.existsPhoto(id, idd, DataStatus.ACCEPTED)) {
  30.                 throw new ResourceNotFoundException("No element found with id " + idd);
  31.             }
  32.         }
  33.  
  34.         final List<PhotoRequest> listPhotos = new ArrayList<>();
  35.         for(final MultipartFile multipartFile : elementsToAdd) {
  36.             final String idInCloud = this.storageService.save(FileUtils.convert(multipartFile), multipartFile.getContentType());
  37.             final PhotoRequest.Builder builder = new PhotoRequest.Builder(
  38.                     idInCloud
  39.             );
  40.             listPhotos.add(builder.build());
  41.         }
  42.  
  43.         final HashMap<Long, PhotoRequest> mapPhotos = new HashMap<>();
  44.         final Iterator<Long> longIterator = idsToUpdate.iterator();
  45.         final Iterator<MultipartFile> multipartFileIterator = elementsToUpdate.iterator();
  46.         if(idsToUpdate.size() == elementsToUpdate.size()) {
  47.             while(longIterator.hasNext() && multipartFileIterator.hasNext()) {
  48.                 final Long photoId = longIterator.next();
  49.                 final MultipartFile multipartFile = multipartFileIterator.next();
  50.                 final String idInCloud = this.storageService.save(FileUtils.convert(multipartFile), multipartFile.getContentType());
  51.                 final PhotoRequest.Builder builder = new PhotoRequest.Builder(
  52.                         idInCloud
  53.                 );
  54.                 mapPhotos.put(photoId , builder.build());
  55.             }
  56.         }
  57.  
  58.         final ContributionNew<PhotoRequest> contribution = new ContributionNew<>();
  59.         contribution.setElementsToAdd(listPhotos);
  60.         contribution.setElementsToUpdate(mapPhotos);
  61.         contribution.setIdsToDelete(idsToDelete);
  62.         contribution.setSources(sources);
  63.  
  64.         final Long cId = this.movieContributionPersistenceService.createPhotoContribution(contribution, id, "d427d119-1c3d-4435-8f6d-5c8dd35a45b5");
  65.  
  66.         final HttpHeaders httpHeaders = new HttpHeaders();
  67.         httpHeaders.setLocation(
  68.                 MvcUriComponentsBuilder
  69.                         .fromMethodName(MovieContributionRestController.class, "getReviewContribution", cId)
  70.                         .buildAndExpand(cId)
  71.                         .toUri()
  72.         );
  73.  
  74.         return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement