Search in sources :

Example 6 with ExecutionResult

use of org.alfresco.rest.framework.jacksonextensions.ExecutionResult in project alfresco-remote-api by Alfresco.

the class ResourceWebScriptHelper method processAdditionsToTheResponse.

/**
 * Looks at the object passed in and recursively expands any @EmbeddedEntityResource annotations or related relationship.
 * {@link org.alfresco.rest.framework.resource.EmbeddedEntityResource EmbeddedEntityResource} is expanded by calling the ReadById method for this entity.
 *
 * Either returns a ExecutionResult object or a CollectionWithPagingInfo containing a collection of ExecutionResult objects.
 *
 * @param api Api
 * @param entityCollectionName String
 * @param params  Params
 * @param objectToWrap Object
 * @return Object - Either ExecutionResult or CollectionWithPagingInfo<ExecutionResult>
 */
public Object processAdditionsToTheResponse(WebScriptResponse res, Api api, String entityCollectionName, Params params, Object objectToWrap) {
    PropertyCheck.mandatory(this, null, params);
    if (objectToWrap == null)
        return null;
    if (objectToWrap instanceof CollectionWithPagingInfo<?>) {
        CollectionWithPagingInfo<?> collectionToWrap = (CollectionWithPagingInfo<?>) objectToWrap;
        Object sourceEntity = executeIncludedSource(api, params, entityCollectionName, collectionToWrap);
        Collection<Object> resultCollection = new ArrayList(collectionToWrap.getCollection().size());
        if (!collectionToWrap.getCollection().isEmpty()) {
            for (Object obj : collectionToWrap.getCollection()) {
                resultCollection.add(processAdditionsToTheResponse(res, api, entityCollectionName, params, obj));
            }
        }
        return CollectionWithPagingInfo.asPaged(collectionToWrap.getPaging(), resultCollection, collectionToWrap.hasMoreItems(), collectionToWrap.getTotalItems(), sourceEntity, collectionToWrap.getContext());
    } else {
        if (BeanUtils.isSimpleProperty(objectToWrap.getClass()) || objectToWrap instanceof Collection) {
            // Simple property or Collection that can't be embedded so just return it.
            return objectToWrap;
        }
        final ExecutionResult execRes = new ExecutionResult(objectToWrap, params.getFilter());
        Map<String, Pair<String, Method>> embeddded = ResourceInspector.findEmbeddedResources(objectToWrap.getClass());
        if (embeddded != null && !embeddded.isEmpty()) {
            Map<String, Object> results = executeEmbeddedResources(api, params, objectToWrap, embeddded);
            execRes.addEmbedded(results);
        }
        if (params.getRelationsFilter() != null && !params.getRelationsFilter().isEmpty()) {
            Map<String, ResourceWithMetadata> relationshipResources = locator.locateRelationResource(api, entityCollectionName, params.getRelationsFilter().keySet(), HttpMethod.GET);
            String uniqueEntityId = ResourceInspector.findUniqueId(objectToWrap);
            Map<String, Object> relatedResources = executeRelatedResources(api, params, relationshipResources, uniqueEntityId);
            execRes.addRelated(relatedResources);
        }
        return execRes;
    }
}
Also used : ArrayList(java.util.ArrayList) CollectionWithPagingInfo(org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo) ExecutionResult(org.alfresco.rest.framework.jacksonextensions.ExecutionResult) Collection(java.util.Collection) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) Pair(org.alfresco.util.Pair)

Example 7 with ExecutionResult

use of org.alfresco.rest.framework.jacksonextensions.ExecutionResult in project alfresco-remote-api by Alfresco.

the class InfoWebScriptGet method execute.

@Override
public void execute(final Api api, WebScriptRequest req, WebScriptResponse res) throws IOException {
    ResourceDictionary resourceDic = lookupDictionary.getDictionary();
    final Map<String, ResourceWithMetadata> apiResources = resourceDic.getAllResources().get(api);
    if (apiResources == null) {
        throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_INVALID_API);
    }
    assistant.getJsonHelper().withWriter(res.getOutputStream(), new Writer() {

        @Override
        public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException {
            List<ExecutionResult> entities = new ArrayList<ExecutionResult>();
            for (ResourceWithMetadata resource : apiResources.values()) {
                entities.add(new ExecutionResult(resource.getMetaData(), null));
            }
            Collections.sort(entities, new Comparator<ExecutionResult>() {

                public int compare(ExecutionResult r1, ExecutionResult r2) {
                    return ((ResourceMetadata) r1.getRoot()).getUniqueId().compareTo(((ResourceMetadata) r2.getRoot()).getUniqueId());
                }
            });
            objectMapper.writeValue(generator, CollectionWithPagingInfo.asPaged(Paging.DEFAULT, entities));
        }
    });
}
Also used : ResourceDictionary(org.alfresco.rest.framework.core.ResourceDictionary) ExecutionResult(org.alfresco.rest.framework.jacksonextensions.ExecutionResult) IOException(java.io.IOException) Comparator(java.util.Comparator) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ArrayList(java.util.ArrayList) List(java.util.List) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) Writer(org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 8 with ExecutionResult

use of org.alfresco.rest.framework.jacksonextensions.ExecutionResult in project alfresco-remote-api by Alfresco.

the class SearchQuerySerializerTests method testSerializeContext.

@Test
public void testSerializeContext() throws IOException {
    ExecutionResult exec1 = new ExecutionResult(new Farmer("180"), null);
    FacetFieldContext ffc = new FacetFieldContext("theLabel", Arrays.asList(new Bucket("b1", "name:b1", 23, "displayText1"), new Bucket("b2", null, 34, "displayText2")));
    SearchContext searchContext = new SearchContext(23l, null, Arrays.asList(new FacetQueryContext("f1", "creator:bob", 15), new FacetQueryContext("f2", null, 20)), Arrays.asList(ffc), new SpellCheckContext("aFlag", Arrays.asList("bish", "bash")), null);
    CollectionWithPagingInfo<ExecutionResult> coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1), false, 2, null, searchContext);
    String out = helper.writeResponse(coll);
    assertTrue("There must 'context' json output", out.contains("\"context\":{\"consistency\":{\"lastTxId\":23}"));
    assertTrue("There must 'facetQueries' json output", out.contains("\"facetQueries\":"));
    assertTrue("There must 'facetQueries f1' json output", out.contains("{\"label\":\"f1\",\"filterQuery\":\"creator:bob\",\"count\":15}"));
    assertTrue("There must 'facetQueries f2' json output", out.contains("{\"label\":\"f2\",\"count\":20}"));
    assertTrue("There must 'spellCheck' json output", out.contains("\"spellCheck\":{\"type\":\"aFlag\",\"suggestions\":[\"bish\",\"bash\"]}"));
    assertTrue("There must 'facetsFields' json output", out.contains("\"facetsFields\":[{\"label\":\"theLabel\",\"buckets\""));
    assertTrue("There must 'bucket1' json output", out.contains("{\"label\":\"b1\",\"filterQuery\":\"name:b1\",\"count\":23,\"display\":\"displayText1\"}"));
    assertTrue("There must 'bucket2' json output", out.contains("{\"label\":\"b2\",\"count\":34,\"display\":\"displayText2\"}"));
    searchContext = new SearchContext(-1, null, null, null, null, null);
    coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1), false, 2, null, searchContext);
    out = helper.writeResponse(coll);
    assertTrue("There must NOT BE a 'context' json output", out.contains("\"context\":{}"));
    assertFalse("There must NOT BE a 'facetsFields' json output", out.contains("\"facetsFields\":{}"));
}
Also used : FacetFieldContext(org.alfresco.rest.api.search.context.FacetFieldContext) FacetQueryContext(org.alfresco.rest.api.search.context.FacetQueryContext) Bucket(org.alfresco.rest.api.search.context.FacetFieldContext.Bucket) SearchContext(org.alfresco.rest.api.search.context.SearchContext) ExecutionResult(org.alfresco.rest.framework.jacksonextensions.ExecutionResult) Farmer(org.alfresco.rest.framework.tests.api.mocks.Farmer) SpellCheckContext(org.alfresco.rest.api.search.context.SpellCheckContext) Test(org.junit.Test)

Aggregations

ExecutionResult (org.alfresco.rest.framework.jacksonextensions.ExecutionResult)8 Test (org.junit.Test)6 Farmer (org.alfresco.rest.framework.tests.api.mocks.Farmer)5 JSONObject (org.json.JSONObject)5 Sheep (org.alfresco.rest.framework.tests.api.mocks.Sheep)4 ArrayList (java.util.ArrayList)2 ResourceWithMetadata (org.alfresco.rest.framework.core.ResourceWithMetadata)2 BeanPropertiesFilter (org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter)2 SlimGoat (org.alfresco.rest.framework.tests.api.mocks3.SlimGoat)2 WebScriptResponse (org.springframework.extensions.webscripts.WebScriptResponse)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 FacetFieldContext (org.alfresco.rest.api.search.context.FacetFieldContext)1