This is an automated email from the git hooks/post-receive script. New commit to branch feature/6009-create-document in repository coselmar. See http://git.codelutin.com/coselmar.git commit 3de9b629042d43f41c3c97ff7bbcd5b05ce53509 Author: Yannick Martel <martel@©odelutin.com> Date: Wed Nov 5 18:11:13 2014 +0100 add document file upload during document creation --- .../services/DefaultCoselmarServicesContext.java | 2 +- .../coselmar/services/v1/DocumentsWebService.java | 59 ++++++++++++++++++---- .../src/main/webapp/js/coselmar-controllers.js | 32 ++++++++---- .../src/main/webapp/js/coselmar-services.js | 22 +++++--- coselmar-ui/src/main/webapp/views/newdocument.html | 8 +-- 5 files changed, 91 insertions(+), 32 deletions(-) diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/DefaultCoselmarServicesContext.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/DefaultCoselmarServicesContext.java index 3b48cc2..db78d34 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/DefaultCoselmarServicesContext.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/DefaultCoselmarServicesContext.java @@ -57,7 +57,7 @@ public class DefaultCoselmarServicesContext implements CoselmarServicesContext { @Override public CoselmarServicesConfig getCoselmarServicesConfig() { - return null; + return this.servicesConfig; } @Override 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 56381ca..d05a16b 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 @@ -1,5 +1,7 @@ package fr.ifremer.coselmar.services.v1; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -10,12 +12,20 @@ import fr.ifremer.coselmar.persistence.entity.Document; import fr.ifremer.coselmar.persistence.entity.DocumentPrivacy; import fr.ifremer.coselmar.services.CoselmarTechnicalException; import fr.ifremer.coselmar.services.CoselmarWebServiceSupport; +import org.apache.commons.io.FileUtils; +import org.apache.commons.logging.Log; +import org.debux.webmotion.server.call.UploadFile; +import org.nuiton.util.DateUtil; + +import static org.apache.commons.logging.LogFactory.getLog; /** * @author ymartel <martel@codelutin.com> */ public class DocumentsWebService extends CoselmarWebServiceSupport { + private static final Log log = getLog(DocumentsWebService.class); + public DocumentBean getDocument(String documentId) { Document document = getDocumentDao().forTopiaIdEquals(documentId).findUnique(); @@ -40,21 +50,50 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { return result; } - public void addDocument(DocumentBean documentBean) { - Preconditions.checkNotNull(documentBean); - Document document = getDocumentDao().create(); + public void addDocument(DocumentBean document, UploadFile uploadFile) { + Preconditions.checkNotNull(document); + Preconditions.checkNotNull(uploadFile); + + // Document File + String fileName = uploadFile.getName(); + File uploadedFile = uploadFile.getFile(); + String contentType = uploadFile.getContentType(); + + if (log.isInfoEnabled()) { + String message = String.format("File name : %s, content-type : %s", fileName, contentType); + log.info(message); + } + + // put the document in the good directory + // TODO ymartel 20141105 : with user management, put the document in a user specifique folder + File dataDirectory = getCoselmarServicesConfig().getDataDirectory(); + String absolutePath = dataDirectory.getAbsolutePath(); + Date now = getNow(); + String formatedDay = DateUtil.formatDate(now, "yyyymmdd"); + String prefix = formatedDay + "-"; + File destFile = new File(absolutePath + "/" + prefix + fileName); + try { + FileUtils.moveFile(uploadedFile, destFile); + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("error during File transfer", e); + } + throw new CoselmarTechnicalException("Internal error during file transfer"); + } + + // Document Metadata + Document documentEntity = getDocumentDao().create(); - document.setName(documentBean.getName()); - document.setPrivacy(DocumentPrivacy.valueOf(documentBean.getPrivacy().toUpperCase())); - document.addAllKeywords(documentBean.getKeywords()); + documentEntity.setName(fileName); + documentEntity.setPrivacy(DocumentPrivacy.valueOf(document.getPrivacy().toUpperCase())); + documentEntity.addAllKeywords(document.getKeywords()); - Date depositDate = documentBean.getDepositDate(); + Date depositDate = document.getDepositDate(); if (depositDate != null) { - document.setDepositDate(new Date(depositDate.getTime())); + documentEntity.setDepositDate(new Date(depositDate.getTime())); } else { - document.setDepositDate(new Date()); + documentEntity.setDepositDate(new Date()); } - //TODO ymartel 20141103 : manage file commit(); } diff --git a/coselmar-ui/src/main/webapp/js/coselmar-controllers.js b/coselmar-ui/src/main/webapp/js/coselmar-controllers.js index b7dd9e1..4164fc8 100644 --- a/coselmar-ui/src/main/webapp/js/coselmar-controllers.js +++ b/coselmar-ui/src/main/webapp/js/coselmar-controllers.js @@ -10,24 +10,18 @@ coselmarControllers.controller("DocumentsCtrl", ['$scope','documentService', fun // Controller for new document View -coselmarControllers.controller("NewDocumentCtrl", ['$scope', 'documentService', function($scope, documentService){ +coselmarControllers.controller("NewDocumentCtrl", ['$scope', '$route', 'documentService', function($scope, $route, documentService){ $scope.privacy="public"; $scope.createNewDocument = function(){ - var newdocument = {'name':$scope.name, 'file':$scope.file, 'privacy':$scope.privacy, 'keywords':$scope.keywords}; + var documentMetadata = {'privacy':$scope.privacy, 'keywords':$scope.keywords}; // Call service to create a new document - documentService.createDocument(newdocument, $scope); + documentService.createDocument(documentMetadata, $scope.documentFile, $scope); - // Push new doc to existing table column - //$scope.documents.push(newdocument); - - // Reset fields values - $scope.name=''; - $scope.file=''; - $scope.privacy=''; - $scope.keywords=''; + // Reload the page + $route.reload(); }; }]); @@ -35,3 +29,19 @@ coselmarControllers.controller("NewDocumentCtrl", ['$scope', 'documentService', coselmarControllers.controller("DocumentViewCtrl", [ '$scope','documentService', '$routeParams', function($scope, documentService, $routeParams) { documentService.getDocument($routeParams.documentId, $scope); } ]); + +coselmarControllers.directive('ngFileModel', ['$parse', function ($parse) { + return { + restrict: 'A', + link: function(scope, element, attrs) { + var model = $parse(attrs.ngFileModel); + var modelSetter = model.assign; + + element.bind('change', function(){ + scope.$apply(function(){ + modelSetter(scope, element[0].files[0]); + }); + }); + } + }; +}]); \ No newline at end of file diff --git a/coselmar-ui/src/main/webapp/js/coselmar-services.js b/coselmar-ui/src/main/webapp/js/coselmar-services.js index fe40926..86c98b0 100644 --- a/coselmar-ui/src/main/webapp/js/coselmar-services.js +++ b/coselmar-ui/src/main/webapp/js/coselmar-services.js @@ -8,15 +8,23 @@ function Document(resource){ this.resource = resource; - this.createDocument = function(document, scope){ - - console.log("document to create"); - console.log(document); + this.createDocument = function(metadata, file, scope){ + var formData = new FormData(); + formData.append("uploadFile", file); + formData.append("document", JSON.stringify(metadata)); // Save the document - var docResource = resource('http://localhost:8081/services/v1/documents', {'documentBean':document}); - docResource.save(document, function(response){ - scope.message = response.message; + var docResource = resource('http://localhost:8081/services/v1/documents', null, { + 'upload': { + method:'POST', + transformRequest: angular.identity, + headers:{ + 'Content-Type':undefined + } + } + }); + docResource.upload(formData, function(response){ + //manage result }); } diff --git a/coselmar-ui/src/main/webapp/views/newdocument.html b/coselmar-ui/src/main/webapp/views/newdocument.html index 2a27b38..28405d9 100644 --- a/coselmar-ui/src/main/webapp/views/newdocument.html +++ b/coselmar-ui/src/main/webapp/views/newdocument.html @@ -14,10 +14,12 @@ <div class=""> <form class="form-horizontal" role="form" ng-submit="createNewDocument()"> + <div class="form-group"> - <label class="col-md-4 control-label">Nom du document</label> + <label class="col-md-4 control-label">File</label> + <div class="col-md-5"> - <input type="text" class="form-control" name="name" ng-model="name" required/> + <input type="file" class="form-control" name="file" ng-file-model="documentFile" required/> </div> </div> @@ -25,7 +27,7 @@ <label class="col-md-4 control-label">keyword</label> <div class="col-md-5"> - <input type="text" class="form-control" name="keyword" ng-model="keywords" ng-list required=""/> + <input type="text" class="form-control" name="keyword" ng-model="keywords" ng-list required/> </div> </div> -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.