Search in sources :

Example 1 with DocumentRolledBackEvent

use of org.xwiki.bridge.event.DocumentRolledBackEvent 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 2 with DocumentRolledBackEvent

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

the class XWiki method rollback.

public XWikiDocument rollback(final XWikiDocument tdoc, String rev, XWikiContext context) throws XWikiException {
    LOGGER.debug("Rolling back [{}] to version [{}]", tdoc, rev);
    // Let's clone rolledbackDoc since we might modify it
    XWikiDocument rolledbackDoc = getDocument(tdoc, rev, context).clone();
    if ("1".equals(getConfiguration().getProperty("xwiki.store.rollbackattachmentwithdocuments", "1"))) {
        // Attachment handling strategy:
        // - Two lists: Old Attachments, Current Attachments
        // Goals:
        // 1. Attachments that are only in OA must be restored from the trash
        // 2. Attachments that are only in CA must be sent to the trash
        // 3. Attachments that are in both lists should be reverted to the right version
        // 4. Gotcha: deleted and re-uploaded attachments should be both trashed and restored.
        // Plan:
        // - Construct two lists: to restore, to revert
        // - Iterate over OA.
        // -- If the attachment is not in CA, add it to the restore list
        // -- If it is in CA, but the date of the first version of the current attachment is after the date of the
        // restored document version, add it the restore & move the current attachment to the recycle bin
        // -- Otherwise, add it to the revert list
        // - Iterate over CA
        // -- If the attachment is not in OA, delete it
        List<XWikiAttachment> oldAttachments = rolledbackDoc.getAttachmentList();
        List<XWikiAttachment> currentAttachments = tdoc.getAttachmentList();
        List<XWikiAttachment> toRestore = new ArrayList<>();
        List<XWikiAttachment> toRevert = new ArrayList<>();
        // First step, determine what to do with each attachment
        LOGGER.debug("Checking attachments");
        for (XWikiAttachment oldAttachment : oldAttachments) {
            String filename = oldAttachment.getFilename();
            XWikiAttachment equivalentAttachment = tdoc.getAttachment(filename);
            if (equivalentAttachment == null) {
                // Deleted attachment
                LOGGER.debug("Deleted attachment: [{}]", filename);
                toRestore.add(oldAttachment);
                continue;
            }
            XWikiAttachment equivalentAttachmentRevision = equivalentAttachment.getAttachmentRevision(oldAttachment.getVersion(), context);
            // because the nanoseconds component of the passed date is unknown.
            if (equivalentAttachmentRevision == null || equivalentAttachmentRevision.getDate().getTime() != oldAttachment.getDate().getTime()) {
                // Recreated attachment
                LOGGER.debug("Recreated attachment: [{}]", filename);
                // If the attachment trash is not available, don't lose the existing attachment
                if (getAttachmentRecycleBinStore() != null) {
                    getAttachmentRecycleBinStore().saveToRecycleBin(equivalentAttachment, context.getUser(), new Date(), context, true);
                    toRestore.add(oldAttachment);
                }
                continue;
            }
            if (!StringUtils.equals(oldAttachment.getVersion(), equivalentAttachment.getVersion())) {
                // Updated attachment
                LOGGER.debug("Updated attachment: [{}]", filename);
                toRevert.add(equivalentAttachment);
            }
        }
        for (XWikiAttachment attachment : currentAttachments) {
            if (rolledbackDoc.getAttachment(attachment.getFilename()) == null) {
                LOGGER.debug("New attachment: " + attachment.getFilename());
                // XWikiDocument#save() is actually the only way to delete an attachment cleanly
                rolledbackDoc.getAttachmentsToRemove().add(new XWikiAttachmentToRemove(attachment, true));
            }
        }
        // Revert updated attachments to the old version
        for (XWikiAttachment attachmentToRevert : toRevert) {
            String oldAttachmentVersion = rolledbackDoc.getAttachment(attachmentToRevert.getFilename()).getVersion();
            XWikiAttachment oldAttachmentRevision = attachmentToRevert.getAttachmentRevision(oldAttachmentVersion, context);
            if (oldAttachmentRevision == null) {
                // Previous version is lost, just leave the current version in place
                rolledbackDoc.setAttachment(attachmentToRevert);
                continue;
            }
            // We can't just leave the old version in place, since it will break the revision history, given the
            // current implementation, so we set the attachment version to the most recent version, mark the content
            // as dirty, and the storage will automatically bump up the version number.
            // This is a hack, to be fixed once the storage doesn't take care of updating the history and version,
            // and once the current attachment version can point to an existing version from the history.
            oldAttachmentRevision.setVersion(attachmentToRevert.getVersion());
            oldAttachmentRevision.setMetaDataDirty(true);
            oldAttachmentRevision.getAttachment_content().setContentDirty(true);
            rolledbackDoc.setAttachment(oldAttachmentRevision);
        }
        // Restore deleted attachments from the trash
        if (getAttachmentRecycleBinStore() != null) {
            for (XWikiAttachment attachmentToRestore : toRestore) {
                // There might be multiple versions of the attachment in the trash, search for the right one
                List<DeletedAttachment> deletedVariants = getAttachmentRecycleBinStore().getAllDeletedAttachments(attachmentToRestore, context, true);
                DeletedAttachment correctVariant = null;
                for (DeletedAttachment variant : deletedVariants) {
                    // Reverse chronological order
                    if (variant.getDate().before(rolledbackDoc.getDate())) {
                        break;
                    }
                    correctVariant = variant;
                }
                if (correctVariant == null) {
                    // Not found in the trash, nothing left to do
                    continue;
                }
                XWikiAttachment restoredAttachment = correctVariant.restoreAttachment();
                XWikiAttachment restoredAttachmentRevision = restoredAttachment.getAttachmentRevision(attachmentToRestore.getVersion(), context);
                if (restoredAttachmentRevision != null) {
                    restoredAttachmentRevision.setAttachment_archive(restoredAttachment.getAttachment_archive());
                    restoredAttachmentRevision.getAttachment_archive().setAttachment(restoredAttachmentRevision);
                    restoredAttachmentRevision.setVersion(restoredAttachment.getVersion());
                    restoredAttachmentRevision.setMetaDataDirty(true);
                    restoredAttachmentRevision.getAttachment_content().setContentDirty(true);
                    rolledbackDoc.setAttachment(restoredAttachmentRevision);
                } else {
                    // This particular version is lost, update to the one available
                    rolledbackDoc.setAttachment(restoredAttachment);
                }
            }
        } else {
            // No trash, can't restore. Remove the attachment references, so that the document is not broken
            for (XWikiAttachment attachmentToRestore : toRestore) {
                rolledbackDoc.getAttachmentList().remove(attachmentToRestore);
            }
        }
    }
    // Special treatment for deleted objects
    rolledbackDoc.addXObjectsToRemoveFromVersion(tdoc);
    // now we save the final document..
    rolledbackDoc.setOriginalDocument(tdoc);
    rolledbackDoc.setAuthorReference(context.getUserReference());
    rolledbackDoc.setRCSVersion(tdoc.getRCSVersion());
    rolledbackDoc.setVersion(tdoc.getVersion());
    rolledbackDoc.setContentDirty(true);
    ObservationManager om = getObservationManager();
    if (om != null) {
        // Notify listeners about the document that is going to be rolled back.
        // Note that for the moment the event being send is a bridge event, as we are still passing around
        // an XWikiDocument as source and an XWikiContext as data.
        om.notify(new DocumentRollingBackEvent(rolledbackDoc.getDocumentReference(), rev), rolledbackDoc, context);
    }
    saveDocument(rolledbackDoc, localizePlainOrKey("core.comment.rollback", rev), context);
    // Since the the store resets the original document, we need to temporarily put it back to send notifications.
    XWikiDocument newOriginalDocument = rolledbackDoc.getOriginalDocument();
    rolledbackDoc.setOriginalDocument(tdoc);
    try {
        if (om != null) {
            // Notify listeners about the document that was rolled back.
            // Note that for the moment the event being send is a bridge event, as we are still passing around an
            // XWikiDocument as source and an XWikiContext as data.
            om.notify(new DocumentRolledBackEvent(rolledbackDoc.getDocumentReference(), rev), rolledbackDoc, context);
        }
    } finally {
        rolledbackDoc.setOriginalDocument(newOriginalDocument);
    }
    return rolledbackDoc;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentRollingBackEvent(org.xwiki.bridge.event.DocumentRollingBackEvent) ArrayList(java.util.ArrayList) DocumentRolledBackEvent(org.xwiki.bridge.event.DocumentRolledBackEvent) ObservationManager(org.xwiki.observation.ObservationManager) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) XWikiAttachmentToRemove(com.xpn.xwiki.doc.XWikiDocument.XWikiAttachmentToRemove) DeletedAttachment(com.xpn.xwiki.doc.DeletedAttachment) Date(java.util.Date)

Aggregations

XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 DocumentRolledBackEvent (org.xwiki.bridge.event.DocumentRolledBackEvent)2 DocumentRollingBackEvent (org.xwiki.bridge.event.DocumentRollingBackEvent)2 ObservationManager (org.xwiki.observation.ObservationManager)2 DeletedAttachment (com.xpn.xwiki.doc.DeletedAttachment)1 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 XWikiAttachmentToRemove (com.xpn.xwiki.doc.XWikiDocument.XWikiAttachmentToRemove)1 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)1 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Test (org.junit.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 DocumentUpdatedEvent (org.xwiki.bridge.event.DocumentUpdatedEvent)1 DocumentUpdatingEvent (org.xwiki.bridge.event.DocumentUpdatingEvent)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1