Search in sources :

Example 11 with ClassPropertyReference

use of org.xwiki.model.reference.ClassPropertyReference in project xwiki-platform by xwiki.

the class XClassMigratorListener method migrate.

private void migrate(PropertyClass newPropertyClass, String documentName, XWikiContext xcontext) throws XWikiException {
    BaseProperty newProperty = newPropertyClass.newProperty();
    ClassPropertyReference propertyReference = newPropertyClass.getReference();
    EntityReference classReference = propertyReference.extractReference(EntityType.DOCUMENT);
    XWikiDocument document = xcontext.getWiki().getDocument(this.resolver.resolve(documentName, classReference), xcontext);
    boolean modified = false;
    for (BaseObject xobject : document.getXObjects(classReference)) {
        BaseProperty property = (BaseProperty) xobject.getField(propertyReference.getName());
        // If the existing field is of different kind than what is produced by the new class property
        if (property != null && property.getClass() != newProperty.getClass()) {
            BaseProperty<?> convertedProperty = this.propertyConverter.convertProperty(property, newPropertyClass);
            // Set new field
            if (convertedProperty != null) {
                // Mark old field for removal, only if the conversion was successful, to avoid losing data.
                xobject.removeField(propertyReference.getName());
                // Don't set the new property if it's null (it means the property is not set).
                xobject.safeput(propertyReference.getName(), convertedProperty);
                modified = true;
            }
        }
    }
    // If anything changed save the document
    if (modified) {
        xcontext.getWiki().saveDocument(document, "Migrated property [" + propertyReference.getName() + "] from class [" + this.localSerializer.serialize(classReference) + "]", xcontext);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) EntityReference(org.xwiki.model.reference.EntityReference) BaseProperty(com.xpn.xwiki.objects.BaseProperty) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 12 with ClassPropertyReference

use of org.xwiki.model.reference.ClassPropertyReference in project xwiki-platform by xwiki.

the class DocumentUnifiedDiffBuilder method addClassPropertyDiff.

private void addClassPropertyDiff(PropertyClass previousProperty, PropertyClass nextProperty, DocumentUnifiedDiff documentDiff) {
    ClassPropertyReference previousReference = getClassPropertyVersionReference(previousProperty, documentDiff.getPreviousReference());
    ClassPropertyReference nextReference = getClassPropertyVersionReference(nextProperty, documentDiff.getNextReference());
    EntityUnifiedDiff<ClassPropertyReference> classPropertyDiff = new EntityUnifiedDiff<>(previousReference, nextReference);
    // Catch a property type change.
    maybeAddDiff(classPropertyDiff, "type", previousProperty == null ? null : previousProperty.getClassType(), nextProperty == null ? null : nextProperty.getClassType());
    addObjectDiff(previousProperty == null ? new PropertyClass() : previousProperty, nextProperty == null ? new PropertyClass() : nextProperty, classPropertyDiff);
    // The property name is already specified by the previous / next reference.
    classPropertyDiff.remove("name");
    // This meta property is not used (there's no UI to change it).
    classPropertyDiff.remove("unmodifiable");
    if (classPropertyDiff.size() > 0) {
        documentDiff.getClassPropertyDiffs().add(classPropertyDiff);
    }
}
Also used : EntityUnifiedDiff(org.xwiki.extension.xar.job.diff.EntityUnifiedDiff) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Example 13 with ClassPropertyReference

use of org.xwiki.model.reference.ClassPropertyReference in project xwiki-platform by xwiki.

the class ClassPropertiesTreeNode method getChildren.

@Override
protected List<String> getChildren(DocumentReference documentReference, int offset, int limit) throws Exception {
    XWikiContext xcontext = this.xcontextProvider.get();
    XWikiDocument document = xcontext.getWiki().getDocument(documentReference, xcontext);
    List<String> properties = new ArrayList<String>(document.getXClass().getPropertyList());
    Collections.sort(properties);
    List<String> children = new ArrayList<String>();
    for (String property : subList(properties, offset, limit)) {
        children.add(serialize(new ClassPropertyReference(property, documentReference)));
    }
    return children;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference)

Example 14 with ClassPropertyReference

use of org.xwiki.model.reference.ClassPropertyReference in project xwiki-platform by xwiki.

the class DBListClassPropertyValuesProviderTest method getValuesForMissingProperty.

@Test
public void getValuesForMissingProperty() throws Exception {
    ClassPropertyReference propertyReference = new ClassPropertyReference("status", this.classReference);
    when(this.entityReferenceSerializer.serialize(propertyReference)).thenReturn("status reference");
    try {
        this.mocker.getComponentUnderTest().getValues(propertyReference, 0);
        fail();
    } catch (XWikiRestException expected) {
        assertEquals("Property [status reference] not found.", expected.getMessage());
    }
}
Also used : XWikiRestException(org.xwiki.rest.XWikiRestException) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) Test(org.junit.Test)

Example 15 with ClassPropertyReference

use of org.xwiki.model.reference.ClassPropertyReference in project xwiki-platform by xwiki.

the class DBListClassPropertyValuesProviderTest method getValuesAllowed.

@Test
public void getValuesAllowed() throws Exception {
    ClassPropertyReference propertyReference = new ClassPropertyReference("category", this.classReference);
    DocumentReference authorReference = this.dbListClass.getOwnerDocument().getAuthorReference();
    PropertyValues values = new PropertyValues();
    when(this.authorExecutor.call(any(), eq(authorReference))).thenReturn(values);
    assertSame(values, this.mocker.getComponentUnderTest().getValues(propertyReference, 3));
    assertSame(values, this.mocker.getComponentUnderTest().getValues(propertyReference, 0, "text"));
}
Also used : PropertyValues(org.xwiki.rest.model.jaxb.PropertyValues) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

ClassPropertyReference (org.xwiki.model.reference.ClassPropertyReference)19 Test (org.junit.Test)12 DocumentReference (org.xwiki.model.reference.DocumentReference)7 PropertyValues (org.xwiki.rest.model.jaxb.PropertyValues)5 EntityReference (org.xwiki.model.reference.EntityReference)4 XWikiRestException (org.xwiki.rest.XWikiRestException)4 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 ComponentManager (org.xwiki.component.manager.ComponentManager)2 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)2 WikiReference (org.xwiki.model.reference.WikiReference)2 Query (org.xwiki.query.Query)2 PropertyValue (org.xwiki.rest.model.jaxb.PropertyValue)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiException (com.xpn.xwiki.XWikiException)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 BaseProperty (com.xpn.xwiki.objects.BaseProperty)1 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)1 DateClass (com.xpn.xwiki.objects.classes.DateClass)1 ListClass (com.xpn.xwiki.objects.classes.ListClass)1