]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-665: Support for keyword search on collectionobjects.
authorRichard Millet <richard.millet@berkeley.edu>
Fri, 11 Dec 2009 19:37:01 +0000 (19:37 +0000)
committerRichard Millet <richard.millet@berkeley.edu>
Fri, 11 Dec 2009 19:37:01 +0000 (19:37 +0000)
services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java
services/common/src/main/java/org/collectionspace/services/common/query/IQueryManager.java
services/common/src/main/java/org/collectionspace/services/common/query/QueryManager.java
services/common/src/main/java/org/collectionspace/services/common/query/nuxeo/QueryManagerNuxeoImpl.java
services/query/service/src/main/java/org/collectionspace/services/query/QueryResource.java

index b1fd2c79616649c86b4d201319b127f16d54f802..21b412ae67650c2b982cd93b115c094dc9911011 100644 (file)
@@ -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;
+    }    
 }
index 98c4887b7123e0779558523c1d8238eaed79a3cc..d83d7a4a047f7d11baf1407a34d587fe6b97e988 100644 (file)
@@ -1,7 +1,47 @@
+/**    \r
+ * IQueryManager.java\r
+ *\r
+ * {Purpose of This Class}\r
+ *\r
+ * {Other Notes Relating to This Class (Optional)}\r
+ *\r
+ * $LastChangedBy: $\r
+ * $LastChangedRevision: $\r
+ * $LastChangedDate: $\r
+ *\r
+ * This document is a part of the source code and related artifacts\r
+ * for CollectionSpace, an open source collections management system\r
+ * for museums and related institutions:\r
+ *\r
+ * http://www.collectionspace.org\r
+ * http://wiki.collectionspace.org\r
+ *\r
+ * Copyright © 2009 {Contributing Institution}\r
+ *\r
+ * Licensed under the Educational Community License (ECL), Version 2.0.\r
+ * You may not use this file except in compliance with this License.\r
+ *\r
+ * You may obtain a copy of the ECL 2.0 License at\r
+ * https://source.collectionspace.org/collection-space/LICENSE.txt\r
+ */\r
 package org.collectionspace.services.common.query;\r
 \r
 public interface IQueryManager {\r
        \r
+       final static String ECM_FULLTEXT_LIKE = "ecm:fulltext LIKE ";\r
+       final static String SEARCH_QUALIFIER_AND = "AND";\r
+       final static String SEARCH_QUALIFIER_OR = "OR";\r
+       final static String SEARCH_TERM_SEPARATOR = " ";\r
+\r
        public void execQuery(String queryString);\r
+       \r
+       /**\r
+        * Creates the where clause from keywords.\r
+        * \r
+        * @param keywords the keywords\r
+        * \r
+        * @return the string\r
+        */\r
+       public String createWhereClauseFromKeywords(String keywords);\r
 \r
 }\r
index 4cc2bf88f553e167976764fa37f2534f13dfec88..ec22822bcbb13bc508d772862de08a1490f292fa 100644 (file)
@@ -1,3 +1,29 @@
+/**    \r
+ * QueryManager.java\r
+ *\r
+ * {Purpose of This Class}\r
+ *\r
+ * {Other Notes Relating to This Class (Optional)}\r
+ *\r
+ * $LastChangedBy: $\r
+ * $LastChangedRevision: $\r
+ * $LastChangedDate: $\r
+ *\r
+ * This document is a part of the source code and related artifacts\r
+ * for CollectionSpace, an open source collections management system\r
+ * for museums and related institutions:\r
+ *\r
+ * http://www.collectionspace.org\r
+ * http://wiki.collectionspace.org\r
+ *\r
+ * Copyright © 2009 {Contributing Institution}\r
+ *\r
+ * Licensed under the Educational Community License (ECL), Version 2.0.\r
+ * You may not use this file except in compliance with this License.\r
+ *\r
+ * You may obtain a copy of the ECL 2.0 License at\r
+ * https://source.collectionspace.org/collection-space/LICENSE.txt\r
+ */\r
 package org.collectionspace.services.common.query;\r
 \r
 import org.collectionspace.services.common.query.nuxeo.QueryManagerNuxeoImpl;\r
@@ -8,4 +34,15 @@ public class QueryManager {
        static public void execQuery(String queryString) {\r
                queryManager.execQuery(queryString);\r
        }\r
+       \r
+       /**\r
+        * Creates the where clause from keywords.\r
+        * \r
+        * @param keywords the keywords\r
+        * \r
+        * @return the string\r
+        */\r
+       static public String createWhereClauseFromKeywords(String keywords) {\r
+               return queryManager.createWhereClauseFromKeywords(keywords);\r
+       }\r
 }\r
index 8240ef4b848b37085feb0a79cff54c29bdca55ee..736bce98db206e39b30a9e655b505bbeff73592a 100644 (file)
@@ -1,8 +1,40 @@
+/**    \r
+ * QueryManagerNuxeoImpl.java\r
+ *\r
+ * {Purpose of This Class}\r
+ *\r
+ * {Other Notes Relating to This Class (Optional)}\r
+ *\r
+ * $LastChangedBy: $\r
+ * $LastChangedRevision: $\r
+ * $LastChangedDate: $\r
+ *\r
+ * This document is a part of the source code and related artifacts\r
+ * for CollectionSpace, an open source collections management system\r
+ * for museums and related institutions:\r
+ *\r
+ * http://www.collectionspace.org\r
+ * http://wiki.collectionspace.org\r
+ *\r
+ * Copyright © 2009 {Contributing Institution}\r
+ *\r
+ * Licensed under the Educational Community License (ECL), Version 2.0.\r
+ * You may not use this file except in compliance with this License.\r
+ *\r
+ * You may obtain a copy of the ECL 2.0 License at\r
+ * https://source.collectionspace.org/collection-space/LICENSE.txt\r
+ */\r
 package org.collectionspace.services.common.query.nuxeo;\r
 \r
+import java.util.StringTokenizer;\r
+\r
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
+import java.util.Map;\r
+import java.util.HashMap;\r
+import java.util.StringTokenizer;\r
+\r
 import org.nuxeo.ecm.core.api.DocumentModel;\r
 import org.nuxeo.ecm.core.api.DocumentModelList;\r
 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;\r
@@ -10,6 +42,7 @@ import org.nuxeo.ecm.core.client.NuxeoClient;
 \r
 import org.collectionspace.services.nuxeo.client.java.NuxeoConnector;\r
 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClient;\r
+import org.collectionspace.services.common.document.DocumentFilter;\r
 import org.collectionspace.services.common.query.IQueryManager;\r
 \r
 public class QueryManagerNuxeoImpl implements IQueryManager {\r
@@ -17,6 +50,11 @@ public class QueryManagerNuxeoImpl implements IQueryManager {
        private final Logger logger = LoggerFactory\r
                        .getLogger(RepositoryJavaClient.class);\r
        \r
+       //TODO: This is currently just an example fixed query.  This should eventually be\r
+       // removed or replaced with a more generic method.\r
+       /* (non-Javadoc)\r
+        * @see org.collectionspace.services.common.query.IQueryManager#execQuery(java.lang.String)\r
+        */\r
        public void execQuery(String queryString) {\r
                NuxeoClient client = null;\r
                try {\r
@@ -40,4 +78,31 @@ public class QueryManagerNuxeoImpl implements IQueryManager {
                }               \r
        }\r
 \r
+       /* (non-Javadoc)\r
+        * @see org.collectionspace.services.common.query.IQueryManager#createWhereClauseFromKeywords(java.lang.String)\r
+        */\r
+       public String createWhereClauseFromKeywords(String keywords) {\r
+               String result = null;\r
+               StringBuffer whereClause = new StringBuffer();\r
+               StringTokenizer stringTokenizer = new StringTokenizer(keywords);\r
+               while (stringTokenizer.hasMoreElements() == true) {\r
+                       whereClause.append(ECM_FULLTEXT_LIKE + "'" +\r
+                                       stringTokenizer.nextToken() + "'");\r
+                       if (stringTokenizer.hasMoreElements() == true) {\r
+                               whereClause.append(SEARCH_TERM_SEPARATOR +\r
+                                               SEARCH_QUALIFIER_OR +\r
+                                               SEARCH_TERM_SEPARATOR);\r
+                       }\r
+                       if (logger.isDebugEnabled() == true) {\r
+                               logger.debug("Current built whereClause is: " + whereClause.toString());\r
+                       }\r
+               }               \r
+               \r
+               result = whereClause.toString();\r
+           if (logger.isDebugEnabled()) {\r
+               logger.debug("Final built WHERE clause is: " + result);\r
+           }\r
+           \r
+           return result;\r
+       }\r
 }\r
index 952c785af91dd2a22a8e71e6ff2ca29bceedb922..6b7ad0733d65ff37ebe9ee5b4a9ee3f2ab569dd3 100644 (file)
@@ -42,8 +42,8 @@ import javax.xml.bind.Marshaller;
 import org.collectionspace.services.common.query.QueryManager;\r
 //import org.collectionspace.services.common.NuxeoClientType;\r
 import org.collectionspace.services.common.ServiceMain;\r
-import org.collectionspace.services.common.repository.DocumentNotFoundException;\r
-import org.collectionspace.services.common.repository.DocumentHandler;\r
+import org.collectionspace.services.common.document.DocumentNotFoundException;\r
+import org.collectionspace.services.common.document.DocumentHandler;\r
 import org.collectionspace.services.common.repository.RepositoryClient;\r
 import org.collectionspace.services.common.repository.RepositoryClientFactory;\r
 import org.jboss.resteasy.util.HttpResponseCodes;\r