This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository coselmar. See http://git.codelutin.com/coselmar.git commit 4e09a9665c18f531809074b5a80cae3ca7deb8df Author: Yannick Martel <martel@©odelutin.com> Date: Wed Jan 21 11:57:34 2015 +0100 prepare document edit service --- .../coselmar/services/v1/DocumentsWebService.java | 74 +++++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java index 9392bf6..6305cf8 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java @@ -346,8 +346,78 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { } - public void saveDocument(DocumentBean documentBean) { - throw new CoselmarTechnicalException("not yet implemented"); + public void saveDocument(DocumentBean document) throws InvalidCredentialException, UnauthorizedException { + Preconditions.checkNotNull(document); + Preconditions.checkNotNull(document.getId()); + + // Check authentication + String authorization = getContext().getHeader("Authorization"); + UserWebToken userWebToken = checkAuthentication(authorization); + + String documentId = getDocumentFullId(document.getId()); + + Document documentEntity = getDocumentDao().forTopiaIdEquals(documentId).findUnique(); + + if (!isAllowedToAccessDocument(userWebToken, documentEntity)) { + + String message = String.format("User %s %s ('%s') try to modify document '%s'", + userWebToken.getFirstName(), userWebToken.getLastName(), userWebToken.getUserId(), documentId); + if (log.isWarnEnabled()) { + log.warn(message); + } + throw new UnauthorizedException(message); + + } + + boolean isUsedByQuestions = getQuestionDao().forRelatedDocumentsContains(documentEntity).exists(); + if (isUsedByQuestions) { + String message = "Document is used by some questions, cannot be modified."; + if (log.isWarnEnabled()) { + log.warn(message); + } + throw new UnauthorizedException(message); + } + + documentEntity.setName(document.getName()); + documentEntity.setPrivacy(Privacy.valueOf(document.getPrivacy().toUpperCase())); + + documentEntity.clearKeywords(); + documentEntity.addAllKeywords(document.getKeywords()); + + documentEntity.setType(document.getType()); + documentEntity.setSummary(document.getSummary()); + documentEntity.setLanguage(document.getLanguage()); + documentEntity.setPublicationDate(document.getPublicationDate()); + + // Legal / copyright part + documentEntity.setAuthors(document.getAuthors()); + documentEntity.setCopyright(document.getCopyright()); + documentEntity.setLicense(document.getLicense()); + + // Resource part + documentEntity.setExternalUrl(document.getExternalUrl()); + + documentEntity.setComment(document.getComment()); + + commit(); + + // Update index information for this document + String lightId = document.getId(); + DocumentBean result = BeanEntityConverter.toBean(lightId, documentEntity); + + DocumentsIndexationService documentsIndexationService = getServicesContext().newService(DocumentsIndexationService.class); + try { + documentsIndexationService.indexDocument(result); + if (log.isDebugEnabled()) { + String message = String.format("Document '%s' was updated in index", document.getName()); + log.debug(message); + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Unable to update document index information", e); + } + } + } public void deleteDocument(String documentId) throws InvalidCredentialException, UnauthorizedException { -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.