String oldLocation = null;
final DocumentCommandValidator validator = new DocumentCommandValidator(documentCommand);
validator.validateForUpdate();
// TODO check if entity id is valid and within data scope for the
// user
final Document documentForUpdate = this.documentRepository.findOne(documentCommand.getId());
if (documentForUpdate == null) { throw new DocumentNotFoundException(documentCommand.getParentEntityType(),
documentCommand.getParentEntityId(), documentCommand.getId()); }
final StorageType documentStoreType = documentForUpdate.storageType();
oldLocation = documentForUpdate.getLocation();
if (inputStream != null && documentCommand.isFileNameChanged()) {
final ContentRepository contentRepository = this.contentRepositoryFactory.getRepository();
documentCommand.setLocation(contentRepository.saveFile(inputStream, documentCommand));
documentCommand.setStorageType(contentRepository.getStorageType().getValue());
}
documentForUpdate.update(documentCommand);
if (inputStream != null && documentCommand.isFileNameChanged()) {
final ContentRepository contentRepository = this.contentRepositoryFactory.getRepository(documentStoreType);
contentRepository.deleteFile(documentCommand.getName(), oldLocation);
}
this.documentRepository.saveAndFlush(documentForUpdate);
return new CommandProcessingResult(documentForUpdate.getId());
} catch (final DataIntegrityViolationException dve) {
logger.error(dve.getMessage(), dve);
throw new PlatformDataIntegrityException("error.msg.document.unknown.data.integrity.issue",
"Unknown data integrity issue with resource.");
} catch (final ContentManagementException cme) {