use of org.xwiki.bridge.event.DocumentDeletingEvent in project xwiki-platform by xwiki.
the class LegacyNotificationDispatcher method onEvent.
@Override
public void onEvent(Event event, Object source, Object data) {
XWikiDocument document = (XWikiDocument) source;
XWikiDocument originalDocument = document.getOriginalDocument();
XWikiContext context = (XWikiContext) data;
XWikiNotificationManager manager = getNotificationManager((XWikiContext) data);
if (manager != null) {
if (event instanceof DocumentCreatedEvent) {
manager.verify(document, originalDocument, XWikiDocChangeNotificationInterface.EVENT_NEW, context);
} else if (event instanceof DocumentUpdatedEvent) {
manager.verify(document, originalDocument, XWikiDocChangeNotificationInterface.EVENT_CHANGE, context);
} else if (event instanceof DocumentCreatingEvent) {
manager.preverify(document, originalDocument, XWikiDocChangeNotificationInterface.EVENT_NEW, context);
} else if (event instanceof DocumentUpdatingEvent) {
manager.preverify(document, originalDocument, XWikiDocChangeNotificationInterface.EVENT_CHANGE, context);
} else if (event instanceof DocumentDeletedEvent) {
manager.verify(new XWikiDocument(document.getDocumentReference()), document, XWikiDocChangeNotificationInterface.EVENT_DELETE, context);
} else if (event instanceof DocumentDeletingEvent) {
manager.preverify(document, new XWikiDocument(document.getDocumentReference()), XWikiDocChangeNotificationInterface.EVENT_DELETE, context);
} else if (event instanceof ActionExecutedEvent) {
manager.verify(document, ((ActionExecutedEvent) event).getActionName(), context);
} else if (event instanceof ActionExecutingEvent) {
manager.preverify(document, ((ActionExecutingEvent) event).getActionName(), context);
}
} else {
this.logger.error("Can't find old [XWikiNotificationManager] system");
}
}
use of org.xwiki.bridge.event.DocumentDeletingEvent in project xwiki-platform by xwiki.
the class XWikiMockitoTest method deleteDocument.
@Test
public void deleteDocument() throws Exception {
final DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
XWikiDocument document = mock(XWikiDocument.class);
when(document.getDocumentReference()).thenReturn(documentReference);
final XWikiDocument originalDocument = mock(XWikiDocument.class);
when(document.getOriginalDocument()).thenReturn(originalDocument);
this.xwiki.deleteDocument(document, this.context);
ObservationManager observation = this.mocker.getInstance(ObservationManager.class);
ArgumentMatcher<XWikiDocument> matcher = new ArgumentMatcher<XWikiDocument>() {
@Override
public boolean matches(XWikiDocument argument) {
return argument.getDocumentReference().equals(documentReference) && argument.getOriginalDocument() == originalDocument;
}
};
// Make sure the right events have been sent
verify(observation).notify(eq(new DocumentDeletingEvent(documentReference)), argThat(matcher), same(this.context));
verify(observation).notify(eq(new DocumentDeletedEvent(documentReference)), argThat(matcher), same(this.context));
verifyNoMoreInteractions(observation);
}
use of org.xwiki.bridge.event.DocumentDeletingEvent in project xwiki-platform by xwiki.
the class LegacyNotificationDispatcher method getEvents.
@Override
public List<Event> getEvents() {
return new ArrayList<Event>() {
{
add(new DocumentDeletedEvent());
add(new DocumentCreatedEvent());
add(new DocumentUpdatedEvent());
add(new DocumentDeletingEvent());
add(new DocumentCreatingEvent());
add(new DocumentUpdatingEvent());
add(new ActionExecutedEvent());
add(new ActionExecutingEvent());
}
};
}
use of org.xwiki.bridge.event.DocumentDeletingEvent in project xwiki-platform by xwiki.
the class XWikiTest method testDeleteDocumentSendsObservationEvents.
/**
* We only verify here that the deleteDocument API calls the Observation component.
*/
public void testDeleteDocumentSendsObservationEvents() throws Exception {
Mock mockListener = mock(EventListener.class);
mockListener.stubs().method("getName").will(returnValue("testlistener"));
DocumentReference ref = new DocumentReference("xwikitest", "Another", "Document");
mockListener.expects(once()).method("getEvents").will(returnValue(Arrays.asList(new DocumentDeletedEvent(ref), new DocumentDeletingEvent(ref))));
ObservationManager om = getComponentManager().getInstance(ObservationManager.class);
om.addListener((EventListener) mockListener.proxy());
XWikiDocument document = new XWikiDocument(new DocumentReference("xwikitest", "Another", "Document"));
document.setContent("the content");
// Not expectation on mock Listener since we're not subscribed to Document save events
this.xwiki.saveDocument(document, getContext());
// Ensure that the onEvent method has been called before and after the deletion
mockListener.expects(once()).method("onEvent").with(isA(DocumentDeletingEvent.class), isA(XWikiDocument.class), isA(XWikiContext.class));
mockListener.expects(once()).method("onEvent").with(isA(DocumentDeletedEvent.class), isA(XWikiDocument.class), isA(XWikiContext.class));
this.xwiki.deleteDocument(document, false, getContext());
}
use of org.xwiki.bridge.event.DocumentDeletingEvent in project xwiki-platform by xwiki.
the class XWiki method deleteDocument.
public void deleteDocument(XWikiDocument doc, boolean totrash, XWikiContext context) throws XWikiException {
String currentWiki = null;
currentWiki = context.getWikiId();
try {
context.setWikiId(doc.getDocumentReference().getWikiReference().getName());
// The source document is a new empty XWikiDocument to follow
// DocumentUpdatedEvent policy: source document in new document and the old version is available using
// doc.getOriginalDocument()
XWikiDocument blankDoc = new XWikiDocument(doc.getDocumentReference());
// Again to follow general event policy, new document author is the user who modified the document
// (here the modification is delete)
blankDoc.setOriginalDocument(doc.getOriginalDocument());
blankDoc.setAuthorReference(context.getUserReference());
blankDoc.setContentAuthorReference(context.getUserReference());
ObservationManager om = getObservationManager();
// an XWikiDocument as source and an XWikiContext as data.
if (om != null) {
om.notify(new DocumentDeletingEvent(doc.getDocumentReference()), blankDoc, context);
}
if (hasRecycleBin(context) && totrash) {
// Extract any existing batchId from the context.
String batchId = Utils.getComponent(BatchOperationExecutor.class).getCurrentBatchId();
// Save to recycle bin together with any determined batch ID.
getRecycleBinStore().saveToRecycleBin(doc, context.getUser(), new Date(), batchId, context, true);
}
getStore().deleteXWikiDoc(doc, context);
try {
// an XWikiDocument as source and an XWikiContext as data.
if (om != null) {
om.notify(new DocumentDeletedEvent(doc.getDocumentReference()), blankDoc, context);
}
} catch (Exception ex) {
LOGGER.error("Failed to send document delete notifications for document [{}]", doc.getDocumentReference(), ex);
}
} finally {
context.setWikiId(currentWiki);
}
}
Aggregations