diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/discussions/forum/forum-posts.get.js b/config/alfresco/templates/webscripts/org/alfresco/repository/discussions/forum/forum-posts.get.js deleted file mode 100644 index 4c3044b8e5..0000000000 --- a/config/alfresco/templates/webscripts/org/alfresco/repository/discussions/forum/forum-posts.get.js +++ /dev/null @@ -1,52 +0,0 @@ - - - - -/** - * Fetches all posts found in the forum. - */ -function getTopicPostList(node, tag, index, count) -{ - // query information - 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) - { - luceneQuery += " +PATH:\"/cm:taggable/cm:" + search.ISO9075Encode(tag) + "/member\" "; - } - - // 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; - - // selected tag - var tag = (args["tag"] != undefined && args["tag"].length > 0) ? args["tag"] : null; - - model.data = getTopicPostList(node, tag, index, count); - - // fetch the contentLength param - var contentLength = args["contentLength"] != undefined ? parseInt(args["contentLength"]) : -1; - model.contentLength = isNaN(contentLength) ? -1 : contentLength; - - // also set the forum node - model.forum = node; -} - -main(); diff --git a/config/alfresco/web-scripts-application-context.xml b/config/alfresco/web-scripts-application-context.xml index 523d71c2e9..aae3ffa14b 100644 --- a/config/alfresco/web-scripts-application-context.xml +++ b/config/alfresco/web-scripts-application-context.xml @@ -1608,13 +1608,11 @@ - - ---> diff --git a/source/java/org/alfresco/repo/web/scripts/discussion/AbstractDiscussionWebScript.java b/source/java/org/alfresco/repo/web/scripts/discussion/AbstractDiscussionWebScript.java index 5349e9fa60..c4500f1332 100644 --- a/source/java/org/alfresco/repo/web/scripts/discussion/AbstractDiscussionWebScript.java +++ b/source/java/org/alfresco/repo/web/scripts/discussion/AbstractDiscussionWebScript.java @@ -19,12 +19,15 @@ package org.alfresco.repo.web.scripts.discussion; import java.io.IOException; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.alfresco.query.PagingRequest; import org.alfresco.query.PagingResults; import org.alfresco.repo.content.MimetypeMap; +import org.alfresco.repo.discussion.DiscussionServiceImpl; import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.site.SiteServiceImpl; import org.alfresco.service.cmr.activities.ActivityService; @@ -157,7 +160,7 @@ public abstract class AbstractDiscussionWebScript extends DeclarativeWebScript } PagingRequest paging = new PagingRequest( startIndex, pageSize ); - paging.setRequestTotalCountMax( Math.max(10,startIndex+1) * pageSize ); + paging.setRequestTotalCountMax( Math.max(10*pageSize,startIndex+2*pageSize) ); return paging; } @@ -350,6 +353,33 @@ public abstract class AbstractDiscussionWebScript extends DeclarativeWebScript return item; } + /* + * Renders out the list of topics + * TODO Fetch the post data in one go, rather than one at a time + */ + protected Map renderTopics(PagingResults topics, + PagingRequest paging, SiteInfo site) + { + Map model = new HashMap(); + + // Paging info + model.put("total", topics.getTotalResultCount().getFirst()); + model.put("pageSize", paging.getMaxItems()); + model.put("startIndex", paging.getSkipCount()); + model.put("itemCount", topics.getPage().size()); + + // Data + List> items = new ArrayList>(); + for(TopicInfo topic : topics.getPage()) + { + items.add(renderTopic(topic, site)); + } + model.put("items", items); + + // All done + return model; + } + protected Map buildCommonModel(SiteInfo site, TopicInfo topic, PostInfo post, WebScriptRequest req) { @@ -452,6 +482,14 @@ public abstract class AbstractDiscussionWebScript extends DeclarativeWebScript } 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") && diff --git a/source/java/org/alfresco/repo/web/scripts/discussion/DiscussionRestApiTest.java b/source/java/org/alfresco/repo/web/scripts/discussion/DiscussionRestApiTest.java index bc38481d5f..e8f5930095 100644 --- a/source/java/org/alfresco/repo/web/scripts/discussion/DiscussionRestApiTest.java +++ b/source/java/org/alfresco/repo/web/scripts/discussion/DiscussionRestApiTest.java @@ -1054,11 +1054,8 @@ public class DiscussionRestApiTest extends BaseWebScriptTest assertEquals(3, result.getInt("total")); assertEquals(3, result.getInt("itemCount")); assertEquals(3, result.getJSONArray("items").length()); - // TODO This appears to be incorrect... - assertEquals("NodeTitle3", result.getJSONArray("items").getJSONObject(0).getString("title")); - assertEquals("NodeTitle2", result.getJSONArray("items").getJSONObject(1).getString("title")); -// assertEquals("NodeTitle2", result.getJSONArray("items").getJSONObject(0).getString("title")); -// assertEquals("NodeTitle3", result.getJSONArray("items").getJSONObject(1).getString("title")); + assertEquals("NodeTitle2", result.getJSONArray("items").getJSONObject(0).getString("title")); + assertEquals("NodeTitle3", result.getJSONArray("items").getJSONObject(1).getString("title")); assertEquals("NodeTitle1", result.getJSONArray("items").getJSONObject(2).getString("title")); assertEquals(0, result.getJSONArray("items").getJSONObject(0).getInt("replyCount")); assertEquals(0, result.getJSONArray("items").getJSONObject(1).getInt("replyCount")); @@ -1141,9 +1138,7 @@ public class DiscussionRestApiTest extends BaseWebScriptTest assertEquals(3, result.getInt("total")); assertEquals(1, result.getInt("itemCount")); assertEquals(1, result.getJSONArray("items").length()); - // TODO This appears to be incorrect... - assertEquals("NodeTitle3", result.getJSONArray("items").getJSONObject(0).getString("title")); -// assertEquals("NodeTitle2", result.getJSONArray("items").getJSONObject(0).getString("title")); + assertEquals("NodeTitle2", result.getJSONArray("items").getJSONObject(0).getString("title")); assertEquals(0, result.getJSONArray("items").getJSONObject(0).getInt("replyCount")); } diff --git a/source/java/org/alfresco/repo/web/scripts/discussion/ForumTopicPost.java b/source/java/org/alfresco/repo/web/scripts/discussion/ForumTopicPost.java index 70958bf80f..b97c0ce9a7 100644 --- a/source/java/org/alfresco/repo/web/scripts/discussion/ForumTopicPost.java +++ b/source/java/org/alfresco/repo/web/scripts/discussion/ForumTopicPost.java @@ -35,7 +35,7 @@ import org.springframework.extensions.webscripts.WebScriptException; import org.springframework.extensions.webscripts.WebScriptRequest; /** - * This class is the controller for the discussions page editing forum-posts.postt webscript. + * This class is the controller for the discussions page editing forum-posts.post webscript. * * @author Nick Burch * @since 4.0 diff --git a/source/java/org/alfresco/repo/web/scripts/discussion/ForumTopicsGet.java b/source/java/org/alfresco/repo/web/scripts/discussion/ForumTopicsGet.java new file mode 100644 index 0000000000..e43ab60996 --- /dev/null +++ b/source/java/org/alfresco/repo/web/scripts/discussion/ForumTopicsGet.java @@ -0,0 +1,108 @@ +/* + * 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 . + */ +package org.alfresco.repo.web.scripts.discussion; + +import java.util.Map; + +import org.alfresco.query.PagingRequest; +import org.alfresco.query.PagingResults; +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.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 topics fetching forum-posts.get webscript. + * + * @author Nick Burch + * @since 4.0 + */ +public class ForumTopicsGet extends AbstractDiscussionWebScript +{ + @Override + protected Map executeImpl(SiteInfo site, NodeRef nodeRef, + TopicInfo topic, PostInfo post, WebScriptRequest req, JSONObject json, + Status status, Cache cache) + { + // They shouldn't be trying to list of an existing Post or Topic + if(topic != null || post != null) + { + String error = "Can't list Topics inside an existing Topic or Post"; + throw new WebScriptException(Status.STATUS_BAD_REQUEST, error); + } + + // Do we need to list or search? + boolean tagSearch = false; + String tag = req.getParameter("tag"); + if(tag != null && tag.length() > 0) + { + tagSearch = true; + } + + // Get the topics + PagingResults topics = null; + PagingRequest paging = buildPagingRequest(req); + if(tagSearch) + { + if(site != null) + { + topics = discussionService.findTopics(site.getShortName(), tag, paging); + } + else + { + topics = discussionService.findTopics(nodeRef, tag, paging); + } + } + else + { + if(site != null) + { + topics = discussionService.listTopics(site.getShortName(), paging); + } + else + { + topics = discussionService.listTopics(nodeRef, buildPagingRequest(req)); + } + } + + + // If they did a site based search, and the component hasn't + // been created yet, use the site for the permissions checking + if(site != null && nodeRef == null) + { + nodeRef = site.getNodeRef(); + } + + + // Build the common model parts + Map model = buildCommonModel(site, topic, post, req); + model.put("forum", nodeRef); + + // Have the topics rendered + model.put("data", renderTopics(topics, paging, site)); + + // All done + return model; + } +}