Search in sources :

Example 1 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class AnnotationEventGeneratorEventListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    // Don`t rely on the context from the data parameter.
    XWikiContext context = (XWikiContext) execution.getContext().getProperty("xwikicontext");
    String currentWiki = context.getWikiId();
    try {
        XWikiDocument document = (XWikiDocument) source;
        String wikiOfAffectedDocument = document.getDocumentReference().getWikiReference().getName();
        // Always work on the wiki of the source document. The Annotation Application's configuration looks at the
        // context to provide values for the current wiki. Objects could be modified cross-wiki and the context
        // database might not be right.
        context.setWikiId(wikiOfAffectedDocument);
        // Only work if the Annotations Application is installed on the wiki.
        if (!annotationConfiguration.isInstalled()) {
            return;
        }
        // Extract the BaseObjectReference to be able to inspect the XClassReference.
        BaseObjectReference objectReference = getBaseObjectReference((XObjectEvent) event);
        DocumentReference objectClassReference = objectReference.getXClassReference();
        // Only interested in objects that are of the same class as the currently configured annotation class.
        if (!objectClassReference.equals(annotationConfiguration.getAnnotationClassReference())) {
            return;
        }
        // The object is needed for the final check. See below.
        BaseObject object = document.getXObject(objectReference);
        // Build the new event to launch using the current document reference and object number.
        Event newEvent = null;
        String documentReference = defaultEntityReferenceSerializer.serialize(document.getDocumentReference());
        String number = String.valueOf(objectReference.getObjectNumber());
        if (event instanceof XObjectAddedEvent) {
            newEvent = new AnnotationAddedEvent(documentReference, number);
        } else if (event instanceof XObjectUpdatedEvent) {
            newEvent = new AnnotationUpdatedEvent(documentReference, number);
        } else if (event instanceof XObjectDeletedEvent) {
            // Current document might be deleted. Always use the original document for *Deleted events.
            object = document.getOriginalDocument().getXObject(objectReference);
            newEvent = new AnnotationDeletedEvent(documentReference, number);
        }
        // Handle specially the default annotations class which coincides with the default comments class. We need
        // to avoid mistaking comments for annotations.
        DocumentReference defaultCommentsClassReference = context.getWiki().getCommentsClass(context).getDocumentReference();
        if (defaultCommentsClassReference.equals(object.getXClassReference())) {
            // A comment is considered an annotation when it has a text selection.
            String selection = object.getStringValue(Annotation.SELECTION_FIELD);
            if (selection == null || selection.trim().length() == 0) {
                // This is a simple comment. Skip it.
                return;
            }
        }
        // Launch the new event.
        observationManager.get().notify(newEvent, source, context);
    } catch (Exception e) {
        logger.error("Failed to handle event of type [{}]", event.getClass().getName(), e);
    } finally {
        // Restore the context database.
        context.setWikiId(currentWiki);
    }
}
Also used : AnnotationUpdatedEvent(org.xwiki.annotation.event.AnnotationUpdatedEvent) XObjectAddedEvent(com.xpn.xwiki.internal.event.XObjectAddedEvent) XWikiContext(com.xpn.xwiki.XWikiContext) BaseObject(com.xpn.xwiki.objects.BaseObject) XObjectUpdatedEvent(com.xpn.xwiki.internal.event.XObjectUpdatedEvent) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObjectReference(com.xpn.xwiki.objects.BaseObjectReference) AnnotationAddedEvent(org.xwiki.annotation.event.AnnotationAddedEvent) AnnotationAddedEvent(org.xwiki.annotation.event.AnnotationAddedEvent) XObjectEvent(com.xpn.xwiki.internal.event.XObjectEvent) AnnotationUpdatedEvent(org.xwiki.annotation.event.AnnotationUpdatedEvent) XObjectUpdatedEvent(com.xpn.xwiki.internal.event.XObjectUpdatedEvent) XObjectAddedEvent(com.xpn.xwiki.internal.event.XObjectAddedEvent) XObjectDeletedEvent(com.xpn.xwiki.internal.event.XObjectDeletedEvent) AnnotationDeletedEvent(org.xwiki.annotation.event.AnnotationDeletedEvent) Event(org.xwiki.observation.event.Event) AnnotationDeletedEvent(org.xwiki.annotation.event.AnnotationDeletedEvent) XObjectDeletedEvent(com.xpn.xwiki.internal.event.XObjectDeletedEvent) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 2 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class DefaultIOService method getAnnotation.

@Override
public Annotation getAnnotation(String target, String annotationID) throws IOServiceException {
    try {
        if (annotationID == null || target == null) {
            return null;
        }
        // parse the target and extract the local reference serialized from it, by the same rules
        EntityReference targetReference = referenceResolver.resolve(target, EntityType.DOCUMENT);
        // build the target identifier for the annotation
        String localTargetId = target;
        // and the name of the document where it should be stored
        String docName = target;
        if (targetReference.getType() == EntityType.DOCUMENT || targetReference.getType() == EntityType.OBJECT_PROPERTY) {
            localTargetId = localSerializer.serialize(targetReference);
            docName = serializer.serialize(targetReference.extractReference(EntityType.DOCUMENT));
        }
        // get the document
        XWikiContext deprecatedContext = getXWikiContext();
        XWikiDocument document = deprecatedContext.getWiki().getDocument(docName, deprecatedContext);
        // and the annotation class objects in it
        // parse the annotation id as object index
        BaseObject object = document.getXObject(configuration.getAnnotationClassReference(), Integer.valueOf(annotationID.toString()));
        if (object == null || !localTargetId.equals(object.getStringValue(Annotation.TARGET_FIELD))) {
            return null;
        }
        // use the object number as annotation id
        return loadAnnotationFromObject(object, deprecatedContext);
    } catch (NumberFormatException e) {
        throw new IOServiceException("Could not parse annotation id " + annotationID, e);
    } catch (XWikiException e) {
        throw new IOServiceException("An exception has occurred while loading the annotation with id " + annotationID, e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) IOServiceException(org.xwiki.annotation.io.IOServiceException) EntityReference(org.xwiki.model.reference.EntityReference) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 3 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class DefaultIOService method updateAnnotations.

/**
 * {@inheritDoc}
 * <p>
 * Implementation which gets all the annotation class objects in the document pointed by the target, and matches
 * their ids against the ids in the passed collection of annotations. If they match, they are updated with the new
 * data in the annotations in annotation.
 * </p>
 *
 * @see org.xwiki.annotation.io.IOService#updateAnnotations(String, java.util.Collection)
 */
@Override
public void updateAnnotations(String target, Collection<Annotation> annotations) throws IOServiceException {
    try {
        EntityReference targetReference = referenceResolver.resolve(target, EntityType.DOCUMENT);
        // get the document name from the parsed reference
        String docName = target;
        if (targetReference.getType() == EntityType.DOCUMENT || targetReference.getType() == EntityType.OBJECT_PROPERTY) {
            docName = serializer.serialize(targetReference.extractReference(EntityType.DOCUMENT));
        }
        // get the document pointed to by the target
        XWikiContext deprecatedContext = getXWikiContext();
        XWikiDocument document = deprecatedContext.getWiki().getDocument(docName, deprecatedContext);
        List<String> updateNotifs = new ArrayList<String>();
        boolean updated = false;
        for (Annotation annotation : annotations) {
            // parse annotation id as string. If cannot parse, then ignore annotation, is not valid
            int annId = 0;
            try {
                annId = Integer.parseInt(annotation.getId());
            } catch (NumberFormatException e) {
                continue;
            }
            BaseObject object = document.getXObject(configuration.getAnnotationClassReference(), annId);
            if (object == null) {
                continue;
            }
            updated = updateObject(object, annotation, deprecatedContext) || updated;
            updateNotifs.add(annotation.getId());
        }
        if (updated) {
            // set the author of the document to the current user
            document.setAuthor(deprecatedContext.getUser());
            deprecatedContext.getWiki().saveDocument(document, "Updated annotations", deprecatedContext);
        }
    } catch (XWikiException e) {
        throw new IOServiceException("An exception has occurred while updating the annotation", e);
    }
}
Also used : ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) Annotation(org.xwiki.annotation.Annotation) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) IOServiceException(org.xwiki.annotation.io.IOServiceException) EntityReference(org.xwiki.model.reference.EntityReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 4 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class AbstractAnnotationRESTResource method setUpDocuments.

/**
 * Helper function to prepare the XWiki documents and translations on the context and velocity context. <br>
 * TODO: check how this code could be written only once (not duplicate the prepareDocuments function in XWiki)
 *
 * @param docName the full name of the document to prepare context for
 * @param language the language of the document
 * @throws XWikiException if anything goes wrong accessing documents
 */
private void setUpDocuments(String docName, String language) throws XWikiException {
    XWikiContext context = xcontextProvider.get();
    XWiki xwiki = context.getWiki();
    // prepare the messaging tools and set them on context
    xwiki.prepareResources(context);
    XWikiDocument doc = xwiki.getDocument(docName, context);
    // setup the xwiki context
    context.put("doc", doc);
    context.put("cdoc", doc);
    XWikiDocument tdoc = doc.getTranslatedDocument(language, context);
    context.put("tdoc", tdoc);
    // and render the xwikivars to have all the variables set ($has*, $blacklistedSpaces, etc)
    context.getWiki().renderTemplate("xwikivars.vm", context);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki)

Example 5 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class AbstractAnnotationRESTResource method updateContext.

/**
 * Helper method to make sure that the context is set to the right document and database name.
 *
 * @param wiki the REST wikiName path parameter
 * @param space the REST spaceName path parameter
 * @param page the REST pageName path parameter
 */
protected void updateContext(DocumentReference documentReference) {
    try {
        // Set the database to the current wiki.
        XWikiContext deprecatedContext = (XWikiContext) execution.getContext().getProperty("xwikicontext");
        deprecatedContext.setWikiId(documentReference.getWikiReference().getName());
        // Set the document to the current document.
        XWiki xwiki = deprecatedContext.getWiki();
        XWikiDocument currentDocument = xwiki.getDocument(documentReference, deprecatedContext);
        deprecatedContext.setDoc(currentDocument);
    } catch (Exception e) {
        // Just log it.
        getLogger().error("Failed to update the context for page [{}].", documentReference, e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) AnnotationServiceException(org.xwiki.annotation.AnnotationServiceException)

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