Search in sources :

Example 1 with TupleList

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);
}
Also used : TupleList(org.alfresco.rest.api.search.model.TupleList) TupleEntry(org.alfresco.rest.api.search.model.TupleEntry) JSONObject(org.json.JSONObject) Paging(org.alfresco.rest.framework.resource.parameters.Paging) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException)

Example 2 with TupleList

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);
        }
    }
}
Also used : FilteringResultSet(org.alfresco.repo.security.permissions.impl.acegi.FilteringResultSet) TupleList(org.alfresco.rest.api.search.model.TupleList) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) SolrSQLJSONResultSet(org.alfresco.repo.search.impl.solr.SolrSQLJSONResultSet) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) ResultSet(org.alfresco.service.cmr.search.ResultSet) FilteringResultSet(org.alfresco.repo.security.permissions.impl.acegi.FilteringResultSet) SolrSQLJSONResultSet(org.alfresco.repo.search.impl.solr.SolrSQLJSONResultSet) SearchSQLQuery(org.alfresco.rest.api.search.model.SearchSQLQuery) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException)

Example 3 with TupleList

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());
    }
}
Also used : TupleList(org.alfresco.rest.api.search.model.TupleList) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) SearchSQLQuery(org.alfresco.rest.api.search.model.SearchSQLQuery) JSONException(org.json.JSONException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

TupleList (org.alfresco.rest.api.search.model.TupleList)3 IOException (java.io.IOException)2 SearchSQLQuery (org.alfresco.rest.api.search.model.SearchSQLQuery)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 ArrayList (java.util.ArrayList)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 LuceneQueryParserException (org.alfresco.repo.search.impl.lucene.LuceneQueryParserException)1 SolrSQLJSONResultSet (org.alfresco.repo.search.impl.solr.SolrSQLJSONResultSet)1 FilteringResultSet (org.alfresco.repo.security.permissions.impl.acegi.FilteringResultSet)1 TupleEntry (org.alfresco.rest.api.search.model.TupleEntry)1 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)1 Paging (org.alfresco.rest.framework.resource.parameters.Paging)1 ResultSet (org.alfresco.service.cmr.search.ResultSet)1 SearchParameters (org.alfresco.service.cmr.search.SearchParameters)1 JSONArray (org.json.JSONArray)1 Test (org.junit.Test)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1