use of org.alfresco.service.cmr.rating.RatingScheme in project alfresco-remote-api by Alfresco.
the class RatingPost method executeImpl.
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
Map<String, Object> model = new HashMap<String, Object>();
NodeRef nodeRefToBeRated = parseRequestForNodeRef(req);
JSONObject json = null;
try {
// read request json
json = new JSONObject(new JSONTokener(req.getContent().getContent()));
// Check mandatory parameters.
if (json.has(RATING) == false) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "rating parameter missing when applying rating");
}
if (json.has(RATING_SCHEME) == false) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "schemeName parameter missing when applying rating");
}
// Check that the scheme name actually exists
final String schemeName = json.getString(RATING_SCHEME);
RatingScheme scheme = ratingService.getRatingScheme(schemeName);
if (scheme == null) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Unknown scheme name: " + schemeName);
}
// Range checking of the rating score will be done within the RatingService.
// So we can just apply the rating.
final float rating = (float) json.getDouble(RATING);
ratingService.applyRating(nodeRefToBeRated, rating, schemeName);
// We'll return the URL to the ratings of the just-rated node.
String ratedNodeUrlFragment = nodeRefToBeRated.toString().replace("://", "/");
String ratedNodeUrl = MessageFormat.format(NODE_RATINGS_URL_FORMAT, ratedNodeUrlFragment);
model.put(RATED_NODE, ratedNodeUrl);
model.put(RATING, rating);
model.put(RATING_SCHEME, schemeName);
model.put(AVERAGE_RATING, ratingService.getAverageRating(nodeRefToBeRated, schemeName));
model.put(RATINGS_TOTAL, ratingService.getTotalRating(nodeRefToBeRated, schemeName));
model.put(RATINGS_COUNT, ratingService.getRatingsCount(nodeRefToBeRated, schemeName));
} catch (IOException iox) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from req.", iox);
} catch (JSONException je) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", je);
}
return model;
}
Aggregations