Search in sources :

Example 61 with BaseClass

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

the class BaseClassOutputFilterStream method beginWikiClass.

// Events
@Override
public void beginWikiClass(FilterEventParameters parameters) throws FilterException {
    if (this.enabled) {
        if (this.entity == null) {
            this.entity = new BaseClass();
        }
        this.entity.setDocumentReference(getDocumentReference(WikiClassFilter.PARAMETER_NAME, parameters, null));
        this.entity.setCustomClass(getString(WikiClassFilter.PARAMETER_CUSTOMCLASS, parameters, null));
        this.entity.setCustomMapping(getString(WikiClassFilter.PARAMETER_CUSTOMMAPPING, parameters, null));
        this.entity.setDefaultViewSheet(getString(WikiClassFilter.PARAMETER_SHEET_DEFAULTVIEW, parameters, null));
        this.entity.setDefaultEditSheet(getString(WikiClassFilter.PARAMETER_SHEET_DEFAULTEDIT, parameters, null));
        this.entity.setDefaultWeb(getString(WikiClassFilter.PARAMETER_DEFAULTSPACE, parameters, null));
        this.entity.setNameField(getString(WikiClassFilter.PARAMETER_NAMEFIELD, parameters, null));
        this.entity.setValidationScript(getString(WikiClassFilter.PARAMETER_VALIDATIONSCRIPT, parameters, null));
        getPropertyClassOutputFilterStream().setCurrentXClass(this.entity);
        getPropertyClassOutputFilterStream().enable();
    }
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass)

Example 62 with BaseClass

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

the class BaseObjectOutputFilterStream method endWikiClass.

// Events
@Override
public void endWikiClass(FilterEventParameters parameters) throws FilterException {
    if (this.entity != null) {
        BaseClass xclass = getBaseClassOutputFilterStream().getEntity();
        if (xclass != null) {
            // Re-create the object instance if not already provided and if there is a custom class
            if (this.externalEntity == null && StringUtils.isNotEmpty(xclass.getCustomClass())) {
                BaseObject customObject;
                try {
                    customObject = xclass.newCustomClassInstance(this.xcontextProvider.get());
                    customObject.setDocumentReference(this.entity.getDocumentReference());
                    customObject.setXClassReference(this.entity.getXClassReference());
                    customObject.setOwnerDocument(this.entity.getOwnerDocument());
                    // Pass false as an optimization since there is nothing to clean on a new object
                    customObject.apply(this.entity, false);
                    this.entity = customObject;
                } catch (XWikiException e) {
                // TODO: should probably log a warning
                }
            }
            setCurrentXClass(xclass);
            getBaseClassOutputFilterStream().setEntity(null);
        }
    }
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 63 with BaseClass

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

the class ContextTest method setDisplayMode.

/**
 * Tests that pages can override the default property display mode using {@code $xcontext.setDisplayMode}.
 *
 * @see "XWIKI-2436"
 */
@Test
public void setDisplayMode() throws Exception {
    XWikiContext xcontext = this.oldcoreRule.getXWikiContext();
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    XWikiDocument document = new XWikiDocument(documentReference);
    BaseClass baseClass = document.getXClass();
    doReturn("xwiki/2.1").when(this.oldcoreRule.getSpyXWiki()).getCurrentContentSyntaxId("xwiki/2.1", xcontext);
    doReturn(baseClass).when(this.oldcoreRule.getSpyXWiki()).getXClass(documentReference, xcontext);
    baseClass.addTextField("prop", "prop", 5);
    BaseObject obj = (BaseObject) document.getXClass().newObject(xcontext);
    obj.setStringValue("prop", "value");
    document.addXObject(obj);
    Context context = new Context(xcontext);
    context.setDisplayMode("edit");
    // We verify that the result contains a form input
    assertEquals("<input size='5' id='space.page_0_prop' value='value' name='space.page_0_prop' " + "type='text'/>", document.display("prop", xcontext));
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 64 with BaseClass

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

the class XWikiDocumentMergeTest method before.

@Before
public void before() throws Exception {
    this.oldcore.registerMockEnvironment();
    this.currentDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
    this.previousDocument = this.currentDocument.clone();
    this.nextDocument = this.currentDocument.clone();
    this.xclass = new BaseClass();
    this.xclass.setDocumentReference(new DocumentReference("wiki", "classspace", "class"));
    this.xclass.addTextField("string", "String", 30);
    this.xclass.addTextAreaField("area", "Area", 10, 10);
    this.xclass.addTextAreaField("puretextarea", "Pure text area", 10, 10);
    // set the text areas an non interpreted content
    ((TextAreaClass) this.xclass.getField("puretextarea")).setContentType("puretext");
    this.xclass.addPasswordField("passwd", "Password", 30);
    this.xclass.addBooleanField("boolean", "Boolean", "yesno");
    this.xclass.addNumberField("int", "Int", 10, "integer");
    this.xclass.addStaticListField("stringlist", "StringList", "value1, value2");
    this.xobject = new BaseObject();
    this.xobject.setXClassReference(this.xclass.getDocumentReference());
    this.xobject.setStringValue("string", "string");
    this.xobject.setLargeStringValue("area", "area");
    this.xobject.setStringValue("passwd", "passwd");
    this.xobject.setIntValue("boolean", 1);
    this.xobject.setIntValue("int", 42);
    this.xobject.setStringListValue("stringlist", Arrays.asList("VALUE1", "VALUE2"));
    this.configuration = new MergeConfiguration();
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass) MergeConfiguration(com.xpn.xwiki.doc.merge.MergeConfiguration) TextAreaClass(com.xpn.xwiki.objects.classes.TextAreaClass) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Before(org.junit.Before)

Example 65 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass 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());
}
Also used : Locale(java.util.Locale) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) MandatoryDocumentInitializer(com.xpn.xwiki.doc.MandatoryDocumentInitializer) XarInstalledExtension(org.xwiki.extension.xar.internal.repository.XarInstalledExtension) 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

BaseClass (com.xpn.xwiki.objects.classes.BaseClass)100 DocumentReference (org.xwiki.model.reference.DocumentReference)42 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)41 BaseObject (com.xpn.xwiki.objects.BaseObject)40 XWikiException (com.xpn.xwiki.XWikiException)26 XWikiContext (com.xpn.xwiki.XWikiContext)24 ArrayList (java.util.ArrayList)18 PropertyClass (com.xpn.xwiki.objects.classes.PropertyClass)16 XWiki (com.xpn.xwiki.XWiki)15 Test (org.junit.Test)15 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)15 EntityReference (org.xwiki.model.reference.EntityReference)10 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)9 BaseProperty (com.xpn.xwiki.objects.BaseProperty)9 List (java.util.List)9 ToString (org.suigeneris.jrcs.util.ToString)9 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)7 Before (org.junit.Before)6 TextAreaClass (com.xpn.xwiki.objects.classes.TextAreaClass)5 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)5