Search in sources :

Example 6 with XWikiDeletedDocument

use of com.xpn.xwiki.doc.XWikiDeletedDocument in project xwiki-platform by xwiki.

the class UndeleteAction method getDeletedDocument.

private XWikiDeletedDocument getDeletedDocument(XWikiContext context) throws XWikiException {
    XWikiDeletedDocument result = null;
    XWikiRequest request = context.getRequest();
    XWiki xwiki = context.getWiki();
    String sindex = request.getParameter(ID_PARAMETER);
    try {
        long index = Long.parseLong(sindex);
        result = xwiki.getDeletedDocument(index, context);
    } catch (Exception e) {
        LOGGER.error("Failed to get deleted document with ID [{}]", sindex, e);
    }
    return result;
}
Also used : XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) JobException(org.xwiki.job.JobException)

Example 7 with XWikiDeletedDocument

use of com.xpn.xwiki.doc.XWikiDeletedDocument in project xwiki-platform by xwiki.

the class DefaultModelBridge method restoreDeletedDocument.

@Override
public boolean restoreDeletedDocument(long deletedDocumentId, boolean checkContextUser) {
    XWikiContext context = this.xcontextProvider.get();
    XWiki xwiki = context.getWiki();
    DocumentReference deletedDocumentReference = null;
    try {
        // Retrieve the deleted document by ID.
        XWikiDeletedDocument deletedDocument = xwiki.getDeletedDocument(deletedDocumentId, context);
        if (deletedDocument == null) {
            logger.error("Deleted document with ID [{}] does not exist.", deletedDocumentId);
            return false;
        }
        deletedDocumentReference = deletedDocument.getDocumentReference();
        // If the document (or the translation) that we want to restore does not exist, restore it.
        if (xwiki.exists(deletedDocumentReference, context)) {
            // TODO: Add overwrite support maybe also with interactive (question/answer) mode.
            // Default for now is to skip and log as error to restore over existing documents.
            logger.error("Document [{}] with ID [{}] can not be restored. Document already exists", deletedDocument.getFullName(), deletedDocumentId);
        } else if (checkContextUser && !canRestoreDeletedDocument(deletedDocumentId, context.getUserReference())) {
            logger.error("You are not allowed to restore document [{}] with ID [{}]", deletedDocumentReference, deletedDocumentId);
        } else {
            // Restore the document.
            xwiki.restoreFromRecycleBin(deletedDocument.getId(), "Restored from recycle bin", context);
            logger.info("Document [{}] has been restored", deletedDocumentReference);
            return true;
        }
    } catch (Exception e) {
        // Try to log the document reference since it`s more useful than the ID.
        if (deletedDocumentReference != null) {
            logger.error("Failed to restore document [{}] with ID [{}]", deletedDocumentReference, deletedDocumentId, e);
        } else {
            logger.error("Failed to restore deleted document with ID [{}]", deletedDocumentId, e);
        }
    }
    return false;
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 8 with XWikiDeletedDocument

use of com.xpn.xwiki.doc.XWikiDeletedDocument in project xwiki-platform by xwiki.

the class DefaultModelBridgeTest method restoreDeletedDocumentAlreadyExists.

@Test
public void restoreDeletedDocumentAlreadyExists() throws Exception {
    long deletedDocumentId = 42;
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    String fullName = "space.page";
    XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
    when(deletedDocument.getDocumentReference()).thenReturn(documentReference);
    when(deletedDocument.getId()).thenReturn(deletedDocumentId);
    when(deletedDocument.getFullName()).thenReturn(fullName);
    when(xwiki.getDeletedDocument(deletedDocumentId, xcontext)).thenReturn(deletedDocument);
    when(xwiki.exists(documentReference, xcontext)).thenReturn(true);
    assertFalse(mocker.getComponentUnderTest().restoreDeletedDocument(deletedDocumentId, false));
    verify(mocker.getMockedLogger()).error("Document [{}] with ID [{}] can not be restored. Document already exists", fullName, deletedDocumentId);
    verify(xwiki, never()).restoreFromRecycleBin(any(), any(), any());
}
Also used : XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 9 with XWikiDeletedDocument

use of com.xpn.xwiki.doc.XWikiDeletedDocument in project xwiki-platform by xwiki.

the class DefaultModelBridgeTest method restoreDocumentTranslation.

/**
 * @see "XWIKI-9567: Cannot restore document translations from recycle bin"
 */
@Test
public void restoreDocumentTranslation() throws Exception {
    long deletedDocumentId = 42;
    Locale locale = new Locale("ro");
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    DocumentReference translationDocumentReference = new DocumentReference(documentReference, locale);
    XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
    when(deletedDocument.getDocumentReference()).thenReturn(translationDocumentReference);
    when(deletedDocument.getId()).thenReturn(deletedDocumentId);
    when(xwiki.getDeletedDocument(deletedDocumentId, xcontext)).thenReturn(deletedDocument);
    when(xwiki.exists(translationDocumentReference, xcontext)).thenReturn(false);
    assertTrue(mocker.getComponentUnderTest().restoreDeletedDocument(deletedDocumentId, false));
    verify(xwiki).restoreFromRecycleBin(deletedDocumentId, "Restored from recycle bin", xcontext);
    // Make sure that the main document is not checked for existence, but the translated document which we actually
    // want to restore.
    verify(xwiki, never()).exists(documentReference, xcontext);
    verify(xwiki).exists(translationDocumentReference, xcontext);
}
Also used : Locale(java.util.Locale) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 10 with XWikiDeletedDocument

use of com.xpn.xwiki.doc.XWikiDeletedDocument in project xwiki-platform by xwiki.

the class DefaultModelBridgeTest method restoreDeletedDocument.

@Test
public void restoreDeletedDocument() throws Exception {
    long deletedDocumentId = 42;
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
    when(deletedDocument.getDocumentReference()).thenReturn(documentReference);
    when(deletedDocument.getId()).thenReturn(deletedDocumentId);
    when(xwiki.getDeletedDocument(deletedDocumentId, xcontext)).thenReturn(deletedDocument);
    when(xwiki.exists(documentReference, xcontext)).thenReturn(false);
    assertTrue(mocker.getComponentUnderTest().restoreDeletedDocument(deletedDocumentId, false));
    verify(xwiki).restoreFromRecycleBin(deletedDocumentId, "Restored from recycle bin", xcontext);
}
Also used : XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

XWikiDeletedDocument (com.xpn.xwiki.doc.XWikiDeletedDocument)24 Test (org.junit.Test)13 XWikiContext (com.xpn.xwiki.XWikiContext)10 DocumentReference (org.xwiki.model.reference.DocumentReference)8 XWiki (com.xpn.xwiki.XWiki)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 XWikiException (com.xpn.xwiki.XWikiException)5 CSRFToken (org.xwiki.csrf.CSRFToken)5 DeletedDocument (com.xpn.xwiki.api.DeletedDocument)4 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 XWikiRecycleBinStoreInterface (com.xpn.xwiki.store.XWikiRecycleBinStoreInterface)4 XWikiRightService (com.xpn.xwiki.user.api.XWikiRightService)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)2 XWikiDeletedDocumentContent (com.xpn.xwiki.doc.XWikiDeletedDocumentContent)1 XWikiHibernateDeletedDocumentContent (com.xpn.xwiki.internal.store.hibernate.XWikiHibernateDeletedDocumentContent)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 Map (java.util.Map)1 HasToString (org.hamcrest.object.HasToString)1