ALF-9153 Convert the forum-post-replies.post discussions webscript to be lucene free and Java backed

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29780 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2011-08-15 17:54:30 +00:00
parent 5ae9918204
commit 12575caa53
8 changed files with 129 additions and 104 deletions

View File

@@ -11,6 +11,7 @@ function getTopicPostList(node, tag, index, count)
var luceneQuery = " +TYPE:\"{http://www.alfresco.org/model/forum/1.0}topic\"" +
" +PATH:\"" + node.qnamePath + "/*\" ";
var sortAttribute = "@{http://www.alfresco.org/model/content/1.0}published";
//var sortAttribute = "@{http://www.alfresco.org/model/content/1.0}created"; // TODO Switch to this
// is a tag selected?
if (tag != null)

View File

@@ -1,99 +0,0 @@
<import resource="classpath:alfresco/templates/webscripts/org/alfresco/repository/requestutils.lib.js">
<import resource="classpath:alfresco/templates/webscripts/org/alfresco/repository/nodenameutils.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";
/**
* Creates a post inside the passed forum node.
*/
function createPostReplyImpl(topicNode, parentPostNode)
{
// fetch the data required to create a topic
var title = "";
if (json.has("title"))
{
title = json.get("title");
}
var content = "";
if (json.has("content"))
{
content = json.get("content");
}
// create the post node using a unique name
var name = getUniqueChildName(topicNode, "post");
var postNode = topicNode.createNode(name, "fm:post");
postNode.mimetype = "text/html";
postNode.properties.title = title;
postNode.content = content;
postNode.save();
// add the cm:syndication aspect
var props = new Array();
props[PROP_PUBLISHED] = new Date();
postNode.addAspect(ASPECT_SYNDICATION, props);
// link it to the parent post
postNode.addAspect("cm:referencing");
postNode.createAssociation(parentPostNode, "cm:references");
postNode.save(); // probably not necessary here
return getReplyPostData(postNode);
}
/**
* Creates a reply to a post.
* @param node The parent post node
*/
function createPostReply(node)
{
// we have to differentiate here whether this is a top-level post or a reply
if (node.type == "{http://www.alfresco.org/model/forum/1.0}topic")
{
// find the primary post node
var topic = node;
var post = findPostNode(node);
return createPostReplyImpl(topic, post);
}
else if (node.type == "{http://www.alfresco.org/model/forum/1.0}post")
{
// the forum is the parent of the node
var topic = node.parent;
var post = node;
return createPostReplyImpl(topic, post);
}
else
{
status.setCode(STATUS_BAD_REQUEST, "Incompatible node type. Required either fm:topic or fm:post. Received: " + node.type);
return null;
}
}
function main()
{
// get requested node
var node = getRequestNode();
if (status.getCode() != status.STATUS_OK)
{
return;
}
model.postData = createPostReply(node);
// add an activity item
if (json.has("site") && json.has("page"))
{
// fetch the topic (and with it the root post
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.reply-created", json.get("site"), "discussions", jsonUtils.toJSONString(data));
}
}
main();

View File

@@ -38,6 +38,7 @@ function getOrderedPosts(topic)
var query = " +TYPE:\"{http://www.alfresco.org/model/forum/1.0}post\"" +
" +PATH:\"" + topic.qnamePath + "/*\" ";
var sortAttribute = "@{http://www.alfresco.org/model/content/1.0}published";
//var sortAttribute = "@{http://www.alfresco.org/model/content/1.0}created"; // TODO Switch to this
return search.luceneSearch(topic.nodeRef.storeRef.toString(), query, sortAttribute, true) ;
}

View File

@@ -1589,6 +1589,12 @@
parent="abstractDiscussionWebScript">
</bean>
<!-- Creates a new discussions reply post for a topic -->
<bean id="webscript.org.alfresco.repository.discussions.posts.forum-post-replies.post"
class="org.alfresco.repo.web.scripts.discussion.ForumPostRepliesPost"
parent="abstractDiscussionWebScript">
</bean>
<!-- Lists the Discussions for a site -->
<!--
<bean id="webscript.org.alfresco.repository.discussions.forum.forum-posts.get"

View File

@@ -66,6 +66,7 @@ public abstract class AbstractDiscussionWebScript extends DeclarativeWebScript
private static Log logger = LogFactory.getLog(AbstractDiscussionWebScript.class);
protected static final String KEY_POSTDATA = "postData";
protected static final String KEY_IS_TOPIC_POST = "isTopicPost";
protected static final String KEY_TOPIC = "topic";
protected static final String KEY_POST = "post";

View File

@@ -38,8 +38,6 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
*/
public class ForumPostGet 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,

View File

@@ -33,15 +33,13 @@ 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.
* This class is the controller for the discussions page editing 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,

View File

@@ -0,0 +1,119 @@
/*
* 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.model.ContentModel;
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.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 creating forum-post-replies.post webscript.
*
* @author Nick Burch
* @since 4.0
*/
public class ForumPostRepliesPost extends AbstractDiscussionWebScript
{
@Override
protected Map<String, Object> executeImpl(SiteInfo site, NodeRef nodeRef,
TopicInfo topic, PostInfo post, WebScriptRequest req, JSONObject json,
Status status, Cache cache)
{
// If they're trying to create a reply to a topic, they actually
// 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;
}
private PostInfo doCreatePost(PostInfo post, TopicInfo topic, WebScriptRequest req,
JSONObject json)
{
// Fetch the details from the JSON
String title = null;
String contents = null;
try
{
// Grab the data
if(json.has("title"))
{
title = json.getString("title");
}
if(json.has("content"))
{
contents = json.getString("content");
}
}
catch(JSONException e)
{
throw new WebScriptException("Invalid JSON: " + e.getMessage());
}
// 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;
}
}