use of com.xpn.xwiki.internal.event.AttachmentAddedEvent in project xwiki-platform by xwiki.
the class ActivityStreamImpl method onEvent.
@Override
public void onEvent(Event event, Object source, Object data) {
// Do not record some ignored events
ObservationContext observationContext = Utils.getComponent(ObservationContext.class);
if (observationContext.isIn(IGNORED_EVENTS)) {
return;
}
XWikiDocument currentDoc = (XWikiDocument) source;
XWikiDocument originalDoc = currentDoc.getOriginalDocument();
XWikiContext context = (XWikiContext) data;
String wiki = context.getWikiId();
String msgPrefix = "activitystream.event.";
String streamName = getStreamName(currentDoc.getSpace(), context);
// If we haven't found a stream to store the event or if both currentDoc and originalDoc are null: exit
if (streamName == null) {
return;
}
Execution executionContext = Utils.getComponent(Execution.class);
// Take events into account only once in a cluster
if (!Utils.getComponent(RemoteObservationManagerContext.class).isRemoteState() && !executionContext.getContext().hasProperty(AbstractEventStreamEvent.EVENT_LOOP_CONTEXT_LOCK_PROPERTY)) {
executionContext.getContext().setProperty(AbstractEventStreamEvent.EVENT_LOOP_CONTEXT_LOCK_PROPERTY, true);
String eventType;
String displayTitle;
String additionalIdentifier = null;
if (event instanceof DocumentCreatedEvent) {
eventType = ActivityEventType.CREATE;
displayTitle = currentDoc.getRenderedTitle(context);
} else if (event instanceof DocumentUpdatedEvent) {
eventType = ActivityEventType.UPDATE;
displayTitle = originalDoc.getRenderedTitle(context);
} else if (event instanceof DocumentDeletedEvent) {
eventType = ActivityEventType.DELETE;
displayTitle = originalDoc.getRenderedTitle(context);
// When we receive a DELETE event, the given document is blank and does not have version & hidden tag
// properly set.
currentDoc.setVersion(originalDoc.getVersion());
currentDoc.setHidden(originalDoc.isHidden());
} else if (event instanceof CommentAddedEvent) {
eventType = ActivityEventType.ADD_COMMENT;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((CommentAddedEvent) event).getIdentifier();
} else if (event instanceof CommentDeletedEvent) {
eventType = ActivityEventType.DELETE_COMMENT;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((CommentDeletedEvent) event).getIdentifier();
} else if (event instanceof CommentUpdatedEvent) {
eventType = ActivityEventType.UPDATE_COMMENT;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((CommentUpdatedEvent) event).getIdentifier();
} else if (event instanceof AttachmentAddedEvent) {
eventType = ActivityEventType.ADD_ATTACHMENT;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((AttachmentAddedEvent) event).getName();
} else if (event instanceof AttachmentDeletedEvent) {
eventType = ActivityEventType.DELETE_ATTACHMENT;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((AttachmentDeletedEvent) event).getName();
} else if (event instanceof AttachmentUpdatedEvent) {
eventType = ActivityEventType.UPDATE_ATTACHMENT;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((AttachmentUpdatedEvent) event).getName();
} else if (event instanceof AnnotationAddedEvent) {
eventType = ActivityEventType.ADD_ANNOTATION;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((AnnotationAddedEvent) event).getIdentifier();
} else if (event instanceof AnnotationDeletedEvent) {
eventType = ActivityEventType.DELETE_ANNOTATION;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((AnnotationDeletedEvent) event).getIdentifier();
} else {
// update annotation
eventType = ActivityEventType.UPDATE_ANNOTATION;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((AnnotationUpdatedEvent) event).getIdentifier();
}
List<String> params = new ArrayList<String>();
params.add(displayTitle);
if (additionalIdentifier != null) {
params.add(additionalIdentifier);
}
try {
addDocumentActivityEvent(streamName, currentDoc, eventType, msgPrefix + eventType, params, context);
} catch (ActivityStreamException e) {
LOGGER.error("Exception while trying to add a document activity event, updated document: [" + wiki + ":" + currentDoc + "]");
}
executionContext.getContext().removeProperty(AbstractEventStreamEvent.EVENT_LOOP_CONTEXT_LOCK_PROPERTY);
}
}
use of com.xpn.xwiki.internal.event.AttachmentAddedEvent in project xwiki-platform by xwiki.
the class SolrIndexEventListener method onEvent.
@Override
public void onEvent(Event event, Object source, Object data) {
try {
if (event instanceof DocumentUpdatedEvent) {
XWikiDocument document = (XWikiDocument) source;
if (Locale.ROOT.equals(document.getLocale())) {
// Index all the translations of a document when its default translation has been updated because
// the default translation holds meta data shared by all translations (attachments, objects).
indexTranslations(document, (XWikiContext) data);
} else {
// Index only the updated translation.
this.solrIndexer.get().index(document.getDocumentReferenceWithLocale(), false);
}
} else if (event instanceof DocumentCreatedEvent) {
XWikiDocument document = (XWikiDocument) source;
if (!Locale.ROOT.equals(document.getLocale())) {
// If a new translation is added to a document reindex the whole document (could be optimized a bit
// by reindexing only the parent locales but that would always include objects and attachments
// anyway)
indexTranslations(document, (XWikiContext) data);
} else {
this.solrIndexer.get().index(document.getDocumentReferenceWithLocale(), false);
}
} else if (event instanceof DocumentDeletedEvent) {
XWikiDocument document = ((XWikiDocument) source).getOriginalDocument();
// We must pass the document reference with the REAL locale because when the indexer is going to delete
// the document from the Solr index (later, on a different thread) the real locale won't be accessible
// anymore since the XWiki document has been already deleted from the database. The real locale (taken
// from the XWiki document) is used to compute the id of the Solr document when the document reference
// locale is ROOT (i.e. for default document translations).
// Otherwise the document won't be deleted from the Solr index (because the computed id won't match any
// document from the Solr index) and we're going to have deleted documents that are still in the Solr
// index. These documents will be filtered from the search results but not from the facet counts.
// See XWIKI-10003: Cache problem with Solr facet filter results count
this.solrIndexer.get().delete(new DocumentReference(document.getDocumentReference(), document.getRealLocale()), false);
} else if (event instanceof AttachmentUpdatedEvent || event instanceof AttachmentAddedEvent) {
XWikiDocument document = (XWikiDocument) source;
String fileName = ((AbstractAttachmentEvent) event).getName();
XWikiAttachment attachment = document.getAttachment(fileName);
this.solrIndexer.get().index(attachment.getReference(), false);
} else if (event instanceof AttachmentDeletedEvent) {
XWikiDocument document = ((XWikiDocument) source).getOriginalDocument();
String fileName = ((AbstractAttachmentEvent) event).getName();
XWikiAttachment attachment = document.getAttachment(fileName);
this.solrIndexer.get().delete(attachment.getReference(), false);
} else if (event instanceof XObjectUpdatedEvent || event instanceof XObjectAddedEvent) {
EntityEvent entityEvent = (EntityEvent) event;
this.solrIndexer.get().index(entityEvent.getReference(), false);
} else if (event instanceof XObjectDeletedEvent) {
EntityEvent entityEvent = (EntityEvent) event;
this.solrIndexer.get().delete(entityEvent.getReference(), false);
} else if (event instanceof XObjectPropertyUpdatedEvent || event instanceof XObjectPropertyAddedEvent) {
EntityEvent entityEvent = (EntityEvent) event;
this.solrIndexer.get().index(entityEvent.getReference(), false);
} else if (event instanceof XObjectPropertyDeletedEvent) {
EntityEvent entityEvent = (EntityEvent) event;
this.solrIndexer.get().delete(entityEvent.getReference(), false);
} else if (event instanceof WikiDeletedEvent) {
String wikiName = (String) source;
WikiReference wikiReference = new WikiReference(wikiName);
this.solrIndexer.get().delete(wikiReference, false);
}
} catch (Exception e) {
this.logger.error("Failed to handle event [{}] with source [{}]", event, source, e);
}
}
Aggregations