Search in sources :

Example 1 with CollectionWithPagingInfo

use of org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo in project alfresco-remote-api by Alfresco.

the class GroupsImpl method getGroupsByPersonId.

@Override
public CollectionWithPagingInfo<Group> getGroupsByPersonId(String requestedPersonId, Parameters parameters) {
    // Canonicalize the person ID, performing -me- alias substitution.
    final String personId = people.validatePerson(requestedPersonId);
    // Non-admins can only access their own data
    // TODO: this is also in PeopleImpl.update(personId,personInfo) - refactor?
    boolean isAdmin = authorityService.hasAdminAuthority();
    String currentUserId = AuthenticationUtil.getFullyAuthenticatedUser();
    if (!isAdmin && !currentUserId.equalsIgnoreCase(personId)) {
        // The user is not an admin user and is not attempting to retrieve *their own* details.
        throw new PermissionDeniedException();
    }
    Query q = parameters.getQuery();
    Boolean isRootParam = null;
    String zoneFilter = null;
    if (q != null) {
        GroupsQueryWalker propertyWalker = new GroupsQueryWalker();
        QueryHelper.walk(q, propertyWalker);
        isRootParam = propertyWalker.getIsRoot();
        List<String> zonesParam = propertyWalker.getZones();
        if (zonesParam != null) {
            validateZonesParam(zonesParam);
            zoneFilter = zonesParam.get(0);
        }
    }
    final List<String> includeParam = parameters.getInclude();
    Paging paging = parameters.getPaging();
    // Retrieve sort column. This is limited for now to sort column due to
    // v0 api implementation. Should be improved in the future.
    Pair<String, Boolean> sortProp = getGroupsSortProp(parameters);
    // Get all the authorities for a user, including but not limited to, groups.
    Set<String> userAuthorities = runAsSystem(() -> authorityService.getAuthoritiesForUser(personId));
    final Set<String> rootAuthorities = getAllRootAuthorities(AuthorityType.GROUP);
    // Filter, transform and sort the list of user authorities into
    // a suitable list of AuthorityInfo objects.
    final String finalZoneFilter = zoneFilter;
    final Boolean finalIsRootParam = isRootParam;
    List<AuthorityInfo> groupAuthorities = userAuthorities.stream().filter(a -> a.startsWith(AuthorityType.GROUP.getPrefixString())).filter(a -> isRootPredicate(finalIsRootParam, rootAuthorities, a)).filter(a -> zonePredicate(a, finalZoneFilter)).map(this::getAuthorityInfo).sorted(new AuthorityInfoComparator(sortProp.getFirst(), sortProp.getSecond())).collect(Collectors.toList());
    PagingResults<AuthorityInfo> pagingResult = Util.wrapPagingResults(paging, groupAuthorities);
    // Create response.
    final List<AuthorityInfo> page = pagingResult.getPage();
    int totalItems = pagingResult.getTotalResultCount().getFirst();
    // Transform the page of results into Group objects
    List<Group> groups = page.stream().map(authority -> getGroup(authority, includeParam, rootAuthorities)).collect(Collectors.toList());
    return CollectionWithPagingInfo.asPaged(paging, groups, pagingResult.hasMoreItems(), totalItems);
}
Also used : CannedQueryPageDetails(org.alfresco.query.CannedQueryPageDetails) Arrays(java.util.Arrays) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) MapBasedQueryWalkerOrSupported(org.alfresco.rest.workflow.api.impl.MapBasedQueryWalkerOrSupported) Query(org.alfresco.rest.framework.resource.parameters.where.Query) AuthorityService(org.alfresco.service.cmr.security.AuthorityService) Paging(org.alfresco.rest.framework.resource.parameters.Paging) AuthenticationUtil.runAsSystem(org.alfresco.repo.security.authentication.AuthenticationUtil.runAsSystem) AbstractList(java.util.AbstractList) HashMap(java.util.HashMap) PagingRequest(org.alfresco.query.PagingRequest) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) WhereClauseParser(org.alfresco.rest.antlr.WhereClauseParser) GroupMember(org.alfresco.rest.api.model.GroupMember) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) PagingResults(org.alfresco.query.PagingResults) PermissionService(org.alfresco.service.cmr.security.PermissionService) People(org.alfresco.rest.api.People) Map(java.util.Map) QueryHelper(org.alfresco.rest.framework.resource.parameters.where.QueryHelper) AuthorityDAO(org.alfresco.repo.security.authority.AuthorityDAO) Group(org.alfresco.rest.api.model.Group) Collator(java.text.Collator) UnknownAuthorityException(org.alfresco.repo.security.authority.UnknownAuthorityException) AuthorityType(org.alfresco.service.cmr.security.AuthorityType) Iterator(java.util.Iterator) SortColumn(org.alfresco.rest.framework.resource.parameters.SortColumn) Set(java.util.Set) Pair(org.alfresco.util.Pair) AuthorityInfo(org.alfresco.repo.security.authority.AuthorityInfo) Collectors(java.util.stream.Collectors) EmptyPagingResults(org.alfresco.query.EmptyPagingResults) AlfrescoCollator(org.alfresco.util.AlfrescoCollator) List(java.util.List) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) CollectionWithPagingInfo(org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo) MapBasedQueryWalker(org.alfresco.rest.workflow.api.impl.MapBasedQueryWalker) I18NUtil(org.springframework.extensions.surf.util.I18NUtil) AuthenticationUtil(org.alfresco.repo.security.authentication.AuthenticationUtil) Groups(org.alfresco.rest.api.Groups) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) Comparator(java.util.Comparator) Collections(java.util.Collections) AuthorityException(org.alfresco.repo.security.authority.AuthorityException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Parameters(org.alfresco.rest.framework.resource.parameters.Parameters) Group(org.alfresco.rest.api.model.Group) Query(org.alfresco.rest.framework.resource.parameters.where.Query) Paging(org.alfresco.rest.framework.resource.parameters.Paging) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) AuthorityInfo(org.alfresco.repo.security.authority.AuthorityInfo)

Example 2 with CollectionWithPagingInfo

use of org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo in project alfresco-remote-api by Alfresco.

the class ResourceWebScriptGet method executeAction.

/**
 * Executes the action on the resource
 * @param resource ResourceWithMetadata
 * @param params parameters to use
 * @return anObject the result of the execute
 */
@Override
public Object executeAction(ResourceWithMetadata resource, Params params, WithResponse withResponse) throws Throwable {
    switch(resource.getMetaData().getType()) {
        case ENTITY:
            if (StringUtils.isBlank(params.getEntityId())) {
                // Get the collection
                if (EntityResourceAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.Read.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.Read<?> getter = (Read<?>) resource.getResource();
                    CollectionWithPagingInfo<?> resources = getter.readAll(params);
                    return resources;
                } else if (EntityResourceAction.ReadWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.ReadWithResponse.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.ReadWithResponse<?> getter = (EntityResourceAction.ReadWithResponse<?>) resource.getResource();
                    CollectionWithPagingInfo<?> resources = getter.readAll(params, withResponse);
                    return resources;
                } else if (EntityResourceAction.DeleteSetWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.DeleteSetWithResponse.class)) {
                        throw new DeletedResourceException("(DELETE) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.DeleteSetWithResponse relationDeleter = (EntityResourceAction.DeleteSetWithResponse) resource.getResource();
                    relationDeleter.deleteSet(params, withResponse);
                    // Don't pass anything to the callback - its just successful
                    return null;
                } else if (EntityResourceAction.DeleteSet.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.Delete.class)) {
                        throw new DeletedResourceException("(DELETE) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.DeleteSet relationDeleter = (EntityResourceAction.DeleteSet) resource.getResource();
                    relationDeleter.deleteSet(params);
                    // Don't pass anything to the callback - its just successful
                    return null;
                } else {
                    throw new UnsupportedResourceOperationException();
                }
            } else {
                if (EntityResourceAction.ReadById.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.ReadById.class)) {
                        throw new DeletedResourceException("(GET by id) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.ReadById<?> entityGetter = (ReadById<?>) resource.getResource();
                    Object result = entityGetter.readById(params.getEntityId(), params);
                    return result;
                } else if (EntityResourceAction.ReadByIdWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.ReadByIdWithResponse.class)) {
                        throw new DeletedResourceException("(GET by id) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.ReadByIdWithResponse<?> entityGetter = (EntityResourceAction.ReadByIdWithResponse<?>) resource.getResource();
                    Object result = entityGetter.readById(params.getEntityId(), params, withResponse);
                    return result;
                } else {
                    throw new UnsupportedResourceOperationException();
                }
            }
        case RELATIONSHIP:
            if (StringUtils.isNotBlank(params.getRelationshipId())) {
                if (RelationshipResourceAction.ReadById.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(RelationshipResourceAction.ReadById.class)) {
                        throw new DeletedResourceException("(GET by id) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceAction.ReadById<?> relationGetter = (RelationshipResourceAction.ReadById<?>) resource.getResource();
                    Object result = relationGetter.readById(params.getEntityId(), params.getRelationshipId(), params);
                    return result;
                } else if (RelationshipResourceAction.ReadByIdWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(RelationshipResourceAction.ReadByIdWithResponse.class)) {
                        throw new DeletedResourceException("(GET by id) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceAction.ReadByIdWithResponse<?> relationGetter = (RelationshipResourceAction.ReadByIdWithResponse<?>) resource.getResource();
                    Object result = relationGetter.readById(params.getEntityId(), params.getRelationshipId(), params, withResponse);
                    return result;
                } else {
                    throw new UnsupportedResourceOperationException();
                }
            } else {
                if (RelationshipResourceAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(RelationshipResourceAction.Read.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceAction.Read<?> relationGetter = (RelationshipResourceAction.Read<?>) resource.getResource();
                    CollectionWithPagingInfo<?> relations = relationGetter.readAll(params.getEntityId(), params);
                    return relations;
                } else {
                    if (resource.getMetaData().isDeleted(RelationshipResourceAction.ReadWithResponse.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceAction.ReadWithResponse<?> relationGetter = (RelationshipResourceAction.ReadWithResponse<?>) resource.getResource();
                    CollectionWithPagingInfo<?> relations = relationGetter.readAll(params.getEntityId(), params, withResponse);
                    return relations;
                }
            }
        case PROPERTY:
            if (StringUtils.isNotBlank(params.getEntityId())) {
                if (BinaryResourceAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(BinaryResourceAction.Read.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    BinaryResourceAction.Read getter = (BinaryResourceAction.Read) resource.getResource();
                    BinaryResource prop = getter.readProperty(params.getEntityId(), params);
                    return prop;
                }
                if (BinaryResourceAction.ReadWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(BinaryResourceAction.ReadWithResponse.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    BinaryResourceAction.ReadWithResponse getter = (BinaryResourceAction.ReadWithResponse) resource.getResource();
                    BinaryResource prop = getter.readProperty(params.getEntityId(), params, withResponse);
                    return prop;
                }
                if (RelationshipResourceBinaryAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(RelationshipResourceBinaryAction.Read.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceBinaryAction.Read getter = (RelationshipResourceBinaryAction.Read) resource.getResource();
                    BinaryResource prop = getter.readProperty(params.getEntityId(), params.getRelationshipId(), params);
                    return prop;
                }
                if (RelationshipResourceBinaryAction.ReadWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(RelationshipResourceBinaryAction.ReadWithResponse.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceBinaryAction.ReadWithResponse getter = (RelationshipResourceBinaryAction.ReadWithResponse) resource.getResource();
                    BinaryResource prop = getter.readProperty(params.getEntityId(), params.getRelationshipId(), params, withResponse);
                    return prop;
                }
            } else {
                throw new UnsupportedResourceOperationException();
            }
        default:
            throw new UnsupportedResourceOperationException("GET not supported for Actions");
    }
}
Also used : CollectionWithPagingInfo(org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo) DeletedResourceException(org.alfresco.rest.framework.core.exceptions.DeletedResourceException) Read(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.Read) EntityResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) BinaryResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction) RelationshipResourceBinaryAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceBinaryAction) RelationshipResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction) ReadById(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.ReadById) BinaryResource(org.alfresco.rest.framework.resource.content.BinaryResource)

Example 3 with CollectionWithPagingInfo

use of org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo 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 4 with CollectionWithPagingInfo

use of org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo in project alfresco-remote-api by Alfresco.

the class SerializeTests method testSerializePagedCollection.

@SuppressWarnings({ "rawtypes" })
@Test
public void testSerializePagedCollection() throws IOException {
    assertNotNull(helper);
    CollectionWithPagingInfo paged = CollectionWithPagingInfo.asPaged(null, null);
    String out = writeResponse(helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, null, Params.valueOf("notUsed", null, null), paged));
    assertTrue("There must be json output as List with pagination", StringUtils.startsWith(out, "{\"list\":{\"pagination\":{\"count\":0,"));
    Paging pageRequest = Paging.valueOf(1, 2);
    paged = CollectionWithPagingInfo.asPaged(pageRequest, Arrays.asList(new Goat(), new Sheep("ABCD"), new Sheep("XYZ")));
    out = writeResponse(helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, null, Params.valueOf("notUsed", null, null), paged));
    assertTrue("There must be json output as List with pagination", StringUtils.startsWith(out, "{\"list\":{\"pagination\":{\"count\":3,"));
    paged = CollectionWithPagingInfo.asPaged(pageRequest, Arrays.asList(new Goat(), new Sheep("ABCD"), new Sheep("XYZ")), true, 5000);
    out = writeResponse(helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, null, Params.valueOf("notUsed", null, null), paged));
    assertTrue("There must be json output as List with pagination", StringUtils.startsWith(out, "{\"list\":{\"pagination\":{\"count\":3,\"hasMoreItems\":true,\"totalItems\":5000"));
}
Also used : Paging(org.alfresco.rest.framework.resource.parameters.Paging) WebScriptResponse(org.springframework.extensions.webscripts.WebScriptResponse) CollectionWithPagingInfo(org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo) Sheep(org.alfresco.rest.framework.tests.api.mocks.Sheep) Goat(org.alfresco.rest.framework.tests.api.mocks.Goat) SlimGoat(org.alfresco.rest.framework.tests.api.mocks3.SlimGoat) Test(org.junit.Test)

Aggregations

CollectionWithPagingInfo (org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo)4 ArrayList (java.util.ArrayList)2 UnsupportedResourceOperationException (org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException)2 Paging (org.alfresco.rest.framework.resource.parameters.Paging)2 Pair (org.alfresco.util.Pair)2 Collator (java.text.Collator)1 AbstractList (java.util.AbstractList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 CannedQueryPageDetails (org.alfresco.query.CannedQueryPageDetails)1 EmptyPagingResults (org.alfresco.query.EmptyPagingResults)1