Search in sources :

Example 1 with DocumentUpdatingEvent

use of org.xwiki.bridge.event.DocumentUpdatingEvent 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");
    }
}
Also used : DocumentUpdatingEvent(org.xwiki.bridge.event.DocumentUpdatingEvent) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) DocumentDeletingEvent(org.xwiki.bridge.event.DocumentDeletingEvent) ActionExecutedEvent(org.xwiki.bridge.event.ActionExecutedEvent) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiNotificationManager(com.xpn.xwiki.notify.XWikiNotificationManager) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) ActionExecutingEvent(org.xwiki.bridge.event.ActionExecutingEvent) DocumentCreatingEvent(org.xwiki.bridge.event.DocumentCreatingEvent)

Example 2 with DocumentUpdatingEvent

use of org.xwiki.bridge.event.DocumentUpdatingEvent 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());
        }
    };
}
Also used : DocumentUpdatingEvent(org.xwiki.bridge.event.DocumentUpdatingEvent) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) ArrayList(java.util.ArrayList) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) DocumentDeletingEvent(org.xwiki.bridge.event.DocumentDeletingEvent) ActionExecutedEvent(org.xwiki.bridge.event.ActionExecutedEvent) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) ActionExecutingEvent(org.xwiki.bridge.event.ActionExecutingEvent) DocumentCreatingEvent(org.xwiki.bridge.event.DocumentCreatingEvent)

Example 3 with DocumentUpdatingEvent

use of org.xwiki.bridge.event.DocumentUpdatingEvent in project xwiki-platform by xwiki.

the class XWikiMockitoTest method rollbackFiresEvents.

/**
 * Verify that {@link XWiki#rollback(XWikiDocument, String, XWikiContext)} fires the right events.
 */
@Test
public void rollbackFiresEvents() throws Exception {
    ObservationManager observationManager = mocker.getInstance(ObservationManager.class);
    DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getDocumentReference()).thenReturn(documentReference);
    XWikiDocument originalDocument = mock(XWikiDocument.class);
    // Mark the document as existing so that the roll-back method will fire an update event.
    when(originalDocument.isNew()).thenReturn(false);
    XWikiDocument result = mock(XWikiDocument.class);
    when(result.clone()).thenReturn(result);
    when(result.getDocumentReference()).thenReturn(documentReference);
    when(result.getOriginalDocument()).thenReturn(originalDocument);
    String revision = "3.5";
    when(this.documentRevisionProvider.getRevision(document, revision)).thenReturn(result);
    this.mocker.registerMockComponent(ContextualLocalizationManager.class);
    xwiki.rollback(document, revision, context);
    verify(observationManager).notify(new DocumentRollingBackEvent(documentReference, revision), result, context);
    verify(observationManager).notify(new DocumentUpdatingEvent(documentReference), result, context);
    verify(observationManager).notify(new DocumentUpdatedEvent(documentReference), result, context);
    verify(observationManager).notify(new DocumentRolledBackEvent(documentReference, revision), result, context);
}
Also used : DocumentUpdatingEvent(org.xwiki.bridge.event.DocumentUpdatingEvent) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentRollingBackEvent(org.xwiki.bridge.event.DocumentRollingBackEvent) DocumentRolledBackEvent(org.xwiki.bridge.event.DocumentRolledBackEvent) ObservationManager(org.xwiki.observation.ObservationManager) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 4 with DocumentUpdatingEvent

use of org.xwiki.bridge.event.DocumentUpdatingEvent in project xwiki-platform by xwiki.

the class LinkCheckerEventListenerTest method testOnEvent.

@Test
public void testOnEvent() throws Exception {
    final DocumentModelBridge documentModelBridge = getMockery().mock(DocumentModelBridge.class);
    final EntityReferenceSerializer serializer = getComponentManager().getInstance(EntityReferenceSerializer.TYPE_STRING);
    final DocumentReference reference = new DocumentReference("wiki", "space", "page");
    final LinkStateManager linkStateManager = getComponentManager().getInstance(LinkStateManager.class);
    final Map<String, Map<String, LinkState>> states = new HashMap<String, Map<String, LinkState>>();
    Map<String, LinkState> referenceStates1 = new HashMap<String, LinkState>();
    referenceStates1.put("wiki1:space1.page1", new LinkState(200, System.currentTimeMillis()));
    referenceStates1.put("wiki2:space2.page2", new LinkState(200, System.currentTimeMillis()));
    states.put("url1", referenceStates1);
    Map<String, LinkState> referenceStates2 = new HashMap<String, LinkState>();
    referenceStates2.put("wiki1:space1.page1", new LinkState(200, System.currentTimeMillis()));
    states.put("url2", referenceStates2);
    getMockery().checking(new Expectations() {

        {
            oneOf(documentModelBridge).getDocumentReference();
            will(returnValue(reference));
            oneOf(serializer).serialize(reference);
            will(returnValue("wiki1:space1.page1"));
            oneOf(linkStateManager).getLinkStates();
            will(returnValue(states));
        }
    });
    this.listener.onEvent(new DocumentUpdatingEvent(), documentModelBridge, null);
    Assert.assertEquals(1, states.size());
    Assert.assertEquals(1, states.get("url1").size());
    Assert.assertNull(states.get("url2"));
}
Also used : EntityReferenceSerializer(org.xwiki.model.reference.EntityReferenceSerializer) Expectations(org.jmock.Expectations) DocumentUpdatingEvent(org.xwiki.bridge.event.DocumentUpdatingEvent) LinkStateManager(org.xwiki.rendering.transformation.linkchecker.LinkStateManager) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) LinkState(org.xwiki.rendering.transformation.linkchecker.LinkState) Test(org.junit.Test)

Example 5 with DocumentUpdatingEvent

use of org.xwiki.bridge.event.DocumentUpdatingEvent 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);
    }
}
Also used : DocumentUpdatingEvent(org.xwiki.bridge.event.DocumentUpdatingEvent) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) ObservationManager(org.xwiki.observation.ObservationManager) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) DocumentCreatingEvent(org.xwiki.bridge.event.DocumentCreatingEvent) CancelableEvent(org.xwiki.observation.event.CancelableEvent) Date(java.util.Date) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) IOException(java.io.IOException) JobException(org.xwiki.job.JobException) ParseException(org.xwiki.rendering.parser.ParseException) QueryException(org.xwiki.query.QueryException) URIException(org.apache.commons.httpclient.URIException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NamingException(javax.naming.NamingException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiAttachmentToRemove(com.xpn.xwiki.doc.XWikiDocument.XWikiAttachmentToRemove) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference)

Aggregations

DocumentUpdatingEvent (org.xwiki.bridge.event.DocumentUpdatingEvent)5 DocumentUpdatedEvent (org.xwiki.bridge.event.DocumentUpdatedEvent)4 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 DocumentCreatedEvent (org.xwiki.bridge.event.DocumentCreatedEvent)3 DocumentCreatingEvent (org.xwiki.bridge.event.DocumentCreatingEvent)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 Test (org.junit.Test)2 ActionExecutedEvent (org.xwiki.bridge.event.ActionExecutedEvent)2 ActionExecutingEvent (org.xwiki.bridge.event.ActionExecutingEvent)2 DocumentDeletedEvent (org.xwiki.bridge.event.DocumentDeletedEvent)2 DocumentDeletingEvent (org.xwiki.bridge.event.DocumentDeletingEvent)2 ObservationManager (org.xwiki.observation.ObservationManager)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiAttachmentToRemove (com.xpn.xwiki.doc.XWikiDocument.XWikiAttachmentToRemove)1 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)1 XWikiNotificationManager (com.xpn.xwiki.notify.XWikiNotificationManager)1 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1