use of com.xpn.xwiki.doc.XWikiDocument 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.doc.XWikiDocument 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);
}
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class AverageRatingProtectionListener method onEvent.
@Override
public void onEvent(Event event, Object source, Object data) {
XWikiDocument document = (XWikiDocument) source;
BaseObject ratingObject = document.getXObject(RatingsManager.AVERAGE_RATINGS_CLASSREFERENCE);
if (ratingObject != null) {
// If the modification is not part of an official rating cancel it
if (!this.observationContext.isIn(PARENT)) {
XWikiDocument previousDocument = document.getOriginalDocument();
BaseObject previousObject = previousDocument.getXObject(RatingsManager.AVERAGE_RATINGS_CLASSREFERENCE);
if (previousObject != null) {
ratingObject.apply(previousObject, true);
}
}
}
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class DefaultRating method createObject.
/**
* Creates a new ratings object.
*
* @param documentName the document being rated
* @param author the author of the rating
* @param date the date when the rating took place
* @param vote the vote that the author gave
* @throws XWikiException when failig to create new object
*/
private void createObject(String documentName, String author, Date date, int vote) throws XWikiException {
XWikiDocument doc = getDocument();
BaseObject obj = doc.newXObject(RatingsManager.RATINGS_CLASSREFERENCE, context);
// read data from map
obj.setStringValue(RatingsManager.RATING_CLASS_FIELDNAME_AUTHOR, author);
obj.setDateValue(RatingsManager.RATING_CLASS_FIELDNAME_DATE, date);
obj.setIntValue(RatingsManager.RATING_CLASS_FIELDNAME_VOTE, vote);
obj.setStringValue(RatingsManager.RATING_CLASS_FIELDNAME_PARENT, documentName);
// set the internal variable
object = obj;
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class DefaultRatingsConfiguration method getConfigurationDocument.
/**
* Get configuration document.
*
* @param documentReference the documentReference for which to return the configuration document
* @return the configuration document
*/
public XWikiDocument getConfigurationDocument(DocumentReference documentReference) {
SpaceReference lastSpaceReference = documentReference.getLastSpaceReference();
while (lastSpaceReference.getType() == EntityType.SPACE) {
DocumentReference configurationDocumentReference = new DocumentReference(RatingsManager.RATINGS_CONFIG_SPACE_PAGE, lastSpaceReference);
XWikiDocument spaceConfigurationDocument = getDocument((EntityReference) configurationDocumentReference);
if (spaceConfigurationDocument != null && spaceConfigurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE) != null) {
return spaceConfigurationDocument;
}
if (lastSpaceReference.getParent().getType() == EntityType.SPACE) {
lastSpaceReference = new SpaceReference(lastSpaceReference.getParent());
} else {
break;
}
}
XWikiDocument globalConfigurationDocument = getDocument(RatingsManager.RATINGS_CONFIG_GLOBAL_REFERENCE);
return globalConfigurationDocument;
}
Aggregations