From: Richard Millet Date: Fri, 11 Dec 2009 19:37:01 +0000 (+0000) Subject: CSPACE-665: Support for keyword search on collectionobjects. X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=ae2b7fa8019cebba62ed99c6df24454f5f013fb5;p=tmp%2Fjakarta-migration.git CSPACE-665: Support for keyword search on collectionobjects. --- diff --git a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java index b1fd2c796..21b412ae6 100644 --- a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java +++ b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java @@ -31,13 +31,18 @@ import javax.ws.rs.DELETE; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.PathParam; +import javax.ws.rs.QueryParam; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; +import java.util.Map; +import java.util.HashMap; +import java.util.StringTokenizer; +import org.collectionspace.services.common.query.QueryManager; import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectHandlerFactory; import org.collectionspace.services.common.AbstractCollectionSpaceResource; import org.collectionspace.services.common.context.MultipartServiceContext; @@ -45,6 +50,7 @@ import org.collectionspace.services.common.context.MultipartServiceContextFactor import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.DocumentNotFoundException; import org.collectionspace.services.common.document.DocumentHandler; +import org.collectionspace.services.common.document.DocumentFilter; import org.collectionspace.services.common.security.UnauthorizedException; import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; @@ -60,6 +66,11 @@ public class CollectionObjectResource final private String serviceName = "collectionobjects"; final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class); + final private static String SEARCH_TYPE_KEYWORDS = "keywords"; + final private static String ECM_FULLTEXT_LIKE = "ecm:fulltext LIKE "; + final private static String SEARCH_QUALIFIER_AND = "AND"; + final private static String SEARCH_QUALIFIER_OR = "OR"; + final private static String SEARCH_TERM_SEPARATOR = " "; @Override public String getServiceName() { @@ -256,4 +267,43 @@ public class CollectionObjectResource } } + + @GET + @Path("/search") + @Produces("application/xml") + public CollectionobjectsCommonList keywordsSearchCollectionObjects(@Context UriInfo ui, + @QueryParam (SEARCH_TYPE_KEYWORDS) String keywords) { + CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList(); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); + DocumentHandler handler = createDocumentHandler(ctx); + + // perform a keyword search + if (keywords != null && !keywords.isEmpty()) { + String whereClause = QueryManager.createWhereClauseFromKeywords(keywords); + DocumentFilter documentFilter = handler.getDocumentFilter(); + documentFilter.setWhereClause(whereClause); + if (logger.isDebugEnabled()) { + logger.debug("The WHERE clause is: " + documentFilter.getWhereClause()); + } + getRepositoryClient(ctx).getFiltered(ctx, handler); + } else { + getRepositoryClient(ctx).getAll(ctx, handler); + } + collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList(); + + } catch (UnauthorizedException ue) { + Response response = Response.status( + Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build(); + throw new WebApplicationException(response); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception in getCollectionObjectList", e); + } + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + return collectionObjectList; + } } diff --git a/services/common/src/main/java/org/collectionspace/services/common/query/IQueryManager.java b/services/common/src/main/java/org/collectionspace/services/common/query/IQueryManager.java index 98c4887b7..d83d7a4a0 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/query/IQueryManager.java +++ b/services/common/src/main/java/org/collectionspace/services/common/query/IQueryManager.java @@ -1,7 +1,47 @@ +/** + * IQueryManager.java + * + * {Purpose of This Class} + * + * {Other Notes Relating to This Class (Optional)} + * + * $LastChangedBy: $ + * $LastChangedRevision: $ + * $LastChangedDate: $ + * + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + * + * http://www.collectionspace.org + * http://wiki.collectionspace.org + * + * Copyright © 2009 {Contributing Institution} + * + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * https://source.collectionspace.org/collection-space/LICENSE.txt + */ package org.collectionspace.services.common.query; public interface IQueryManager { + final static String ECM_FULLTEXT_LIKE = "ecm:fulltext LIKE "; + final static String SEARCH_QUALIFIER_AND = "AND"; + final static String SEARCH_QUALIFIER_OR = "OR"; + final static String SEARCH_TERM_SEPARATOR = " "; + public void execQuery(String queryString); + + /** + * Creates the where clause from keywords. + * + * @param keywords the keywords + * + * @return the string + */ + public String createWhereClauseFromKeywords(String keywords); } diff --git a/services/common/src/main/java/org/collectionspace/services/common/query/QueryManager.java b/services/common/src/main/java/org/collectionspace/services/common/query/QueryManager.java index 4cc2bf88f..ec22822bc 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/query/QueryManager.java +++ b/services/common/src/main/java/org/collectionspace/services/common/query/QueryManager.java @@ -1,3 +1,29 @@ +/** + * QueryManager.java + * + * {Purpose of This Class} + * + * {Other Notes Relating to This Class (Optional)} + * + * $LastChangedBy: $ + * $LastChangedRevision: $ + * $LastChangedDate: $ + * + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + * + * http://www.collectionspace.org + * http://wiki.collectionspace.org + * + * Copyright © 2009 {Contributing Institution} + * + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * https://source.collectionspace.org/collection-space/LICENSE.txt + */ package org.collectionspace.services.common.query; import org.collectionspace.services.common.query.nuxeo.QueryManagerNuxeoImpl; @@ -8,4 +34,15 @@ public class QueryManager { static public void execQuery(String queryString) { queryManager.execQuery(queryString); } + + /** + * Creates the where clause from keywords. + * + * @param keywords the keywords + * + * @return the string + */ + static public String createWhereClauseFromKeywords(String keywords) { + return queryManager.createWhereClauseFromKeywords(keywords); + } } diff --git a/services/common/src/main/java/org/collectionspace/services/common/query/nuxeo/QueryManagerNuxeoImpl.java b/services/common/src/main/java/org/collectionspace/services/common/query/nuxeo/QueryManagerNuxeoImpl.java index 8240ef4b8..736bce98d 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/query/nuxeo/QueryManagerNuxeoImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/query/nuxeo/QueryManagerNuxeoImpl.java @@ -1,8 +1,40 @@ +/** + * QueryManagerNuxeoImpl.java + * + * {Purpose of This Class} + * + * {Other Notes Relating to This Class (Optional)} + * + * $LastChangedBy: $ + * $LastChangedRevision: $ + * $LastChangedDate: $ + * + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + * + * http://www.collectionspace.org + * http://wiki.collectionspace.org + * + * Copyright © 2009 {Contributing Institution} + * + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * https://source.collectionspace.org/collection-space/LICENSE.txt + */ package org.collectionspace.services.common.query.nuxeo; +import java.util.StringTokenizer; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Map; +import java.util.HashMap; +import java.util.StringTokenizer; + import org.nuxeo.ecm.core.api.DocumentModel; import org.nuxeo.ecm.core.api.DocumentModelList; import org.nuxeo.ecm.core.api.repository.RepositoryInstance; @@ -10,6 +42,7 @@ import org.nuxeo.ecm.core.client.NuxeoClient; import org.collectionspace.services.nuxeo.client.java.NuxeoConnector; import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClient; +import org.collectionspace.services.common.document.DocumentFilter; import org.collectionspace.services.common.query.IQueryManager; public class QueryManagerNuxeoImpl implements IQueryManager { @@ -17,6 +50,11 @@ public class QueryManagerNuxeoImpl implements IQueryManager { private final Logger logger = LoggerFactory .getLogger(RepositoryJavaClient.class); + //TODO: This is currently just an example fixed query. This should eventually be + // removed or replaced with a more generic method. + /* (non-Javadoc) + * @see org.collectionspace.services.common.query.IQueryManager#execQuery(java.lang.String) + */ public void execQuery(String queryString) { NuxeoClient client = null; try { @@ -40,4 +78,31 @@ public class QueryManagerNuxeoImpl implements IQueryManager { } } + /* (non-Javadoc) + * @see org.collectionspace.services.common.query.IQueryManager#createWhereClauseFromKeywords(java.lang.String) + */ + public String createWhereClauseFromKeywords(String keywords) { + String result = null; + StringBuffer whereClause = new StringBuffer(); + StringTokenizer stringTokenizer = new StringTokenizer(keywords); + while (stringTokenizer.hasMoreElements() == true) { + whereClause.append(ECM_FULLTEXT_LIKE + "'" + + stringTokenizer.nextToken() + "'"); + if (stringTokenizer.hasMoreElements() == true) { + whereClause.append(SEARCH_TERM_SEPARATOR + + SEARCH_QUALIFIER_OR + + SEARCH_TERM_SEPARATOR); + } + if (logger.isDebugEnabled() == true) { + logger.debug("Current built whereClause is: " + whereClause.toString()); + } + } + + result = whereClause.toString(); + if (logger.isDebugEnabled()) { + logger.debug("Final built WHERE clause is: " + result); + } + + return result; + } } diff --git a/services/query/service/src/main/java/org/collectionspace/services/query/QueryResource.java b/services/query/service/src/main/java/org/collectionspace/services/query/QueryResource.java index 952c785af..6b7ad0733 100644 --- a/services/query/service/src/main/java/org/collectionspace/services/query/QueryResource.java +++ b/services/query/service/src/main/java/org/collectionspace/services/query/QueryResource.java @@ -42,8 +42,8 @@ import javax.xml.bind.Marshaller; import org.collectionspace.services.common.query.QueryManager; //import org.collectionspace.services.common.NuxeoClientType; import org.collectionspace.services.common.ServiceMain; -import org.collectionspace.services.common.repository.DocumentNotFoundException; -import org.collectionspace.services.common.repository.DocumentHandler; +import org.collectionspace.services.common.document.DocumentNotFoundException; +import org.collectionspace.services.common.document.DocumentHandler; import org.collectionspace.services.common.repository.RepositoryClient; import org.collectionspace.services.common.repository.RepositoryClientFactory; import org.jboss.resteasy.util.HttpResponseCodes;