use of com.xpn.xwiki.doc.MandatoryDocumentInitializer 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.MandatoryDocumentInitializer in project xwiki-platform by xwiki.
the class XarExtensionHandlerTest method testUninstallMandatory.
@Test
public void testUninstallMandatory() throws Throwable {
// register a mandatory document initializer
MandatoryDocumentInitializer mandatoryInitializer = this.componentManager.registerMockComponent(MandatoryDocumentInitializer.class, "space.page");
when(mandatoryInitializer.updateDocument(any(XWikiDocument.class))).thenReturn(true);
install(this.localXarExtensiontId1, "wiki", this.contextUser);
verifyHasAdminRight(2);
// uninstall
uninstall(this.localXarExtensiontId1, "wiki");
verifyHasAdminRight(3);
// validate
XWikiDocument page = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"), getXWikiContext());
Assert.assertFalse("Document wiki.space.page has been removed from the database", page.isNew());
}
use of com.xpn.xwiki.doc.MandatoryDocumentInitializer in project xwiki-platform by xwiki.
the class DefaultMandatoryDocumentInitializerManager method getMandatoryDocumentInitializer.
@Override
public MandatoryDocumentInitializer getMandatoryDocumentInitializer(DocumentReference documentReference) {
ComponentManager componentManager = this.componentManagerProvider.get();
String fullReference = this.serializer.serialize(documentReference);
try {
if (componentManager.hasComponent(MandatoryDocumentInitializer.class, fullReference)) {
return componentManager.getInstance(MandatoryDocumentInitializer.class, fullReference);
} else {
String localReference = this.localSerializer.serialize(documentReference);
if (componentManager.hasComponent(MandatoryDocumentInitializer.class, localReference)) {
return componentManager.getInstance(MandatoryDocumentInitializer.class, localReference);
}
}
} catch (ComponentLookupException e) {
this.logger.error("Failed to initialize component [{}] for document", MandatoryDocumentInitializer.class, documentReference, e);
}
return null;
}
use of com.xpn.xwiki.doc.MandatoryDocumentInitializer in project xwiki-platform by xwiki.
the class XarExtensionHandlerTest method testInstallOnWiki.
// Tests
@Test
public void testInstallOnWiki() throws Throwable {
XWikiDocument existingDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
BaseObject object = new BaseObject();
object.setXClassReference(new DocumentReference("wiki", "space", "class"));
existingDocument.addXObject(object);
existingDocument.setCreatorReference(new DocumentReference("wiki", "space", "existingcreator"));
this.oldcore.getSpyXWiki().saveDocument(existingDocument, "", true, getXWikiContext());
MandatoryDocumentInitializer mandatoryInitializer = this.componentManager.registerMockComponent(MandatoryDocumentInitializer.class, "space.mandatory");
when(mandatoryInitializer.updateDocument(any(XWikiDocument.class))).thenReturn(true);
XWikiDocument mandatoryDocument = new XWikiDocument(new DocumentReference("wiki", "space", "mandatory"));
mandatoryDocument.setCreatorReference(new DocumentReference("wiki", "space", "existingcreator"));
mandatoryDocument.setSyntax(Syntax.PLAIN_1_0);
mandatoryDocument.setContent("modified content");
this.oldcore.getSpyXWiki().saveDocument(mandatoryDocument, "", true, getXWikiContext());
// install
XarInstalledExtension xarInstalledExtension = install(this.localXarExtensiontId1, "wiki", this.contextUser);
verifyHasAdminRight(2);
// validate
// space.page
XWikiDocument page = this.oldcore.getSpyXWiki().getDocument(existingDocument.getDocumentReference(), getXWikiContext());
Assert.assertFalse("Document wiki:space.page has not been saved in the database", page.isNew());
Assert.assertNull(page.getXObject(object.getXClassReference()));
Assert.assertEquals("Wrong content", "content", page.getContent());
Assert.assertEquals("Wrong creator", new DocumentReference("wiki", "space", "existingcreator"), page.getCreatorReference());
Assert.assertEquals("Wrong author", this.contextUser, page.getAuthorReference());
Assert.assertEquals("Wrong content author", this.contextUser, page.getContentAuthorReference());
Assert.assertEquals("Wrong version", "2.1", page.getVersion());
Assert.assertEquals("Wrong version", Locale.ROOT, page.getLocale());
Assert.assertFalse("Document is hidden", page.isHidden());
Assert.assertFalse("Document is minor edit", page.isMinorEdit());
BaseClass baseClass = page.getXClass();
Assert.assertNotNull(baseClass.getField("property"));
Assert.assertEquals("property", baseClass.getField("property").getName());
Assert.assertSame(NumberClass.class, baseClass.getField("property").getClass());
// space.pagewithattachment
XWikiDocument pagewithattachment = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "pagewithattachment"), getXWikiContext());
Assert.assertFalse(pagewithattachment.isNew());
Assert.assertEquals("Wrong version", "1.1", pagewithattachment.getVersion());
Assert.assertEquals("Wrong creator", this.contextUser, pagewithattachment.getCreatorReference());
Assert.assertEquals("Wrong author", this.contextUser, pagewithattachment.getAuthorReference());
Assert.assertEquals("Wrong content author", this.contextUser, pagewithattachment.getContentAuthorReference());
XWikiAttachment attachment = pagewithattachment.getAttachment("attachment.txt");
Assert.assertNotNull(attachment);
Assert.assertEquals("attachment.txt", attachment.getFilename());
Assert.assertEquals(18, attachment.getContentLongSize(getXWikiContext()));
Assert.assertEquals("attachment content", IOUtils.toString(attachment.getContentInputStream(getXWikiContext()), StandardCharsets.UTF_8));
Assert.assertEquals(this.contextUser, attachment.getAuthorReference());
// space1.page1
XWikiDocument page1 = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space1", "page1"), getXWikiContext());
Assert.assertFalse("Document wiki:space1.page1 has not been saved in the database", page1.isNew());
assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository.getXarInstalledExtensions(page1.getDocumentReference()));
assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository.getXarInstalledExtensions(page1.getDocumentReferenceWithLocale()));
assertEquals(0, this.installedExtensionRepository.getXarInstalledExtensions(new DocumentReference("wiki", "space1", "page1", Locale.ENGLISH)).size());
assertEquals(0, this.installedExtensionRepository.getXarInstalledExtensions(new DocumentReference("otherwiki", "space1", "page1")).size());
// translated.translated
DocumentReference translatedReference = new DocumentReference("wiki", "translated", "translated");
XWikiDocument defaultTranslated = this.oldcore.getSpyXWiki().getDocument(translatedReference, getXWikiContext());
Assert.assertNotNull("Document wiki:translated.translated has not been saved in the database", defaultTranslated);
Assert.assertFalse("Document wiki:translated.translated has not been saved in the database", defaultTranslated.isNew());
Assert.assertEquals("Wrong content", "default content", defaultTranslated.getContent());
Assert.assertEquals("Wrong creator", this.contextUser, defaultTranslated.getCreatorReference());
Assert.assertEquals("Wrong author", this.contextUser, defaultTranslated.getAuthorReference());
Assert.assertEquals("Wrong content author", this.contextUser, defaultTranslated.getContentAuthorReference());
Assert.assertEquals("Wrong version", "1.1", defaultTranslated.getVersion());
assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository.getXarInstalledExtensions(defaultTranslated.getDocumentReferenceWithLocale()));
// translated.translated.tr
XWikiDocument translated = this.oldcore.getDocuments().get(new DocumentReference(translatedReference, new Locale("tr")));
Assert.assertNotNull("Document wiki:translated.translated in langauge tr has not been saved in the database", translated);
Assert.assertFalse("Document wiki:translated.translated in langauge tr has not been saved in the database", translated.isNew());
Assert.assertEquals("Wrong content", "tr content", translated.getContent());
Assert.assertEquals("Wrong creator", this.contextUser, translated.getCreatorReference());
Assert.assertEquals("Wrong author", this.contextUser, translated.getAuthorReference());
Assert.assertEquals("Wrong content author", this.contextUser, translated.getContentAuthorReference());
Assert.assertEquals("Wrong version", "1.1", translated.getVersion());
assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository.getXarInstalledExtensions(translated.getDocumentReferenceWithLocale()));
// translated.translated.fr
XWikiDocument translated2 = this.oldcore.getDocuments().get(new DocumentReference(translatedReference, new Locale("fr")));
Assert.assertNotNull("Document wiki:translated.translated in language fr has not been saved in the database", translated2);
Assert.assertFalse("Document wiki:translated.translated in langauge fr has not been saved in the database", translated2.isNew());
Assert.assertEquals("Wrong content", "fr content", translated2.getContent());
Assert.assertEquals("Wrong creator", this.contextUser, translated2.getCreatorReference());
Assert.assertEquals("Wrong author", this.contextUser, translated2.getAuthorReference());
Assert.assertEquals("Wrong content author", this.contextUser, translated2.getContentAuthorReference());
Assert.assertEquals("Wrong version", "1.1", translated2.getVersion());
assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository.getXarInstalledExtensions(translated2.getDocumentReferenceWithLocale()));
// space.hiddenpage
XWikiDocument hiddenpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "hiddenpage"), getXWikiContext());
Assert.assertFalse("Document wiki:space.hiddenpage has not been saved in the database", hiddenpage.isNew());
Assert.assertTrue("Document is not hidden", hiddenpage.isHidden());
// space.mandatory
XWikiDocument mandatorypage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "mandatory"), getXWikiContext());
Assert.assertEquals("Document wiki:space.mandatory has been overwritten", "1.1", mandatorypage.getVersion());
}
use of com.xpn.xwiki.doc.MandatoryDocumentInitializer in project xwiki-platform by xwiki.
the class XWiki method initializeMandatoryDocuments.
/**
* Ensure that mandatory classes (ie classes XWiki needs to work properly) exist and create them if they don't
* exist.
*
* @param context see {@link XWikiContext}
*/
public void initializeMandatoryDocuments(XWikiContext context) {
if (context.get("initdone") == null) {
@SuppressWarnings("deprecation") List<MandatoryDocumentInitializer> initializers = Utils.getComponentList(MandatoryDocumentInitializer.class);
// Sort the initializers based on priority. Lower priority values are first.
Collections.sort(initializers, new Comparator<MandatoryDocumentInitializer>() {
@Override
public int compare(MandatoryDocumentInitializer left, MandatoryDocumentInitializer right) {
Priority leftPriority = left.getClass().getAnnotation(Priority.class);
int leftPriorityValue = leftPriority != null ? leftPriority.value() : MandatoryDocumentInitializer.DEFAULT_PRIORITY;
Priority rightPriority = right.getClass().getAnnotation(Priority.class);
int rightPriorityValue = rightPriority != null ? rightPriority.value() : MandatoryDocumentInitializer.DEFAULT_PRIORITY;
// Compare the two.
return leftPriorityValue - rightPriorityValue;
}
});
for (MandatoryDocumentInitializer initializer : initializers) {
initializeMandatoryDocument(initializer, context);
}
}
}
Aggregations