Search in sources :

Example 1 with TopicInfoImpl

use of org.alfresco.repo.discussion.TopicInfoImpl 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)

Aggregations

ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 Date (java.util.Date)1 TreeSet (java.util.TreeSet)1 EmptyPagingResults (org.alfresco.query.EmptyPagingResults)1 PagingResults (org.alfresco.query.PagingResults)1 TopicInfoImpl (org.alfresco.repo.discussion.TopicInfoImpl)1 PostInfo (org.alfresco.service.cmr.discussion.PostInfo)1 TopicInfo (org.alfresco.service.cmr.discussion.TopicInfo)1 ResultSetRow (org.alfresco.service.cmr.search.ResultSetRow)1 Pair (org.alfresco.util.Pair)1