diff --git a/src/main/java/org/alfresco/rest/api/search/SearchSQLApiWebscript.java b/src/main/java/org/alfresco/rest/api/search/SearchSQLApiWebscript.java index 0528b8562b..ec28d8db7a 100644 --- a/src/main/java/org/alfresco/rest/api/search/SearchSQLApiWebscript.java +++ b/src/main/java/org/alfresco/rest/api/search/SearchSQLApiWebscript.java @@ -90,7 +90,7 @@ public class SearchSQLApiWebscript extends AbstractWebScript implements Recogniz } else { - CollectionWithPagingInfo nodes = resultMapper.toCollectionWithPagingInfo(ssjr.getDocs()); + CollectionWithPagingInfo nodes = resultMapper.toCollectionWithPagingInfo(ssjr.getDocs(), searchQuery); renderJsonResponse(res, nodes, assistant.getJsonHelper()); } setResponse(res, DEFAULT_SUCCESS); diff --git a/src/main/java/org/alfresco/rest/api/search/impl/ResultMapper.java b/src/main/java/org/alfresco/rest/api/search/impl/ResultMapper.java index 7ec5b0c200..b0c907b7b5 100644 --- a/src/main/java/org/alfresco/rest/api/search/impl/ResultMapper.java +++ b/src/main/java/org/alfresco/rest/api/search/impl/ResultMapper.java @@ -71,6 +71,7 @@ import org.alfresco.rest.api.search.model.FacetQuery; import org.alfresco.rest.api.search.model.HighlightEntry; import org.alfresco.rest.api.search.model.SearchEntry; import org.alfresco.rest.api.search.model.SearchQuery; +import org.alfresco.rest.api.search.model.SearchSQLQuery; import org.alfresco.rest.api.search.model.TupleEntry; import org.alfresco.rest.api.search.model.TupleList; import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException; @@ -641,12 +642,16 @@ public class ResultMapper return null; } - public CollectionWithPagingInfo toCollectionWithPagingInfo(JSONArray docs) throws JSONException + public CollectionWithPagingInfo toCollectionWithPagingInfo(JSONArray docs, SearchSQLQuery searchQuery) throws JSONException { - if(docs == null) + if(docs == null ) { throw new RuntimeException("Solr response is required instead of JSONArray docs was null" ); } + if(searchQuery == null ) + { + throw new RuntimeException("SearchSQLQuery is required" ); + } List entries = new ArrayList(); for(int i = 0; i < docs.length() -1; i++) { @@ -665,7 +670,7 @@ public class ResultMapper }); entries.add(new TupleList(row)); } - Paging paging = Paging.valueOf(0, 1000); + Paging paging = Paging.valueOf(0, searchQuery.getItemLimit()); return CollectionWithPagingInfo.asPaged(paging, entries); } } diff --git a/src/main/java/org/alfresco/rest/api/search/model/SearchSQLQuery.java b/src/main/java/org/alfresco/rest/api/search/model/SearchSQLQuery.java index 05dc6bed3a..df5fa651d7 100644 --- a/src/main/java/org/alfresco/rest/api/search/model/SearchSQLQuery.java +++ b/src/main/java/org/alfresco/rest/api/search/model/SearchSQLQuery.java @@ -38,17 +38,19 @@ import com.fasterxml.jackson.annotation.JsonProperty; public class SearchSQLQuery { private final String stmt; - private Integer paging; + private Integer itemLimit; private final String format; private List locales; public SearchSQLQuery(@JsonProperty("stmt") String stmt, @JsonProperty("format") String format, - @JsonProperty("locales") List locales) + @JsonProperty("locales") List locales, + @JsonProperty("limit") Integer itemLimit) { this.stmt = stmt; this.format = format != null ? format : "default"; - this.locales = locales != null ? locales : Collections.EMPTY_LIST; + this.locales = locales != null ? locales : Collections.emptyList(); + this.itemLimit = itemLimit == null || itemLimit < 1 ? new Integer(1000) : itemLimit; } public String getStmt() @@ -56,9 +58,9 @@ public class SearchSQLQuery return stmt; } - public Integer getPaging() + public Integer getItemLimit() { - return paging; + return itemLimit; } public String getFormat() diff --git a/src/test/java/org/alfresco/rest/api/search/ResultMapperTests.java b/src/test/java/org/alfresco/rest/api/search/ResultMapperTests.java index be841040f9..206efe3e3a 100644 --- a/src/test/java/org/alfresco/rest/api/search/ResultMapperTests.java +++ b/src/test/java/org/alfresco/rest/api/search/ResultMapperTests.java @@ -79,6 +79,7 @@ import org.alfresco.rest.api.search.impl.SearchMapper; import org.alfresco.rest.api.search.impl.StoreMapper; import org.alfresco.rest.api.search.model.HighlightEntry; import org.alfresco.rest.api.search.model.SearchQuery; +import org.alfresco.rest.api.search.model.SearchSQLQuery; import org.alfresco.rest.api.search.model.TupleList; import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException; import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo; @@ -852,24 +853,34 @@ public class ResultMapperTests { JSONObject response = new JSONObject("{\"docs\":[{\"SITE\":\"_REPOSITORY_\"},{\"SITE\":\"surf-config\"},{\"SITE\":\"swsdp\"},{\"EOF\":true,\"RESPONSE_TIME\":96}]}"); JSONArray docs = response.getJSONArray("docs"); - CollectionWithPagingInfo info = mapper.toCollectionWithPagingInfo(docs); - assertEquals(1000, info.getPaging().getMaxItems()); + SearchSQLQuery query = new SearchSQLQuery("select SITE from alfresco group by SITE", null, null, 100); + CollectionWithPagingInfo info = mapper.toCollectionWithPagingInfo(docs, query); + assertEquals(100, info.getPaging().getMaxItems()); assertEquals(0, info.getPaging().getSkipCount()); assertEquals(false, info.getCollection().isEmpty()); assertEquals(3, info.getCollection().size()); - info = mapper.toCollectionWithPagingInfo(new JSONArray()); - assertEquals(1000, info.getPaging().getMaxItems()); + info = mapper.toCollectionWithPagingInfo(new JSONArray(), query); + assertEquals(100, info.getPaging().getMaxItems()); assertEquals(0, info.getPaging().getSkipCount()); assertEquals(true, info.getCollection().isEmpty()); assertEquals(0, info.getCollection().size()); try { - mapper.toCollectionWithPagingInfo(null); + mapper.toCollectionWithPagingInfo(null, query); } catch (Exception e) { assertNotNull(e); - assertEquals("Solr response is required instead of docs:null", e.getMessage()); + assertEquals("Solr response is required instead of JSONArray docs was null", e.getMessage()); + } + try + { + mapper.toCollectionWithPagingInfo(docs, null); + } + catch (Exception e) + { + assertNotNull(e); + assertEquals("SearchSQLQuery is required", e.getMessage()); } } } diff --git a/src/test/java/org/alfresco/rest/api/search/SearchSQLApiWebscriptTests.java b/src/test/java/org/alfresco/rest/api/search/SearchSQLApiWebscriptTests.java index 8381aaea55..61c0c69c62 100644 --- a/src/test/java/org/alfresco/rest/api/search/SearchSQLApiWebscriptTests.java +++ b/src/test/java/org/alfresco/rest/api/search/SearchSQLApiWebscriptTests.java @@ -47,7 +47,7 @@ public class SearchSQLApiWebscriptTests public void testSearchQueryParams() throws Exception { String query = "select SITE from alfresco"; - SearchSQLQuery searchQuery = new SearchSQLQuery(query, "solr", Collections.EMPTY_LIST); + SearchSQLQuery searchQuery = new SearchSQLQuery(query, "solr", Collections.emptyList(), 1000); SearchParameters sparams = webscript.buildSearchParameters(searchQuery); assertNotNull(sparams); @@ -57,7 +57,7 @@ public class SearchSQLApiWebscriptTests @Test public void testSearchQueryNullStmt() throws Exception { - SearchSQLQuery searchQuery = new SearchSQLQuery(null, "solr", Collections.EMPTY_LIST); + SearchSQLQuery searchQuery = new SearchSQLQuery(null, "solr", Collections.emptyList(), null); try { webscript.buildSearchParameters(searchQuery); @@ -70,7 +70,7 @@ public class SearchSQLApiWebscriptTests @Test public void testSearchSQLQueryAgainstNonInsight() throws Exception { - SearchSQLQuery searchQuery = new SearchSQLQuery(null, "solr", Collections.EMPTY_LIST); + SearchSQLQuery searchQuery = new SearchSQLQuery(null, "solr", Collections.emptyList(), 1000); try { webscript.buildSearchParameters(searchQuery);