Esempio n. 1
0
 @Override
 @HasPermission(action = UGC_UPDATE, type = SocialPermission.class)
 public FileInfo updateAttachment(
     @SecuredObject final String ugcId,
     final String contextId,
     final String attachmentId,
     final InputStream newAttachment)
     throws UGCException, FileNotFoundException {
   if (!ObjectId.isValid(ugcId)) {
     throw new IllegalArgumentException("Given Ugc Id is not valid");
   }
   if (!ObjectId.isValid(attachmentId)) {
     throw new IllegalArgumentException("Given UGC Id is not valid");
   }
   try {
     T ugc = (T) ugcRepository.findUGC(contextId, ugcId);
     if (ugc == null) {
       throw new IllegalUgcException("Given UGC Id does not exists");
     }
     FileInfo oldInfo = ugcRepository.getFileInfo(attachmentId);
     ugc.getAttachments().remove(oldInfo);
     FileInfo newInfo =
         ugcRepository.updateFile(
             new ObjectId(attachmentId),
             newAttachment,
             oldInfo.getFileName(),
             oldInfo.getContentType(),
             true);
     ugc.getAttachments().add(newInfo);
     ugcRepository.update(ugcId, ugc);
     reactor.notify(
         UGCEvent.DELETE_ATTACHMENT.getName(),
         Event.wrap(new SocialEvent<>(ugcId, attachmentId, UGCEvent.DELETE_ATTACHMENT)));
     reactor.notify(
         UGCEvent.ADD_ATTACHMENT.getName(),
         Event.wrap(
             new SocialEvent<>(
                 ugcId, new InputStream[] {new CloseShieldInputStream(newAttachment)}),
             UGCEvent.ADD_ATTACHMENT));
     return newInfo;
   } catch (MongoDataException e) {
     log.error("logging.ugc.attachmentError");
     throw new UGCException("Unable to removeWatcher Attachment");
   } catch (FileExistsException e) {
     log.error("logging.ugc.attachmentNotFound", attachmentId);
     throw new UGCException("Unable to find attachment with given id", e);
   }
 }