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;
}
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;
}
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);
}
}
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);
}
}
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;
}
Aggregations