Search in sources :

Example 1 with GadgetRenderer

use of org.xwiki.rendering.macro.dashboard.GadgetRenderer in project xwiki-platform by xwiki.

the class DashboardMacro method execute.

@Override
public List<Block> execute(DashboardMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    // We don't allow calling the Dashboard macro inside the Dashboard macro to prevent recursions!
    preventDashboardRecursion();
    // get the gadgets from the objects
    List<Gadget> gadgets;
    try {
        gadgets = this.gadgetSource.getGadgets(parameters.getSource(), context);
    } catch (Exception e) {
        String message = "Could not get the gadgets.";
        // log and throw further
        this.logger.error(message, e);
        throw new MacroExecutionException(message, e);
    }
    boolean isInEditMode = this.gadgetSource.isEditing();
    DashboardRenderer renderer = getDashboardRenderer(StringUtils.isEmpty(parameters.getLayout()) ? "columns" : parameters.getLayout());
    if (renderer == null) {
        String message = "Could not find dashboard renderer " + parameters.getLayout();
        // log and throw further
        this.logger.error(message);
        throw new MacroExecutionException(message);
    }
    GadgetRenderer gadgetRenderer = getGadgetRenderer(isInEditMode);
    // else, layout
    List<Block> layoutedResult;
    try {
        layoutedResult = renderer.renderGadgets(gadgets, gadgetRenderer, context);
    } catch (Exception e) {
        String message = "Could not render the gadgets for layout " + parameters.getLayout();
        // log and throw further
        this.logger.error(message, e);
        throw new MacroExecutionException(message, e);
    }
    // include the css and js for this macro. here so that it's included after any dependencies have included their
    // css, so that it cascades properly
    this.includeResources(isInEditMode);
    // put everything in a nice toplevel group for this dashboard, to be able to add classes to it
    GroupBlock topLevel = new GroupBlock();
    // mode
    if (isInEditMode) {
        topLevel.addChildren(this.gadgetSource.getDashboardSourceMetadata(parameters.getSource(), context));
    }
    topLevel.addChildren(layoutedResult);
    // add the style attribute of the dashboard macro as a class to the toplevel container
    topLevel.setParameter("class", MACRO_NAME + (StringUtils.isEmpty(parameters.getStyle()) ? "" : " " + parameters.getStyle()));
    // Reduce by 1 the recursive count so that we can have several dashboard macros rendered in the same context
    reduceDashboardRecursionCounter();
    return Collections.<Block>singletonList(topLevel);
}
Also used : Gadget(org.xwiki.rendering.macro.dashboard.Gadget) DashboardRenderer(org.xwiki.rendering.macro.dashboard.DashboardRenderer) GadgetRenderer(org.xwiki.rendering.macro.dashboard.GadgetRenderer) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) GroupBlock(org.xwiki.rendering.block.GroupBlock) Block(org.xwiki.rendering.block.Block) GroupBlock(org.xwiki.rendering.block.GroupBlock) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 2 with GadgetRenderer

use of org.xwiki.rendering.macro.dashboard.GadgetRenderer in project xwiki-platform by xwiki.

the class DashboardMacroTest method executeWhenNotInsideDashboardMacro.

@Test
public void executeWhenNotInsideDashboardMacro() throws Exception {
    BeanManager beanManager = this.mocker.getInstance(BeanManager.class);
    BeanDescriptor descriptor = mock(BeanDescriptor.class);
    when(beanManager.getBeanDescriptor(any())).thenReturn(descriptor);
    when(descriptor.getProperties()).thenReturn(Collections.emptyList());
    DashboardRenderer renderer = this.mocker.registerMockComponent(DashboardRenderer.class, "columns");
    GadgetRenderer gadgetRenderer = this.mocker.registerMockComponent(GadgetRenderer.class);
    Execution execution = this.mocker.getInstance(Execution.class);
    ExecutionContext ec = new ExecutionContext();
    when(execution.getContext()).thenReturn(ec);
    DashboardMacroParameters parameters = new DashboardMacroParameters();
    MacroTransformationContext macroContext = new MacroTransformationContext();
    this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
    // We verify that the counter ends up at 0 so that calls to subsequent dashboard macros can succeed.
    assertEquals(0, ec.getProperty("dashboardMacroCalls"));
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) BeanDescriptor(org.xwiki.properties.BeanDescriptor) DashboardRenderer(org.xwiki.rendering.macro.dashboard.DashboardRenderer) GadgetRenderer(org.xwiki.rendering.macro.dashboard.GadgetRenderer) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) DashboardMacroParameters(org.xwiki.rendering.macro.dashboard.DashboardMacroParameters) BeanManager(org.xwiki.properties.BeanManager) Test(org.junit.Test)

Aggregations

DashboardRenderer (org.xwiki.rendering.macro.dashboard.DashboardRenderer)2 GadgetRenderer (org.xwiki.rendering.macro.dashboard.GadgetRenderer)2 Test (org.junit.Test)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 Execution (org.xwiki.context.Execution)1 ExecutionContext (org.xwiki.context.ExecutionContext)1 BeanDescriptor (org.xwiki.properties.BeanDescriptor)1 BeanManager (org.xwiki.properties.BeanManager)1 Block (org.xwiki.rendering.block.Block)1 GroupBlock (org.xwiki.rendering.block.GroupBlock)1 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)1 DashboardMacroParameters (org.xwiki.rendering.macro.dashboard.DashboardMacroParameters)1 Gadget (org.xwiki.rendering.macro.dashboard.Gadget)1 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)1