use of com.xpn.xwiki.XWikiException 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 com.xpn.xwiki.XWikiException 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 com.xpn.xwiki.XWikiException 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;
}
use of com.xpn.xwiki.XWikiException 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);
}
}
use of com.xpn.xwiki.XWikiException 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);
}
}
Aggregations