mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Rating Service. Allow fractional ratings.
I've changed the ratings 'score' from an integer to a float as fractional ratings seems like a reasonable idea to me. This had impact all through the Java layer, the REST layer, the model and the test code. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@21128 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -82,7 +82,7 @@ public class RatingPost extends AbstractRatingWebScript
|
|||||||
|
|
||||||
// Range checking of the rating score will be done within the RatingService.
|
// Range checking of the rating score will be done within the RatingService.
|
||||||
// So we can just apply the rating.
|
// So we can just apply the rating.
|
||||||
final int rating = json.getInt(RATING);
|
final float rating = (float)json.getDouble(RATING);
|
||||||
ratingService.applyRating(nodeRefToBeRated, rating, schemeName);
|
ratingService.applyRating(nodeRefToBeRated, rating, schemeName);
|
||||||
|
|
||||||
// We'll return the URL to the ratings of the just-rated node.
|
// We'll return the URL to the ratings of the just-rated node.
|
||||||
|
@@ -149,11 +149,11 @@ public class RatingRestApiTest extends BaseWebScriptTest
|
|||||||
JSONObject scheme2 = ratingSchemesArray.getJSONObject(1);
|
JSONObject scheme2 = ratingSchemesArray.getJSONObject(1);
|
||||||
|
|
||||||
assertEquals(LIKES_RATING_SCHEME, scheme1.getString(NAME));
|
assertEquals(LIKES_RATING_SCHEME, scheme1.getString(NAME));
|
||||||
assertEquals(1, scheme1.getInt(MIN_RATING));
|
assertEquals(1.0, scheme1.getDouble(MIN_RATING));
|
||||||
assertEquals(1, scheme1.getInt(MAX_RATING));
|
assertEquals(1.0, scheme1.getDouble(MAX_RATING));
|
||||||
assertEquals(FIVE_STAR_RATING_SCHEME, scheme2.getString(NAME));
|
assertEquals(FIVE_STAR_RATING_SCHEME, scheme2.getString(NAME));
|
||||||
assertEquals(1, scheme2.getInt(MIN_RATING));
|
assertEquals(1.0, scheme2.getDouble(MIN_RATING));
|
||||||
assertEquals(5, scheme2.getInt(MAX_RATING));
|
assertEquals(5.0, scheme2.getDouble(MAX_RATING));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetRatingsFromUnratedNodeRef() throws Exception
|
public void testGetRatingsFromUnratedNodeRef() throws Exception
|
||||||
@@ -178,7 +178,7 @@ public class RatingRestApiTest extends BaseWebScriptTest
|
|||||||
JSONObject likesStats = statsObject.getJSONObject(LIKES_RATING_SCHEME);
|
JSONObject likesStats = statsObject.getJSONObject(LIKES_RATING_SCHEME);
|
||||||
assertEquals("Average rating was wrong.", -1.0, likesStats.getDouble(AVERAGE_RATING));
|
assertEquals("Average rating was wrong.", -1.0, likesStats.getDouble(AVERAGE_RATING));
|
||||||
assertEquals("Ratings count rating was wrong.", 0, likesStats.getInt(RATINGS_COUNT));
|
assertEquals("Ratings count rating was wrong.", 0, likesStats.getInt(RATINGS_COUNT));
|
||||||
assertEquals("Ratings total was wrong.", 0, likesStats.getInt(RATINGS_TOTAL));
|
assertEquals("Ratings total was wrong.", 0.0, likesStats.getDouble(RATINGS_TOTAL));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -193,7 +193,7 @@ public class RatingRestApiTest extends BaseWebScriptTest
|
|||||||
|
|
||||||
final String testNodeRatingUrl = getRatingUrl(testNode);
|
final String testNodeRatingUrl = getRatingUrl(testNode);
|
||||||
|
|
||||||
final int userOneRatingValue = 5;
|
final float userOneRatingValue = 4.5f;
|
||||||
String jsonString = new JSONStringer().object()
|
String jsonString = new JSONStringer().object()
|
||||||
.key("rating").value(userOneRatingValue)
|
.key("rating").value(userOneRatingValue)
|
||||||
.key("ratingScheme").value(FIVE_STAR_RATING_SCHEME)
|
.key("ratingScheme").value(FIVE_STAR_RATING_SCHEME)
|
||||||
@@ -213,7 +213,7 @@ public class RatingRestApiTest extends BaseWebScriptTest
|
|||||||
String returnedUrl = dataObj.getString("ratedNodeUrl");
|
String returnedUrl = dataObj.getString("ratedNodeUrl");
|
||||||
assertEquals(testNodeRatingUrl, returnedUrl);
|
assertEquals(testNodeRatingUrl, returnedUrl);
|
||||||
assertEquals(FIVE_STAR_RATING_SCHEME, dataObj.getString("ratingScheme"));
|
assertEquals(FIVE_STAR_RATING_SCHEME, dataObj.getString("ratingScheme"));
|
||||||
assertEquals(userOneRatingValue, dataObj.getInt("rating"));
|
assertEquals(userOneRatingValue, (float)dataObj.getDouble("rating"));
|
||||||
|
|
||||||
// Now GET the ratings via that returned URL
|
// Now GET the ratings via that returned URL
|
||||||
rsp = sendRequest(new GetRequest(testNodeRatingUrl), 200);
|
rsp = sendRequest(new GetRequest(testNodeRatingUrl), 200);
|
||||||
@@ -227,21 +227,21 @@ public class RatingRestApiTest extends BaseWebScriptTest
|
|||||||
final JSONArray ratingsArray = dataObj.getJSONArray(RATINGS);
|
final JSONArray ratingsArray = dataObj.getJSONArray(RATINGS);
|
||||||
assertEquals(1, ratingsArray.length());
|
assertEquals(1, ratingsArray.length());
|
||||||
JSONObject recoveredRating = (JSONObject)ratingsArray.get(0);
|
JSONObject recoveredRating = (JSONObject)ratingsArray.get(0);
|
||||||
assertEquals(userOneRatingValue, recoveredRating.getInt("rating"));
|
assertEquals(userOneRatingValue, (float)recoveredRating.getDouble("rating"));
|
||||||
assertEquals(FIVE_STAR_RATING_SCHEME, recoveredRating.getString("ratingScheme"));
|
assertEquals(FIVE_STAR_RATING_SCHEME, recoveredRating.getString("ratingScheme"));
|
||||||
|
|
||||||
// As well as the average, total ratings.
|
// As well as the average, total ratings.
|
||||||
JSONObject statsObject = dataObj.getJSONObject(NODE_STATISTICS);
|
JSONObject statsObject = dataObj.getJSONObject(NODE_STATISTICS);
|
||||||
JSONObject fiveStarStats = statsObject.getJSONObject(FIVE_STAR_RATING_SCHEME);
|
JSONObject fiveStarStats = statsObject.getJSONObject(FIVE_STAR_RATING_SCHEME);
|
||||||
assertEquals("Average rating was wrong.", (double)userOneRatingValue, fiveStarStats.getDouble(AVERAGE_RATING));
|
assertEquals("Average rating was wrong.", userOneRatingValue, (float)fiveStarStats.getDouble(AVERAGE_RATING));
|
||||||
assertEquals("Ratings count rating was wrong.", 1, fiveStarStats.getInt(RATINGS_COUNT));
|
assertEquals("Ratings count rating was wrong.", 1, fiveStarStats.getInt(RATINGS_COUNT));
|
||||||
assertEquals("Ratings total was wrong.", userOneRatingValue, fiveStarStats.getInt(RATINGS_TOTAL));
|
assertEquals("Ratings total was wrong.", userOneRatingValue, (float)fiveStarStats.getDouble(RATINGS_TOTAL));
|
||||||
|
|
||||||
|
|
||||||
// Now POST a second new rating to the testNode - as User Two.
|
// Now POST a second new rating to the testNode - as User Two.
|
||||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_TWO);
|
AuthenticationUtil.setFullyAuthenticatedUser(USER_TWO);
|
||||||
|
|
||||||
final int userTwoRatingValue = 3;
|
final float userTwoRatingValue = 3.5f;
|
||||||
jsonString = new JSONStringer().object()
|
jsonString = new JSONStringer().object()
|
||||||
.key("rating").value(userTwoRatingValue)
|
.key("rating").value(userTwoRatingValue)
|
||||||
.key("ratingScheme").value(FIVE_STAR_RATING_SCHEME)
|
.key("ratingScheme").value(FIVE_STAR_RATING_SCHEME)
|
||||||
@@ -272,7 +272,7 @@ public class RatingRestApiTest extends BaseWebScriptTest
|
|||||||
final JSONArray userTwoRatingsArray = dataObj.getJSONArray(RATINGS);
|
final JSONArray userTwoRatingsArray = dataObj.getJSONArray(RATINGS);
|
||||||
assertEquals(1, userTwoRatingsArray.length());
|
assertEquals(1, userTwoRatingsArray.length());
|
||||||
JSONObject secondRating = (JSONObject)userTwoRatingsArray.get(0);
|
JSONObject secondRating = (JSONObject)userTwoRatingsArray.get(0);
|
||||||
assertEquals(userTwoRatingValue, secondRating.getInt("rating"));
|
assertEquals(userTwoRatingValue, (float)secondRating.getDouble("rating"));
|
||||||
assertEquals(FIVE_STAR_RATING_SCHEME, secondRating.getString("ratingScheme"));
|
assertEquals(FIVE_STAR_RATING_SCHEME, secondRating.getString("ratingScheme"));
|
||||||
|
|
||||||
// Now the average should have changed.
|
// Now the average should have changed.
|
||||||
@@ -282,7 +282,7 @@ public class RatingRestApiTest extends BaseWebScriptTest
|
|||||||
fiveStarStats.getDouble(AVERAGE_RATING));
|
fiveStarStats.getDouble(AVERAGE_RATING));
|
||||||
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,
|
||||||
fiveStarStats.getInt(RATINGS_TOTAL));
|
(float)fiveStarStats.getDouble(RATINGS_TOTAL));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -57,7 +57,7 @@ public class RatingsGet extends AbstractRatingWebScript
|
|||||||
// These maps hold the average rating, accumulated total of all ratings and
|
// These maps hold the average rating, accumulated total of all ratings and
|
||||||
// the number of ratings applied for this node as a function of rating scheme.
|
// the number of ratings applied for this node as a function of rating scheme.
|
||||||
Map<String, Float> averageRatings = new HashMap<String, Float>();
|
Map<String, Float> averageRatings = new HashMap<String, Float>();
|
||||||
Map<String, Integer> ratingsTotals = new HashMap<String, Integer>();
|
Map<String, Float> ratingsTotals = new HashMap<String, Float>();
|
||||||
Map<String, Integer> ratingsCounts = new HashMap<String, Integer>();
|
Map<String, Integer> ratingsCounts = new HashMap<String, Integer>();
|
||||||
|
|
||||||
for (String schemeName : ratingService.getRatingSchemes().keySet())
|
for (String schemeName : ratingService.getRatingSchemes().keySet())
|
||||||
|
Reference in New Issue
Block a user