Search in sources :

Example 26 with ObservationManager

use of org.xwiki.observation.ObservationManager in project xwiki-platform by xwiki.

the class ContextComponentManagerTest method testDeleteDocument.

@Test
public void testDeleteDocument() throws Exception {
    getMockery().checking(new Expectations() {

        {
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            will(returnValue("wiki"));
            allowing(mockCurrentSpaceReferenceProvider).get();
            will(returnValue(new SpaceReference("space", new WikiReference("wiki"))));
            allowing(mockCurrentDocumentReferenceProvider).get();
            will(returnValue(new DocumentReference("wiki", "space", "document")));
            allowing(mockDocumentAccessBridge).getCurrentUserReference();
            will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
        }
    });
    ComponentManager documentCM = getComponentManager().getInstance(ComponentManager.class, "document");
    DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
    cd.setRoleType(Role.class);
    cd.setImplementation(RoleImpl.class);
    // Register component for the current user
    documentCM.registerComponent(cd);
    // Verify we can lookup the component from the Context CM
    ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
    Assert.assertNotNull(contextCM.getComponentDescriptor(Role.class, "default"));
    ObservationManager observationManager = getComponentManager().getInstance(ObservationManager.class);
    observationManager.notify(new DocumentDeletedEvent(new DocumentReference("wiki", "space", "document")), null, null);
    Assert.assertNull(contextCM.getComponentDescriptor(Role.class, "default"));
}
Also used : Expectations(org.jmock.Expectations) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) SpaceReference(org.xwiki.model.reference.SpaceReference) ComponentManager(org.xwiki.component.manager.ComponentManager) NamespacedComponentManager(org.xwiki.component.manager.NamespacedComponentManager) ObservationManager(org.xwiki.observation.ObservationManager) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 27 with ObservationManager

use of org.xwiki.observation.ObservationManager 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)

Example 28 with ObservationManager

use of org.xwiki.observation.ObservationManager 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);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentDeletingEvent(org.xwiki.bridge.event.DocumentDeletingEvent) ObservationManager(org.xwiki.observation.ObservationManager) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) 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) BatchOperationExecutor(org.xwiki.refactoring.batch.BatchOperationExecutor)

Aggregations

ObservationManager (org.xwiki.observation.ObservationManager)28 DocumentReference (org.xwiki.model.reference.DocumentReference)11 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)9 Test (org.junit.Test)7 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)6 DocumentDeletedEvent (org.xwiki.bridge.event.DocumentDeletedEvent)4 DocumentUpdatedEvent (org.xwiki.bridge.event.DocumentUpdatedEvent)4 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)3 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)3 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)3 IOException (java.io.IOException)3 Date (java.util.Date)3 Mock (org.jmock.Mock)3 DocumentDeletingEvent (org.xwiki.bridge.event.DocumentDeletingEvent)3 XWikiAttachmentToRemove (com.xpn.xwiki.doc.XWikiDocument.XWikiAttachmentToRemove)2 XARImportedEvent (com.xpn.xwiki.internal.event.XARImportedEvent)2 XARImportingEvent (com.xpn.xwiki.internal.event.XARImportingEvent)2 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)2 FileNotFoundException (java.io.FileNotFoundException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2