Search in sources :

Example 1 with UpdatingRatingEvent

use of org.xwiki.ratings.UpdatingRatingEvent in project xwiki-platform by xwiki.

the class DefaultRatingsManager method setRating.

@Override
public Rating setRating(DocumentReference documentRef, DocumentReference author, int vote) throws RatingsException {
    Rating rating = getRating(documentRef, author);
    int oldVote;
    if (rating == null) {
        oldVote = 0;
        try {
            rating = new DefaultRating(documentRef, author, vote, getXWikiContext(), this);
        } catch (XWikiException e) {
            throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, e.getCode(), "Failed to create new rating object", e);
        }
    } else {
        oldVote = rating.getVote();
        rating.setVote(vote);
        rating.setDate(new Date());
    }
    // Indicate that we start modifying the rating
    this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null);
    boolean updateFailed = true;
    try {
        // update rating
        rating.save();
        // update average rating count
        updateAverageRatings(documentRef, rating, oldVote);
        updateFailed = false;
    } finally {
        if (updateFailed) {
            // Indicate that the we start modifying the rating
            this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null);
        } else {
            // Indicate that we finished updating the rating
            this.observationManager.notify(new UpdateRatingEvent(documentRef, rating, oldVote), null);
        }
    }
    return rating;
}
Also used : UpdatingRatingEvent(org.xwiki.ratings.UpdatingRatingEvent) UpdateRatingEvent(org.xwiki.ratings.UpdateRatingEvent) Rating(org.xwiki.ratings.Rating) RatingsException(org.xwiki.ratings.RatingsException) XWikiException(com.xpn.xwiki.XWikiException) Date(java.util.Date)

Example 2 with UpdatingRatingEvent

use of org.xwiki.ratings.UpdatingRatingEvent in project xwiki-platform by xwiki.

the class SeparatePageRatingsManager method setRating.

@Override
public Rating setRating(DocumentReference documentRef, DocumentReference author, int vote) throws RatingsException {
    Rating rating = getRating(documentRef, author);
    int oldVote;
    if (rating == null) {
        oldVote = 0;
        rating = new SeparatePageRating(documentRef, author, vote, getXWikiContext(), this);
    } else {
        oldVote = rating.getVote();
        rating.setVote(vote);
        rating.setDate(new Date());
    }
    // Indicate that we start modifying the rating
    this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null);
    boolean updateFailed = true;
    try {
        // saving rating
        rating.save();
        // update the average rating
        updateAverageRatings(documentRef, rating, oldVote);
        updateFailed = false;
    } finally {
        if (updateFailed) {
            // Indicate that the we start modifying the rating
            this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null);
        } else {
            // Indicate that we finished updating the rating
            this.observationManager.notify(new UpdateRatingEvent(documentRef, rating, oldVote), null);
        }
    }
    return rating;
}
Also used : UpdatingRatingEvent(org.xwiki.ratings.UpdatingRatingEvent) UpdateRatingEvent(org.xwiki.ratings.UpdateRatingEvent) Rating(org.xwiki.ratings.Rating) Date(java.util.Date)

Aggregations

Date (java.util.Date)2 Rating (org.xwiki.ratings.Rating)2 UpdateRatingEvent (org.xwiki.ratings.UpdateRatingEvent)2 UpdatingRatingEvent (org.xwiki.ratings.UpdatingRatingEvent)2 XWikiException (com.xpn.xwiki.XWikiException)1 RatingsException (org.xwiki.ratings.RatingsException)1