Search in sources :

Example 6 with RatingsException

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

the class SeparatePageRatingsManager method getRating.

@Override
public Rating getRating(String ratingId) throws RatingsException {
    try {
        int i1 = ratingId.indexOf(".");
        if (i1 == -1) {
            throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, RatingsException.ERROR_RATINGS_INVALID_RATING_ID, "Invalid rating ID, cannot parse rating id");
        }
        XWikiDocument doc = getXWiki().getDocument(ratingId, getXWikiContext());
        if (doc.isNew()) {
            throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, RatingsException.ERROR_RATINGS_INVALID_RATING_ID, "Invalid rating ID, rating does not exist");
        }
        BaseObject object = doc.getObject(getRatingsClassName());
        if (object == null) {
            throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, RatingsException.ERROR_RATINGS_INVALID_RATING_ID, "Invalid rating ID, rating does not exist");
        }
        String parentDocName = object.getStringValue(RATING_CLASS_FIELDNAME_PARENT);
        XWikiDocument parentDoc = getXWikiContext().getWiki().getDocument(parentDocName, getXWikiContext());
        return new SeparatePageRating(parentDoc.getDocumentReference(), doc, getXWikiContext(), this);
    } catch (XWikiException e) {
        throw new RatingsException(e);
    }
}
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 7 with RatingsException

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

the class SeparatePageRatingsManager method getRating.

@Override
public Rating getRating(DocumentReference documentRef, int id) throws RatingsException {
    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 desc";
    List<?> params = new ArrayList<String>(Arrays.asList(getRatingsClassName(), RATING_CLASS_FIELDNAME_PARENT, entityReferenceSerializer.serialize(documentRef), getRatingsClassName(), "status", "moderated", "refused"));
    try {
        List<DocumentReference> ratingPageReferenceList = getXWikiContext().getWiki().getStore().searchDocumentReferences(sql, 1, id, params, getXWikiContext());
        if ((ratingPageReferenceList == null) || (ratingPageReferenceList.size() == 0)) {
            return null;
        } else {
            return new SeparatePageRatingsManager().getRatingFromDocument(documentRef, getXWiki().getDocument(ratingPageReferenceList.get(0), getXWikiContext()));
        }
    } catch (XWikiException e) {
        throw new RatingsException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) RatingsException(org.xwiki.ratings.RatingsException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 8 with RatingsException

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

the class SimpleReputationAlgorithm method getUserReputation.

/**
 * Gets or calculates the user reputation.
 *
 * @param documentRef the document which the ratings are for
 * @param username Person to calculate the reputation for
 * @return AverageRating of the voter
 */
public AverageRating getUserReputation(DocumentReference documentRef, DocumentReference username) throws ReputationException {
    try {
        AverageRating aveRating = getRatingsManager(documentRef).getAverageRating(username, RatingsManager.RATING_REPUTATION_METHOD_AVERAGE);
        float oldRep = aveRating.getAverageVote();
        aveRating.setAverageVote(aveRating.getAverageVote() * 100 / getTotalReputation());
        totalReputation += aveRating.getAverageVote() - oldRep;
        return aveRating;
    } catch (RatingsException e) {
        throw new ReputationException(e);
    }
}
Also used : AverageRating(org.xwiki.ratings.AverageRating) RatingsException(org.xwiki.ratings.RatingsException) ReputationException(org.xwiki.ratings.ReputationException)

Example 9 with RatingsException

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

the class AbstractRatingsManager method getAverageRating.

@Override
public AverageRating getAverageRating(DocumentReference documentRef, String method, boolean create) throws RatingsException {
    try {
        if (isAverageRatingStored(documentRef)) {
            String className = getAverageRatingsClassName();
            XWikiDocument doc = getXWikiContext().getWiki().getDocument(documentRef, getXWikiContext());
            BaseObject averageRatingObject = doc.getObject(className, RatingsManager.AVERAGERATING_CLASS_FIELDNAME_AVERAGEVOTE_METHOD, method, false);
            if (averageRatingObject == null) {
                if (!create) {
                    return calcAverageRating(documentRef, method);
                }
                // initiate a new average rating object
                averageRatingObject = doc.newObject(className, getXWikiContext());
                averageRatingObject.setStringValue(RatingsManager.AVERAGERATING_CLASS_FIELDNAME_AVERAGEVOTE_METHOD, method);
            }
            return new StoredAverageRating(doc, averageRatingObject, getXWikiContext());
        } else {
            return calcAverageRating(documentRef, method);
        }
    } catch (XWikiException e) {
        throw new RatingsException(e);
    }
}
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 10 with RatingsException

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

the class DefaultRatingsManager method getRating.

@Override
public Rating getRating(String ratingId) throws RatingsException {
    try {
        int i1 = ratingId.indexOf(":");
        if (i1 == -1) {
            throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, RatingsException.ERROR_RATINGS_INVALID_RATING_ID, "Invalid rating ID, cannot parse rating id");
        }
        String docName = ratingId.substring(0, i1);
        String sObjectNb = ratingId.substring(i1 + 1);
        int objectNb = Integer.parseInt(sObjectNb);
        XWikiDocument doc = getXWiki().getDocument(docName, getXWikiContext());
        DocumentReference docRef = doc.getDocumentReference();
        if (doc.isNew()) {
            throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, RatingsException.ERROR_RATINGS_INVALID_RATING_ID, "Invalid rating ID, document does not exist");
        }
        BaseObject object = doc.getObject(getRatingsClassName(), objectNb);
        if (object == null) {
            throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, RatingsException.ERROR_RATINGS_INVALID_RATING_ID, "Invalid rating ID, could not find rating");
        }
        return new DefaultRating(docRef, object, getXWikiContext(), this);
    } catch (XWikiException e) {
        throw new RatingsException(e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) RatingsException(org.xwiki.ratings.RatingsException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

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