Search in sources :

Example 1 with PagingResults

use of org.alfresco.query.PagingResults in project alfresco-remote-api by Alfresco.

the class SitesImpl method getSiteContainers.

public PagingResults<SiteContainer> getSiteContainers(String siteId, Paging paging) {
    SiteInfo siteInfo = validateSite(siteId);
    if (siteInfo == null) {
        // site does not exist
        throw new EntityNotFoundException(siteId);
    }
    final PagingResults<FileInfo> pagingResults = siteService.listContainers(siteInfo.getShortName(), Util.getPagingRequest(paging));
    List<FileInfo> containerFileInfos = pagingResults.getPage();
    final List<SiteContainer> siteContainers = new ArrayList<SiteContainer>(containerFileInfos.size());
    for (FileInfo containerFileInfo : containerFileInfos) {
        NodeRef nodeRef = containerFileInfo.getNodeRef();
        String containerId = (String) nodeService.getProperty(nodeRef, SiteModel.PROP_COMPONENT_ID);
        SiteContainer siteContainer = new SiteContainer(containerId, nodeRef);
        siteContainers.add(siteContainer);
    }
    return new PagingResults<SiteContainer>() {

        @Override
        public List<SiteContainer> getPage() {
            return siteContainers;
        }

        @Override
        public boolean hasMoreItems() {
            return pagingResults.hasMoreItems();
        }

        @Override
        public Pair<Integer, Integer> getTotalResultCount() {
            return pagingResults.getTotalResultCount();
        }

        @Override
        public String getQueryExecutionId() {
            return null;
        }
    };
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) NodeRef(org.alfresco.service.cmr.repository.NodeRef) PagingResults(org.alfresco.query.PagingResults) FileInfo(org.alfresco.service.cmr.model.FileInfo) ArrayList(java.util.ArrayList) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) FilterPropString(org.alfresco.repo.node.getchildren.FilterPropString)

Example 2 with PagingResults

use of org.alfresco.query.PagingResults in project alfresco-remote-api by Alfresco.

the class ForumTopicsFilteredGet method wrap.

/**
 * Wrap up search results as {@link TopicInfo} instances
 *
 * @param finalResults ResultSet
 * @param paging PagingRequest
 */
protected PagingResults<TopicInfo> wrap(final ResultSet finalResults, PagingRequest paging) {
    int maxItems = paging.getMaxItems();
    Comparator<TopicInfo> lastPostDesc = new Comparator<TopicInfo>() {

        @Override
        public int compare(TopicInfo t1, TopicInfo t2) {
            Date t1LastPostDate = t1.getCreatedAt();
            if (discussionService.getMostRecentPost(t1) != null) {
                t1LastPostDate = discussionService.getMostRecentPost(t1).getCreatedAt();
            }
            Date t2LastPostDate = t2.getCreatedAt();
            if (discussionService.getMostRecentPost(t2) != null) {
                t2LastPostDate = discussionService.getMostRecentPost(t2).getCreatedAt();
            }
            return t2LastPostDate.compareTo(t1LastPostDate);
        }
    };
    final Set<TopicInfo> topics = new TreeSet<TopicInfo>(lastPostDesc);
    for (ResultSetRow row : finalResults) {
        Pair<TopicInfo, PostInfo> pair = discussionService.getForNodeRef(row.getNodeRef());
        TopicInfo topic = pair.getFirst();
        if (topic != null) {
            String path = nodeService.getPath(topic.getNodeRef()).toDisplayPath(nodeService, permissionService);
            String site = path.split("/")[3];
            TopicInfoImpl tii = (TopicInfoImpl) topic;
            tii.setShortSiteName(site);
            topics.add(tii);
            if (topics.size() >= maxItems) {
                break;
            }
        }
    }
    // Wrap
    return new PagingResults<TopicInfo>() {

        @Override
        public boolean hasMoreItems() {
            try {
                return finalResults.hasMore();
            } catch (UnsupportedOperationException e) {
                // Not all search results support paging
                return false;
            }
        }

        @Override
        public Pair<Integer, Integer> getTotalResultCount() {
            int skipCount = 0;
            int itemsRemainingAfterThisPage = 0;
            try {
                skipCount = finalResults.getStart();
            } catch (UnsupportedOperationException e) {
            }
            try {
                itemsRemainingAfterThisPage = finalResults.length();
            } catch (UnsupportedOperationException e) {
            }
            final int totalItemsInUnpagedResultSet = skipCount + itemsRemainingAfterThisPage;
            return new Pair<Integer, Integer>(totalItemsInUnpagedResultSet, totalItemsInUnpagedResultSet);
        }

        @Override
        public List<TopicInfo> getPage() {
            return new ArrayList<TopicInfo>(topics);
        }

        @Override
        public String getQueryExecutionId() {
            return null;
        }
    };
}
Also used : PagingResults(org.alfresco.query.PagingResults) EmptyPagingResults(org.alfresco.query.EmptyPagingResults) TopicInfoImpl(org.alfresco.repo.discussion.TopicInfoImpl) ArrayList(java.util.ArrayList) ResultSetRow(org.alfresco.service.cmr.search.ResultSetRow) TopicInfo(org.alfresco.service.cmr.discussion.TopicInfo) Date(java.util.Date) Comparator(java.util.Comparator) TreeSet(java.util.TreeSet) PostInfo(org.alfresco.service.cmr.discussion.PostInfo) Pair(org.alfresco.util.Pair)

Example 3 with PagingResults

use of org.alfresco.query.PagingResults in project alfresco-remote-api by Alfresco.

the class GroupsImpl method getAuthoritiesInfo.

private PagingResults<AuthorityInfo> getAuthoritiesInfo(AuthorityType authorityType, Boolean isRootParam, String zoneFilter, Set<String> rootAuthorities, Pair<String, Boolean> sortProp, Paging paging) {
    PagingResults<AuthorityInfo> pagingResult;
    if (isRootParam != null) {
        List<AuthorityInfo> groupList;
        if (isRootParam) {
            // Limit the post processing work by using the already loaded
            // list of root authorities.
            List<AuthorityInfo> authorities = rootAuthorities.stream().map(this::getAuthorityInfo).filter(auth -> zonePredicate(auth.getAuthorityName(), zoneFilter)).collect(Collectors.toList());
            groupList = new ArrayList<>(rootAuthorities.size());
            groupList.addAll(authorities);
            // Post process sorting - this should be moved to service
            // layer. It is done here because sorting is not supported at
            // service layer.
            AuthorityInfoComparator authorityComparator = new AuthorityInfoComparator(sortProp.getFirst(), sortProp.getSecond());
            Collections.sort(groupList, authorityComparator);
        } else {
            PagingRequest pagingNoMaxItems = new PagingRequest(CannedQueryPageDetails.DEFAULT_PAGE_SIZE);
            // Get authorities using canned query but without using
            // the requested paginating now because we need to filter out
            // the root authorities.
            PagingResults<AuthorityInfo> nonPagingResult = authorityService.getAuthoritiesInfo(authorityType, zoneFilter, null, sortProp.getFirst(), sortProp.getSecond(), pagingNoMaxItems);
            // Post process filtering - this should be moved to service
            // layer. It is done here because filtering by "isRoot" is not
            // supported at service layer.
            groupList = nonPagingResult.getPage();
            if (groupList != null) {
                for (Iterator<AuthorityInfo> i = groupList.iterator(); i.hasNext(); ) {
                    AuthorityInfo authorityInfo = i.next();
                    if (!isRootParam.equals(isRootAuthority(rootAuthorities, authorityInfo.getAuthorityName()))) {
                        i.remove();
                    }
                }
            }
        }
        // Post process paging - this should be moved to service layer.
        pagingResult = Util.wrapPagingResults(paging, groupList);
    } else {
        PagingRequest pagingRequest = Util.getPagingRequest(paging);
        // Get authorities using canned query.
        pagingResult = authorityService.getAuthoritiesInfo(authorityType, zoneFilter, null, sortProp.getFirst(), sortProp.getSecond(), pagingRequest);
    }
    return pagingResult;
}
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) AuthorityInfo(org.alfresco.repo.security.authority.AuthorityInfo) PagingRequest(org.alfresco.query.PagingRequest)

Example 4 with PagingResults

use of org.alfresco.query.PagingResults 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 5 with PagingResults

use of org.alfresco.query.PagingResults in project alfresco-remote-api by Alfresco.

the class SitesImpl method getFavouriteSites.

private PagingResults<SiteInfo> getFavouriteSites(String userName, PagingRequest pagingRequest) {
    final Collator collator = Collator.getInstance();
    final Set<SiteInfo> sortedFavouriteSites = new TreeSet<SiteInfo>(new Comparator<SiteInfo>() {

        @Override
        public int compare(SiteInfo o1, SiteInfo o2) {
            return collator.compare(o1.getTitle(), o2.getTitle());
        }
    });
    Map<String, Serializable> prefs = preferenceService.getPreferences(userName, FAVOURITE_SITES_PREFIX);
    for (Entry<String, Serializable> entry : prefs.entrySet()) {
        boolean isFavourite = false;
        Serializable s = entry.getValue();
        if (s instanceof Boolean) {
            isFavourite = (Boolean) s;
        }
        if (isFavourite) {
            String siteShortName = entry.getKey().substring(FAVOURITE_SITES_PREFIX_LENGTH).replace(".favourited", "");
            SiteInfo siteInfo = siteService.getSite(siteShortName);
            if (siteInfo != null) {
                sortedFavouriteSites.add(siteInfo);
            }
        }
    }
    int totalSize = sortedFavouriteSites.size();
    final PageDetails pageDetails = PageDetails.getPageDetails(pagingRequest, totalSize);
    final List<SiteInfo> page = new ArrayList<SiteInfo>(pageDetails.getPageSize());
    Iterator<SiteInfo> it = sortedFavouriteSites.iterator();
    for (int counter = 0; counter < pageDetails.getEnd() && it.hasNext(); counter++) {
        SiteInfo favouriteSite = it.next();
        if (counter < pageDetails.getSkipCount()) {
            continue;
        }
        if (counter > pageDetails.getEnd() - 1) {
            break;
        }
        page.add(favouriteSite);
    }
    return new PagingResults<SiteInfo>() {

        @Override
        public List<SiteInfo> getPage() {
            return page;
        }

        @Override
        public boolean hasMoreItems() {
            return pageDetails.hasMoreItems();
        }

        @Override
        public Pair<Integer, Integer> getTotalResultCount() {
            Integer total = Integer.valueOf(sortedFavouriteSites.size());
            return new Pair<Integer, Integer>(total, total);
        }

        @Override
        public String getQueryExecutionId() {
            return null;
        }
    };
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) Serializable(java.io.Serializable) PagingResults(org.alfresco.query.PagingResults) ArrayList(java.util.ArrayList) FilterPropString(org.alfresco.repo.node.getchildren.FilterPropString) Collator(java.text.Collator) PageDetails(org.alfresco.query.PageDetails) TreeSet(java.util.TreeSet) FilterPropBoolean(org.alfresco.repo.node.getchildren.FilterPropBoolean) Pair(org.alfresco.util.Pair)

Aggregations

ArrayList (java.util.ArrayList)8 PagingResults (org.alfresco.query.PagingResults)8 Pair (org.alfresco.util.Pair)7 Collator (java.text.Collator)4 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)4 Comparator (java.util.Comparator)3 HashMap (java.util.HashMap)3 TreeSet (java.util.TreeSet)3 CannedQueryPageDetails (org.alfresco.query.CannedQueryPageDetails)3 EmptyPagingResults (org.alfresco.query.EmptyPagingResults)3 PageDetails (org.alfresco.query.PageDetails)3 PagingRequest (org.alfresco.query.PagingRequest)3 FilterPropString (org.alfresco.repo.node.getchildren.FilterPropString)3 AuthenticationUtil (org.alfresco.repo.security.authentication.AuthenticationUtil)3 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)3 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)3 Serializable (java.io.Serializable)2 AbstractList (java.util.AbstractList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2