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;
}
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;
}
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());
}
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);
}
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);
}
Aggregations