diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/rating/rating.delete.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/rating/rating.delete.desc.xml new file mode 100644 index 0000000000..1abac142ed --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/rating/rating.delete.desc.xml @@ -0,0 +1,14 @@ + + DELETE rating + + The rated node is identified by the {store_type}/{store_id}/{id} part of the URL.
+ The rating scheme name in which to delete the rating is indicated by {scheme}.
+ ]]> +
+ /api/node/{store_type}/{store_id}/{id}/ratings/{scheme} + + user + required + internal +
\ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/rating/rating.delete.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/rating/rating.delete.json.ftl new file mode 100644 index 0000000000..adf1572e9e --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/rating/rating.delete.json.ftl @@ -0,0 +1,13 @@ +<#macro dateFormat date>${date?string("dd MMM yyyy HH:mm:ss 'GMT'Z '('zzz')'")} +<#escape x as jsonUtils.encodeJSONString(x)> +{ + "data": + { + "nodeRef": "${nodeRef}", + "ratingScheme": "${rating.scheme.name!""}", + "rating": ${rating.score?c}, + "appliedAt": "<@dateFormat rating.appliedAt />", + "appliedBy": "${rating.appliedBy!""}" + } +} + \ No newline at end of file diff --git a/config/alfresco/web-scripts-application-context.xml b/config/alfresco/web-scripts-application-context.xml index f524c1ea73..24e3a3a87e 100644 --- a/config/alfresco/web-scripts-application-context.xml +++ b/config/alfresco/web-scripts-application-context.xml @@ -758,6 +758,11 @@ parent="abstractRatingWebScript"> + + + diff --git a/source/java/org/alfresco/repo/web/scripts/rating/AbstractRatingWebScript.java b/source/java/org/alfresco/repo/web/scripts/rating/AbstractRatingWebScript.java index b0694ed754..c1c61c802a 100644 --- a/source/java/org/alfresco/repo/web/scripts/rating/AbstractRatingWebScript.java +++ b/source/java/org/alfresco/repo/web/scripts/rating/AbstractRatingWebScript.java @@ -39,6 +39,18 @@ import org.springframework.extensions.webscripts.WebScriptRequest; */ 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 RatingService ratingService; diff --git a/source/java/org/alfresco/repo/web/scripts/rating/RatingDelete.java b/source/java/org/alfresco/repo/web/scripts/rating/RatingDelete.java new file mode 100644 index 0000000000..bc80e2d6eb --- /dev/null +++ b/source/java/org/alfresco/repo/web/scripts/rating/RatingDelete.java @@ -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 . + */ +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 executeImpl(WebScriptRequest req, Status status, Cache cache) + { + Map model = new HashMap(); + + 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 templateVars = req.getServiceMatch().getTemplateVars(); + String scheme = templateVars.get("scheme"); + + return scheme; + } +} diff --git a/source/java/org/alfresco/repo/web/scripts/rating/RatingPost.java b/source/java/org/alfresco/repo/web/scripts/rating/RatingPost.java index 95cc2b8b28..842b8d387b 100644 --- a/source/java/org/alfresco/repo/web/scripts/rating/RatingPost.java +++ b/source/java/org/alfresco/repo/web/scripts/rating/RatingPost.java @@ -41,11 +41,6 @@ import org.springframework.extensions.webscripts.WebScriptRequest; */ 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 private final static String NODE_RATINGS_URL_FORMAT = "/api/node/{0}/ratings"; diff --git a/source/java/org/alfresco/repo/web/scripts/rating/RatingRestApiTest.java b/source/java/org/alfresco/repo/web/scripts/rating/RatingRestApiTest.java index 3acb632254..9a901b8d16 100644 --- a/source/java/org/alfresco/repo/web/scripts/rating/RatingRestApiTest.java +++ b/source/java/org/alfresco/repo/web/scripts/rating/RatingRestApiTest.java @@ -35,6 +35,7 @@ import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONStringer; import org.json.JSONTokener; +import org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest; 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 total was wrong.", userOneRatingValue + userTwoRatingValue, (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)); } /** diff --git a/source/java/org/alfresco/repo/web/scripts/rating/RatingsGet.java b/source/java/org/alfresco/repo/web/scripts/rating/RatingsGet.java index bb6d24749e..33c88b4b04 100644 --- a/source/java/org/alfresco/repo/web/scripts/rating/RatingsGet.java +++ b/source/java/org/alfresco/repo/web/scripts/rating/RatingsGet.java @@ -37,13 +37,6 @@ import org.springframework.extensions.webscripts.WebScriptRequest; */ 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 protected Map executeImpl(WebScriptRequest req, Status status, Cache cache) {