Search in sources :

Example 1 with RatingsException

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

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

the class DefaultRatingsManager method getRating.

@Override
public Rating getRating(DocumentReference documentRef, int id) throws RatingsException {
    try {
        int skipped = 0;
        XWikiDocument doc = getXWiki().getDocument(documentRef, getXWikiContext());
        List<BaseObject> bobjects = doc.getObjects(getRatingsClassName());
        if (bobjects != null) {
            for (BaseObject bobj : bobjects) {
                if (bobj != null) {
                    if (skipped < id) {
                        skipped++;
                    } else {
                        return getDefaultRating(documentRef, bobj);
                    }
                }
            }
        }
    } catch (XWikiException e) {
        throw new RatingsException(e);
    }
    return null;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) RatingsException(org.xwiki.ratings.RatingsException) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 3 with RatingsException

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

the class SeparatePageRating method addDocument.

/**
 * Adds a new document in which to store ratings.
 *
 * @param documentRef reference to the document with which the rating is associated
 * @param author the author of the rating
 * @param date the date when the rating was done
 * @param vote the author's vote
 * @return the newly created document
 * @throws RatingsException when an error occurs while creating the document
 */
private XWikiDocument addDocument(DocumentReference documentRef, DocumentReference author, Date date, int vote) throws RatingsException {
    try {
        DocumentReference pageRef = getPageReference(documentRef);
        String parentDocName = ratingsManager.entityReferenceSerializer.serialize(documentRef);
        XWiki xwiki = context.getWiki();
        XWikiDocument doc = xwiki.getDocument(pageRef, context);
        doc.setParent(parentDocName);
        doc.setHidden(true);
        BaseObject obj = doc.newXObject(RatingsManager.RATINGS_CLASSREFERENCE, context);
        obj.setStringValue(RatingsManager.RATING_CLASS_FIELDNAME_AUTHOR, ratingsManager.entityReferenceSerializer.serialize(author));
        obj.setDateValue(RatingsManager.RATING_CLASS_FIELDNAME_DATE, date);
        obj.setIntValue(RatingsManager.RATING_CLASS_FIELDNAME_VOTE, vote);
        obj.setStringValue(RatingsManager.RATING_CLASS_FIELDNAME_PARENT, parentDocName);
        return doc;
    } catch (XWikiException e) {
        throw new RatingsException(e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWiki(com.xpn.xwiki.XWiki) RatingsException(org.xwiki.ratings.RatingsException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 4 with RatingsException

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

the class SeparatePageRating method save.

@Override
public void save() throws RatingsException {
    DocumentReference superadmin = new DocumentReference("xwiki", XWiki.SYSTEM_SPACE, XWikiRightService.SUPERADMIN_USER);
    try {
        if (document == null) {
            throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, RatingsException.ERROR_RATINGS_SAVERATING_NULLDOCUMENT, "Cannot save invalid separate page rating, the rating document is null");
        }
        document.setCreatorReference(superadmin);
        document.setAuthorReference(superadmin);
        ContextualLocalizationManager localization = Utils.getComponent(ContextualLocalizationManager.class);
        context.getWiki().saveDocument(getDocument(), localization.getTranslationPlain("rating.saveComment"), true, context);
    } catch (XWikiException e) {
        throw new RatingsException(e);
    }
}
Also used : ContextualLocalizationManager(org.xwiki.localization.ContextualLocalizationManager) RatingsException(org.xwiki.ratings.RatingsException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 5 with RatingsException

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

the class SeparatePageRatingsManager method getRatings.

@Override
public List<Rating> getRatings(DocumentReference documentRef, int start, int count, boolean asc) throws RatingsException {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Calling separate page manager code for ratings");
    }
    String sql = ", BaseObject as obj, StringProperty as parentprop where doc.fullName=obj.name and obj.className=?" + " and obj.id=parentprop.id.id and parentprop.id.name=?" + " and parentprop.value=?" + " and obj.name not in (select obj2.name from BaseObject as obj2, StringProperty as statusprop where obj2.className=?" + " and obj2.id=statusprop.id.id and statusprop.id.name=? and (statusprop.value=? or statusprop.value= ?) and obj.id=obj2.id) order by doc.date " + (asc ? "asc" : "desc");
    List<?> params = new ArrayList<String>(Arrays.asList(getRatingsClassName(), RATING_CLASS_FIELDNAME_PARENT, entityReferenceSerializer.serialize(documentRef), getRatingsClassName(), "status", "moderated", "refused"));
    List<Rating> ratings = new ArrayList<Rating>();
    try {
        List<DocumentReference> ratingPageReferenceList = getXWikiContext().getWiki().getStore().searchDocumentReferences(sql, params, getXWikiContext());
        for (DocumentReference ratingPageReference : ratingPageReferenceList) {
            ratings.add(getRatingFromDocument(documentRef, getXWiki().getDocument(ratingPageReference, getXWikiContext())));
        }
    } catch (XWikiException e) {
        throw new RatingsException(e);
    }
    return ratings;
}
Also used : Rating(org.xwiki.ratings.Rating) ArrayList(java.util.ArrayList) RatingsException(org.xwiki.ratings.RatingsException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

RatingsException (org.xwiki.ratings.RatingsException)15 XWikiException (com.xpn.xwiki.XWikiException)14 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)7 BaseObject (com.xpn.xwiki.objects.BaseObject)6 DocumentReference (org.xwiki.model.reference.DocumentReference)5 AverageRating (org.xwiki.ratings.AverageRating)4 ArrayList (java.util.ArrayList)2 Rating (org.xwiki.ratings.Rating)2 ReputationException (org.xwiki.ratings.ReputationException)2 XWiki (com.xpn.xwiki.XWiki)1 Date (java.util.Date)1 List (java.util.List)1 ContextualLocalizationManager (org.xwiki.localization.ContextualLocalizationManager)1 UpdateRatingEvent (org.xwiki.ratings.UpdateRatingEvent)1 UpdatingRatingEvent (org.xwiki.ratings.UpdatingRatingEvent)1