Search in sources :

Example 6 with TopicInfo

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

the class ForumTopicsRecentGet 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);
    }
    // Grab the date range to search over
    String numDaysS = req.getParameter("numdays");
    int numDays = RECENT_SEARCH_PERIOD_DAYS;
    if (numDaysS != null) {
        numDays = Integer.parseInt(numDaysS);
    }
    Date now = new Date();
    Date from = new Date(now.getTime() - numDays * ONE_DAY_MS);
    Date to = new Date(now.getTime() + ONE_DAY_MS);
    // Get the recent topics, newest first
    PagingResults<TopicInfo> topics = null;
    PagingRequest paging = buildPagingRequest(req);
    if (site != null) {
        topics = discussionService.listTopics(site.getShortName(), from, to, false, paging);
    } else {
        topics = discussionService.listTopics(nodeRef, from, to, false, paging);
    }
    // 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) Date(java.util.Date) TopicInfo(org.alfresco.service.cmr.discussion.TopicInfo) PagingRequest(org.alfresco.query.PagingRequest)

Example 7 with TopicInfo

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

the class AbstractDiscussionWebScript method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    if (templateVars == null) {
        String error = "No parameters supplied";
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }
    // Parse the JSON, if supplied
    JSONObject json = null;
    String contentType = req.getContentType();
    if (contentType != null && contentType.indexOf(';') != -1) {
        contentType = contentType.substring(0, contentType.indexOf(';'));
    }
    if (MimetypeMap.MIMETYPE_JSON.equals(contentType)) {
        JSONParser parser = new JSONParser();
        try {
            json = (JSONObject) parser.parse(req.getContent().getContent());
        } catch (IOException io) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + io.getMessage());
        } catch (ParseException pe) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + pe.getMessage());
        }
    }
    // Did they request it by node reference or site?
    NodeRef nodeRef = null;
    SiteInfo site = null;
    TopicInfo topic = null;
    PostInfo post = null;
    if (templateVars.containsKey("site")) {
        // Site, and optionally topic
        String siteName = templateVars.get("site");
        site = siteService.getSite(siteName);
        if (site == null) {
            String error = "Could not find site: " + siteName;
            throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
        }
        // Did they give a topic name too?
        if (templateVars.containsKey("path")) {
            String name = templateVars.get("path");
            topic = discussionService.getTopic(site.getShortName(), name);
            if (topic == null) {
                String error = "Could not find topic '" + name + "' for site '" + site.getShortName() + "'";
                throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
            }
            nodeRef = topic.getNodeRef();
        } else {
            // The NodeRef is the container (if it exists)
            if (siteService.hasContainer(siteName, DiscussionServiceImpl.DISCUSSION_COMPONENT)) {
                nodeRef = siteService.getContainer(siteName, DiscussionServiceImpl.DISCUSSION_COMPONENT);
            }
        }
    } else if (templateVars.containsKey("store_type") && templateVars.containsKey("store_id") && templateVars.containsKey("id")) {
        // NodeRef, normally Topic or Discussion
        StoreRef store = new StoreRef(templateVars.get("store_type"), templateVars.get("store_id"));
        nodeRef = new NodeRef(store, templateVars.get("id"));
        if (!nodeService.exists(nodeRef)) {
            String error = "Could not find node: " + nodeRef;
            throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
        }
        // Try to build the appropriate object for it
        Pair<TopicInfo, PostInfo> objects = discussionService.getForNodeRef(nodeRef);
        if (objects != null) {
            topic = objects.getFirst();
            post = objects.getSecond();
        }
        // See if it's actually attached to a site
        if (topic != null) {
            NodeRef container = topic.getContainerNodeRef();
            if (container != null) {
                NodeRef maybeSite = nodeService.getPrimaryParent(container).getParentRef();
                if (maybeSite != null) {
                    // Try to make it a site, will return Null if it isn't one
                    site = siteService.getSite(maybeSite);
                }
            }
        }
    } else {
        String error = "Unsupported template parameters found";
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }
    // Have the real work done
    return executeImpl(site, nodeRef, topic, post, req, json, status, cache);
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) StoreRef(org.alfresco.service.cmr.repository.StoreRef) IOException(java.io.IOException) TopicInfo(org.alfresco.service.cmr.discussion.TopicInfo) NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) PostInfo(org.alfresco.service.cmr.discussion.PostInfo) ParseException(org.json.simple.parser.ParseException) Pair(org.alfresco.util.Pair)

Example 8 with TopicInfo

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

the class ForumTopicsHotGet 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);
    }
    // Grab the date range to search over
    String numDaysS = req.getParameter("numdays");
    int numDays = RECENT_POSTS_PERIOD_DAYS;
    if (numDaysS != null) {
        numDays = Integer.parseInt(numDaysS);
    }
    Date now = new Date();
    Date since = new Date(now.getTime() - numDays * ONE_DAY_MS);
    // Get the topics with recent replies
    PagingResults<Pair<TopicInfo, Integer>> topicsAndCounts = null;
    PagingRequest paging = buildPagingRequest(req);
    if (site != null) {
        topicsAndCounts = discussionService.listHotTopics(site.getShortName(), since, paging);
    } else {
        topicsAndCounts = discussionService.listHotTopics(nodeRef, since, paging);
    }
    // For this, we actually only need the topics, not their counts too
    List<TopicInfo> topics = new ArrayList<TopicInfo>();
    for (Pair<TopicInfo, Integer> tc : topicsAndCounts.getPage()) {
        topics.add(tc.getFirst());
    }
    // 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, topicsAndCounts.getTotalResultCount(), paging, site));
    // All done
    return model;
}
Also used : ArrayList(java.util.ArrayList) Date(java.util.Date) PagingRequest(org.alfresco.query.PagingRequest) TopicInfo(org.alfresco.service.cmr.discussion.TopicInfo) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.simple.JSONObject) Pair(org.alfresco.util.Pair)

Example 9 with TopicInfo

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

the class ForumTopicsMineGet 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);
    }
    // Grab the current user to list for
    String username = AuthenticationUtil.getFullyAuthenticatedUser();
    // Get the topics for the user, oldest first
    PagingResults<TopicInfo> topics = null;
    PagingRequest paging = buildPagingRequest(req);
    if (site != null) {
        topics = discussionService.listTopics(site.getShortName(), username, true, paging);
    } else {
        topics = discussionService.listTopics(nodeRef, username, true, paging);
    }
    // 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