Search in sources :

Example 56 with XWikiDocument

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);
    }
}
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 57 with XWikiDocument

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);
    }
}
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 58 with XWikiDocument

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

Example 59 with XWikiDocument

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

Example 60 with XWikiDocument

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;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) SpaceReference(org.xwiki.model.reference.SpaceReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)869 DocumentReference (org.xwiki.model.reference.DocumentReference)469 BaseObject (com.xpn.xwiki.objects.BaseObject)318 Test (org.junit.Test)284 XWikiContext (com.xpn.xwiki.XWikiContext)232 XWikiException (com.xpn.xwiki.XWikiException)178 ArrayList (java.util.ArrayList)99 XWiki (com.xpn.xwiki.XWiki)97 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)86 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)71 Document (com.xpn.xwiki.api.Document)48 EntityReference (org.xwiki.model.reference.EntityReference)48 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)41 Date (java.util.Date)41 IOException (java.io.IOException)40 HashMap (java.util.HashMap)33 QueryException (org.xwiki.query.QueryException)27 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)25 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)23 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)23