use of org.xwiki.bridge.event.DocumentCreatingEvent 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.DocumentCreatingEvent in project xwiki-platform by xwiki.
the class XWikiTest method testSaveDocumentSendsObservationEvents.
/**
* We only verify here that the saveDocument API calls the Observation component.
*/
public void testSaveDocumentSendsObservationEvents() throws Exception {
Mock mockListener = mock(EventListener.class);
mockListener.stubs().method("getName").will(returnValue("testlistener"));
DocumentReference ref = new DocumentReference("xwikitest", "Some", "Document");
mockListener.expects(once()).method("getEvents").will(returnValue(Arrays.asList(new DocumentCreatedEvent(ref), new DocumentCreatingEvent(ref))));
ObservationManager om = getComponentManager().getInstance(ObservationManager.class);
om.addListener((EventListener) mockListener.proxy());
XWikiDocument document = new XWikiDocument(new DocumentReference("xwikitest", "Some", "Document"));
document.setContent("the content");
// Ensure that the onEvent method has been called before and after the save
mockListener.expects(once()).method("onEvent").with(isA(DocumentCreatingEvent.class), same(document), isA(XWikiContext.class));
mockListener.expects(once()).method("onEvent").with(isA(DocumentCreatedEvent.class), same(document), isA(XWikiContext.class));
this.xwiki.saveDocument(document, getContext());
}
use of org.xwiki.bridge.event.DocumentCreatingEvent 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.DocumentCreatingEvent in project xwiki-platform by xwiki.
the class XWiki method saveDocument.
/**
* @param document the document to save
* @param comment the comment to associated to the new version of the saved document
* @param isMinorEdit true if the new version is a minor version
* @param context see {@link XWikiContext}
*/
public void saveDocument(XWikiDocument document, String comment, boolean isMinorEdit, XWikiContext context) throws XWikiException {
String currentWiki = context.getWikiId();
try {
// Switch to document wiki
context.setWikiId(document.getDocumentReference().getWikiReference().getName());
// Setting comment & minor edit before saving
document.setComment(StringUtils.defaultString(comment));
document.setMinorEdit(isMinorEdit);
// We need to save the original document since saveXWikiDoc() will reset it and we
// need that original document for the notification below.
XWikiDocument originalDocument = document.getOriginalDocument();
// (which is not a good practice)
if (originalDocument == null) {
originalDocument = getDocument(new DocumentReference(document.getDocumentReference(), document.getLocale()), context);
document.setOriginalDocument(originalDocument);
}
ObservationManager om = getObservationManager();
if (om != null) {
CancelableEvent documentEvent;
if (originalDocument.isNew()) {
documentEvent = new DocumentCreatingEvent(document.getDocumentReference());
} else {
documentEvent = new DocumentUpdatingEvent(document.getDocumentReference());
}
om.notify(documentEvent, document, context);
// If the action has been canceled by the user then don't perform any save and throw an exception
if (documentEvent.isCanceled()) {
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_DOC, String.format("An Event Listener has cancelled the document save for [%s]. Reason: [%s]", document.getDocumentReference(), documentEvent.getReason()));
}
}
// Put attachments to remove in recycle bin
if (hasAttachmentRecycleBin(context)) {
for (XWikiAttachmentToRemove attachment : document.getAttachmentsToRemove()) {
if (attachment.isToRecycleBin()) {
getAttachmentRecycleBinStore().saveToRecycleBin(attachment.getAttachment(), context.getUser(), new Date(), context, true);
}
}
}
// Actually save the document.
getStore().saveXWikiDoc(document, context);
// Since the store#saveXWikiDoc resets originalDocument, we need to temporarily put it
// back to send notifications.
XWikiDocument newOriginal = document.getOriginalDocument();
try {
document.setOriginalDocument(originalDocument);
if (om != null) {
if (originalDocument.isNew()) {
om.notify(new DocumentCreatedEvent(document.getDocumentReference()), document, context);
} else {
om.notify(new DocumentUpdatedEvent(document.getDocumentReference()), document, context);
}
}
} catch (Exception ex) {
LOGGER.error("Failed to send document save notification for document [" + getDefaultEntityReferenceSerializer().serialize(document.getDocumentReference()) + "]", ex);
} finally {
document.setOriginalDocument(newOriginal);
}
} finally {
context.setWikiId(currentWiki);
}
}
Aggregations