Bert Leunis pushed to branch feature/HSTTWO-3922 at cms-community / 
hippo-site-toolkit


Commits:
cde548d8 by Bert Leunis at 2017-02-17T14:18:28+01:00
HSTTWO-3922 add _orderBy and _sortOrder query parameters

- - - - -


1 changed file:

- 
components/restapi/src/main/java/org/hippoecm/hst/restapi/content/DocumentsResource.java


Changes:

=====================================
components/restapi/src/main/java/org/hippoecm/hst/restapi/content/DocumentsResource.java
=====================================
--- 
a/components/restapi/src/main/java/org/hippoecm/hst/restapi/content/DocumentsResource.java
+++ 
b/components/restapi/src/main/java/org/hippoecm/hst/restapi/content/DocumentsResource.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015-2016 Hippo B.V. (http://www.onehippo.com)
+ * Copyright 2015-2017 Hippo B.V. (http://www.onehippo.com)
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@ import javax.jcr.PathNotFoundException;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.query.InvalidQueryException;
+import javax.ws.rs.DefaultValue;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
@@ -33,6 +34,7 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.Response;
 
+import org.apache.commons.lang.StringUtils;
 import org.hippoecm.hst.container.RequestContextProvider;
 import org.hippoecm.hst.restapi.AbstractResource;
 import org.hippoecm.hst.restapi.NodeVisitor;
@@ -42,6 +44,7 @@ import org.hippoecm.hst.util.SearchInputParsingUtils;
 import org.onehippo.cms7.services.contenttype.ContentType;
 import org.onehippo.cms7.services.search.query.Query;
 import org.onehippo.cms7.services.search.query.QueryUtils;
+import org.onehippo.cms7.services.search.query.constraint.ExistsConstraint;
 import org.onehippo.cms7.services.search.result.QueryResult;
 import org.onehippo.cms7.services.search.service.SearchService;
 import org.onehippo.cms7.services.search.service.SearchServiceException;
@@ -61,12 +64,13 @@ public class DocumentsResource extends AbstractResource {
     private static final int DEFAULT_MAX_SEARCH_RESULT_ITEMS = 100;
     private int maxSearchResultItems = DEFAULT_MAX_SEARCH_RESULT_ITEMS;
 
+    public enum SortOrder { ASCENDING, ASC, DESCENDING, DESC }
+
     @Override
     public Logger getLogger() {
         return log;
     }
 
-
     public void setMaxSearchResultItems(final Integer maxSearchResultItems) {
         if (maxSearchResultItems != null) {
             this.maxSearchResultItems = maxSearchResultItems;
@@ -133,18 +137,29 @@ public class DocumentsResource extends AbstractResource {
         throw new IllegalArgumentException(String.format("_nodetype must be of 
(sub)type: '%s'", NT_DOCUMENT));
     }
 
+    private SortOrder parseSortOrder(final String sortOrder) {
+        try {
+            return SortOrder.valueOf(sortOrder.toUpperCase());
+        } catch (IllegalArgumentException e) {
+            throw new IllegalArgumentException("_sortorder value must be one 
of: " + StringUtils.join(SortOrder.values(), ", ").toLowerCase());
+        }
+    }
+
     @GET
     @Path("/documents")
     public Response getDocuments(@QueryParam("_offset") final String 
offsetString,
                                  @QueryParam("_max") final String maxString,
                                  @QueryParam("_query") final String 
queryString,
-                                 @QueryParam("_nodetype") final String 
nodeTypeString) {
+                                 @QueryParam("_nodetype") final String 
nodeTypeString,
+                                 @QueryParam("_orderBy") 
@DefaultValue(HIPPOSTDPUBWF_PUBLICATION_DATE) final String orderBy,
+                                 @QueryParam("_sortOrder") 
@DefaultValue("ascending") final String sortOrder) {
         try {
             ResourceContext context = 
getResourceContextFactory().createResourceContext();
             final int offset = parseOffset(offsetString);
             final int max = parseMax(maxString);
             final String parsedQuery = parseQuery(queryString);
             final String parsedNodeType = parseNodeType(context, 
nodeTypeString);
+            final SortOrder parsedSortOrder = parseSortOrder(sortOrder);
 
             final String availability;
             if (RequestContextProvider.get().isPreview() ) {
@@ -153,20 +168,20 @@ public class DocumentsResource extends AbstractResource {
                 availability = "live";
             }
             final SearchService searchService = getSearchService(context);
-            final Query query = searchService.createQuery()
+            Query query = searchService.createQuery()
                     .from(RequestContextProvider.get()
                             .getResolvedMount()
                             .getMount()
                             .getContentPath())
                     .ofType(parsedNodeType)
-                    .where(QueryUtils.text()
-                            .contains(parsedQuery == null ? "" : parsedQuery))
-                    .and(QueryUtils.text(HIPPO_AVAILABILITY)
-                            .isEqualTo(availability))
-                    .orderBy(HIPPOSTDPUBWF_PUBLICATION_DATE)
-                    .descending()
+                    .where(QueryUtils.text().contains(parsedQuery == null ? "" 
: parsedQuery))
+                    
.and(QueryUtils.text(HIPPO_AVAILABILITY).isEqualTo(availability))
+                    .and(new ExistsConstraint(orderBy))
                     .offsetBy(offset)
                     .limitTo(max);
+
+            query = applyOrdering(query, orderBy, parsedSortOrder);
+
             final QueryResult queryResult = searchService.search(query);
             final SearchResult result = new SearchResult();
             result.populateFromDocument(offset, max, queryResult, context);
@@ -188,6 +203,16 @@ public class DocumentsResource extends AbstractResource {
         }
     }
 
+    private Query applyOrdering(final Query query, final String orderBy, final 
SortOrder sortOrder) {
+        switch(sortOrder) {
+            case ASCENDING:
+            case ASC:
+                return query.orderBy(orderBy).ascending();
+            default:
+                return query.orderBy(orderBy).descending();
+        }
+    }
+
     @GET
     @Path("/documents/{uuid}")
     public Response getDocumentsByUUID(@PathParam("uuid") final String 
uuidString) {



View it on GitLab: 
https://code.onehippo.org/cms-community/hippo-site-toolkit/commit/cde548d8661e8af2261fb63000b7e6a64828b248
_______________________________________________
Hippocms-svn mailing list
Hippocms-svn@lists.onehippo.org
https://lists.onehippo.org/mailman/listinfo/hippocms-svn

Reply via email to