Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public
- ResponseEntity<Void> createPhotoContribution(
- @ApiParam(value = "The movie ID", required = true)
- @PathVariable("id") final Long id,
- @ApiParam(value = "Elements to be added")
- @RequestPart(required = false) List<MultipartFile> elementsToAdd,
- @ApiParam(value = "Element IDs to be updated")
- @RequestParam(required = false) Set<Long> idsToUpdate,
- @ApiParam(value = "Element to be updated")
- @RequestPart(required = false) List<MultipartFile> elementsToUpdate,
- @ApiParam(value = "Element IDs to be deleted")
- @RequestParam(required = false) Set<Long> idsToDelete,
- @ApiParam(value = "Sources of information(elements)", required = true)
- @RequestParam final Set<String> sources,
- @ApiParam(value = "Comment from the user")
- @RequestParam(required = false) final String comment
- ) throws IOException {
- log.info("Called with id {}, elementsToAdd {}, idsToUpdate {}, elementsToUpdate{}, idsToDelete {}," +
- " sources {}, comment {}",
- id, elementsToAdd, idsToUpdate, elementsToUpdate, idsToDelete, sources, comment);
- if(elementsToAdd == null) { elementsToAdd = new ArrayList<>(); }
- if(idsToUpdate == null) { idsToUpdate = new HashSet<>(); }
- if(elementsToUpdate == null) { elementsToUpdate = new ArrayList<>(); }
- if(idsToDelete == null) { idsToDelete = new HashSet<>(); }
- final Set<Long> allIds = Stream.concat(idsToUpdate.stream(), idsToDelete.stream()).collect(Collectors.toSet());
- for(final Long idd : allIds) {
- if(!this.movieSearchService.existsPhoto(id, idd, DataStatus.ACCEPTED)) {
- throw new ResourceNotFoundException("No element found with id " + idd);
- }
- }
- final List<PhotoRequest> listPhotos = new ArrayList<>();
- for(final MultipartFile multipartFile : elementsToAdd) {
- final String idInCloud = this.storageService.save(FileUtils.convert(multipartFile), multipartFile.getContentType());
- final PhotoRequest.Builder builder = new PhotoRequest.Builder(
- idInCloud
- );
- listPhotos.add(builder.build());
- }
- final HashMap<Long, PhotoRequest> mapPhotos = new HashMap<>();
- final Iterator<Long> longIterator = idsToUpdate.iterator();
- final Iterator<MultipartFile> multipartFileIterator = elementsToUpdate.iterator();
- if(idsToUpdate.size() == elementsToUpdate.size()) {
- while(longIterator.hasNext() && multipartFileIterator.hasNext()) {
- final Long photoId = longIterator.next();
- final MultipartFile multipartFile = multipartFileIterator.next();
- final String idInCloud = this.storageService.save(FileUtils.convert(multipartFile), multipartFile.getContentType());
- final PhotoRequest.Builder builder = new PhotoRequest.Builder(
- idInCloud
- );
- mapPhotos.put(photoId , builder.build());
- }
- }
- final ContributionNew<PhotoRequest> contribution = new ContributionNew<>();
- contribution.setElementsToAdd(listPhotos);
- contribution.setElementsToUpdate(mapPhotos);
- contribution.setIdsToDelete(idsToDelete);
- contribution.setSources(sources);
- final Long cId = this.movieContributionPersistenceService.createPhotoContribution(contribution, id, "d427d119-1c3d-4435-8f6d-5c8dd35a45b5");
- final HttpHeaders httpHeaders = new HttpHeaders();
- httpHeaders.setLocation(
- MvcUriComponentsBuilder
- .fromMethodName(MovieContributionRestController.class, "getReviewContribution", cId)
- .buildAndExpand(cId)
- .toUri()
- );
- return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement