use of org.xwiki.rendering.macro.dashboard.Gadget 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.Gadget in project xwiki-platform by xwiki.
the class ColumnsDashboardRenderer method renderGadgets.
@Override
public List<Block> renderGadgets(List<Gadget> gadgets, GadgetRenderer gadgetsRenderer, MacroTransformationContext context) throws MacroExecutionException {
// transform the passed gagdets in a list of column gadgets
List<ColumnGadget> columnGadgets = new ArrayList<ColumnGadget>();
List<Gadget> invalidGadgets = new ArrayList<Gadget>();
for (Gadget gadget : gadgets) {
ColumnGadget cGadget = new ColumnGadget(gadget);
if (cGadget.getColumn() != null && cGadget.getIndex() != null) {
columnGadgets.add(cGadget);
} else {
invalidGadgets.add(gadget);
}
}
// sort the column gadgets by first the column number then by index in column, for all those which have a valid
// position
Collections.sort(columnGadgets, new Comparator<ColumnGadget>() {
public int compare(ColumnGadget g1, ColumnGadget g2) {
return g1.getColumn().equals(g2.getColumn()) ? g1.getIndex() - g2.getIndex() : g1.getColumn() - g2.getColumn();
}
});
// get the maximmum column number in the gadgets list and create that number of columns. This is the column
// number of the last gadget in the ordered list. Default is 1 column, the empty dashboard is made of one empty
// column
int columns = 1;
if (!columnGadgets.isEmpty()) {
// prevent bad configurations to mess up the dashboard layout
int lastGadgetsColumn = columnGadgets.get(columnGadgets.size() - 1).getColumn();
if (lastGadgetsColumn > 1) {
columns = lastGadgetsColumn;
}
}
// create the list of gadget containers
List<Block> gadgetContainers = new ArrayList<Block>();
for (int i = 0; i < columns; i++) {
GroupBlock gContainer = new GroupBlock();
gContainer.setParameter(CLASS, DashboardMacro.GADGET_CONTAINER);
// and generate the ids of the gadget containers, which are column numbers, 1 based
gContainer.setParameter(ID, DashboardMacro.GADGET_CONTAINER_PREFIX + (i + 1));
gadgetContainers.add(gContainer);
}
// render them as columns using the container macro and appropriate parameters
ContainerMacroParameters containerParams = new ContainerMacroParameters();
containerParams.setLayoutStyle("columns");
BlocksContainerMacro containerMacro = new BlocksContainerMacro();
containerMacro.setComponentManager(this.componentManager);
containerMacro.setContent(gadgetContainers);
List<Block> layoutedResult = containerMacro.execute(containerParams, null, context);
for (ColumnGadget gadget : columnGadgets) {
int columnIndex = gadget.getColumn() - 1;
gadgetContainers.get(columnIndex).addChildren(gadgetsRenderer.decorateGadget(gadget));
}
// and return the result
return layoutedResult;
}
use of org.xwiki.rendering.macro.dashboard.Gadget in project xwiki-platform by xwiki.
the class DefaultGadgetSource method prepareGadgets.
/**
* Prepares a list of gadgets from a list of XWiki objects.
*
* @param objects the objects to read the gadgets from
* @param sourceSyntax the syntax of the source of the gadget objects
* @param context the macro transformation context, where the dashboard macro is being executed
* @return the list of gadgets, as read from the xwiki objects
* @throws Exception in case something happens while rendering the content in the objects
*/
private List<Gadget> prepareGadgets(List<BaseObject> objects, Syntax sourceSyntax, MacroTransformationContext context) throws Exception {
List<Gadget> gadgets = new ArrayList<>();
// prepare velocity tools to render title
VelocityContext velocityContext = velocityManager.getVelocityContext();
// Use the Transformation id as the name passed to the Velocity Engine. This name is used internally
// by Velocity as a cache index key for caching macros.
String key = context.getTransformationContext().getId();
if (key == null) {
key = "unknown namespace";
}
VelocityEngine velocityEngine = velocityManager.getVelocityEngine();
for (BaseObject xObject : objects) {
if (xObject == null) {
continue;
}
// get the data about the gadget from the object
// TODO: filter for dashboard name when that field will be in
String title = xObject.getStringValue("title");
String content = xObject.getLargeStringValue("content");
String position = xObject.getStringValue("position");
String id = xObject.getNumber() + "";
// render title with velocity
StringWriter writer = new StringWriter();
// FIXME: the engine has an issue with $ and # as last character. To test and fix if it happens
velocityEngine.evaluate(velocityContext, writer, key, title);
String gadgetTitle = writer.toString();
// parse both the title and content in the syntax of the transformation context
List<Block> titleBlocks = renderGadgetProperty(gadgetTitle, sourceSyntax, xObject.getDocumentReference(), context);
List<Block> contentBlocks = renderGadgetProperty(content, sourceSyntax, xObject.getDocumentReference(), context);
// create a gadget will all these and add the gadget to the container of gadgets
Gadget gadget = new Gadget(id, titleBlocks, contentBlocks, position);
gadget.setTitleSource(title);
gadgets.add(gadget);
}
return gadgets;
}
Aggregations