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
This commit is contained in:
Nick Burch
2011-08-15 16:03:41 +00:00
parent 8acb571ac5
commit d5de16b4ba
3 changed files with 141 additions and 156 deletions

View File

@@ -1,156 +0,0 @@
<import resource="classpath:alfresco/templates/webscripts/org/alfresco/repository/requestutils.lib.js">
<import resource="classpath:alfresco/templates/webscripts/org/alfresco/repository/discussions/topicpost.lib.js">
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();

View File

@@ -1577,6 +1577,12 @@
parent="abstractDiscussionWebScript">
</bean>
<!-- Updates a discussions post or topic -->
<bean id="webscript.org.alfresco.repository.discussions.posts.forum-post.put"
class="org.alfresco.repo.web.scripts.discussion.ForumPostPut"
parent="abstractDiscussionWebScript">
</bean>
<!-- Deletes a discussion post or topic -->
<bean id="webscript.org.alfresco.repository.discussions.posts.forum-post.delete"
class="org.alfresco.repo.web.scripts.discussion.ForumPostDelete"

View File

@@ -0,0 +1,135 @@
/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
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<String, Object> executeImpl(SiteInfo site, NodeRef nodeRef,
TopicInfo topic, PostInfo post, WebScriptRequest req, JSONObject json,
Status status, Cache cache)
{
// Build the common model parts
Map<String, Object> 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<tags.length(); i++)
{
topic.getTags().add( tags.getString(i) );
}
}
}
catch(JSONException e)
{
throw new WebScriptException("Invalid JSON: " + e.getMessage());
}
// Save the topic and the post
discussionService.updateTopic(topic);
discussionService.updatePost(post);
}
}