Search in sources :

Example 1 with ConflictQuestion

use of org.xwiki.extension.xar.question.ConflictQuestion in project xwiki-platform by xwiki.

the class DocumentMergeImporter method askDocumentToSave.

private XWikiDocument askDocumentToSave(XWikiDocument currentDocument, XWikiDocument previousDocument, XWikiDocument nextDocument, XWikiDocument mergedDocument, PackageConfiguration configuration, MergeResult documentMergeResult) {
    // Indicate future author to whoever is going to answer the question
    if (currentDocument != null) {
        nextDocument.setCreatorReference(currentDocument.getCreatorReference());
    }
    if (mergedDocument != null) {
        mergedDocument.setCreatorReference(currentDocument.getCreatorReference());
    }
    DocumentReference userReference = configuration != null ? configuration.getUserReference() : null;
    if (userReference != null) {
        nextDocument.setAuthorReference(userReference);
        nextDocument.setContentAuthorReference(userReference);
        for (XWikiAttachment attachment : nextDocument.getAttachmentList()) {
            attachment.setAuthorReference(nextDocument.getAuthorReference());
        }
        if (mergedDocument != null) {
            mergedDocument.setAuthorReference(userReference);
            mergedDocument.setContentAuthorReference(userReference);
            for (XWikiAttachment attachment : mergedDocument.getAttachmentList()) {
                if (attachment.isContentDirty()) {
                    attachment.setAuthorReference(mergedDocument.getAuthorReference());
                }
            }
        }
    }
    // Calculate the conflict type
    ConflictQuestion.ConflictType type;
    if (previousDocument == null) {
        type = ConflictQuestion.ConflictType.CURRENT_EXIST;
    } else if (currentDocument == null) {
        type = ConflictQuestion.ConflictType.CURRENT_DELETED;
    } else if (documentMergeResult != null) {
        if (!documentMergeResult.getLog().getLogs(LogLevel.ERROR).isEmpty()) {
            type = ConflictQuestion.ConflictType.MERGE_FAILURE;
        } else {
            type = ConflictQuestion.ConflictType.MERGE_SUCCESS;
        }
    } else {
        type = null;
    }
    // Create a question
    ConflictQuestion question = new ConflictQuestion(currentDocument, previousDocument, nextDocument, mergedDocument, type);
    // Find the answer
    GlobalAction contextAction = getMergeConflictAnswer(question.getType(), configuration);
    if (contextAction != null && contextAction != GlobalAction.ASK) {
        question.setGlobalAction(contextAction);
    } else if (configuration != null && configuration.getJobStatus() != null && configuration.isInteractive()) {
        try {
            // Ask what to do
            configuration.getJobStatus().ask(question);
            if (question.isAlways()) {
                setMergeConflictAnswer(question.getType(), question.getGlobalAction());
            }
        } catch (InterruptedException e) {
        // TODO: log something ?
        }
    }
    // Find the XWikiDocument to save
    XWikiDocument documentToSave;
    switch(question.getGlobalAction()) {
        case CURRENT:
            documentToSave = currentDocument;
            break;
        case NEXT:
            documentToSave = nextDocument;
            break;
        case PREVIOUS:
            documentToSave = previousDocument;
            break;
        case CUSTOM:
            documentToSave = question.getCustomDocument() != null ? question.getCustomDocument() : mergedDocument;
            break;
        default:
            documentToSave = mergedDocument;
            break;
    }
    return documentToSave;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) GlobalAction(org.xwiki.extension.xar.question.ConflictQuestion.GlobalAction) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) ConflictQuestion(org.xwiki.extension.xar.question.ConflictQuestion)

Example 2 with ConflictQuestion

use of org.xwiki.extension.xar.question.ConflictQuestion in project xwiki-platform by xwiki.

the class ConflictQuestionRecorderTest method recordAndReplay.

@Test
public void recordAndReplay() {
    XWikiDocument alice = mock(XWikiDocument.class, "Alice");
    when(alice.getDocumentReference()).thenReturn(new DocumentReference("dev", "Users", "Alice"));
    ConflictQuestion aliceQuestion = new ConflictQuestion(null, null, alice, null, null);
    aliceQuestion.setGlobalAction(GlobalAction.CURRENT);
    XWikiDocument bob = mock(XWikiDocument.class, "Bob");
    when(bob.getDocumentReference()).thenReturn(new DocumentReference("dev", "Users", "Bob"));
    ConflictQuestion bobQuestion = new ConflictQuestion(null, null, bob, null, null);
    bobQuestion.setGlobalAction(GlobalAction.PREVIOUS);
    bobQuestion.setAlways(true);
    ConflictQuestionRecorder recorder = new ConflictQuestionRecorder();
    recorder.record(aliceQuestion);
    recorder.record(bobQuestion);
    XWikiDocument carol = mock(XWikiDocument.class, "Carol");
    when(carol.getDocumentReference()).thenReturn(new DocumentReference("drafts", "Users", "Carol"));
    ConflictQuestion carolQuestion = new ConflictQuestion(null, null, carol, null, null);
    assertFalse(recorder.replay(carolQuestion));
    assertEquals(GlobalAction.MERGED, carolQuestion.getGlobalAction());
    assertFalse(carolQuestion.isAlways());
    XWikiDocument aliceDrafts = mock(XWikiDocument.class, "AliceDrafts");
    when(aliceDrafts.getDocumentReference()).thenReturn(new DocumentReference("drafts", "Users", "Alice"));
    ConflictQuestion aliceDraftsQuestion = new ConflictQuestion(null, null, aliceDrafts, null, null);
    assertTrue(recorder.replay(aliceDraftsQuestion));
    assertEquals(GlobalAction.CURRENT, aliceDraftsQuestion.getGlobalAction());
    assertFalse(aliceDraftsQuestion.isAlways());
    XWikiDocument bobDrafts = mock(XWikiDocument.class, "BobDrafts");
    when(bobDrafts.getDocumentReference()).thenReturn(new DocumentReference("drafts", "Users", "Bob"));
    ConflictQuestion bobDraftsQuestion = new ConflictQuestion(null, null, bobDrafts, null, null);
    assertTrue(recorder.replay(bobDraftsQuestion));
    assertEquals(GlobalAction.PREVIOUS, bobDraftsQuestion.getGlobalAction());
    assertTrue(bobDraftsQuestion.isAlways());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) ConflictQuestion(org.xwiki.extension.xar.question.ConflictQuestion) Test(org.junit.Test)

Aggregations

XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 ConflictQuestion (org.xwiki.extension.xar.question.ConflictQuestion)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 Test (org.junit.Test)1 GlobalAction (org.xwiki.extension.xar.question.ConflictQuestion.GlobalAction)1 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)1