Search in sources :

Example 6 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument 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 7 with XWikiDocument

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

the class DocumentMergeImporter method getMandatoryDocument.

private XWikiDocument getMandatoryDocument(DocumentReference documentReference) {
    MandatoryDocumentInitializer initializer = this.initializerManager.getMandatoryDocumentInitializer(documentReference);
    XWikiDocument mandatoryDocument;
    if (initializer != null) {
        // Generate clean mandatory document
        mandatoryDocument = new XWikiDocument(documentReference);
        if (!initializer.updateDocument(mandatoryDocument)) {
            mandatoryDocument = null;
        }
    } else {
        mandatoryDocument = null;
    }
    return mandatoryDocument;
}
Also used : MandatoryDocumentInitializer(com.xpn.xwiki.doc.MandatoryDocumentInitializer) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument)

Example 8 with XWikiDocument

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

the class DocumentMergeImporter method merge.

private XarEntryMergeResult merge(String comment, XWikiDocument currentDocument, XWikiDocument previousDocument, XWikiDocument nextDocument, PackageConfiguration configuration) throws Exception {
    XWikiContext xcontext = this.xcontextProvider.get();
    // 3 ways merge
    XWikiDocument mergedDocument = currentDocument.clone();
    MergeConfiguration mergeConfiguration = new MergeConfiguration();
    mergeConfiguration.setProvidedVersionsModifiables(true);
    MergeResult documentMergeResult;
    try {
        documentMergeResult = mergedDocument.merge(previousDocument, nextDocument, mergeConfiguration, xcontext);
    } catch (Exception e) {
        // Unexpected error, lets behave as if there was a conflict
        documentMergeResult = new MergeResult();
        documentMergeResult.getLog().error("Unexpected exception thrown. Usually means there is a bug in the merge.", e);
        documentMergeResult.setModified(true);
    }
    documentMergeResult.getLog().log(this.logger);
    XWikiDocument documentToSave;
    if (documentMergeResult.isModified() || !documentMergeResult.getLog().getLogsFrom(LogLevel.ERROR).isEmpty()) {
        documentToSave = askDocumentToSave(currentDocument, previousDocument, nextDocument, mergedDocument, configuration, documentMergeResult);
        if (documentToSave != currentDocument) {
            saveDocument(documentToSave, comment, false, configuration);
        }
    }
    return new XarEntryMergeResult(new XarEntry(new LocalDocumentReference(mergedDocument.getDocumentReferenceWithLocale())), documentMergeResult);
}
Also used : LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XarEntry(org.xwiki.xar.XarEntry) MergeResult(com.xpn.xwiki.doc.merge.MergeResult) XWikiContext(com.xpn.xwiki.XWikiContext) MergeConfiguration(com.xpn.xwiki.doc.merge.MergeConfiguration)

Example 9 with XWikiDocument

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

the class DefaultModelBridgeTest method testAuthorReference.

@Test
public void testAuthorReference() throws Exception {
    XWikiContext context = mock(XWikiContext.class);
    XWiki xwiki = mock(XWiki.class);
    XWikiDocument document = mock(XWikiDocument.class);
    DocumentReference authorReference = mock(DocumentReference.class);
    EntityReference entityReference = mock(EntityReference.class);
    when(this.contextProvider.get()).thenReturn(context);
    when(context.getWiki()).thenReturn(xwiki);
    when(xwiki.getDocument(entityReference, context)).thenReturn(document);
    when(document.getAuthorReference()).thenReturn(authorReference);
    DocumentReference result = this.mocker.getComponentUnderTest().getAuthorReference(entityReference);
    assertEquals(authorReference, result);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) EntityReference(org.xwiki.model.reference.EntityReference) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) Test(org.junit.Test)

Example 10 with XWikiDocument

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

the class DefaultModelBridgeTest method testCheckXObjectPresenceWithPresentXObject.

@Test
public void testCheckXObjectPresenceWithPresentXObject() throws Exception {
    XWikiDocument document = mock(XWikiDocument.class);
    List<String> xObjectTypes = Arrays.asList("type1");
    DocumentReference documentReferenceFromDocumentXObjects = mock(DocumentReference.class);
    Map<DocumentReference, List<BaseObject>> documentXObjects = new HashMap<DocumentReference, List<BaseObject>>() {

        {
            put(documentReferenceFromDocumentXObjects, Collections.EMPTY_LIST);
        }
    };
    DocumentReference resolvedType1 = mock(DocumentReference.class);
    LocalDocumentReference localDocumentReferenceType1 = mock(LocalDocumentReference.class);
    when(resolvedType1.getLocalDocumentReference()).thenReturn(localDocumentReferenceType1);
    when(this.documentReferenceResolver.resolve(eq("type1"))).thenReturn(resolvedType1);
    when(document.getXObjects()).thenReturn(documentXObjects);
    when(documentReferenceFromDocumentXObjects.getLocalDocumentReference()).thenReturn(localDocumentReferenceType1);
    assertTrue(this.mocker.getComponentUnderTest().checkXObjectPresence(xObjectTypes, document));
}
Also used : LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) HashMap(java.util.HashMap) List(java.util.List) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Aggregations

XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)869 DocumentReference (org.xwiki.model.reference.DocumentReference)469 BaseObject (com.xpn.xwiki.objects.BaseObject)318 Test (org.junit.Test)284 XWikiContext (com.xpn.xwiki.XWikiContext)232 XWikiException (com.xpn.xwiki.XWikiException)178 ArrayList (java.util.ArrayList)99 XWiki (com.xpn.xwiki.XWiki)97 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)86 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)71 Document (com.xpn.xwiki.api.Document)48 EntityReference (org.xwiki.model.reference.EntityReference)48 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)41 Date (java.util.Date)41 IOException (java.io.IOException)40 HashMap (java.util.HashMap)33 QueryException (org.xwiki.query.QueryException)27 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)25 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)23 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)23