Search in sources :

Example 6 with EntityReference

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

the class IndexedObjectReferenceTest method testConstructors.

/**
 * Ensures the equivalence of constructors.
 */
@Test
public void testConstructors() {
    IndexedObjectReference reference1 = new IndexedObjectReference(new EntityReference("XWiki.Class[2]", EntityType.OBJECT, new EntityReference("Page", EntityType.DOCUMENT, new EntityReference("Space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)))));
    IndexedObjectReference reference2 = new IndexedObjectReference("XWiki.Class", 2, new EntityReference("Page", EntityType.DOCUMENT, new EntityReference("Space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI))));
    assertEquals(reference1, reference2);
    assertEquals(reference1.getClassName(), reference2.getClassName());
    assertEquals(reference1.getObjectNumber(), reference2.getObjectNumber());
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) Test(org.junit.Test)

Example 7 with EntityReference

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

the class DefaultModelBridgeTest method testAuthorReference.

@Test
public void testAuthorReference() throws Exception {
    XWikiContext context = mock(XWikiContext.class);
    XWiki xwiki = mock(XWiki.class);
    XWikiDocument document = mock(XWikiDocument.class);
    DocumentReference authorReference = mock(DocumentReference.class);
    EntityReference entityReference = mock(EntityReference.class);
    when(this.contextProvider.get()).thenReturn(context);
    when(context.getWiki()).thenReturn(xwiki);
    when(xwiki.getDocument(entityReference, context)).thenReturn(document);
    when(document.getAuthorReference()).thenReturn(authorReference);
    DocumentReference result = this.mocker.getComponentUnderTest().getAuthorReference(entityReference);
    assertEquals(authorReference, result);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) EntityReference(org.xwiki.model.reference.EntityReference) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) Test(org.junit.Test)

Example 8 with EntityReference

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

the class AbstractExtensionValidator method checkAccess.

protected void checkAccess(Right entityRight, String namespaceString, Request request) throws AccessDeniedException {
    Namespace namespace = NamespaceUtils.toNamespace(namespaceString);
    // Root namespace
    if (namespace == null) {
        checkRootRight(entityRight, request);
        return;
    }
    if (namespace.getType() != null) {
        // User
        if (namespace.getType().equals("user")) {
            EntityReference reference = this.resolver.resolve(namespace.getValue(), EntityType.DOCUMENT);
            checkUserRight(reference, request);
            return;
        }
        // Entity
        EntityType entityType = EnumUtils.getEnum(EntityType.class, namespace.getType().toUpperCase());
        if (entityType != null) {
            EntityReference reference = this.resolver.resolve(namespace.getValue(), entityType);
            checkAccess(reference, entityRight, request);
            return;
        }
    }
    // Unknown namespace
    checkNamespaceRight(namespace, Right.PROGRAM, request);
}
Also used : EntityType(org.xwiki.model.EntityType) EntityReference(org.xwiki.model.reference.EntityReference) Namespace(org.xwiki.component.namespace.Namespace)

Example 9 with EntityReference

use of org.xwiki.model.reference.EntityReference 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 10 with EntityReference

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

the class AbstractSheetBinder method bind.

private boolean bind(DocumentModelBridge document, String sheetReferenceString) {
    EntityReference sheetBindingClassReference = this.relativeReferenceResolver.resolve(getSheetBindingClass(), EntityType.DOCUMENT);
    List<BaseObject> sheetBindingObjects = ((XWikiDocument) document).getXObjects(sheetBindingClassReference);
    if (sheetBindingObjects != null) {
        for (BaseObject sheetBindingObject : sheetBindingObjects) {
            // The list of XWiki objects can contain null values due to a design flaw in the old XWiki core.
            if (sheetBindingObject != null) {
                String boundSheetStringRef = sheetBindingObject.getStringValue(SHEET_PROPERTY);
                if (StringUtils.equals(boundSheetStringRef, sheetReferenceString)) {
                    return false;
                }
            }
        }
    }
    try {
        BaseObject sheetBindingObject = ((XWikiDocument) document).newXObject(sheetBindingClassReference, getXWikiContext());
        sheetBindingObject.setStringValue(SHEET_PROPERTY, sheetReferenceString);
    } catch (XWikiException e) {
        String docStringReference = this.defaultEntityReferenceSerializer.serialize(document.getDocumentReference());
        this.logger.warn("Failed to bind sheet [{}] to document [{}].", sheetReferenceString, docStringReference);
        return false;
    }
    return true;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) EntityReference(org.xwiki.model.reference.EntityReference) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

EntityReference (org.xwiki.model.reference.EntityReference)338 Test (org.junit.Test)157 DocumentReference (org.xwiki.model.reference.DocumentReference)107 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)39 BaseObject (com.xpn.xwiki.objects.BaseObject)38 ArrayList (java.util.ArrayList)27 XWikiContext (com.xpn.xwiki.XWikiContext)24 WikiReference (org.xwiki.model.reference.WikiReference)24 SpaceReference (org.xwiki.model.reference.SpaceReference)23 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)18 XWikiException (com.xpn.xwiki.XWikiException)17 EntityType (org.xwiki.model.EntityType)11 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)10 EntityReferenceProvider (org.xwiki.model.reference.EntityReferenceProvider)9 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)8 XDOM (org.xwiki.rendering.block.XDOM)8 URL (java.net.URL)7 AttachmentReference (org.xwiki.model.reference.AttachmentReference)7 HashMap (java.util.HashMap)6 Before (org.junit.Before)6