ALF-9153 Convert the Discussions Topics Listing webscript to be Java backed and Lucene free, though not fully optimised yet

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29893 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2011-08-18 16:48:28 +00:00
parent 2e5b89599b
commit b36edc807a
6 changed files with 153 additions and 66 deletions

View File

@@ -1,52 +0,0 @@
<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 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();

View File

@@ -1608,13 +1608,11 @@
</bean> </bean>
<!-- Lists the Discussions for a site --> <!-- Lists the discussion topics for a site -->
<!--
<bean id="webscript.org.alfresco.repository.discussions.forum.forum-posts.get" <bean id="webscript.org.alfresco.repository.discussions.forum.forum-posts.get"
class="org.alfresco.repo.web.scripts.discussions.ForumPostsGet" class="org.alfresco.repo.web.scripts.discussion.ForumTopicsGet"
parent="abstractDiscussionWebScript"> parent="abstractDiscussionWebScript">
</bean> </bean>
-->
<!-- --> <!-- -->

View File

@@ -19,12 +19,15 @@
package org.alfresco.repo.web.scripts.discussion; package org.alfresco.repo.web.scripts.discussion;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.alfresco.query.PagingRequest; import org.alfresco.query.PagingRequest;
import org.alfresco.query.PagingResults; import org.alfresco.query.PagingResults;
import org.alfresco.repo.content.MimetypeMap; import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.discussion.DiscussionServiceImpl;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.site.SiteServiceImpl; import org.alfresco.repo.site.SiteServiceImpl;
import org.alfresco.service.cmr.activities.ActivityService; import org.alfresco.service.cmr.activities.ActivityService;
@@ -157,7 +160,7 @@ public abstract class AbstractDiscussionWebScript extends DeclarativeWebScript
} }
PagingRequest paging = new PagingRequest( startIndex, pageSize ); 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; return paging;
} }
@@ -350,6 +353,33 @@ public abstract class AbstractDiscussionWebScript extends DeclarativeWebScript
return item; return item;
} }
/*
* Renders out the list of topics
* TODO Fetch the post data in one go, rather than one at a time
*/
protected Map<String, Object> renderTopics(PagingResults<TopicInfo> topics,
PagingRequest paging, SiteInfo site)
{
Map<String, Object> model = new HashMap<String, Object>();
// 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<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
for(TopicInfo topic : topics.getPage())
{
items.add(renderTopic(topic, site));
}
model.put("items", items);
// All done
return model;
}
protected Map<String, Object> buildCommonModel(SiteInfo site, TopicInfo topic, protected Map<String, Object> buildCommonModel(SiteInfo site, TopicInfo topic,
PostInfo post, WebScriptRequest req) PostInfo post, WebScriptRequest req)
{ {
@@ -452,6 +482,14 @@ public abstract class AbstractDiscussionWebScript extends DeclarativeWebScript
} }
nodeRef = topic.getNodeRef(); 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") && else if(templateVars.containsKey("store_type") &&
templateVars.containsKey("store_id") && templateVars.containsKey("store_id") &&

View File

@@ -1054,11 +1054,8 @@ public class DiscussionRestApiTest extends BaseWebScriptTest
assertEquals(3, result.getInt("total")); assertEquals(3, result.getInt("total"));
assertEquals(3, result.getInt("itemCount")); assertEquals(3, result.getInt("itemCount"));
assertEquals(3, result.getJSONArray("items").length()); assertEquals(3, result.getJSONArray("items").length());
// TODO This appears to be incorrect... assertEquals("NodeTitle2", result.getJSONArray("items").getJSONObject(0).getString("title"));
assertEquals("NodeTitle3", result.getJSONArray("items").getJSONObject(0).getString("title")); assertEquals("NodeTitle3", result.getJSONArray("items").getJSONObject(1).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("NodeTitle1", result.getJSONArray("items").getJSONObject(2).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(0).getInt("replyCount"));
assertEquals(0, result.getJSONArray("items").getJSONObject(1).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(3, result.getInt("total"));
assertEquals(1, result.getInt("itemCount")); assertEquals(1, result.getInt("itemCount"));
assertEquals(1, result.getJSONArray("items").length()); assertEquals(1, result.getJSONArray("items").length());
// TODO This appears to be incorrect... assertEquals("NodeTitle2", result.getJSONArray("items").getJSONObject(0).getString("title"));
assertEquals("NodeTitle3", 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")); assertEquals(0, result.getJSONArray("items").getJSONObject(0).getInt("replyCount"));
} }

View File

@@ -35,7 +35,7 @@ import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest; 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 * @author Nick Burch
* @since 4.0 * @since 4.0

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
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<String, Object> 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<TopicInfo> 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<String, Object> 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;
}
}