Search in sources :

Example 1 with RatingScheme

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;
}
Also used : JSONTokener(org.json.JSONTokener) NodeRef(org.alfresco.service.cmr.repository.NodeRef) RatingScheme(org.alfresco.service.cmr.rating.RatingScheme) JSONObject(org.json.JSONObject) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 RatingScheme (org.alfresco.service.cmr.rating.RatingScheme)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 JSONTokener (org.json.JSONTokener)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1