Rating Service. Added DELETE webscript and fixed bug.

There is now a rating.delete webscript.
  Plus a bug was fixed where the ratings weren't actually being deleted.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@21161 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Neil McErlean
2010-07-14 13:28:46 +00:00
parent bd829f9c08
commit 2f6aaaa819
8 changed files with 146 additions and 12 deletions

View File

@@ -0,0 +1,14 @@
<webscript>
<shortname>DELETE rating</shortname>
<description><![CDATA[
Deletes the specified rating from the specified NodeRef.<br/>
The rated node is identified by the {store_type}/{store_id}/{id} part of the URL.<br/>
The rating scheme name in which to delete the rating is indicated by {scheme}.<br/>
]]>
</description>
<url>/api/node/{store_type}/{store_id}/{id}/ratings/{scheme}</url>
<format default="json" />
<authentication>user</authentication>
<transaction>required</transaction>
<lifecycle>internal</lifecycle>
</webscript>

View File

@@ -0,0 +1,13 @@
<#macro dateFormat date>${date?string("dd MMM yyyy HH:mm:ss 'GMT'Z '('zzz')'")}</#macro>
<#escape x as jsonUtils.encodeJSONString(x)>
{
"data":
{
"nodeRef": "${nodeRef}",
"ratingScheme": "${rating.scheme.name!""}",
"rating": ${rating.score?c},
"appliedAt": "<@dateFormat rating.appliedAt />",
"appliedBy": "${rating.appliedBy!""}"
}
}
</#escape>

View File

@@ -758,6 +758,11 @@
parent="abstractRatingWebScript"> parent="abstractRatingWebScript">
</bean> </bean>
<bean id="webscript.org.alfresco.repository.rating.rating.delete"
class="org.alfresco.repo.web.scripts.rating.RatingDelete"
parent="abstractRatingWebScript">
</bean>
<bean id="webscript.org.alfresco.repository.rating.ratingdefinitions.get" <bean id="webscript.org.alfresco.repository.rating.ratingdefinitions.get"
class="org.alfresco.repo.web.scripts.rating.RatingDefinitionsGet" class="org.alfresco.repo.web.scripts.rating.RatingDefinitionsGet"
parent="abstractRatingWebScript"> parent="abstractRatingWebScript">

View File

@@ -39,6 +39,18 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
*/ */
public abstract class AbstractRatingWebScript extends DeclarativeWebScript public abstract class AbstractRatingWebScript extends DeclarativeWebScript
{ {
// Web script parameters.
protected static final String RATING_SCHEME = "ratingScheme";
protected static final String RATING = "rating";
protected static final String RATED_NODE = "ratedNode";
protected static final String NODE_REF = "nodeRef";
protected static final String RATINGS = "ratings";
protected static final String AVERAGE_RATINGS = "averageRatings";
protected static final String RATINGS_TOTALS = "ratingsTotals";
protected static final String RATINGS_COUNTS = "ratingsCounts";
// Injected services
protected NodeService nodeService; protected NodeService nodeService;
protected RatingService ratingService; protected RatingService ratingService;

View File

@@ -0,0 +1,72 @@
/*
* Copyright (C) 2005-2010 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.rating;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.alfresco.service.cmr.rating.Rating;
import org.alfresco.service.cmr.repository.NodeRef;
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 rating.delete web script.
*
* @author Neil McErlean
* @since 3.4
*/
public class RatingDelete extends AbstractRatingWebScript
{
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
Map<String, Object> model = new HashMap<String, Object>();
NodeRef nodeRef = parseRequestForNodeRef(req);
String ratingSchemeName = parseRequestForScheme(req);
Rating deletedRating = ratingService.removeRatingByCurrentUser(nodeRef, ratingSchemeName);
if (deletedRating == null)
{
// There was no rating in the specified scheme to delete.
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Unable to delete non-existent rating: "
+ ratingSchemeName + " from " + nodeRef.toString());
}
model.put(NODE_REF, nodeRef.toString());
model.put("rating", deletedRating);
return model;
}
private String parseRequestForScheme(WebScriptRequest req)
{
// We know the 'scheme' URL element is there because if it wasn't
// the URL would not have matched.
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
String scheme = templateVars.get("scheme");
return scheme;
}
}

View File

@@ -41,11 +41,6 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
*/ */
public class RatingPost extends AbstractRatingWebScript public class RatingPost extends AbstractRatingWebScript
{ {
// Web script parameters.
private static final String RATING_SCHEME = "ratingScheme";
private static final String RATING = "rating";
private static final String RATED_NODE = "ratedNode";
// Url format // Url format
private final static String NODE_RATINGS_URL_FORMAT = "/api/node/{0}/ratings"; private final static String NODE_RATINGS_URL_FORMAT = "/api/node/{0}/ratings";

View File

@@ -35,6 +35,7 @@ import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONStringer; import org.json.JSONStringer;
import org.json.JSONTokener; import org.json.JSONTokener;
import org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.Response; import org.springframework.extensions.webscripts.TestWebScriptServer.Response;
@@ -283,6 +284,35 @@ public class RatingRestApiTest extends BaseWebScriptTest
assertEquals("Ratings count rating was wrong.", 2, fiveStarStats.getInt(RATINGS_COUNT)); assertEquals("Ratings count rating was wrong.", 2, fiveStarStats.getInt(RATINGS_COUNT));
assertEquals("Ratings total was wrong.", userOneRatingValue + userTwoRatingValue, assertEquals("Ratings total was wrong.", userOneRatingValue + userTwoRatingValue,
(float)fiveStarStats.getDouble(RATINGS_TOTAL)); (float)fiveStarStats.getDouble(RATINGS_TOTAL));
// Now DELETE user two's rating.
// Now POST a second new rating to the testNode - as User Two.
AuthenticationUtil.setFullyAuthenticatedUser(USER_TWO);
rsp = sendRequest(new DeleteRequest(testNodeRatingUrl + "/" + FIVE_STAR_RATING_SCHEME), 200);
rspContent = rsp.getContentAsString();
// GET the ratings again. Although user_one's rating will still be there,
// user two can't see it and so we should see zero ratings.
rsp = sendRequest(new GetRequest(returnedUrl), 200);
jsonRsp = new JSONObject(new JSONTokener(rsp.getContentAsString()));
dataObj = (JSONObject)jsonRsp.get(DATA);
assertNotNull("JSON 'data' object was null", dataObj);
final JSONArray remainingRatings = dataObj.getJSONArray(RATINGS);
assertEquals(0, remainingRatings.length());
// Now the average should have changed.
statsObject = dataObj.getJSONObject(NODE_STATISTICS);
fiveStarStats = statsObject.getJSONObject(FIVE_STAR_RATING_SCHEME);
assertEquals("Average rating was wrong.", userOneRatingValue,
(float)fiveStarStats.getDouble(AVERAGE_RATING));
assertEquals("Ratings count rating was wrong.", 1, fiveStarStats.getInt(RATINGS_COUNT));
assertEquals("Ratings total was wrong.", userOneRatingValue,
(float)fiveStarStats.getDouble(RATINGS_TOTAL));
} }
/** /**

View File

@@ -37,13 +37,6 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
*/ */
public class RatingsGet extends AbstractRatingWebScript public class RatingsGet extends AbstractRatingWebScript
{ {
private static final String NODE_REF = "nodeRef";
private static final String RATINGS = "ratings";
private static final String AVERAGE_RATINGS = "averageRatings";
private static final String RATINGS_TOTALS = "ratingsTotals";
private static final String RATINGS_COUNTS = "ratingsCounts";
@Override @Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{ {