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 d1d48e237301cfe0455a9fcd0f50233d3ddba571 Author: Yannick Martel <martel@©odelutin.com> Date: Mon Dec 29 17:02:59 2014 +0100 use indexation in document service for creation and search --- .../indexation/DocumentsIndexationService.java | 1 + .../coselmar/services/v1/DocumentsWebService.java | 51 +++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/DocumentsIndexationService.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/DocumentsIndexationService.java index 3d5c573..7c166c7 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/DocumentsIndexationService.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/DocumentsIndexationService.java @@ -60,6 +60,7 @@ public class DocumentsIndexationService extends CoselmarSimpleServiceSupport { Document doc = new Document(); doc.add(new StringField(DOCUMENT_ID_INDEX_PROPERTY, document.getId(), Field.Store.YES)); + doc.add(new TextField(DOCUMENT_NAME_INDEX_PROPERTY, document.getName(), Field.Store.YES)); doc.add(new TextField(DOCUMENT_AUTHORS_INDEX_PROPERTY, document.getAuthors(), Field.Store.YES)); doc.add(new TextField(DOCUMENT_SUMMARY_INDEX_PROPERTY, document.getSummary(), Field.Store.YES)); 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 7bd42f7..0bad993 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 @@ -33,6 +33,8 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; +import com.google.common.base.Function; +import com.google.common.base.Joiner; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import fr.ifremer.coselmar.beans.DocumentBean; @@ -46,9 +48,11 @@ 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 fr.ifremer.coselmar.services.indexation.DocumentsIndexationService; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; +import org.apache.lucene.queryparser.classic.ParseException; import org.debux.webmotion.server.call.UploadFile; import org.debux.webmotion.server.render.Render; import org.nuiton.topia.persistence.TopiaNoResultException; @@ -117,7 +121,25 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { // Admin and Supervisor can see all documents (public, private and restricted) if (Lists.newArrayList(CoselmarUserRole.ADMIN.name(), CoselmarUserRole.SUPERVISOR.name()).contains(currentUserRole)) { - documentList = findAllDocuments(searchKeywords); + if (searchKeywords != null && !searchKeywords.isEmpty()) { + DocumentsIndexationService documentsIndexationService = getServicesContext().newService(DocumentsIndexationService.class); + + try { + List<String> documentIds = documentsIndexationService.searchDocuments(Joiner.on(" ").join(searchKeywords)); + List<String> documentFullIds = getDocumentsFullId(documentIds); + + documentList = getDocumentDao().forTopiaIdIn(documentFullIds).findAll(); + + } catch (IOException | ParseException e) { + if (log.isErrorEnabled()) { + log.error("Unable to search by lucene, make search directly in database", e); + } + documentList = getDocumentDao().findAllContainingAllKeywords(searchKeywords); + + } + } else { + documentList = getDocumentDao().findAll(); + } } else { //Other can only see public, his own private and restricted for which he is allowed @@ -220,6 +242,20 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { commit(); String lightId = getPersistenceContext().getTopiaIdFactory().getRandomPart(documentEntity.getTopiaId()); 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' added to index", documentName); + log.debug(message); + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Unable to index new document", e); + } + } + return result; } @@ -409,6 +445,19 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { return Document.class.getCanonicalName() + getPersistenceContext().getTopiaIdFactory().getSeparator() + documentId; } + protected List<String> getDocumentsFullId(List<String> documentShortIds) { + + Function<String, String> getFullIds = new Function<String, String>() { + @Override + public String apply(String shortId) { + return getDocumentFullId(shortId); + } + }; + + List<String> fullIds = Lists.transform(documentShortIds, getFullIds); + return fullIds; + } + protected List<Document> findAllDocuments(List<String> searchKeywords) { List<Document> documentList; if (searchKeywords != null && !searchKeywords.isEmpty()) { -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.