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);
}
}
}
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());
}
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]));
}
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);
}
}
}
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());
}
}
Aggregations