ALF-9153 Convert the "hot" discussions topic webscript to be java backed and lucene free

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@30038 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2011-08-24 16:52:32 +00:00
parent 35f0f3efb0
commit af122ab838
8 changed files with 172 additions and 274 deletions

View File

@@ -45,6 +45,7 @@ import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.util.Pair;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
@@ -164,6 +165,41 @@ public abstract class AbstractDiscussionWebScript extends DeclarativeWebScript
return paging;
}
protected List<String> getTags(JSONObject json) throws JSONException
{
List<String> tags = null;
if(json.has("tags"))
{
// Is it "tags":"" or "tags":[...] ?
if(json.get("tags") instanceof String)
{
// This is normally an empty string, skip
String tagsS = json.getString("tags");
if("".equals(tagsS))
{
// No tags were given
return null;
}
else
{
// Log, and treat as empty
logger.warn("Unexpected tag data: " + tagsS);
return null;
}
}
else
{
tags = new ArrayList<String>();
JSONArray jsTags = json.getJSONArray("tags");
for(int i=0; i<jsTags.length(); i++)
{
tags.add( jsTags.getString(i) );
}
}
}
return tags;
}
/**
* Generates an activity entry for the discussion item
*
@@ -359,18 +395,27 @@ public abstract class AbstractDiscussionWebScript extends DeclarativeWebScript
*/
protected Map<String, Object> renderTopics(PagingResults<TopicInfo> topics,
PagingRequest paging, SiteInfo site)
{
return renderTopics(topics.getPage(), topics.getTotalResultCount(), paging, site);
}
/*
* 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(List<TopicInfo> topics,
Pair<Integer,Integer> size, PagingRequest paging, SiteInfo site)
{
Map<String, Object> model = new HashMap<String, Object>();
// Paging info
model.put("total", topics.getTotalResultCount().getFirst());
model.put("total", size.getFirst());
model.put("pageSize", paging.getMaxItems());
model.put("startIndex", paging.getSkipCount());
model.put("itemCount", topics.getPage().size());
model.put("itemCount", topics.size());
// Data
List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
for(TopicInfo topic : topics.getPage())
for(TopicInfo topic : topics)
{
items.add(renderTopic(topic, site));
}

View File

@@ -18,6 +18,7 @@
*/
package org.alfresco.repo.web.scripts.discussion;
import java.util.List;
import java.util.Map;
import org.alfresco.service.cmr.discussion.PostInfo;
@@ -114,10 +115,11 @@ public class ForumPostPut extends AbstractDiscussionWebScript
if(json.has("tags"))
{
topic.getTags().clear();
JSONArray tags = json.getJSONArray("tags");
for(int i=0; i<tags.length(); i++)
List<String> tags = getTags(json);
if(tags != null)
{
topic.getTags().add( tags.getString(i) );
topic.getTags().addAll(tags);
}
}
}

View File

@@ -69,15 +69,7 @@ public class ForumTopicPost extends AbstractDiscussionWebScript
{
contents = json.getString("content");
}
if(json.has("tags"))
{
tags = new ArrayList<String>();
JSONArray jsTags = json.getJSONArray("tags");
for(int i=0; i<jsTags.length(); i++)
{
tags.add( jsTags.getString(i) );
}
}
tags = getTags(json);
}
catch(JSONException e)
{

View File

@@ -65,6 +65,7 @@ public class ForumTopicsGet extends AbstractDiscussionWebScript
PagingRequest paging = buildPagingRequest(req);
if(tagSearch)
{
// Tag based is a search rather than a listing
if(site != null)
{
topics = discussionService.findTopics(site.getShortName(), null, tag, paging);

View File

@@ -0,0 +1,111 @@
/*
* 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.ArrayList;
import java.util.Date;
import java.util.List;
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.alfresco.util.Pair;
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-hot.get webscript.
*
* @author Nick Burch
* @since 4.0
*/
public class ForumTopicsHotGet extends AbstractDiscussionWebScript
{
protected static final int RECENT_POSTS_PERIOD_DAYS = 30;
protected static final long ONE_DAY_MS = 24*60*60*1000;
@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);
}
// Grab the date range to search over
String numDaysS = req.getParameter("numdays");
int numDays = RECENT_POSTS_PERIOD_DAYS;
if(numDaysS != null)
{
numDays = Integer.parseInt(numDaysS);
}
Date now = new Date();
Date since = new Date(now.getTime() - numDays*ONE_DAY_MS);
// Get the topics with recent replies
PagingResults<Pair<TopicInfo,Integer>> topicsAndCounts = null;
PagingRequest paging = buildPagingRequest(req);
if(site != null)
{
topicsAndCounts = discussionService.listHotTopics(site.getShortName(), since, paging);
}
else
{
topicsAndCounts = discussionService.listHotTopics(nodeRef, since, paging);
}
// For this, we actually only need the topics, not their counts too
List<TopicInfo> topics = new ArrayList<TopicInfo>();
for(Pair<TopicInfo,Integer> tc : topicsAndCounts.getPage())
{
topics.add(tc.getFirst());
}
// 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, topicsAndCounts.getTotalResultCount(), paging, site));
// All done
return model;
}
}