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 ea8bffd280ceb0cec0403d8a6d281966ed82584e Author: Yannick Martel <martel@©odelutin.com> Date: Fri Jan 16 12:49:10 2015 +0100 change way to create document with file, in order to have metadata in utf-8 : split into to ways : first create document and then attach file --- .../src/main/xmi/coselmar-model.zargo | Bin 10322 -> 10390 bytes .../fr/ifremer/coselmar/beans/DocumentBean.java | 12 ++++- .../coselmar/converter/BeanEntityConverter.java | 3 +- .../coselmar/services/v1/DocumentsWebService.java | 54 ++++++++++++++++++++- coselmar-rest/src/main/resources/mapping | 3 +- .../src/main/webapp/js/coselmar-controllers.js | 46 ++++++++++-------- .../src/main/webapp/js/coselmar-services.js | 37 ++++++++++++++ .../src/main/webapp/views/documents/document.html | 4 ++ 8 files changed, 135 insertions(+), 24 deletions(-) diff --git a/coselmar-persistence/src/main/xmi/coselmar-model.zargo b/coselmar-persistence/src/main/xmi/coselmar-model.zargo index c48b79e..ee44fcc 100644 Binary files a/coselmar-persistence/src/main/xmi/coselmar-model.zargo and b/coselmar-persistence/src/main/xmi/coselmar-model.zargo differ diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/beans/DocumentBean.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/beans/DocumentBean.java index 51e778d..2b60237 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/beans/DocumentBean.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/beans/DocumentBean.java @@ -50,6 +50,7 @@ public class DocumentBean implements Serializable { protected String summary; protected Date publicationDate; protected String comment; + protected String fileName; // Document could be internal file or external link protected boolean withFile; @@ -61,7 +62,7 @@ public class DocumentBean implements Serializable { String type, String summary, String language, Date publicationDate, String authors, String license, String copyright, boolean withFile, String mimeType, String externalUrl, - String comment) { + String comment, String fileName) { this.id = id; this.name = name; this.ownerName = ownerName; @@ -86,6 +87,7 @@ public class DocumentBean implements Serializable { this.withFile = withFile; this.mimeType = mimeType; this.externalUrl = externalUrl; + this.fileName = fileName; this.comment = comment; } @@ -229,6 +231,14 @@ public class DocumentBean implements Serializable { this.mimeType = mimeType; } + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + public String getComment() { return comment; } diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/converter/BeanEntityConverter.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/converter/BeanEntityConverter.java index 3ae8e53..3dc4765 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/converter/BeanEntityConverter.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/converter/BeanEntityConverter.java @@ -77,7 +77,8 @@ public class BeanEntityConverter { document.isWithFile(), document.getMimeType(), document.getExternalUrl(), - document.getComment() + document.getComment(), + document.getFileName() ); } 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 63dbf28..9392bf6 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 @@ -263,6 +263,58 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { } + public void addDocumentFile(String documentId, UploadFile uploadFile) throws InvalidCredentialException, UnauthorizedException { + + // Check authentication + String authorization = getContext().getHeader("Authorization"); + UserWebToken userWebToken = checkAuthentication(authorization); + + // Only Expert or Supervisor can add document + String userRole = userWebToken.getRole(); + if (!DOCUMENT_EDIT_ALLOWED_USER_ROLES.contains(userRole.toUpperCase())) { + String message = String.format("User %s %s ('%s') is not allowed to add document", + userWebToken.getFirstName(), userWebToken.getLastName(), userWebToken.getUserId()); + if (log.isWarnEnabled()) { + log.warn(message); + } + throw new UnauthorizedException(message); + } + + Preconditions.checkNotNull(documentId); + Preconditions.checkNotNull(uploadFile); + + // retrieve user who will be assigned as document owner + String userFullId = getFullUserIdFromShort(userWebToken.getUserId()); + + CoselmarUser owner; + try { + owner = getCoselmarUserDao().forTopiaIdEquals(userFullId).findUnique(); + } catch (TopiaNoResultException tnre) { + // Should not happened, cause user are not really deleted + String message = String.format("Seems that logged user ('%s') does not exist anymore.", userFullId); + if (log.isErrorEnabled()) { + log.equals(message); + } + throw new InvalidCredentialException(message); + } + + String documentFullId = getFullIdFromShort(Document.class, documentId); + Document document = getDocumentDao().forTopiaIdEquals(documentFullId).findAny(); + + Pair<String, String> pathAndContentType = managerDocumentFile(uploadFile, owner); + String filePath = pathAndContentType.getLeft(); + String contentType = pathAndContentType.getRight(); + + document.setWithFile(true); + document.setMimeType(contentType); + document.setFilePath(filePath); + document.setFileName(uploadFile.getName()); + + commit(); + + } + + public Render getDocumentFile(String documentId) throws NoResultException { // reconstitute full id @@ -278,7 +330,7 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { } File documentFile = new File(filePath); - String fileName = document.getName(); + String fileName = StringUtils.isNotBlank(document.getFileName()) ? document.getFileName() : document.getName(); String fileMimeType = document.getMimeType(); try { InputStream fileStream = new FileInputStream(documentFile); diff --git a/coselmar-rest/src/main/resources/mapping b/coselmar-rest/src/main/resources/mapping index 1a93585..fe0500d 100644 --- a/coselmar-rest/src/main/resources/mapping +++ b/coselmar-rest/src/main/resources/mapping @@ -36,7 +36,8 @@ GET /v1/documents/types DocumentsWebService.getTypes GET /v1/documents/{documentId} DocumentsWebService.getDocument GET /v1/documents/{documentId}/file DocumentsWebService.getDocumentFile POST /v1/documents DocumentsWebService.addDocument -PUT /v1/documents/{documentId} DocumentsWebService.saveDocument +POST /v1/documents/{documentId} DocumentsWebService.saveDocument +POST /v1/documents/{documentId}/file DocumentsWebService.addDocumentFile DELETE /v1/documents/{documentId} DocumentsWebService.deleteDocument # Users Api diff --git a/coselmar-ui/src/main/webapp/js/coselmar-controllers.js b/coselmar-ui/src/main/webapp/js/coselmar-controllers.js index aecc3cb..75c9906 100644 --- a/coselmar-ui/src/main/webapp/js/coselmar-controllers.js +++ b/coselmar-ui/src/main/webapp/js/coselmar-controllers.js @@ -127,15 +127,20 @@ coselmarControllers.controller("NewDocumentCtrl", ['$scope', '$location', 'docum // Call service to create a new document if (isValidForm) { - documentService.createDocument( - $scope.document, $scope.upload.file, function() { - $location.path("/documents"); + documentService.saveDocument($scope.document, function(document) { + if ($scope.upload.file) { + var documentId = document.id; + documentService.saveDocumentFile(documentId, $scope.upload.file, function() { + $location.path("/documents"); + }); + } else { + $location.path("/documents"); + } + }, function(error) { + console.log("error occurs"); + } + ); - },function(error) { - //TODO ymartel 20141118 : deal with error.status or statusText - console.log("error occurs"); - console.log(error.s); - }); } }; @@ -878,18 +883,19 @@ coselmarControllers.controller('ModalCreateDocumentsCtrl', function ($scope, $mo } if (isValidForm) { - if ($scope.upload.file) { - $scope.document.withFile = true; - } - documentService.createDocument( - $scope.document, $scope.upload.file, function(document) { - $modalInstance.close(document); - - },function(error) { - //TODO ymartel 20141118 : deal with error.status or statusText - console.log("error occurs"); - console.log(error.s); - }); + documentService.saveDocument($scope.document, function(document) { + if ($scope.upload.file) { + var documentId = document.id; + documentService.saveDocumentFile(documentId, $scope.upload.file, function() { + $modalInstance.close(document); + }); + } else { + $modalInstance.close(document); + } + }, function(error) { + console.log("error occurs"); + } + ); } }; diff --git a/coselmar-ui/src/main/webapp/js/coselmar-services.js b/coselmar-ui/src/main/webapp/js/coselmar-services.js index c205a04..e0cebeb 100644 --- a/coselmar-ui/src/main/webapp/js/coselmar-services.js +++ b/coselmar-ui/src/main/webapp/js/coselmar-services.js @@ -33,6 +33,43 @@ function Document(resource, config){ var baseURL = config.BASE_URL + "/documents"; + this.saveDocument = function(document, successFunction, failFunction) { + + var param = $.param({ 'document': JSON.stringify(document)}); + + var serviceURL = baseURL; + if (document.id) { + serviceURL = baseURL + "/" + document.id; + } + + // Save the document + var saveDocumentResource = resource(serviceURL, null, { + 'save': { method: 'POST'} + }); + + saveDocumentResource.save(null, param, successFunction, failFunction); + }; + + this.saveDocumentFile = function(documentId, file, successFunction, failFunction) { + + var formData = new FormData(); + if (file) { + formData.append("uploadFile", file); + } + + var serviceURL = baseURL + "/" + documentId + "/file"; + + // Save the document + var docResource = resource(serviceURL, null, { + 'upload': { + method:'POST', + transformRequest: angular.identity, + headers: {'Content-Type': undefined, 'Content-Transfer-Encoding': 'utf-8'} + } + }); + docResource.upload(null, formData, successFunction, failFunction); + }; + this.createDocument = function(metadata, file, successFunction, failFunction) { var formData = new FormData(); diff --git a/coselmar-ui/src/main/webapp/views/documents/document.html b/coselmar-ui/src/main/webapp/views/documents/document.html index 7eae60a..c6f382b 100644 --- a/coselmar-ui/src/main/webapp/views/documents/document.html +++ b/coselmar-ui/src/main/webapp/views/documents/document.html @@ -83,6 +83,10 @@ <td ng-if="document.withFile">{{document.mimeType}}</td> <td ng-if="!document.withFile">External Link</td> </tr> + <tr ng-if="document.withFile && document.fileName"> + <td>Document File</td> + <td>{{document.fileName}}</td> + </tr> <tr> <td>Comment</td> <td>{{document.comment}}</td> -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.