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