Search in sources :

Example 1 with TopicInfo

use of org.alfresco.service.cmr.discussion.TopicInfo in project alfresco-remote-api by Alfresco.

the class AbstractDiscussionWebScript method renderTopics.

/*
     * Renders out the list of topics
     * TODO Fetch the post data in one go, rather than one at a time
     */
protected Map<String, Object> renderTopics(List<TopicInfo> topics, Pair<Integer, Integer> size, PagingRequest paging, SiteInfo site) {
    Map<String, Object> model = new HashMap<String, Object>();
    // Paging info
    model.put("total", size.getFirst());
    model.put("pageSize", paging.getMaxItems());
    model.put("startIndex", paging.getSkipCount());
    model.put("itemCount", topics.size());
    // Data
    List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
    for (TopicInfo topic : topics) {
        // Into "My Discussions" dashlet forum topic will be displayed only if user is a member of that site.
        if (null == site && null != topic.getShortSiteName()) {
            String currentUser = AuthenticationUtil.getFullyAuthenticatedUser();
            String siteShortName = topic.getShortSiteName();
            boolean isSiteMember = siteService.isMember(siteShortName, currentUser);
            if (isSiteMember) {
                items.add(renderTopic(topic, site));
            }
        } else // Display all topics on the forum of the site.
        {
            items.add(renderTopic(topic, site));
        }
    }
    model.put("items", items);
    // All done
    return model;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) MimetypeMap(org.alfresco.repo.content.MimetypeMap) TopicInfo(org.alfresco.service.cmr.discussion.TopicInfo)

Example 2 with TopicInfo

use of org.alfresco.service.cmr.discussion.TopicInfo in project alfresco-remote-api by Alfresco.

the class ForumTopicsFilteredGet method doSearch.

/**
 * Do the actual search
 *
 * @param searchQuery Pair with query string in first and query language in second
 * @param sortAscending boolean
 * @param paging PagingRequest
 */
protected PagingResults<TopicInfo> doSearch(Pair<String, String> searchQuery, boolean sortAscending, PagingRequest paging) {
    ResultSet resultSet = null;
    PagingResults<TopicInfo> pagedResults = new EmptyPagingResults<TopicInfo>();
    String sortOn = "@{http://www.alfresco.org/model/content/1.0}created";
    // Setup the search parameters
    SearchParameters sp = new SearchParameters();
    sp.addStore(SPACES_STORE);
    sp.setQuery(searchQuery.getFirst());
    sp.setLanguage(searchQuery.getSecond());
    sp.addSort(sortOn, sortAscending);
    if (paging.getMaxItems() > 0) {
        // Multiply maxItems by 10.  This is to catch topics that have multiple replies and ensure that the maximum number of topics is shown.
        sp.setLimit(paging.getMaxItems() * 10);
        sp.setLimitBy(LimitBy.FINAL_SIZE);
    }
    if (paging.getSkipCount() > 0) {
        sp.setSkipCount(paging.getSkipCount());
    }
    try {
        resultSet = searchService.query(sp);
        pagedResults = wrap(resultSet, paging);
    } finally {
        try {
            resultSet.close();
        } catch (Exception e) {
        // do nothing
        }
    }
    return pagedResults;
}
Also used : SearchParameters(org.alfresco.service.cmr.search.SearchParameters) ResultSet(org.alfresco.service.cmr.search.ResultSet) EmptyPagingResults(org.alfresco.query.EmptyPagingResults) TopicInfo(org.alfresco.service.cmr.discussion.TopicInfo) WebScriptException(org.springframework.extensions.webscripts.WebScriptException)

Example 3 with TopicInfo

use of org.alfresco.service.cmr.discussion.TopicInfo in project alfresco-remote-api by Alfresco.

the class ForumTopicsFilteredGet method executeImpl.

/**
 * @param site SiteInfo
 * @param nodeRef Not required. It is only included because it is overriding the parent class.
 * @param topic Not required. It is only included because it is overriding the parent class.
 * @param post Not required. It is only included because it is overriding the parent class.
 * @param req WebScriptRequest
 * @param status Not required. It is only included because it is overriding the parent class.
 * @param cache Not required. It is only included because it is overriding the parent class.
 *
 * @return Map
 */
@Override
protected Map<String, Object> executeImpl(SiteInfo site, NodeRef nodeRef, TopicInfo topic, PostInfo post, WebScriptRequest req, JSONObject json, Status status, Cache cache) {
    // They shouldn't be trying to list of an existing Post or Topic
    if (topic != null || post != null) {
        String error = "Can't list Topics inside an existing Topic or Post";
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }
    // Set search filter to users topics or all topics
    String pAuthor = req.getParameter("topics");
    String author = DEFAULT_TOPIC_AUTHOR;
    if (pAuthor != null) {
        author = pAuthor;
    }
    // Set the number of days in the past to search from
    String pDaysAgo = req.getParameter("history");
    int daysAgo = DEFAULT_TOPIC_LATEST_POST_DAYS_AGO;
    if (pDaysAgo != null) {
        try {
            daysAgo = Integer.parseInt(pDaysAgo);
        } catch (NumberFormatException e) {
        // do nothing. history has already been preset to the default value.
        }
    }
    // Get the complete search query
    Pair<String, String> searchQuery = getSearchQuery(site, author, daysAgo);
    // Get the filtered topics
    PagingRequest paging = buildPagingRequest(req);
    PagingResults<TopicInfo> topics = doSearch(searchQuery, false, paging);
    // Build the common model parts
    Map<String, Object> model = buildCommonModel(site, topic, post, req);
    // Have the topics rendered
    model.put("data", renderTopics(topics, paging, site));
    // All done
    return model;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.simple.JSONObject) PagingRequest(org.alfresco.query.PagingRequest) TopicInfo(org.alfresco.service.cmr.discussion.TopicInfo)

Example 4 with TopicInfo

use of org.alfresco.service.cmr.discussion.TopicInfo 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 5 with TopicInfo

use of org.alfresco.service.cmr.discussion.TopicInfo in project alfresco-remote-api by Alfresco.

the class ForumTopicsGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(SiteInfo site, NodeRef nodeRef, TopicInfo topic, PostInfo post, WebScriptRequest req, JSONObject json, Status status, Cache cache) {
    // They shouldn't be trying to list of an existing Post or Topic
    if (topic != null || post != null) {
        String error = "Can't list Topics inside an existing Topic or Post";
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }
    // Do we need to list or search?
    boolean tagSearch = false;
    String tag = req.getParameter("tag");
    if (tag != null && tag.length() > 0) {
        tagSearch = true;
        // Tags can be full unicode strings, so decode
        tag = URLDecoder.decode(tag);
    }
    // Get the topics
    boolean oldestTopicsFirst = false;
    PagingResults<TopicInfo> topics = null;
    PagingRequest paging = buildPagingRequest(req);
    if (tagSearch) {
        // Tag based is a search rather than a listing
        if (site != null) {
            topics = discussionService.findTopics(site.getShortName(), null, tag, oldestTopicsFirst, paging);
        } else {
            topics = discussionService.findTopics(nodeRef, null, tag, oldestTopicsFirst, paging);
        }
    } else {
        if (site != null) {
            topics = discussionService.listTopics(site.getShortName(), oldestTopicsFirst, paging);
        } else {
            topics = discussionService.listTopics(nodeRef, oldestTopicsFirst, buildPagingRequest(req));
        }
    }
    // been created yet, use the site for the permissions checking
    if (site != null && nodeRef == null) {
        nodeRef = site.getNodeRef();
    }
    // Build the common model parts
    Map<String, Object> model = buildCommonModel(site, topic, post, req);
    model.put("forum", nodeRef);
    // Have the topics rendered
    model.put("data", renderTopics(topics, paging, site));
    // All done
    return model;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.simple.JSONObject) TopicInfo(org.alfresco.service.cmr.discussion.TopicInfo) PagingRequest(org.alfresco.query.PagingRequest)

Aggregations

TopicInfo (org.alfresco.service.cmr.discussion.TopicInfo)9 JSONObject (org.json.simple.JSONObject)7 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)7 PagingRequest (org.alfresco.query.PagingRequest)5 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 Pair (org.alfresco.util.Pair)3 EmptyPagingResults (org.alfresco.query.EmptyPagingResults)2 PostInfo (org.alfresco.service.cmr.discussion.PostInfo)2 IOException (java.io.IOException)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 PagingResults (org.alfresco.query.PagingResults)1 MimetypeMap (org.alfresco.repo.content.MimetypeMap)1 TopicInfoImpl (org.alfresco.repo.discussion.TopicInfoImpl)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 StoreRef (org.alfresco.service.cmr.repository.StoreRef)1 ResultSet (org.alfresco.service.cmr.search.ResultSet)1