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 1c77d64152f9b87c42155ddd19830b726bb56d38 Author: Yannick Martel <martel@©odelutin.com> Date: Wed Nov 26 11:23:09 2014 +0100 #6140 manager document metadata in webservice --- .../coselmar/converter/BeanEntityConverter.java | 10 +- .../coselmar/services/v1/DocumentsWebService.java | 144 +++++++++++++++++---- .../coselmar/services/v1/UsersWebService.java | 7 +- 3 files changed, 124 insertions(+), 37 deletions(-) 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 26ea92a..61be990 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 @@ -37,8 +37,7 @@ import org.apache.commons.lang3.StringUtils; */ public class BeanEntityConverter { - public static DocumentBean toBean(Document document) { - String documentLightId = document.getTopiaId().replace(Document.class.getCanonicalName() + "_", ""); + public static DocumentBean toBean(String lightId, Document document) { Date depositeDate = document.getDepositDate(); Date publicationDate = document.getPublicationDate(); CoselmarUser documentOwner = document.getOwner(); @@ -50,7 +49,7 @@ public class BeanEntityConverter { } - return new DocumentBean(documentLightId, + return new DocumentBean(lightId, document.getName(), owner, document.getPrivacy().name(), @@ -72,9 +71,8 @@ public class BeanEntityConverter { ); } - public static UserBean toBean(CoselmarUser user) { - String userLightId = user.getTopiaId().replace(CoselmarUser.class.getCanonicalName() + "_", ""); - return new UserBean(userLightId, + public static UserBean toBean(String lightId ,CoselmarUser user) { + return new UserBean(lightId, user.getFirstname(), user.getName(), user.getMail(), 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 55eabec..d4dbb70 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 @@ -34,16 +34,22 @@ import java.util.Date; import java.util.List; import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; import fr.ifremer.coselmar.beans.DocumentBean; +import fr.ifremer.coselmar.beans.UserWebToken; import fr.ifremer.coselmar.converter.BeanEntityConverter; +import fr.ifremer.coselmar.persistence.entity.CoselmarUser; 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 fr.ifremer.coselmar.services.errors.InvalidCredentialException; +import fr.ifremer.coselmar.services.errors.UnauthorizedException; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.debux.webmotion.server.call.UploadFile; import org.debux.webmotion.server.render.Render; +import org.nuiton.topia.persistence.TopiaNoResultException; import org.nuiton.util.DateUtil; import static org.apache.commons.logging.LogFactory.getLog; @@ -58,11 +64,10 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { public DocumentBean getDocument(String documentId) { // reconstitute full id - String fullId = Document.class.getCanonicalName() + "_" + documentId; + String fullId = Document.class.getCanonicalName() + getPersistenceContext().getTopiaIdFactory().getSeparator() + documentId; Document document = getDocumentDao().forTopiaIdEquals(fullId).findUnique(); - //TODO ymartel 20141103 : manage file ? - DocumentBean documentBean = BeanEntityConverter.toBean(document); + DocumentBean documentBean = BeanEntityConverter.toBean(documentId, document); return documentBean; } @@ -78,51 +83,66 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { for (Document document : documentList) { //TODO ymartel 20141103 : manage file ? - DocumentBean documentBean = BeanEntityConverter.toBean(document); + String lightId = getPersistenceContext().getTopiaIdFactory().getRandomPart(document.getTopiaId()); + DocumentBean documentBean = BeanEntityConverter.toBean(lightId, document); result.add(documentBean); } return result; } - public void addDocument(DocumentBean document, UploadFile uploadFile) { - Preconditions.checkNotNull(document); - Preconditions.checkNotNull(uploadFile); + public void addDocument(DocumentBean document, UploadFile uploadFile) throws InvalidCredentialException, UnauthorizedException { - // Document File - String fileName = uploadFile.getName(); - File uploadedFile = uploadFile.getFile(); - String contentType = uploadFile.getContentType(); + // Check authentication + String authorization = getContext().getHeader("Authorization"); + UserWebToken userWebToken = checkAuthentication(authorization); - if (log.isInfoEnabled()) { - String message = String.format("File name : %s, content-type : %s", fileName, contentType); - log.info(message); + // Only Expert or Supervisor can add document + String userRole = userWebToken.getRole(); + if (!Lists.newArrayList().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); } - // 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); + Preconditions.checkNotNull(document); + + // retrieve user who will be assigned as document owner + String fullId = CoselmarUser.class.getCanonicalName() + + getPersistenceContext().getTopiaIdFactory().getSeparator() + userWebToken.getUserId(); + + CoselmarUser owner; try { - FileUtils.moveFile(uploadedFile, destFile); - } catch (IOException e) { + owner = getCoselmarUserDao().forTopiaIdEquals(fullId).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.", fullId); if (log.isErrorEnabled()) { - log.error("error during File transfer", e); + log.equals(message); } - throw new CoselmarTechnicalException("Internal error during file transfer"); + throw new CoselmarTechnicalException(message); + } + + String documentName = document.getName(); + String contentType = null; + + // If document has a file, manager it ! + if (document.isFile()) { + contentType = managerDocumentFile(uploadFile, owner); + } // Document Metadata Document documentEntity = getDocumentDao().create(); - documentEntity.setName(fileName); + documentEntity.setOwner(owner); + + documentEntity.setName(documentName); documentEntity.setPrivacy(DocumentPrivacy.valueOf(document.getPrivacy().toUpperCase())); documentEntity.addAllKeywords(document.getKeywords()); - documentEntity.setMimeType(contentType); Date depositDate = document.getDepositDate(); if (depositDate != null) { @@ -131,6 +151,26 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { documentEntity.setDepositDate(new Date()); } + 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()); + + // Document resource part + if (document.isFile()) { + documentEntity.setIsFile(true); + documentEntity.setMimeType(contentType); + } else { + documentEntity.setIsFile(false); + documentEntity.setExternalUrl(document.getExternalUrl()); + } + commit(); } @@ -184,4 +224,52 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { commit(); } + + //////////////////////////////////////////////////////////////////////////// + /////////////////////// Internal Parts ///////////////////////////// + //////////////////////////////////////////////////////////////////////////// + + /** + * When a Document is sent, it could have a File part : this manage the + * upload file. The file is stored in the user specific directory, and the + * contentType of document is returned, cause need in Document Metadata. + * + * @return the upload file Metadata + */ + protected String managerDocumentFile(UploadFile uploadFile, CoselmarUser owner) { + 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(); + String userPath = absolutePath + File.separator + owner.getFirstname() + "-" + owner.getName(); + + Date now = getNow(); + String formattedDay = DateUtil.formatDate(now, "yyyymmdd"); + String prefix = formattedDay + "-"; + + File destFile = new File(userPath + File.separator + 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"); + } + return contentType; + } + + } diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/UsersWebService.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/UsersWebService.java index ecd02d2..ebb6f42 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/UsersWebService.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/UsersWebService.java @@ -60,10 +60,10 @@ public class UsersWebService extends CoselmarWebServiceSupport { } // reconstitute full id - String fullId = CoselmarUser.class.getCanonicalName() + "_" + userId; + String fullId = CoselmarUser.class.getCanonicalName() + getPersistenceContext().getTopiaIdFactory().getSeparator() + userId; CoselmarUser user = getCoselmarUserDao().forTopiaIdEquals(fullId).findUnique(); - UserBean userBean = BeanEntityConverter.toBean(user); + UserBean userBean = BeanEntityConverter.toBean(userId, user); return userBean; } @@ -79,7 +79,8 @@ public class UsersWebService extends CoselmarWebServiceSupport { List<UserBean> result = new ArrayList<>(userList.size()); for (CoselmarUser user : userList) { - UserBean userBean = BeanEntityConverter.toBean(user); + String userLightId = getPersistenceContext().getTopiaIdFactory().getRandomPart(user.getTopiaId()); + UserBean userBean = BeanEntityConverter.toBean(userLightId, user); result.add(userBean); } -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.