Search in sources :

Example 1 with UpdateRatingEvent

use of org.xwiki.ratings.UpdateRatingEvent 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 UpdateRatingEvent

use of org.xwiki.ratings.UpdateRatingEvent 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)

Example 3 with UpdateRatingEvent

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

the class UpdateReputationListener method onEvent.

@Override
public void onEvent(Event event, Object arg1, Object arg2) {
    UpdateRatingEvent ratingEvent = (UpdateRatingEvent) event;
    DocumentReference documentRef = ratingEvent.getDocumentReference();
    reputationAlgorithm.get(documentRef).updateReputation(documentRef, ratingEvent.getNewRating(), ratingEvent.getOldRating());
}
Also used : UpdateRatingEvent(org.xwiki.ratings.UpdateRatingEvent) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

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