Search in sources :

Example 1 with XWiki

use of com.xpn.xwiki.XWiki in project xwiki-platform by xwiki.

the class AbstractAnnotationRESTResource method setUpDocuments.

/**
 * Helper function to prepare the XWiki documents and translations on the context and velocity context. <br>
 * TODO: check how this code could be written only once (not duplicate the prepareDocuments function in XWiki)
 *
 * @param docName the full name of the document to prepare context for
 * @param language the language of the document
 * @throws XWikiException if anything goes wrong accessing documents
 */
private void setUpDocuments(String docName, String language) throws XWikiException {
    XWikiContext context = xcontextProvider.get();
    XWiki xwiki = context.getWiki();
    // prepare the messaging tools and set them on context
    xwiki.prepareResources(context);
    XWikiDocument doc = xwiki.getDocument(docName, context);
    // setup the xwiki context
    context.put("doc", doc);
    context.put("cdoc", doc);
    XWikiDocument tdoc = doc.getTranslatedDocument(language, context);
    context.put("tdoc", tdoc);
    // and render the xwikivars to have all the variables set ($has*, $blacklistedSpaces, etc)
    context.getWiki().renderTemplate("xwikivars.vm", context);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki)

Example 2 with XWiki

use of com.xpn.xwiki.XWiki in project xwiki-platform by xwiki.

the class AbstractAnnotationRESTResource method updateContext.

/**
 * Helper method to make sure that the context is set to the right document and database name.
 *
 * @param wiki the REST wikiName path parameter
 * @param space the REST spaceName path parameter
 * @param page the REST pageName path parameter
 */
protected void updateContext(DocumentReference documentReference) {
    try {
        // Set the database to the current wiki.
        XWikiContext deprecatedContext = (XWikiContext) execution.getContext().getProperty("xwikicontext");
        deprecatedContext.setWikiId(documentReference.getWikiReference().getName());
        // Set the document to the current document.
        XWiki xwiki = deprecatedContext.getWiki();
        XWikiDocument currentDocument = xwiki.getDocument(documentReference, deprecatedContext);
        deprecatedContext.setDoc(currentDocument);
    } catch (Exception e) {
        // Just log it.
        getLogger().error("Failed to update the context for page [{}].", documentReference, e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) AnnotationServiceException(org.xwiki.annotation.AnnotationServiceException)

Example 3 with XWiki

use of com.xpn.xwiki.XWiki in project xwiki-platform by xwiki.

the class DefaultModelBridge method findDocument.

private XWikiDocument findDocument(EntityReference entityReference) throws EventStreamException {
    XWikiContext context = contextProvider.get();
    XWiki xwiki = context.getWiki();
    try {
        return xwiki.getDocument(entityReference, context);
    } catch (XWikiException e) {
        throw new EventStreamException(String.format("Unable to retrieve the given document [%s] in the current context.", entityReference), e);
    }
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) EventStreamException(org.xwiki.eventstream.EventStreamException) XWikiException(com.xpn.xwiki.XWikiException)

Example 4 with XWiki

use of com.xpn.xwiki.XWiki 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 5 with XWiki

use of com.xpn.xwiki.XWiki in project xwiki-platform by xwiki.

the class SendMailConfigClassDocumentConfigurationSourceTest method getPropertyWhenNoSendMailConfigClassXObject.

@Test
public void getPropertyWhenNoSendMailConfigClassXObject() throws Exception {
    Cache<Object> cache = mock(Cache.class);
    CacheManager cacheManager = this.mocker.getInstance(CacheManager.class);
    when(cacheManager.createNewCache(any(CacheConfiguration.class))).thenReturn(cache);
    WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
    when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("wiki");
    LocalDocumentReference classReference = new LocalDocumentReference("Mail", "SendMailConfigClass");
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getXObject(classReference)).thenReturn(null);
    DocumentReference documentReference = new DocumentReference("wiki", "Mail", "MailConfig");
    XWiki xwiki = mock(XWiki.class);
    when(xwiki.getDocument(eq(documentReference), any(XWikiContext.class))).thenReturn(document);
    XWikiContext xcontext = mock(XWikiContext.class);
    when(xcontext.getWiki()).thenReturn(xwiki);
    Provider<XWikiContext> xcontextProvider = this.mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
    when(xcontextProvider.get()).thenReturn(xcontext);
    assertEquals("defaultValue", this.mocker.getComponentUnderTest().getProperty("key", "defaultValue"));
}
Also used : LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) CacheManager(org.xwiki.cache.CacheManager) XWiki(com.xpn.xwiki.XWiki) XWikiContext(com.xpn.xwiki.XWikiContext) BaseObject(com.xpn.xwiki.objects.BaseObject) CacheConfiguration(org.xwiki.cache.config.CacheConfiguration) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) Test(org.junit.Test)

Aggregations

XWiki (com.xpn.xwiki.XWiki)174 XWikiContext (com.xpn.xwiki.XWikiContext)112 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)97 DocumentReference (org.xwiki.model.reference.DocumentReference)79 XWikiException (com.xpn.xwiki.XWikiException)61 BaseObject (com.xpn.xwiki.objects.BaseObject)44 Before (org.junit.Before)26 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)19 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)17 ExecutionContext (org.xwiki.context.ExecutionContext)17 ArrayList (java.util.ArrayList)15 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)14 Execution (org.xwiki.context.Execution)13 NotificationException (org.xwiki.notifications.NotificationException)8 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)8 Test (org.junit.Test)7 WikiReference (org.xwiki.model.reference.WikiReference)7 WikiManagerException (org.xwiki.wiki.manager.WikiManagerException)7 XWikiDeletedDocument (com.xpn.xwiki.doc.XWikiDeletedDocument)6 HashMap (java.util.HashMap)6