use of org.alfresco.rest.api.search.model.TupleList in project alfresco-remote-api by Alfresco.
the class ResultMapper method toCollectionWithPagingInfo.
public CollectionWithPagingInfo<TupleList> toCollectionWithPagingInfo(JSONArray docs, SearchSQLQuery searchQuery) throws JSONException {
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<TupleList> entries = new ArrayList<TupleList>();
for (int i = 0; i < docs.length() - 1; i++) {
List<TupleEntry> row = new ArrayList<TupleEntry>();
JSONObject docObj = (JSONObject) docs.get(i);
docObj.keys().forEachRemaining(action -> {
try {
String value = docObj.get(action.toString()).toString();
row.add(new TupleEntry(action.toString(), value));
} catch (JSONException e) {
throw new RuntimeException("Unable to parse SQL response. " + e);
}
});
entries.add(new TupleList(row));
}
Paging paging = Paging.valueOf(0, searchQuery.getItemLimit());
return CollectionWithPagingInfo.asPaged(paging, entries);
}
use of org.alfresco.rest.api.search.model.TupleList in project alfresco-remote-api by Alfresco.
the class SearchSQLApiWebscript method execute.
@Override
public void execute(WebScriptRequest webScriptRequest, WebScriptResponse res) throws IOException {
try {
// Turn JSON into a Java object representation
SearchSQLQuery searchQuery = extractJsonContent(webScriptRequest, assistant.getJsonHelper(), SearchSQLQuery.class);
SearchParameters sparams = buildSearchParameters(searchQuery);
ResultSet results = searchService.query(sparams);
FilteringResultSet frs = (FilteringResultSet) results;
SolrSQLJSONResultSet ssjr = (SolrSQLJSONResultSet) frs.getUnFilteredResultSet();
// When solr format is requested pass the solr output directly.
if (searchQuery.getFormat().equalsIgnoreCase("solr")) {
res.getWriter().write(ssjr.getSolrResponse());
} else {
CollectionWithPagingInfo<TupleList> nodes = resultMapper.toCollectionWithPagingInfo(ssjr.getDocs(), searchQuery);
renderJsonResponse(res, nodes, assistant.getJsonHelper());
}
setResponse(res, DEFAULT_SUCCESS);
} catch (Exception exception) {
if (exception instanceof LuceneQueryParserException) {
renderException(exception, res, assistant);
} else {
renderException(new WebScriptException(400, exception.getMessage()), res, assistant);
}
}
}
use of org.alfresco.rest.api.search.model.TupleList in project alfresco-remote-api by Alfresco.
the class ResultMapperTests method testSqlResponse.
@Test
public void testSqlResponse() throws IOException, JSONException {
JSONObject response = new JSONObject("{\"docs\":[{\"SITE\":\"_REPOSITORY_\"},{\"SITE\":\"surf-config\"},{\"SITE\":\"swsdp\"},{\"EOF\":true,\"RESPONSE_TIME\":96}]}");
JSONArray docs = response.getJSONArray("docs");
SearchSQLQuery query = new SearchSQLQuery("select SITE from alfresco group by SITE", null, null, 100, false, null, null);
CollectionWithPagingInfo<TupleList> 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(), 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, query);
} catch (Exception e) {
assertNotNull(e);
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());
}
}
Aggregations