Search in sources :

Example 6 with PagingResults

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

the class Util method wrapPagingResults.

public static <T> PagingResults<T> wrapPagingResults(Paging paging, Collection<T> result) {
    if (paging == null) {
        throw new InvalidArgumentException("paging object can't be null.");
    }
    if (result == null) {
        result = Collections.emptyList();
    }
    PagingRequest pagingRequest = getPagingRequest(paging);
    final int totalSize = result.size();
    final PageDetails pageDetails = PageDetails.getPageDetails(pagingRequest, totalSize);
    final List<T> page = new ArrayList<>(pageDetails.getPageSize());
    Iterator<T> it = result.iterator();
    for (int counter = 0, end = pageDetails.getEnd(); counter < end && it.hasNext(); counter++) {
        T element = it.next();
        if (counter < pageDetails.getSkipCount()) {
            continue;
        }
        if (counter > end - 1) {
            break;
        }
        page.add(element);
    }
    return new PagingResults<T>() {

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

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

        @Override
        public Pair<Integer, Integer> getTotalResultCount() {
            Integer total = totalSize;
            return new Pair<>(total, total);
        }

        @Override
        public String getQueryExecutionId() {
            return null;
        }
    };
}
Also used : InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) PagingResults(org.alfresco.query.PagingResults) ArrayList(java.util.ArrayList) PagingRequest(org.alfresco.query.PagingRequest) CannedQueryPageDetails(org.alfresco.query.CannedQueryPageDetails) PageDetails(org.alfresco.query.PageDetails) Pair(org.alfresco.util.Pair)

Example 7 with PagingResults

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

the class SiteAdminSitesGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    String currentUser = AuthenticationUtil.getFullyAuthenticatedUser();
    // check the current user access rights
    if (!siteService.isSiteAdmin(currentUser)) {
        // Note: security, no message to indicate why
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Resource not found.");
    }
    // Create paging
    final ScriptPagingDetails paging = new ScriptPagingDetails(getIntParameter(req, MAX_ITEMS, DEFAULT_MAX_ITEMS_PER_PAGE), getIntParameter(req, SKIP_COUNT, 0));
    // request a total count of found items
    paging.setRequestTotalCountMax(Integer.MAX_VALUE);
    final List<FilterProp> filterProp = getFilterProperties(req.getParameter(NAME_FILTER));
    final List<Pair<QName, Boolean>> sortProps = new ArrayList<Pair<QName, Boolean>>();
    sortProps.add(new Pair<QName, Boolean>(ContentModel.PROP_NAME, true));
    PagingResults<SiteInfo> pagingResults = AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<PagingResults<SiteInfo>>() {

        public PagingResults<SiteInfo> doWork() throws Exception {
            return siteService.listSites(filterProp, sortProps, paging);
        }
    }, AuthenticationUtil.getAdminUserName());
    List<SiteInfo> result = pagingResults.getPage();
    List<SiteState> sites = new ArrayList<SiteState>(result.size());
    for (SiteInfo info : result) {
        sites.add(SiteState.create(info, siteService.listMembers(info.getShortName(), null, SiteModel.SITE_MANAGER, 0), currentUser, nodeService, personService));
    }
    Map<String, Object> sitesData = new HashMap<String, Object>(6);
    // Site data
    sitesData.put("items", sites);
    // Paging data
    sitesData.put("count", result.size());
    sitesData.put("hasMoreItems", pagingResults.hasMoreItems());
    sitesData.put("totalItems", (pagingResults.getTotalResultCount() == null ? -1 : pagingResults.getTotalResultCount().getFirst()));
    sitesData.put("skipCount", paging.getSkipCount());
    sitesData.put("maxItems", paging.getMaxItems());
    // Create the model from the site and pagination data
    Map<String, Object> model = new HashMap<String, Object>(1);
    model.put("data", sitesData);
    return model;
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) PagingResults(org.alfresco.query.PagingResults) FilterProp(org.alfresco.repo.node.getchildren.FilterProp) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) FilterPropString(org.alfresco.repo.node.getchildren.FilterPropString) FilterTypeString(org.alfresco.repo.node.getchildren.FilterPropString.FilterTypeString) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) AuthenticationUtil(org.alfresco.repo.security.authentication.AuthenticationUtil) ScriptPagingDetails(org.alfresco.util.ScriptPagingDetails) Pair(org.alfresco.util.Pair)

Example 8 with PagingResults

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

the class RepoService 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 (String key : prefs.keySet()) {
        boolean isFavourite = false;
        Serializable s = prefs.get(key);
        if (s instanceof Boolean) {
            isFavourite = (Boolean) s;
        }
        if (isFavourite) {
            String siteShortName = key.substring(FAVOURITE_SITES_PREFIX_LENGTH);
            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) Collator(java.text.Collator) PageDetails(org.alfresco.query.PageDetails) TreeSet(java.util.TreeSet) 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