Search in sources :

Example 11 with ObjectPropertyReference

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

the class ObjectSolrReferenceResolver method getReferences.

@Override
public List<EntityReference> getReferences(EntityReference objectReference) throws SolrIndexerException {
    List<EntityReference> result = new ArrayList<>();
    // Object itself
    result.add(objectReference);
    // Object properties
    DocumentReference documentReference = new DocumentReference(objectReference.getParent());
    XWikiDocument document;
    try {
        document = getDocument(documentReference);
    } catch (Exception e) {
        throw new SolrIndexerException("Failed to get document for object [" + objectReference + "]", e);
    }
    BaseObject object = document.getXObject(objectReference);
    if (object != null) {
        for (Object field : object.getFieldList()) {
            BaseProperty<ObjectPropertyReference> objectProperty = (BaseProperty<ObjectPropertyReference>) field;
            ObjectPropertyReference objectPropertyReference = objectProperty.getReference();
            try {
                Iterables.addAll(result, this.objectPropertyResolverProvider.get().getReferences(objectPropertyReference));
            } catch (Exception e) {
                this.logger.error("Failed to resolve references for object property [" + objectPropertyReference + "]", e);
            }
        }
    }
    return result;
}
Also used : SolrIndexerException(org.xwiki.search.solr.internal.api.SolrIndexerException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) EntityReference(org.xwiki.model.reference.EntityReference) ArrayList(java.util.ArrayList) BaseObject(com.xpn.xwiki.objects.BaseObject) BaseProperty(com.xpn.xwiki.objects.BaseProperty) DocumentReference(org.xwiki.model.reference.DocumentReference) SolrIndexerException(org.xwiki.search.solr.internal.api.SolrIndexerException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 12 with ObjectPropertyReference

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

the class SolrReferenceResolverTest method setUp.

@Before
public void setUp() throws Exception {
    this.defaultSolrReferenceResolver = this.mocker.getInstance(SolrReferenceResolver.class);
    Utils.setComponentManager(this.mocker);
    // XWiki
    this.xwiki = mock(XWiki.class);
    // XWikiContext
    this.xcontext = new XWikiContext();
    this.xcontext.setWikiId("xwiki");
    this.xcontext.setWiki(this.xwiki);
    // XWikiContext Provider
    Provider<XWikiContext> xcontextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
    when(xcontextProvider.get()).thenReturn(this.xcontext);
    // XWikiContext trough Execution
    Execution execution = this.mocker.getInstance(Execution.class);
    execution.setContext(new ExecutionContext());
    execution.getContext().setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, this.xcontext);
    // References
    this.objectReference1221 = new BaseObjectReference(this.classReference111, 0, this.documentReference122);
    this.objectReference1222 = new BaseObjectReference(this.classReference111, 1, this.documentReference122);
    this.propertyReference12221 = new ObjectPropertyReference("aStringProperty", objectReference1222);
    this.propertyReference12223 = new ObjectPropertyReference("anIntegerProperty", objectReference1222);
    // XWiki model data
    this.xclass111 = mock(BaseClass.class);
    this.xdocument111 = mock(XWikiDocument.class, "xwikiDocument111");
    this.xdocument112 = mock(XWikiDocument.class, "xwikiDocument112");
    this.xdocument113 = mock(XWikiDocument.class, "xwikiDocument113");
    this.xdocument121 = mock(XWikiDocument.class, "xwikiDocument121");
    this.xattachment1211 = mock(XWikiAttachment.class, "xwikiAttachment1211");
    this.xattachment1212 = mock(XWikiAttachment.class, "xwikiAttachment1212");
    this.xdocument122 = mock(XWikiDocument.class, "xwikiDocument122");
    this.xobject1221 = mock(BaseObject.class, "xwikiObject1221");
    this.xobject1222 = mock(BaseObject.class, "xwikiObject1222");
    this.xproperty12221 = mock(StringProperty.class, "xwikiProperty12221");
    this.xproperty12223 = mock(IntegerProperty.class, "xwikiProperty12223");
    this.queryManager = this.mocker.getInstance(QueryManager.class);
    final Query spacesWiki1Query = mock(DefaultQuery.class, "getSpacesWiki1");
    final Query documentsSpace11Query = mock(DefaultQuery.class, "getSpaceDocsNameSpace11");
    final Query documentsSpace12Query = mock(DefaultQuery.class, "getSpaceDocsNameSpace12");
    final Query documentsSpace13Query = mock(DefaultQuery.class, "getSpaceDocsNameSpace13");
    final Query spacesWiki2Query = mock(DefaultQuery.class, "getSpacesWiki2");
    // Data
    when(xwiki.exists(any(DocumentReference.class), any(XWikiContext.class))).thenReturn(true);
    // Query manager and specific queries mocking.
    when(queryManager.getNamedQuery("getSpaces")).thenReturn(spacesWiki1Query);
    when(spacesWiki1Query.setWiki(wikiReference1.getName())).thenReturn(spacesWiki1Query);
    when(spacesWiki1Query.setWiki(wikiReference2.getName())).thenReturn(spacesWiki2Query);
    when(queryManager.getNamedQuery("getSpaceDocsName")).thenReturn(documentsSpace11Query);
    when(documentsSpace11Query.setWiki(any(String.class))).thenReturn(documentsSpace11Query);
    EntityReferenceSerializer<String> localEntityReferenceSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
    when(documentsSpace11Query.bindValue("space", localEntityReferenceSerializer.serialize(spaceReference11))).thenReturn(documentsSpace11Query);
    when(documentsSpace11Query.bindValue("space", localEntityReferenceSerializer.serialize(spaceReference12))).thenReturn(documentsSpace12Query);
    when(documentsSpace11Query.bindValue("space", localEntityReferenceSerializer.serialize(spaceReference13))).thenReturn(documentsSpace13Query);
    // Spaces in wikis.
    when(spacesWiki1Query.execute()).thenReturn(Arrays.<Object>asList(localEntityReferenceSerializer.serialize(spaceReference11), localEntityReferenceSerializer.serialize(spaceReference12), localEntityReferenceSerializer.serialize(spaceReference13)));
    when(spacesWiki2Query.execute()).thenReturn(Collections.emptyList());
    // space 11
    when(documentsSpace11Query.execute()).thenReturn(Arrays.<Object>asList(classReference111.getName(), documentReference112.getName(), documentReference113.getName()));
    // document 111
    when(xwiki.getDocument(eq(documentReference111), any(XWikiContext.class))).thenReturn(xdocument111);
    when(xdocument111.getXClass()).thenReturn(xclass111);
    // document 112
    when(xwiki.getDocument(eq(documentReference112), any(XWikiContext.class))).thenReturn(xdocument112);
    when(xdocument112.getXObjects()).thenReturn(Collections.<DocumentReference, List<BaseObject>>emptyMap());
    when(xdocument112.getTranslationLocales(any(XWikiContext.class))).thenReturn(Collections.<Locale>emptyList());
    // document 113
    when(xwiki.getDocument(eq(documentReference113), any(XWikiContext.class))).thenReturn(xdocument113);
    when(xdocument113.getAttachmentList()).thenReturn(Collections.<XWikiAttachment>emptyList());
    when(xdocument113.getXObjects()).thenReturn(Collections.<DocumentReference, List<BaseObject>>emptyMap());
    when(xdocument113.getTranslationLocales(any(XWikiContext.class))).thenReturn(Arrays.asList(new Locale("ro")));
    // space 12
    when(documentsSpace12Query.execute()).thenReturn(Arrays.<Object>asList(documentReference121.getName(), documentReference122.getName()));
    // document 121
    when(xwiki.getDocument(eq(documentReference121), any(XWikiContext.class))).thenReturn(xdocument121);
    when(xdocument121.getAttachmentList()).thenReturn(Arrays.asList(xattachment1211, xattachment1212));
    when(xattachment1211.getReference()).thenReturn(attachmentReference1211);
    when(xattachment1212.getReference()).thenReturn(attachmentReference1212);
    when(xdocument121.getXObjects()).thenReturn(Collections.<DocumentReference, List<BaseObject>>emptyMap());
    when(xdocument121.getTranslationLocales(any(XWikiContext.class))).thenReturn(Collections.<Locale>emptyList());
    // document 122
    when(xwiki.getDocument(eq(documentReference122), any(XWikiContext.class))).thenReturn(xdocument122);
    when(xdocument122.getAttachmentList()).thenReturn(Collections.<XWikiAttachment>emptyList());
    Map<DocumentReference, List<BaseObject>> xObjects = new HashMap<DocumentReference, List<BaseObject>>();
    // Yes, it seems that we can have null objects for some reason.
    xObjects.put(classReference111, Arrays.asList(null, xobject1221, xobject1222));
    when(xdocument122.getXObjects()).thenReturn(xObjects);
    when(xdocument122.getTranslationLocales(any(XWikiContext.class))).thenReturn(Collections.<Locale>emptyList());
    // object 1221
    when(xdocument122.getXObject(objectReference1221)).thenReturn(xobject1221);
    when(xdocument122.getXObject((EntityReference) objectReference1221)).thenReturn(xobject1221);
    when(xobject1221.getReference()).thenReturn(objectReference1221);
    when(xobject1221.getXClass(any(XWikiContext.class))).thenReturn(xclass111);
    when(xobject1221.getFieldList()).thenReturn(Collections.EMPTY_LIST);
    // object 1222
    when(xdocument122.getXObject(objectReference1222)).thenReturn(xobject1222);
    when(xdocument122.getXObject((EntityReference) objectReference1222)).thenReturn(xobject1222);
    when(xobject1222.getReference()).thenReturn(objectReference1222);
    when(xobject1222.getXClass(any(XWikiContext.class))).thenReturn(xclass111);
    when(xobject1222.getFieldList()).thenReturn(Arrays.asList(xproperty12221, xproperty12223));
    // object 1222 fields
    when(xproperty12221.getReference()).thenReturn(propertyReference12221);
    when(xproperty12221.getName()).thenReturn(propertyReference12221.getName());
    when(xproperty12223.getReference()).thenReturn(propertyReference12223);
    when(xproperty12223.getName()).thenReturn(propertyReference12223.getName());
    // class 111 fields
    when(xclass111.get(propertyReference12221.getName())).thenReturn(null);
    when(xclass111.get(propertyReference12223.getName())).thenReturn(null);
    // space 13
    when(documentsSpace13Query.execute()).thenReturn(Collections.emptyList());
}
Also used : Locale(java.util.Locale) IntegerProperty(com.xpn.xwiki.objects.IntegerProperty) ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) Query(org.xwiki.query.Query) DefaultQuery(org.xwiki.query.internal.DefaultQuery) HashMap(java.util.HashMap) XWiki(com.xpn.xwiki.XWiki) XWikiContext(com.xpn.xwiki.XWikiContext) StringProperty(com.xpn.xwiki.objects.StringProperty) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Execution(org.xwiki.context.Execution) DefaultExecution(org.xwiki.context.internal.DefaultExecution) ExecutionContext(org.xwiki.context.ExecutionContext) BaseObjectReference(com.xpn.xwiki.objects.BaseObjectReference) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) QueryManager(org.xwiki.query.QueryManager) ComponentList(org.xwiki.test.annotation.ComponentList) ReferenceComponentList(com.xpn.xwiki.test.reference.ReferenceComponentList) List(java.util.List) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 13 with ObjectPropertyReference

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

the class TestUtils method setWikiPreference.

/**
 * Sets the value of an existing property of XWiki.XWikiPreferences.
 *
 * @param propertyName name of the property to set
 * @param value value to set to the property
 * @since 9.7RC1
 */
public void setWikiPreference(String propertyName, String value) throws Exception {
    DocumentReference documentReference = new DocumentReference(getCurrentWiki(), "XWiki", "XWikiPreferences");
    ObjectReference objectReference = new ObjectReference("XWiki.XWikiPreferences[0]", documentReference);
    Property property = RestTestUtils.property(propertyName, value);
    org.xwiki.rest.model.jaxb.Object preferenceObject = rest().get(objectReference, false);
    if (preferenceObject == null) {
        // The object does not exist, create it
        preferenceObject = RestTestUtils.object("XWiki.XWikiPreferences");
        preferenceObject.withProperties(property);
        TestUtils.assertStatusCodes(rest().executePost(ObjectsResource.class, preferenceObject, rest().toElements(documentReference)), true, STATUS_CREATED);
    } else {
        // The object exist just set the property (faster than updating the whole object)
        ObjectPropertyReference propertyReference = new ObjectPropertyReference(propertyName, objectReference);
        TestUtils.assertStatusCodes(rest().executePut(ObjectPropertyResource.class, property, rest().toElements(propertyReference)), true, STATUS_ACCEPTED);
    }
}
Also used : ObjectsResource(org.xwiki.rest.resources.objects.ObjectsResource) ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) ObjectReference(org.xwiki.model.reference.ObjectReference) Property(org.xwiki.rest.model.jaxb.Property) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) ObjectPropertyResource(org.xwiki.rest.resources.objects.ObjectPropertyResource)

Example 14 with ObjectPropertyReference

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

the class DefaultStringEntityReferenceResolverTest method testResolveObjectPropertyReference.

/**
 * Tests resolving object references.
 */
@Test
public void testResolveObjectPropertyReference() {
    EntityReference reference = new ObjectPropertyReference(resolver.resolve("wiki:space.page^object.prop", EntityType.OBJECT_PROPERTY));
    assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
    assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
    assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
    assertEquals("object", reference.extractReference(EntityType.OBJECT).getName());
    assertEquals("prop", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
    // default values
    reference = new ObjectPropertyReference(resolver.resolve("", EntityType.OBJECT_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_OBJECT, reference.extractReference(EntityType.OBJECT).getName());
    assertEquals(DEFAULT_OBJECT_PROPERTY, reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
    // without some of the parents
    reference = new ObjectPropertyReference(resolver.resolve("space.page^Object.prop", EntityType.OBJECT_PROPERTY));
    assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
    assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
    assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
    assertEquals("Object", reference.extractReference(EntityType.OBJECT).getName());
    assertEquals("prop", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
    reference = new ObjectPropertyReference(resolver.resolve("page^Object.prop", EntityType.OBJECT_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("Object", reference.extractReference(EntityType.OBJECT).getName());
    assertEquals("prop", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
    reference = new ObjectPropertyReference(resolver.resolve("Object.prop", EntityType.OBJECT_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("Object", reference.extractReference(EntityType.OBJECT).getName());
    assertEquals("prop", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
    reference = new ObjectPropertyReference(resolver.resolve("FooBar", EntityType.OBJECT_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_OBJECT, reference.extractReference(EntityType.OBJECT).getName());
    assertEquals("FooBar", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
    // object without property, parsed as property
    reference = new ObjectPropertyReference(resolver.resolve("page^Object", EntityType.OBJECT_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_OBJECT, reference.extractReference(EntityType.OBJECT).getName());
    assertEquals("page^Object", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
    // empty prop
    reference = new ObjectPropertyReference(resolver.resolve("wiki:space.page^Object.", EntityType.OBJECT_PROPERTY));
    assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
    assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
    assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
    assertEquals("Object", reference.extractReference(EntityType.OBJECT).getName());
    assertEquals(DEFAULT_OBJECT_PROPERTY, reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
    // test separator escape
    reference = new ObjectPropertyReference(resolver.resolve("wiki:space.page^Object.prop\\.erty", EntityType.OBJECT_PROPERTY));
    assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
    assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
    assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
    assertEquals("Object", reference.extractReference(EntityType.OBJECT).getName());
    assertEquals("prop.erty", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
    // and that separators don't need to be escaped other than in the property name
    reference = new ObjectPropertyReference(resolver.resolve("wiki:space.page^x.wiki.class[0].prop", EntityType.OBJECT_PROPERTY));
    assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
    assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
    assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
    assertEquals("x.wiki.class[0]", reference.extractReference(EntityType.OBJECT).getName());
    assertEquals("prop", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
    reference = new ObjectPropertyReference(resolver.resolve(":^\\.@", EntityType.OBJECT_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_OBJECT, reference.extractReference(EntityType.OBJECT).getName());
    assertEquals(":^.@", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
}
Also used : ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) EntityReference(org.xwiki.model.reference.EntityReference) Test(org.junit.Test)

Example 15 with ObjectPropertyReference

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

the class ObjectTreeNode method getChildren.

private List<ObjectPropertyReference> getChildren(ObjectReference objectReference, int offset, int limit) throws Exception {
    List<ObjectPropertyReference> children = new ArrayList<ObjectPropertyReference>();
    XWikiContext xcontext = this.xcontextProvider.get();
    XWikiDocument document = xcontext.getWiki().getDocument(objectReference.getParent(), xcontext);
    BaseObject object = document.getXObject(objectReference);
    if (object != null) {
        List<String> properties = new ArrayList<String>(object.getPropertyList());
        Collections.sort(properties);
        for (String property : subList(properties, offset, limit)) {
            children.add(new ObjectPropertyReference(property, objectReference));
        }
    }
    return children;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

ObjectPropertyReference (org.xwiki.model.reference.ObjectPropertyReference)16 DocumentReference (org.xwiki.model.reference.DocumentReference)10 BaseObject (com.xpn.xwiki.objects.BaseObject)9 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)6 Test (org.junit.Test)5 ObjectReference (org.xwiki.model.reference.ObjectReference)5 BaseObjectReference (com.xpn.xwiki.objects.BaseObjectReference)4 ArrayList (java.util.ArrayList)3 EntityReference (org.xwiki.model.reference.EntityReference)3 XWiki (com.xpn.xwiki.XWiki)2 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 BaseProperty (com.xpn.xwiki.objects.BaseProperty)2 LESSResourceReference (org.xwiki.lesscss.resources.LESSResourceReference)2 AttachmentReference (org.xwiki.model.reference.AttachmentReference)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)2 Property (org.xwiki.rest.model.jaxb.Property)2 ObjectPropertyResource (org.xwiki.rest.resources.objects.ObjectPropertyResource)2 IntegerProperty (com.xpn.xwiki.objects.IntegerProperty)1 StringProperty (com.xpn.xwiki.objects.StringProperty)1