Search in sources :

Example 1 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) throws QueryException {
    ClassPropertyReference propertyReference = newPropertyClass.getReference();
    EntityReference classReference = propertyReference.extractReference(EntityType.DOCUMENT);
    EntityReference wikiReference = propertyReference.extractReference(EntityType.WIKI);
    // Get all document containing object of modified class
    Query query = this.queryManager.createQuery("from doc.object(" + this.localSerializer.serialize(classReference) + ") as obj", Query.XWQL);
    query.setWiki(wikiReference.getName());
    List<String> documents = query.execute();
    if (!documents.isEmpty()) {
        XWikiContext xcontext = this.xcontextProvider.get();
        String currentWikiId = xcontext.getWikiId();
        try {
            // Switch to class wiki to be safer
            xcontext.setWikiId(wikiReference.getName());
            for (String documentName : documents) {
                try {
                    migrate(newPropertyClass, documentName, xcontext);
                } catch (XWikiException e) {
                    this.logger.error("Failed to migrate property [{}] in document [{}]", propertyReference, documentName, xcontext);
                }
            }
        } finally {
            // Restore context wiki
            xcontext.setWikiId(currentWikiId);
        }
    }
}
Also used : Query(org.xwiki.query.Query) EntityReference(org.xwiki.model.reference.EntityReference) XWikiContext(com.xpn.xwiki.XWikiContext) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 2 with ClassPropertyReference

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

the class DefaultStringEntityReferenceResolverTest method testResolveClassPropertyReference.

/**
 * Tests resolving object references.
 */
@Test
public void testResolveClassPropertyReference() {
    EntityReference reference = new ClassPropertyReference(resolver.resolve("wiki:space.page^ClassProp", EntityType.CLASS_PROPERTY));
    assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
    assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
    assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
    assertEquals("ClassProp", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
    // default values
    reference = new ClassPropertyReference(resolver.resolve("", EntityType.CLASS_PROPERTY));
    assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
    assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
    assertEquals(DEFAULT_PAGE, reference.extractReference(EntityType.DOCUMENT).getName());
    assertEquals(DEFAULT_CLASS_PROPERTY, reference.extractReference(EntityType.CLASS_PROPERTY).getName());
    // without some of the parents
    reference = new ClassPropertyReference(resolver.resolve("space.page^ClassProp", EntityType.CLASS_PROPERTY));
    assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
    assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
    assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
    assertEquals("ClassProp", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
    reference = new ClassPropertyReference(resolver.resolve("page^ClassProp", EntityType.CLASS_PROPERTY));
    assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
    assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
    assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
    assertEquals("ClassProp", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
    reference = new ClassPropertyReference(resolver.resolve("XWiki.Class", EntityType.CLASS_PROPERTY));
    assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
    assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
    assertEquals(DEFAULT_PAGE, reference.extractReference(EntityType.DOCUMENT).getName());
    assertEquals("XWiki.Class", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
    // property without object
    reference = new ClassPropertyReference(resolver.resolve("wiki:space.page.property", EntityType.CLASS_PROPERTY));
    assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
    assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
    assertEquals(DEFAULT_PAGE, reference.extractReference(EntityType.DOCUMENT).getName());
    Assert.assertEquals("wiki:space.page.property", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
    // object with no name
    reference = new ClassPropertyReference(resolver.resolve("wiki:space.page^", EntityType.CLASS_PROPERTY));
    assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
    assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
    assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
    assertEquals(DEFAULT_CLASS_PROPERTY, reference.extractReference(EntityType.CLASS_PROPERTY).getName());
    // test separator escape
    reference = new ClassPropertyReference(resolver.resolve("wiki:space.page^obje\\^ct", EntityType.CLASS_PROPERTY));
    assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
    assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
    assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
    assertEquals("obje^ct", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
    // and that separators don't need to be escaped other than in the object name
    reference = new ClassPropertyReference(resolver.resolve("wiki:spa^ce.page^ClassProp", EntityType.CLASS_PROPERTY));
    assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
    assertEquals("spa^ce", reference.extractReference(EntityType.SPACE).getName());
    assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
    assertEquals("ClassProp", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
    reference = new ClassPropertyReference(resolver.resolve(":.\\^@", EntityType.CLASS_PROPERTY));
    assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
    assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
    assertEquals(DEFAULT_PAGE, reference.extractReference(EntityType.DOCUMENT).getName());
    assertEquals(":.^@", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) Test(org.junit.Test)

Example 3 with ClassPropertyReference

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

the class ModelScriptServiceTest method resolveClassPropertyWithHintAndParameters.

@Test
public void resolveClassPropertyWithHintAndParameters() throws Exception {
    when(this.componentManager.getInstance(EntityReferenceResolver.TYPE_STRING, "custom")).thenReturn(this.stringEntityReferenceResolver);
    ClassPropertyReference reference = new ClassPropertyReference("property", new DocumentReference("wiki", "Space", "Class"));
    Object[] parameters = new Object[] { new DocumentReference("wiki", "Space", "Page"), "extra" };
    when(this.stringEntityReferenceResolver.resolve("Class^property", EntityType.CLASS_PROPERTY, parameters)).thenReturn(reference);
    Assert.assertEquals(reference, this.service.resolveClassProperty("Class^property", "custom", parameters[0], parameters[1]));
}
Also used : ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 4 with ClassPropertyReference

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

the class AbstractListClassPropertyValuesProviderTest method addProperty.

protected void addProperty(String name, PropertyClass definition, boolean withQueryBuilders) throws Exception {
    XWiki xwiki = this.xcontext.getWiki();
    BaseClass xclass = this.classDocument.getXClass();
    ClassPropertyReference propertyReference = new ClassPropertyReference(name, this.classReference);
    when(xwiki.getDocument(propertyReference, this.xcontext)).thenReturn(this.classDocument);
    when(xclass.get(name)).thenReturn(definition);
    definition.setOwnerDocument(this.classDocument);
    if (withQueryBuilders) {
        DefaultParameterizedType allowedValuesQueryBuilderType = new DefaultParameterizedType(null, QueryBuilder.class, definition.getClass());
        QueryBuilder allowedValuesQueryBuilder = getMocker().getInstance(allowedValuesQueryBuilderType);
        when(allowedValuesQueryBuilder.build(definition)).thenReturn(this.allowedValuesQuery);
        if (definition instanceof ListClass) {
            DefaultParameterizedType usedValuesQueryBuilderType = new DefaultParameterizedType(null, QueryBuilder.class, ListClass.class);
            this.usedValuesQueryBuilder = getMocker().getInstance(usedValuesQueryBuilderType, "usedValues");
            when(this.usedValuesQueryBuilder.build((ListClass) definition)).thenReturn(this.usedValuesQuery);
        }
    }
}
Also used : ListClass(com.xpn.xwiki.objects.classes.ListClass) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWiki(com.xpn.xwiki.XWiki) QueryBuilder(org.xwiki.query.QueryBuilder) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference)

Example 5 with ClassPropertyReference

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

the class DBListClassPropertyValuesProviderTest method getValuesForWrongProperty.

@Test
public void getValuesForWrongProperty() throws Exception {
    ClassPropertyReference propertyReference = new ClassPropertyReference("date", this.classReference);
    when(this.entityReferenceSerializer.serialize(propertyReference)).thenReturn("status reference");
    try {
        this.mocker.getComponentUnderTest().getValues(propertyReference, 0);
        fail();
    } catch (XWikiRestException expected) {
        assertEquals("This [status reference] is not a [DBListClass] property.", expected.getMessage());
    }
}
Also used : XWikiRestException(org.xwiki.rest.XWikiRestException) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) 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