use of org.xwiki.rendering.macro.dashboard.DashboardRenderer 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);
}
use of org.xwiki.rendering.macro.dashboard.DashboardRenderer 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"));
}
Aggregations