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 1e73bf2ffa9e2408e587234e1d750b4749808e74 Merge: bec58be 5987947 Author: Yannick Martel <martel@©odelutin.com> Date: Fri Jan 23 16:56:46 2015 +0100 move config to persistence package, add an unit test about cascade deletion bug with document and questions, use snapshot topia solution coselmar-persistence/pom.xml | 23 ++++ .../coselmar}/config/CoselmarServicesConfig.java | 4 +- .../config/CoselmarServicesConfigOption.java | 2 +- .../exceptions}/CoselmarTechnicalException.java | 2 +- .../i18n/coselmar-persistence_en_GB.properties | 2 + .../i18n/coselmar-persistence_fr_FR.properties | 2 + .../java/fr/ifremer/AbstractCoselmarDaoTest.java | 23 +--- .../src/test/java/fr/ifremer/DocumentDaoTest.java | 75 +++++++++++ .../ifremer}/FakeCoselmarApplicationContext.java | 141 +++------------------ .../resources/coselmar-persistence-test.properties | 31 +++++ coselmar-rest/pom.xml | 4 - .../services/CoselmarApplicationContext.java | 2 +- .../CoselmarServicesApplicationContext.java | 2 +- .../coselmar/services/CoselmarServicesContext.java | 2 +- .../services/CoselmarSimpleServiceSupport.java | 2 +- .../services/CoselmarWebServiceSupport.java | 3 +- .../services/DefaultCoselmarServicesContext.java | 3 +- .../coselmar/services/indexation/LuceneUtils.java | 2 +- .../coselmar/services/v1/AdminWebService.java | 2 +- .../coselmar/services/v1/DocumentsWebService.java | 21 +-- .../coselmar/services/v1/UsersWebService.java | 4 +- coselmar-rest/src/main/resources/mapping | 2 +- .../services/FakeCoselmarApplicationContext.java | 4 +- .../services/FakeCoselmarServicesContext.java | 2 +- pom.xml | 5 +- 25 files changed, 181 insertions(+), 184 deletions(-) diff --cc coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java index 01e6041,5c7ae0e..68c9eab --- 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 @@@ -44,8 -43,7 +44,8 @@@ import fr.ifremer.coselmar.persistence. import fr.ifremer.coselmar.persistence.entity.CoselmarUserRole; import fr.ifremer.coselmar.persistence.entity.Document; import fr.ifremer.coselmar.persistence.entity.Privacy; +import fr.ifremer.coselmar.persistence.entity.Question; - import fr.ifremer.coselmar.services.CoselmarTechnicalException; + import fr.ifremer.coselmar.exceptions.CoselmarTechnicalException; import fr.ifremer.coselmar.services.CoselmarWebServiceSupport; import fr.ifremer.coselmar.services.errors.InvalidCredentialException; import fr.ifremer.coselmar.services.errors.NoResultException; @@@ -154,14 -144,14 +154,13 @@@ public class DocumentsWebService extend } else { //Other can only see public, his own private and restricted for which he is allowed -- documentList = getDocumentDao().findAllFilterByUser(currentUser, searchKeywords);; ++ documentList = getDocumentDao().findAllFilterByUser(currentUser, searchKeywords); } List<DocumentBean> result = new ArrayList<>(documentList.size()); for (Document document : documentList) { -- String lightId = getPersistenceContext().getTopiaIdFactory().getRandomPart(document.getTopiaId()); - DocumentBean documentBean = BeanEntityConverter.toBean(lightId, document); + DocumentBean documentBean = BeanEntityConverter.toBean(getPersistenceContext().getTopiaIdFactory(), document); result.add(documentBean); } @@@ -197,7 -187,7 +196,7 @@@ // 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.equals(message); ++ log.error(message); } throw new InvalidCredentialException(message); } @@@ -253,8 -243,8 +252,7 @@@ documentEntity.setComment(document.getComment()); commit(); -- String lightId = getPersistenceContext().getTopiaIdFactory().getRandomPart(documentEntity.getTopiaId()); - DocumentBean result = BeanEntityConverter.toBean(lightId, documentEntity); + DocumentBean result = BeanEntityConverter.toBean(getPersistenceContext().getTopiaIdFactory(), documentEntity); DocumentsIndexationService documentsIndexationService = getServicesContext().newService(DocumentsIndexationService.class); try { @@@ -273,58 -263,6 +271,58 @@@ } + 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); ++ log.error(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 @@@ -356,78 -294,8 +354,77 @@@ } - 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(getPersistenceContext().getTopiaIdFactory(), 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 { @@@ -592,14 -460,14 +589,4 @@@ return fullIds; } -- protected List<Document> findAllDocuments(List<String> searchKeywords) { -- List<Document> documentList; -- if (searchKeywords != null && !searchKeywords.isEmpty()) { -- documentList = getDocumentDao().findAllContainingAllKeywords(searchKeywords); -- } else { -- documentList = getDocumentDao().findAll(); -- } -- return documentList; -- } -- } -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.