Search in sources :

Example 66 with BaseClass

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

the class XarExtensionHandlerTest method testUpgradeOnWiki.

@Test
public void testUpgradeOnWiki() throws Throwable {
    install(this.localXarExtensiontId1, "wiki", this.contextUser);
    verifyHasAdminRight(2);
    // Do some local modifications
    XWikiDocument deletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "deletedpage"), getXWikiContext());
    this.oldcore.getSpyXWiki().deleteDocument(deletedpage, getXWikiContext());
    XWikiDocument modifieddeletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "modifieddeletedpage"), getXWikiContext());
    this.oldcore.getSpyXWiki().deleteDocument(modifieddeletedpage, getXWikiContext());
    XWikiDocument pagewithobject = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "pagewithobject"), getXWikiContext());
    pagewithobject.removeXObjects(new LocalDocumentReference("XWiki", "XWikiGroups"));
    this.oldcore.getSpyXWiki().saveDocument(pagewithobject, getXWikiContext());
    XWikiDocument deletedpagewithmodifications = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space1", "modified"), getXWikiContext());
    deletedpagewithmodifications.setContent("modified content");
    // upgrade
    install(this.localXarExtensiontId2, "wiki", this.contextUser);
    verifyHasAdminRight(3);
    // validate
    // samespace.samepage
    XWikiDocument samepage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "samespace", "samepage"), getXWikiContext());
    Assert.assertFalse("Document samespace has been removed from the database", samepage.isNew());
    Assert.assertEquals("Wrong versions", "1.1", samepage.getVersion());
    // space.page
    XWikiDocument modifiedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"), getXWikiContext());
    Assert.assertFalse("Document wiki.space.page has not been saved in the database", modifiedpage.isNew());
    Assert.assertEquals("Wrong content", "content 2", modifiedpage.getContent());
    Assert.assertEquals("Wrong author", this.contextUser, modifiedpage.getAuthorReference());
    Assert.assertEquals("Wrong versions", "2.1", modifiedpage.getVersion());
    Assert.assertEquals("Wrong version", Locale.ROOT, modifiedpage.getLocale());
    Assert.assertEquals("Wrong customclass", "customclass2", modifiedpage.getCustomClass());
    Assert.assertEquals("Wrong defaultTemplate", "defaultTemplate2", modifiedpage.getDefaultTemplate());
    Assert.assertEquals("Wrong hidden", true, modifiedpage.isHidden());
    Assert.assertEquals("Wrong ValidationScript", "validationScript2", modifiedpage.getValidationScript());
    BaseClass baseClass = modifiedpage.getXClass();
    Assert.assertNotNull(baseClass.getField("property"));
    Assert.assertEquals("property", baseClass.getField("property").getName());
    Assert.assertSame(NumberClass.class, baseClass.getField("property").getClass());
    XWikiAttachment attachment = modifiedpage.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));
    // space2.page2
    XWikiDocument newPage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space2", "page2"), getXWikiContext());
    Assert.assertFalse("Document wiki:space2.page2 has not been saved in the database", newPage.isNew());
    // space1.page1
    XWikiDocument removedPage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space1", "page1"), getXWikiContext());
    Assert.assertTrue("Document wiki:space1.page1 has not been removed from the database", removedPage.isNew());
    // space.deletedpage
    deletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "deletedpage"), getXWikiContext());
    Assert.assertTrue("Document wiki:space.deleted has been restored", deletedpage.isNew());
    // space.modifieddeletedpage
    modifieddeletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "modifieddeletedpage"), getXWikiContext());
    Assert.assertTrue("Document wiki:space.modifieddeletedpage has been restored", modifieddeletedpage.isNew());
    // space.pagewithobject
    pagewithobject = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "pagewithobject"), getXWikiContext());
    Assert.assertNull("Document wiki:space.pagewithobject does not contain an XWiki.XWikiGroups object", pagewithobject.getXObject(new LocalDocumentReference("XWiki", "XWikiGroups")));
    // space1.modified
    XWikiDocument space1modified = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space1", "modified"), getXWikiContext());
    Assert.assertFalse("Document wiki:space.modified has been removed from the database", space1modified.isNew());
}
Also used : LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) 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) Test(org.junit.Test)

Example 67 with BaseClass

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

the class DocumentUnifiedDiffBuilder method isPrivateProperty.

private boolean isPrivateProperty(BaseProperty<?> property) {
    BaseCollection<?> object = property == null ? null : property.getObject();
    if (object != null) {
        BaseClass xclass = object.getXClass(this.xcontextProvider.get());
        if (xclass != null) {
            PropertyClass propertyClass = (PropertyClass) xclass.get(property.getName());
            String propertyType = propertyClass == null ? null : propertyClass.getClassType();
            return "Password".equals(propertyType) || "Email".equals(propertyClass);
        }
    }
    return false;
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Example 68 with BaseClass

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

the class BaseCollection method toXML.

/**
 * @deprecated since 9.0RC1, use {@link #toXML()} instead
 */
@Override
@Deprecated
public Element toXML(BaseClass bclass) {
    // Set passed class in the context so that the input event generator finds it
    XWikiContext xcontext = getXWikiContext();
    BaseClass currentBaseClass;
    DocumentReference classReference;
    if (bclass != null && xcontext != null) {
        classReference = bclass.getDocumentReference();
        currentBaseClass = xcontext.getBaseClass(bclass.getDocumentReference());
        xcontext.addBaseClass(bclass);
    } else {
        classReference = null;
        currentBaseClass = null;
    }
    try {
        return super.toXML();
    } finally {
        if (classReference != null) {
            if (currentBaseClass != null) {
                xcontext.addBaseClass(currentBaseClass);
            } else {
                xcontext.removeBaseClass(classReference);
            }
        }
    }
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 69 with BaseClass

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

the class BaseObject method getDiff.

@Override
public List<ObjectDiff> getDiff(Object oldEntity, XWikiContext context) {
    ArrayList<ObjectDiff> difflist = new ArrayList<ObjectDiff>();
    BaseObject oldObject = (BaseObject) oldEntity;
    // Iterate over the new properties first, to handle changed and added objects
    for (String propertyName : this.getPropertyList()) {
        BaseProperty newProperty = (BaseProperty) this.getField(propertyName);
        BaseProperty oldProperty = (BaseProperty) oldObject.getField(propertyName);
        BaseClass bclass = getXClass(context);
        PropertyClass pclass = (PropertyClass) ((bclass == null) ? null : bclass.getField(propertyName));
        String propertyType = (pclass == null) ? "" : pclass.getClassType();
        if (oldProperty == null) {
            // The property exist in the new object, but not in the old one
            if ((newProperty != null) && (!newProperty.toText().equals(""))) {
                String newPropertyValue = (newProperty.getValue() instanceof String || pclass == null) ? newProperty.toText() : pclass.displayView(propertyName, this, context);
                difflist.add(new ObjectDiff(getXClassReference(), getNumber(), getGuid(), ObjectDiff.ACTION_PROPERTYADDED, propertyName, propertyType, "", newPropertyValue));
            }
        } else if (!oldProperty.toText().equals(((newProperty == null) ? "" : newProperty.toText()))) {
            // The property exists in both objects and is different
            if (pclass != null) {
                // Put the values as they would be displayed in the interface
                String newPropertyValue = (newProperty.getValue() instanceof String) ? newProperty.toText() : pclass.displayView(propertyName, this, context);
                String oldPropertyValue = (oldProperty.getValue() instanceof String) ? oldProperty.toText() : pclass.displayView(propertyName, oldObject, context);
                difflist.add(new ObjectDiff(getXClassReference(), getNumber(), getGuid(), ObjectDiff.ACTION_PROPERTYCHANGED, propertyName, propertyType, oldPropertyValue, newPropertyValue));
            } else {
                // Cannot get property definition, so use the plain value
                difflist.add(new ObjectDiff(getXClassReference(), getNumber(), getGuid(), ObjectDiff.ACTION_PROPERTYCHANGED, propertyName, propertyType, oldProperty.toText(), newProperty.toText()));
            }
        }
    }
    // Iterate over the old properties, in case there are some removed properties
    for (String propertyName : oldObject.getPropertyList()) {
        BaseProperty newProperty = (BaseProperty) this.getField(propertyName);
        BaseProperty oldProperty = (BaseProperty) oldObject.getField(propertyName);
        BaseClass bclass = getXClass(context);
        PropertyClass pclass = (PropertyClass) ((bclass == null) ? null : bclass.getField(propertyName));
        String propertyType = (pclass == null) ? "" : pclass.getClassType();
        if (newProperty == null) {
            // The property exists in the old object, but not in the new one
            if ((oldProperty != null) && (!oldProperty.toText().equals(""))) {
                if (pclass != null) {
                    // Put the values as they would be displayed in the interface
                    String oldPropertyValue = (oldProperty.getValue() instanceof String) ? oldProperty.toText() : pclass.displayView(propertyName, oldObject, context);
                    difflist.add(new ObjectDiff(oldObject.getXClassReference(), oldObject.getNumber(), oldObject.getGuid(), ObjectDiff.ACTION_PROPERTYREMOVED, propertyName, propertyType, oldPropertyValue, ""));
                } else {
                    // Cannot get property definition, so use the plain value
                    difflist.add(new ObjectDiff(oldObject.getXClassReference(), oldObject.getNumber(), oldObject.getGuid(), ObjectDiff.ACTION_PROPERTYREMOVED, propertyName, propertyType, oldProperty.toText(), ""));
                }
            }
        }
    }
    return difflist;
}
Also used : ArrayList(java.util.ArrayList) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Example 70 with BaseClass

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

the class BaseObject method mergeField.

@Override
protected void mergeField(PropertyInterface currentElement, ElementInterface previousElement, ElementInterface newElement, MergeConfiguration configuration, XWikiContext context, MergeResult mergeResult) {
    BaseClass baseClass = getXClass(context);
    if (baseClass != null) {
        PropertyClass propertyClass = (PropertyClass) baseClass.get(currentElement.getName());
        if (propertyClass != null) {
            try {
                propertyClass.mergeProperty((BaseProperty) currentElement, (BaseProperty) previousElement, (BaseProperty) newElement, configuration, context, mergeResult);
            } catch (Exception e) {
                mergeResult.getLog().error("Failed to merge field [{}]", currentElement.getName(), e);
            }
            return;
        }
    }
    super.mergeField(currentElement, previousElement, newElement, configuration, context, mergeResult);
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) XWikiException(com.xpn.xwiki.XWikiException)

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