Search in sources :

Example 1 with PostInfo

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

the class AbstractDiscussionWebScript method renderTopic.

/*
     * Was topicpost.lib.js getTopicPostData / getTopicPostDataFromTopicAndPosts
     * 
     * TODO Switch the FTL to prefer the Info object rather than the ScriptNode
     */
protected Map<String, Object> renderTopic(TopicInfo topic, SiteInfo site) {
    // Fetch the primary post
    PostInfo primaryPost = discussionService.getPrimaryPost(topic);
    if (primaryPost == null) {
        throw new WebScriptException(Status.STATUS_PRECONDITION_FAILED, "First (primary) post was missing from the topic, can't fetch");
    }
    // Fetch the most recent reply
    PostInfo mostRecentPost = discussionService.getMostRecentPost(topic);
    // Find out how many replies there are
    int numReplies;
    if (mostRecentPost.getNodeRef().equals(primaryPost.getNodeRef())) {
        // Only the one post in the topic
        mostRecentPost = null;
        numReplies = 0;
    } else {
        // Use this trick to get the number of posts in the topic,
        // but without needing to get lots of data and objects
        PagingRequest paging = new PagingRequest(1);
        paging.setRequestTotalCountMax(MAX_QUERY_ENTRY_COUNT);
        PagingResults<PostInfo> posts = discussionService.listPosts(topic, paging);
        // The primary post is in the list, so exclude from the reply count
        numReplies = posts.getTotalResultCount().getFirst() - 1;
    }
    // Build the details
    Map<String, Object> item = new HashMap<String, Object>();
    item.put(KEY_IS_TOPIC_POST, true);
    item.put(KEY_TOPIC, topic.getNodeRef());
    item.put(KEY_POST, primaryPost.getNodeRef());
    item.put(KEY_CAN_EDIT, canUserEditPost(primaryPost, site));
    item.put(KEY_AUTHOR, buildPerson(topic.getCreator()));
    // The reply count is one less than all posts (first is the primary one)
    item.put("totalReplyCount", numReplies);
    // Add the topic site
    item.put("site", topic.getShortSiteName());
    // We want details on the most recent post
    if (mostRecentPost != null) {
        item.put("lastReply", mostRecentPost.getNodeRef());
        item.put("lastReplyBy", buildPerson(mostRecentPost.getCreator()));
    }
    // Include the tags
    item.put("tags", topic.getTags());
    // All done
    return item;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) PostInfo(org.alfresco.service.cmr.discussion.PostInfo) JSONObject(org.json.simple.JSONObject) PagingRequest(org.alfresco.query.PagingRequest)

Example 2 with PostInfo

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

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

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

the class ForumPostRepliesPost method executeImpl.

@Override
protected Map<String, Object> executeImpl(SiteInfo site, NodeRef nodeRef, TopicInfo topic, PostInfo post, WebScriptRequest req, JSONObject json, Status status, Cache cache) {
    // mean to create the reply on the primary post
    if (post == null) {
        post = discussionService.getPrimaryPost(topic);
        if (post == null) {
            throw new WebScriptException(Status.STATUS_PRECONDITION_FAILED, "First (primary) post was missing from the topic, can't fetch");
        }
    } else if (topic == null) {
        String error = "Node was of the wrong type, only Topic and Post are supported";
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }
    // Have the reply created
    PostInfo reply = doCreatePost(post, topic, req, json);
    // Add the activity entry for the reply change
    addActivityEntry("reply", "created", topic, reply, site, req, json);
    // Build the common model parts
    Map<String, Object> model = buildCommonModel(site, topic, reply, req);
    // Build the JSON for the new reply post
    model.put(KEY_POSTDATA, renderPost(reply, site));
    // All done
    return model;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) PostInfo(org.alfresco.service.cmr.discussion.PostInfo) JSONObject(org.json.simple.JSONObject)

Example 5 with PostInfo

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

the class ForumPostRepliesPost method doCreatePost.

private PostInfo doCreatePost(PostInfo post, TopicInfo topic, WebScriptRequest req, JSONObject json) {
    // Fetch the details from the JSON
    String title = null;
    if (json.containsKey("title")) {
        title = (String) json.get("title");
    }
    String contents = null;
    if (json.containsKey("content")) {
        contents = (String) json.get("content");
    }
    // Create the reply
    PostInfo reply = discussionService.createReply(post, contents);
    // Set the title if needed (it normally isn't)
    if (title != null && title.length() > 0) {
        nodeService.setProperty(reply.getNodeRef(), ContentModel.PROP_TITLE, title);
        reply = discussionService.getPost(topic, reply.getSystemName());
    }
    // All done
    return reply;
}
Also used : PostInfo(org.alfresco.service.cmr.discussion.PostInfo)

Aggregations

PostInfo (org.alfresco.service.cmr.discussion.PostInfo)5 JSONObject (org.json.simple.JSONObject)3 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)3 TopicInfo (org.alfresco.service.cmr.discussion.TopicInfo)2 Pair (org.alfresco.util.Pair)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 EmptyPagingResults (org.alfresco.query.EmptyPagingResults)1 PagingRequest (org.alfresco.query.PagingRequest)1 PagingResults (org.alfresco.query.PagingResults)1 TopicInfoImpl (org.alfresco.repo.discussion.TopicInfoImpl)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 StoreRef (org.alfresco.service.cmr.repository.StoreRef)1 ResultSetRow (org.alfresco.service.cmr.search.ResultSetRow)1 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)1 JSONParser (org.json.simple.parser.JSONParser)1