use of org.xwiki.annotation.event.AnnotationUpdatedEvent 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);
}
}
Aggregations