- new webscript for my forum topics

- proper pagination component
- cleanup of topic list page js code
- tag support for blog REST API (still disabled though)

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@9702 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Michael Ru
2008-07-09 15:20:48 +00:00
parent 4b1d7c48cb
commit 0d2114d4e0
9 changed files with 298 additions and 30 deletions

View File

@@ -1,4 +1,8 @@
<#macro renderTags tags>
[<#list tags as x>"${x?j_string}"<#if x_has_next>, </#if></#list>]
</#macro>
<#macro addContent item>
<#assign maxTextLength=512>
<#if (! contentFormat??) || contentFormat == "" || contentFormat == "full">
@@ -35,7 +39,7 @@
"modifiedOn" : "${item.modifiedDate?string("MMM dd yyyy HH:mm:ss 'GMT'Z '('zzz')'")}",
"permissions" : {"edit" : true, "publishExt" : true, "delete" : true},
"commentCount" : ${item.commentCount?c},
"tags" : ["ECM", "Design"],
"tags" : <@renderTags tags=item.tags />,
<#-- draft vs internal published -->
"isDraft" : ${item.isDraft?string},

View File

@@ -23,6 +23,69 @@ function setOrUpdateReleasedAndUpdatedDates(node)
}
}
function setTags(node, tags)
{
// convert tags, which is probably not of type array
var t = new Array();
for (var a=0; a < tags.length(); a++)
{
t.push(tags.get(a));
}
tags = t;
logger.log("set tags: " + tags);
// get current tags
var oldTags = node.tags;
if (oldTags == undefined)
{
oldTags = [];
}
// remove the tags that should be removed
for (var x=0; x < oldTags.length; x++)
{
var toRemove = true;
for (var y=0; y < tags.length; y++)
{
if (oldTags[x] == tags[y])
{
toRemove = false;
break;
}
}
if (toRemove)
{
logger.log("removing tag " + tags[x]);
oldTags.removeTag(oldTags[x]);
}
}
// add new
for (var x=0; x < tags.length; x++)
{
var toAdd = true;
for (var y=0; y < oldTags.length; y++)
{
if (tags[x] == oldTags[y])
{
toAdd = false;
break;
}
}
if (toAdd)
{
logger.log("adding tag " + tags[x]);
node.addTag(tags[x]);
}
else
{
logger.log("tag " + tags[x] + " already added.");
}
}
logger.log("finished setting tags");
}
/**
* Returns the data of a blog post.
*/
@@ -70,5 +133,15 @@ function getBlogPostData(node)
data.outOfDate = false;
}
// tags
if (node.tags != undefined)
{
data.tags = node.tags;
}
else
{
data.tags = [];
}
return data;
}

View File

@@ -32,17 +32,42 @@ function updateBlogPostDraftMode(postNode)
function updateBlogPost(postNode)
{
// fetch the new data
var title = "";
var title = null;
if (json.has("title"))
{
title = json.get("title");
}
var content = json.get("content");
var content = null;
if (json.has("content"))
{
content = json.get("content");
}
var tags = null;
if (json.has("tags"))
{
// get the tags JSONArray and copy it into a real javascript array object
var tmp = json.get("tags");
tags = new Array();
for (var x=0; x < tmp.length(); x++)
{
tags.push(tmp.get(x));
}
}
// update the node
if (title !== null)
{
postNode.properties.title = title;
}
if (content !== null)
{
postNode.mimetype = "text/html";
postNode.content = content;
postNode.content = content
}
/*if (tags !== null)
{
postNode.tags = tags;
}*/
postNode.save();
updateBlogPostDraftMode(postNode);

View File

@@ -39,7 +39,7 @@ function main()
var numdays = args["numdays"] != undefined ? parseInt(args["numdays"]) : DEFAULT_NUM_DAYS;
// fetch and assign the data
model.data = getBlogPostList(node, fromDate, toDate, index, count);
model.data = getBlogPostList(node, numdays, index, count);
model.contentFormat = (args["contentFormat"] != undefined) ? args["contentFormat"] : "full";
}

View File

@@ -8,17 +8,48 @@
function createBlogPost(blogNode)
{
// fetch the data required to create the post
var title = json.get("title");
var content = json.get("content");
var title = null;
if (json.has("title"))
{
title = json.get("title");
}
var content = null;
if (json.has("content"))
{
content = json.get("content");
}
var tags = null;
if (json.has("tags"))
{
// get the tags JSONArray and copy it into a real javascript array object
var tmp = json.get("tags");
tags = new Array();
for (var x=0; x < tmp.length(); x++)
{
tags.push(tmp.get(x));
}
}
// get a unique name
var nodeName = getUniqueChildName(blogNode, "post");
// we simply create a new file inside the blog folder
var postNode = blogNode.createNode(nodeName, "cm:content");
postNode.mimetype = "text/html";
// set values where supplied
if (title !== null)
{
postNode.properties.title = title;
}
if (content !== null)
{
postNode.mimetype = "text/html";
postNode.content = content;
}
/*if (tags !== null)
{
postNode.tags = tags;
}*/
postNode.save();
// check whether it is in draft mode

View File

@@ -0,0 +1,10 @@
<webscript>
<shortname>Forum my posts</shortname>
<description>Get the forum posts created by the current user</description>
<url>/forum/site/{site}/{container}/{path}/posts/myposts</url>
<url>/forum/site/{site}/{container}/posts/myposts</url>
<url>/forum/node/{store_type}/{store_id}/{id}/posts/myposts</url>
<format default="json"/>
<authentication>user</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -0,0 +1,39 @@
<import resource="classpath:alfresco/templates/webscripts/org/alfresco/repository/requestutils.lib.js">
<import resource="classpath:alfresco/templates/webscripts/org/alfresco/repository/generic-paged-results.lib.js">
<import resource="classpath:alfresco/templates/webscripts/org/alfresco/repository/discussions/topicpost.lib.js">
/**
* Fetches all posts added to the forum in the last numdays days
*/
function getTopicPostList(node, index, count)
{
// query information
var luceneQuery = "+TYPE:\"{http://www.alfresco.org/model/forum/1.0}topic\" " +
"+PATH:\"" + node.qnamePath + "/*\" " +
"+@cm\\:creator:\"" + person.properties.userName + "\"";
var sortAttribute = "@{http://www.alfresco.org/model/content/1.0}created";
// get the data
return getPagedResultsDataByLuceneQuery(node, luceneQuery, sortAttribute, false, index, count, getTopicPostData);
}
function main()
{
// get requested node
var node = getRequestNode();
if (status.getCode() != status.STATUS_OK)
{
return;
}
// process additional parameters
var index = args["startIndex"] != undefined ? parseInt(args["startIndex"]) : 0;
var count = args["pageSize"] != undefined ? parseInt(args["pageSize"]) : 10;
// fetch the data and assign it to the model
model.data = getTopicPostList(node, index, count);
model.contentFormat = (args["contentFormat"] != undefined) ? args["contentFormat"] : "full";
}
main();

View File

@@ -0,0 +1,5 @@
<#import "../topicpost.lib.ftl" as topicpostLib/>
<#import "../../generic-paged-results.lib.ftl" as gen/>
<@gen.pagedResults data=data ; item>
<@topicpostLib.topicpostJSON item=item />
</@gen.pagedResults>

View File

@@ -38,6 +38,7 @@ import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.util.PropertyMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.mock.web.MockHttpServletResponse;
@@ -141,13 +142,35 @@ public class BlogServiceTest extends BaseWebScriptTest
// Test helper methods
private JSONObject createPost(String title, String content, boolean isDraft, int expectedStatus)
private JSONObject getRequestObject(String title, String content, String[] tags, boolean isDraft)
throws Exception
{
JSONObject post = new JSONObject();
if (title != null)
{
post.put("title", title);
}
if (content != null)
{
post.put("content", content);
}
if (tags != null)
{
JSONArray arr = new JSONArray();
for(String s : tags)
{
arr.put(s);
}
post.put("tags", arr);
}
post.put("draft", isDraft);
return post;
}
private JSONObject createPost(String title, String content, String[] tags, boolean isDraft, int expectedStatus)
throws Exception
{
JSONObject post = getRequestObject(title, content, tags, isDraft);
MockHttpServletResponse response = postRequest(URL_BLOG_POSTS, expectedStatus, post.toString(), "application/json");
if (expectedStatus != 200)
@@ -169,13 +192,10 @@ public class BlogServiceTest extends BaseWebScriptTest
return item;
}
private JSONObject updatePost(String name, String title, String content, boolean isDraft, int expectedStatus)
private JSONObject updatePost(String name, String title, String content, String[] tags, boolean isDraft, int expectedStatus)
throws Exception
{
JSONObject post = new JSONObject();
post.put("title", title);
post.put("content", content);
post.put("draft", isDraft);
JSONObject post = getRequestObject(title, content, tags, isDraft);
MockHttpServletResponse response = putRequest(URL_BLOG_POST + name, expectedStatus, post.toString(), "application/json");
if (expectedStatus != 200)
@@ -255,7 +275,7 @@ public class BlogServiceTest extends BaseWebScriptTest
{
String title = "test";
String content = "test";
JSONObject item = createPost(title, content, true, 200);
JSONObject item = createPost(title, content, null, true, 200);
// check that the values
assertEquals(title, item.get("title"));
@@ -273,7 +293,7 @@ public class BlogServiceTest extends BaseWebScriptTest
String title = "published";
String content = "content";
JSONObject item = createPost(title, content, false, 200);
JSONObject item = createPost(title, content, null, false, 200);
// check the values
assertEquals(title, item.get("title"));
@@ -286,24 +306,50 @@ public class BlogServiceTest extends BaseWebScriptTest
this.authenticationComponent.setCurrentUser(USER_ONE);
}
public void testCreateEmptyPost() throws Exception
{
JSONObject item = createPost(null, null, null, false, 200);
// check the values
assertEquals("", item.get("title"));
assertEquals("", item.get("content"));
assertEquals(false, item.get("isDraft"));
// check that user two has access to it as well
this.authenticationComponent.setCurrentUser(USER_TWO);
getPost(item.getString("name"), 200);
this.authenticationComponent.setCurrentUser(USER_ONE);
}
public void testUpdated() throws Exception
{
JSONObject item = createPost("test", "test", false, 200);
JSONObject item = createPost("test", "test", null, false, 200);
String name = item.getString("name");
assertEquals(false, item.getBoolean("isUpdated"));
// wait for 5 sec
Thread.sleep(5000);
item = updatePost(name, "new title", "new content", false, 200);
item = updatePost(name, "new title", "new content", null, false, 200);
assertEquals(true, item.getBoolean("isUpdated"));
assertEquals("new title", item.getString("title"));
assertEquals("new content", item.getString("content"));
}
public void testUpdateWithEmptyValues() throws Exception
{
JSONObject item = createPost("test", "test", null, false, 200);
String name = item.getString("name");
assertEquals(false, item.getBoolean("isUpdated"));
item = updatePost(item.getString("name"), null, null, null, false, 200);
assertEquals("test", item.getString("title"));
assertEquals("test", item.getString("content"));
}
public void testPublishThroughUpdate() throws Exception
{
JSONObject item = createPost("test", "test", true, 200);
JSONObject item = createPost("test", "test", null, true, 200);
String name = item.getString("name");
assertEquals(true, item.getBoolean("isDraft"));
@@ -312,7 +358,7 @@ public class BlogServiceTest extends BaseWebScriptTest
getPost(name, 404);
this.authenticationComponent.setCurrentUser(USER_ONE);
item = updatePost(name, "new title", "new content", false, 200);
item = updatePost(name, "new title", "new content", null, false, 200);
assertEquals("new title", item.getString("title"));
assertEquals("new content", item.getString("content"));
assertEquals(false, item.getBoolean("isDraft"));
@@ -325,11 +371,11 @@ public class BlogServiceTest extends BaseWebScriptTest
public void testCannotDoUnpublish() throws Exception
{
JSONObject item = createPost("test", "test", false, 200);
JSONObject item = createPost("test", "test", null, false, 200);
String name = item.getString("name");
assertEquals(false, item.getBoolean("isDraft"));
item = updatePost(name, "new title", "new content", true, 400); // should return bad request
item = updatePost(name, "new title", "new content", null, true, 400); // should return bad request
}
public void testGetAll() throws Exception
@@ -342,6 +388,16 @@ public class BlogServiceTest extends BaseWebScriptTest
assertEquals(this.posts.size() + this.drafts.size(), result.getInt("total"));
}
public void testGetNew() throws Exception
{
String url = URL_BLOG_POSTS + "/new";
MockHttpServletResponse response = getRequest(url, 200);
JSONObject result = new JSONObject(response.getContentAsString());
// we should have posts.size
assertEquals(this.posts.size(), result.getInt("total"));
}
public void _testGetDrafts() throws Exception
{
String url = URL_BLOG_POSTS + "/mydrafts";
@@ -379,7 +435,7 @@ public class BlogServiceTest extends BaseWebScriptTest
public void testComments() throws Exception
{
JSONObject item = createPost("test", "test", false, 200);
JSONObject item = createPost("test", "test", null, false, 200);
String name = item.getString("name");
String nodeRef = item.getString("nodeRef");
@@ -409,4 +465,29 @@ public class BlogServiceTest extends BaseWebScriptTest
assertEquals("new title", commentTwoUpdated.getString("title"));
assertEquals("new content", commentTwoUpdated.getString("content"));
}
public void _testPostTags() throws Exception
{
String[] tags = { "First", "Test" };
JSONObject item = createPost("tagtest", "tagtest", tags, false, 200);
assertEquals(2, item.getJSONArray("tags").length());
assertEquals("First", item.getJSONArray("tags").get(0));
assertEquals("Test", item.getJSONArray("tags").get(0));
item = updatePost(item.getString("name"), null, null, new String[] { "First", "Test", "Second" }, false, 200);
assertEquals(3, item.getJSONArray("tags").length());
assertEquals("First", item.getJSONArray("tags").get(0));
assertEquals("Test", item.getJSONArray("tags").get(0));
assertEquals("Second", item.getJSONArray("tags").get(0));
}
public void _testClearTags() throws Exception
{
String[] tags = { "abc", "def"};
JSONObject item = createPost("tagtest", "tagtest", tags, false, 200);
assertEquals(2, item.getJSONArray("tags").length());
item = updatePost(item.getString("name"), null, null, new String[0], false, 200);
assertEquals(0, item.getJSONArray("tags").length());
}
}