Search in sources :

Example 36 with XWikiException

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);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWiki(com.xpn.xwiki.XWiki) RatingsException(org.xwiki.ratings.RatingsException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 37 with XWikiException

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);
    }
}
Also used : ContextualLocalizationManager(org.xwiki.localization.ContextualLocalizationManager) RatingsException(org.xwiki.ratings.RatingsException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 38 with XWikiException

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;
}
Also used : Rating(org.xwiki.ratings.Rating) ArrayList(java.util.ArrayList) RatingsException(org.xwiki.ratings.RatingsException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 39 with XWikiException

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);
    }
}
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 40 with XWikiException

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);
    }
}
Also used : ArrayList(java.util.ArrayList) RatingsException(org.xwiki.ratings.RatingsException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

XWikiException (com.xpn.xwiki.XWikiException)442 XWikiContext (com.xpn.xwiki.XWikiContext)156 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)147 DocumentReference (org.xwiki.model.reference.DocumentReference)98 BaseObject (com.xpn.xwiki.objects.BaseObject)88 IOException (java.io.IOException)57 QueryException (org.xwiki.query.QueryException)57 ArrayList (java.util.ArrayList)56 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)51 XWiki (com.xpn.xwiki.XWiki)48 XWikiRestException (org.xwiki.rest.XWikiRestException)44 Session (org.hibernate.Session)42 Document (com.xpn.xwiki.api.Document)38 InitializationException (org.xwiki.component.phase.InitializationException)36 WebApplicationException (javax.ws.rs.WebApplicationException)32 SQLException (java.sql.SQLException)31 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)30 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)29 UnexpectedException (org.xwiki.store.UnexpectedException)29 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)25