Search in sources :

Example 26 with BaseObject

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);
    }
}
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 27 with BaseObject

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);
    }
}
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 28 with BaseObject

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);
            }
        }
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 29 with BaseObject

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;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 30 with BaseObject

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"));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseProperty(com.xpn.xwiki.objects.BaseProperty) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Aggregations

BaseObject (com.xpn.xwiki.objects.BaseObject)484 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)309 DocumentReference (org.xwiki.model.reference.DocumentReference)225 Test (org.junit.Test)137 XWikiException (com.xpn.xwiki.XWikiException)102 XWikiContext (com.xpn.xwiki.XWikiContext)99 ArrayList (java.util.ArrayList)92 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)69 EntityReference (org.xwiki.model.reference.EntityReference)42 XWiki (com.xpn.xwiki.XWiki)41 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)40 List (java.util.List)30 Date (java.util.Date)24 BaseProperty (com.xpn.xwiki.objects.BaseProperty)23 Document (com.xpn.xwiki.api.Document)22 HashMap (java.util.HashMap)21 QueryException (org.xwiki.query.QueryException)18 AbstractComponentTest (com.celements.common.test.AbstractComponentTest)16 IOException (java.io.IOException)16 Vector (java.util.Vector)16