From d5de16b4ba9a53fd342f77a04035c4c9b3b77f38 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 15 Aug 2011 16:03:41 +0000 Subject: [PATCH] ALF-9153 Convert the discussions single post update webscript to de-lucened java backed git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29772 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../discussions/posts/forum-post.put.json.js | 156 ------------------ .../web-scripts-application-context.xml | 6 + .../web/scripts/discussion/ForumPostPut.java | 135 +++++++++++++++ 3 files changed, 141 insertions(+), 156 deletions(-) delete mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/discussions/posts/forum-post.put.json.js create mode 100644 source/java/org/alfresco/repo/web/scripts/discussion/ForumPostPut.java diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/discussions/posts/forum-post.put.json.js b/config/alfresco/templates/webscripts/org/alfresco/repository/discussions/posts/forum-post.put.json.js deleted file mode 100644 index 142dc26b6b..0000000000 --- a/config/alfresco/templates/webscripts/org/alfresco/repository/discussions/posts/forum-post.put.json.js +++ /dev/null @@ -1,156 +0,0 @@ - - - -const ASPECT_SYNDICATION = "cm:syndication"; -const PROP_PUBLISHED = "cm:published"; -const PROP_UPDATED = "cm:updated"; - -/** - * Updates the passed forum post node. - * @param topic the topic node if the post is the top level post - * @param post the post node. - */ -function updatePost(topic, post) -{ - var title = ""; - if (json.has("title")) - { - title = json.get("title"); - } - var content = ""; - if (json.has("content")) - { - content = json.get("content"); - } - var tags = []; - if (json.has("tags")) - { - // get the tags JSONArray and copy it into a real javascript array object - var tmp = json.get("tags"); - for (var x=0; x < tmp.length(); x++) - { - tags.push(tmp.get(x)); - } - } - - // update the topic title - post.properties["cm:title"] = title; - if(title != undefined && title.length > 0 && topic != undefined) - { - topic.properties["cm:title"] = title; - topic.save(); - } - - // make sure the syndication aspect has been added - if (! post.hasAspect(ASPECT_SYNDICATION)) - { - var params = []; - params[PROP_PUBLISHED] = new Date(); - params[PROP_UPDATED] = params[PROP_PUBLISHED]; - post.addAspect(ASPECT_SYNDICATION, params); - } - else - { - post.properties[PROP_UPDATED] = new Date(); - } - post.mimetype = "text/html"; - post.content = content; - post.save(); - - // Only set the tags if it is a topic post - // as we currently don't support individual post tagging - if (topic != null) - { - topic.tags = tags; - } -} - -function main() -{ - // get requested node - var node = getRequestNode(); - if (status.getCode() != status.STATUS_OK) - { - return; - } - - // find the post node if this is a topic node - var topicNode = null; - var postNode = null; - var updatedTopic = false; - if (node.type == "{http://www.alfresco.org/model/forum/1.0}post") - { - postNode = node; - topicNode = node.parent; - updatedTopic = false; - } - else if (node.type == "{http://www.alfresco.org/model/forum/1.0}topic") - { - topicNode = node; - updatedTopic = true; - - var nodes = getOrderedPosts(node); - if (nodes.length > 0) - { - postNode = nodes[0]; - } - else - { - status.setCode(status.STATUS_INTERNAL_SERVER_ERROR, "First post of topic node" + node.nodeRef + " missing"); - return; - } - } - else - { - status.setCode(STATUS_BAD_REQUEST, "Incompatible node type. Required either fm:topic or fm:post. Received: " + node.type); - return; - } - - // update - updatePost(topicNode, postNode); - - // Due to https://issues.alfresco.com/browse/ALFCOM-1775 - // we have to reuse the search results from before altering the nodes, - // that's why we don't use the function fetchPostData here (which would - // do another lucene search in case of a topic post - if (! updatedTopic) - { - model.postData = getReplyPostData(postNode); - - // add an activity item - if (json.has("site") && json.has("page")) - { - var topicData = getTopicPostData(model.postData.post.parent); - var data = - { - title: topicData.post.properties.title, - page: json.get("page") + "?topicId=" + topicData.topic.name, - params: - { - topicId: topicData.topic.name - } - } - activities.postActivity("org.alfresco.discussions.reply-updated", json.get("site"), "discussions", jsonUtils.toJSONString(data)); - } - } - else - { - // we will do the search here as we have to reuse the lucene result later - // See above, use getTopicPostDataFromTopicAndPosts instead of getTopicPostData - //model.topicpost = getTopicPostData(node); - model.postData = getTopicPostDataFromTopicAndPosts(topicNode, nodes); - - // add an activity item - if (json.has("site") && json.has("page")) - { - var topicData = getTopicPostData(model.postData.post.parent); - var data = - { - title: topicData.post.properties.title, - page: json.get("page") + "?topicId=" + topicData.topic.name - } - activities.postActivity("org.alfresco.discussions.post-updated", json.get("site"), "discussions", jsonUtils.toJSONString(data)); - } - } -} -main(); diff --git a/config/alfresco/web-scripts-application-context.xml b/config/alfresco/web-scripts-application-context.xml index d776a0d924..7ed2aa54c5 100644 --- a/config/alfresco/web-scripts-application-context.xml +++ b/config/alfresco/web-scripts-application-context.xml @@ -1577,6 +1577,12 @@ parent="abstractDiscussionWebScript"> + + + + . + */ +package org.alfresco.repo.web.scripts.discussion; + +import java.util.Map; + +import org.alfresco.service.cmr.discussion.PostInfo; +import org.alfresco.service.cmr.discussion.TopicInfo; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.site.SiteInfo; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.springframework.extensions.webscripts.Cache; +import org.springframework.extensions.webscripts.Status; +import org.springframework.extensions.webscripts.WebScriptException; +import org.springframework.extensions.webscripts.WebScriptRequest; + +/** + * This class is the controller for the discussions page fetching forum-post.put webscript. + * + * @author Nick Burch + * @since 4.0 + */ +public class ForumPostPut extends AbstractDiscussionWebScript +{ + private static final String KEY_POSTDATA = "postData"; + + @Override + protected Map executeImpl(SiteInfo site, NodeRef nodeRef, + TopicInfo topic, PostInfo post, WebScriptRequest req, JSONObject json, + Status status, Cache cache) + { + // Build the common model parts + Map model = buildCommonModel(site, topic, post, req); + + // Did they want to change a reply or the whole topic? + if(post != null) + { + // Update the specified post + doUpdatePost(post, post.getTopic(), req, json); + + // Add the activity entry for the reply change + addActivityEntry("reply", "updated", post.getTopic(), post, site, req, json); + + // Build the JSON for just this post + model.put(KEY_POSTDATA, renderPost(post, site)); + } + else if(topic != null) + { + // Update the primary post of the topic + 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"); + } + doUpdatePost(post, topic, req, json); + + // Add the activity entry for the topic change + addActivityEntry("post", "updated", topic, null, site, req, json); + + // Build the JSON for the whole topic + model.put(KEY_POSTDATA, renderTopic(topic, site)); + } + else + { + String error = "Node was of the wrong type, only Topic and Post are supported"; + throw new WebScriptException(Status.STATUS_BAD_REQUEST, error); + } + + // All done + return model; + } + + private void doUpdatePost(PostInfo post, TopicInfo topic, WebScriptRequest req, + JSONObject json) + { + // Fetch the details from the JSON + try + { + // Update the titles on the post and it's topic + if(json.has("title")) + { + String title = json.getString("title"); + post.setTitle(title); + if(title.length() > 0) + { + topic.setTitle(title); + } + } + + // Contents is on the post + if(json.has("content")) + { + post.setContents(json.getString("content")); + } + + // Tags are on the topic + if(json.has("tags")) + { + topic.getTags().clear(); + JSONArray tags = json.getJSONArray("tags"); + for(int i=0; i