Search in sources :

Example 61 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class DefaultWikiUserConfigurationHelper method saveConfiguration.

@Override
public void saveConfiguration(WikiUserConfiguration configuration, String wikiId) throws WikiUserManagerException {
    XWikiContext context = xcontextProvider.get();
    // Get the document
    XWikiDocument document = getDocument(wikiId);
    // Fill the object
    BaseObject object = document.getXObject(WikiUserClassDocumentInitializer.CONFIGURATION_CLASS, true, context);
    object.setStringValue(WikiUserClassDocumentInitializer.FIELD_USERSCOPE, configuration.getUserScope().name().toLowerCase());
    if (configuration.getMembershipType() != null) {
        object.setStringValue(WikiUserClassDocumentInitializer.FIELD_MEMBERSHIPTYPE, configuration.getMembershipType().name().toLowerCase());
    }
    // Save the document
    try {
        XWiki xwiki = context.getWiki();
        document.setHidden(true);
        // The document must have a creator
        if (document.getCreatorReference() == null) {
            document.setCreatorReference(context.getUserReference());
        }
        // The document must have an author
        if (document.getAuthorReference() == null) {
            document.setAuthorReference(context.getUserReference());
        }
        xwiki.saveDocument(document, "Changed configuration.", context);
    } catch (XWikiException e) {
        throw new WikiUserManagerException(String.format("Fail to save the confguration document for wiki [%s].", wikiId), e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 62 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class DefaultWikiUserConfigurationHelper method getConfiguration.

@Override
public WikiUserConfiguration getConfiguration(String wikiId) throws WikiUserManagerException {
    // Create the configuration object to return
    WikiUserConfiguration configuration = new WikiUserConfiguration();
    // Get the document
    XWikiDocument document = getDocument(wikiId);
    // Get the XWiki object
    BaseObject object = document.getXObject(WikiUserClassDocumentInitializer.CONFIGURATION_CLASS);
    if (object != null) {
        // Get the user scope
        String scopeValue = object.getStringValue(WikiUserClassDocumentInitializer.FIELD_USERSCOPE);
        UserScope userScope;
        try {
            userScope = UserScope.valueOf(scopeValue.toUpperCase());
        } catch (Exception e) {
            // Default value
            userScope = UserScope.LOCAL_AND_GLOBAL;
        }
        configuration.setUserScope(userScope);
        // Get the membershipType value
        String membershipTypeValue = object.getStringValue(WikiUserClassDocumentInitializer.FIELD_MEMBERSHIPTYPE);
        MembershipType membershipType;
        try {
            membershipType = MembershipType.valueOf(membershipTypeValue.toUpperCase());
        } catch (Exception e) {
            // Default value
            membershipType = MembershipType.INVITE;
        }
        configuration.setMembershipType(membershipType);
    }
    return configuration;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiUserConfiguration(org.xwiki.wiki.user.WikiUserConfiguration) UserScope(org.xwiki.wiki.user.UserScope) MembershipType(org.xwiki.wiki.user.MembershipType) XWikiException(com.xpn.xwiki.XWikiException) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 63 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class WikiDescriptorListenerTest method onDocumentUpdatedEvent.

@Test
public void onDocumentUpdatedEvent() throws Exception {
    XWikiDocument document = mock(XWikiDocument.class);
    XWikiDocument originalDocument = mock(XWikiDocument.class);
    when(document.getOriginalDocument()).thenReturn(originalDocument);
    Event event = new DocumentUpdatedEvent();
    List<BaseObject> objects = new ArrayList<>();
    BaseObject object = mock(BaseObject.class);
    objects.add(object);
    when(originalDocument.getXObjects(WikiDescriptorListener.SERVER_CLASS)).thenReturn(objects);
    DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwikiA");
    when(originalDocument.getDocumentReference()).thenReturn(documentReference);
    when(wikiDescriptorDocumentHelper.getWikiIdFromDocumentReference(documentReference)).thenReturn("subwikia");
    DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("subwikia", "alias");
    when(cache.getFromId("subwikia")).thenReturn(descriptor);
    // New objects
    List<BaseObject> newObjects = new ArrayList<>();
    BaseObject newObject = mock(BaseObject.class);
    newObjects.add(newObject);
    when(document.getXObjects(WikiDescriptorListener.SERVER_CLASS)).thenReturn(newObjects);
    DefaultWikiDescriptor newDescriptor = new DefaultWikiDescriptor("subwikia", "newAlias");
    when(builder.buildDescriptorObject(newObjects, document)).thenReturn(newDescriptor);
    // Test
    mocker.getComponentUnderTest().onEvent(event, document, null);
    // Verify
    verify(cache).remove(descriptor.getId(), descriptor.getAliases());
    verify(cache).add(newDescriptor);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) Event(org.xwiki.observation.event.Event) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DefaultWikiDescriptor(org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 64 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class WikiDescriptorListenerTest method onDocumentDeletedEvent.

@Test
public void onDocumentDeletedEvent() throws Exception {
    XWikiDocument document = mock(XWikiDocument.class);
    XWikiDocument originalDocument = mock(XWikiDocument.class);
    when(document.getOriginalDocument()).thenReturn(originalDocument);
    Event event = new DocumentDeletedEvent();
    List<BaseObject> objects = new ArrayList<>();
    BaseObject object = mock(BaseObject.class);
    objects.add(object);
    when(originalDocument.getXObjects(WikiDescriptorListener.SERVER_CLASS)).thenReturn(objects);
    DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwikiA");
    when(originalDocument.getDocumentReference()).thenReturn(documentReference);
    when(wikiDescriptorDocumentHelper.getWikiIdFromDocumentReference(documentReference)).thenReturn("subwikia");
    DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("subwikia", "alias");
    when(cache.getFromId("subwikia")).thenReturn(descriptor);
    // Test
    mocker.getComponentUnderTest().onEvent(event, document, null);
    // Verify
    verify(cache).remove(descriptor.getId(), descriptor.getAliases());
    verify(cache, never()).add(any(DefaultWikiDescriptor.class));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) ArrayList(java.util.ArrayList) Event(org.xwiki.observation.event.Event) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DefaultWikiDescriptor(org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 65 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class XarExtensionHandlerTest method testInstallOnWikiWithoutAuthor.

@Test
public void testInstallOnWikiWithoutAuthor() 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());
    // install
    install(this.localXarExtensiontId1, "wiki", null);
    // validate
    DocumentReference xarAuthorReference = new DocumentReference("wiki", "XWiki", "author");
    DocumentReference xarCreatorReference = new DocumentReference("wiki", "XWiki", "creator");
    DocumentReference xarContentAuthorReference = new DocumentReference("wiki", "XWiki", "contentAuthor");
    // 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", xarAuthorReference, page.getAuthorReference());
    Assert.assertEquals("Wrong content author", xarContentAuthorReference, 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());
    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", xarCreatorReference, pagewithattachment.getCreatorReference());
    Assert.assertEquals("Wrong author", xarAuthorReference, pagewithattachment.getAuthorReference());
    Assert.assertEquals("Wrong content author", xarContentAuthorReference, 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(new DocumentReference("wiki", "XWiki", "attachmentauthor"), 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());
    // 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", xarCreatorReference, defaultTranslated.getCreatorReference());
    Assert.assertEquals("Wrong author", xarAuthorReference, defaultTranslated.getAuthorReference());
    Assert.assertEquals("Wrong content author", xarContentAuthorReference, defaultTranslated.getContentAuthorReference());
    Assert.assertEquals("Wrong version", "1.1", defaultTranslated.getVersion());
    // 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", xarCreatorReference, translated.getCreatorReference());
    Assert.assertEquals("Wrong author", xarAuthorReference, translated.getAuthorReference());
    Assert.assertEquals("Wrong content author", xarContentAuthorReference, translated.getContentAuthorReference());
    Assert.assertEquals("Wrong version", "1.1", translated.getVersion());
    // 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", xarCreatorReference, translated2.getCreatorReference());
    Assert.assertEquals("Wrong author", xarAuthorReference, translated2.getAuthorReference());
    Assert.assertEquals("Wrong content author", xarContentAuthorReference, translated2.getContentAuthorReference());
    Assert.assertEquals("Wrong version", "1.1", translated2.getVersion());
    // space.hiddenpage
    XWikiDocument hiddenpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "hiddenpage"), getXWikiContext());
    Assert.assertNotNull("Document wiki:space.hiddenpage has not been saved in the database", hiddenpage);
    Assert.assertFalse("Document wiki:space.hiddenpage has not been saved in the database", hiddenpage.isNew());
    Assert.assertTrue("Document is not hidden", hiddenpage.isHidden());
}
Also used : Locale(java.util.Locale) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Aggregations

BaseObject (com.xpn.xwiki.objects.BaseObject)484 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)309 DocumentReference (org.xwiki.model.reference.DocumentReference)225 Test (org.junit.Test)137 XWikiException (com.xpn.xwiki.XWikiException)102 XWikiContext (com.xpn.xwiki.XWikiContext)99 ArrayList (java.util.ArrayList)92 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)69 EntityReference (org.xwiki.model.reference.EntityReference)42 XWiki (com.xpn.xwiki.XWiki)41 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)40 List (java.util.List)30 Date (java.util.Date)24 BaseProperty (com.xpn.xwiki.objects.BaseProperty)23 Document (com.xpn.xwiki.api.Document)22 HashMap (java.util.HashMap)21 QueryException (org.xwiki.query.QueryException)18 AbstractComponentTest (com.celements.common.test.AbstractComponentTest)16 IOException (java.io.IOException)16 Vector (java.util.Vector)16