use of com.xpn.xwiki.objects.BaseObject 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.objects.BaseObject 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.objects.BaseObject 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.objects.BaseObject 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.objects.BaseObject in project xwiki-platform by xwiki.
the class DefaultRatingsConfigurationTest method getConfigurationParameter.
@Test
public void getConfigurationParameter() throws Exception {
DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Space1", "Space2"), "Page");
XWikiDocument spaceConfigurationDocument = mock(XWikiDocument.class, "Space Configuration Document");
DocumentReference configurationDocumentReference = new DocumentReference("wiki", "Space1", "WebPreferences");
when(xcontext.getWiki().getDocument((EntityReference) configurationDocumentReference, xcontext)).thenReturn(spaceConfigurationDocument);
BaseObject configurationObject = mock(BaseObject.class);
when(spaceConfigurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE)).thenReturn(configurationObject);
BaseProperty displayRatings = mock(BaseProperty.class);
when(configurationObject.get("displayRatings")).thenReturn(displayRatings);
when(displayRatings.getValue()).thenReturn("1");
assertEquals("1", mocker.getComponentUnderTest().getConfigurationParameter(documentReference, "displayRatings", "1"));
}
Aggregations