Hi,
I try to get how to pass to my custom Macro some parameters defined when including this macro to my confluence page.
Any help? tutorial to read please?
Below the code of a POC macro from where I try to get parameters but what is missing to define those parameters somewhere? Thank you.
package org.iter.plugins.macropoc.confluence;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import com.atlassian.confluence.content.render.xhtml.ConversionContext;
import com.atlassian.confluence.macro.Macro;
import com.atlassian.confluence.macro.MacroExecutionException;
import com.atlassian.confluence.xhtml.api.XhtmlContent;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.renderer.v2.RenderMode;
import java.util.ArrayList;
import java.util.List;
import com.atlassian.confluence.content.render.xhtml.XhtmlException;
import com.atlassian.confluence.xhtml.api.MacroDefinition;
import com.atlassian.confluence.xhtml.api.MacroDefinitionHandler;
@Scanned
public class MyMacroPoc implements Macro {
private final XhtmlContent xhtmlUtils;
@Autowired
public MyMacroPoc(@ComponentImport XhtmlContent xhtmlUtils)
{
this.xhtmlUtils = xhtmlUtils;
}
@Override
public String execute(Map<String, String> parameters, String bodyContent, ConversionContext conversionContext)
throws MacroExecutionException {
final List<MacroDefinition> macros = new ArrayList<MacroDefinition>();
String body = conversionContext.getEntity().getBodyAsString();
try
{
xhtmlUtils.handleMacroDefinitions(body, conversionContext, new MacroDefinitionHandler()
{
public void handle(MacroDefinition macroDefinition)
{
macros.add(macroDefinition);
}
});
}
catch (XhtmlException e)
{
throw new MacroExecutionException(e);
}
StringBuilder builder = new StringBuilder();
builder.append("<script type='text/javascript'>\r\n");
builder.append("alert(' ws API url in parameter: ");
builder.append(parameters.get("0"));
builder.append("');\r\n");
builder.append("function updateCarsData(){\r\n");
builder.append("var jsonString = '{\"IsSuccess\":true,\"Message\":\"Data retrieved successfully. Timestamp:7 25 2018 3:30:12 PM from ITER B72\",\"Data\":{\"Cars\":[{\"Name\":\"Shuttle car # 1\",\"CurrentStatus\":\"Awaiting\",\"CurrentPosition\":\"In Use\",\"LastPosition\":\"ITER B81\",\"CurrentUser\":\"De Temmerman Gregory\",\"LastUser\":null,\"LastEvent\":\"Date(1532523222000+0200)\",\"BookingNotes\":null,\"LastEventAge\":2252,\"IsBooked\":false},{\"Name\":\"Shuttle car # 3\",\"CurrentStatus\":\"Awaiting\",\"CurrentPosition\":\"In Use\",\"LastPosition\":\"ITER B72\",\"CurrentUser\":\"Parmar Darshankumar\",\"LastUser\":null,\"LastEvent\":\"Date(1532525257000+0200)\",\"BookingNotes\":null,\"LastEventAge\":217,\"IsBooked\":false},{\"Name\":\"Shuttle car # 2\",\"CurrentStatus\":\"Parked\",\"CurrentPosition\":\"ITER B72\",\"LastPosition\":\"ITER B81\",\"CurrentUser\":null,\"LastUser\":\"Appel Gregory\",\"LastEvent\":\"Date(1532525415000+0200)\",\"BookingNotes\":null,\"LastEventAge\":59,\"IsBooked\":false},{\"Name\":\"Shuttle car # 4\",\"CurrentStatus\":\"Parked\",\"CurrentPosition\":\"ITER B81\",\"LastPosition\":\"ITER B72\",\"CurrentUser\":null,\"LastUser\":\"Appel Gregory\",\"LastEvent\":\"Date(1532524266000+0200)\",\"BookingNotes\":null,\"LastEventAge\":1208,\"IsBooked\":false}],\"DataAge\":62,\"DataTimestamp\":\"Date(1532525412000+0200)\",\"NumberOfCars\":4}}'\r\n");
builder.append("alert(' update by pulling data from Shuttle Cars API');\r\n");
builder.append("var carsDOM = $('<div>Shuttle Cars</div>');\r\n");
builder.append("carsDOM.appendTo('#shuttle-cont');\r\n");
builder.append("var carsData = jQuery.parseJSON(jsonString);\r\n");
builder.append("carsData.Data.Cars.forEach( function(element, index, array) {\r\n");
builder.append("var car = element;\r\n");
builder.append("car.Id = element.Name.replace( 'Shuttle car # ', '' );\r\n");
builder.append("car.Position = element.CurrentPosition.replace( 'ITER B', '' );\r\n");
builder.append("car.Status = element.CurrentStatus;\r\n");
builder.append("car.IsBooked = element.IsBooked;\r\n");
builder.append("car.BookingNotes = element.BookingNotes;\r\n");
builder.append("carsDOM.append('<li class=\"shuttle-car\"><h2> ' + car.Id + '</h2><p> Position: ' + car.Position + ' </p><p> Status: ' + car.Status + ' </p><p> IsBooked: ' + car.IsBooked + ' </p><p> Notes: ' + car.BookingNotes + ' </p></li>');\r\n");
builder.append("alert(' car info: ' + car.Id + ', ' + car.Position + ', ' + car.Status + ', ' + car.IsBooked + ', ' + car.BookingNotes);\r\n");
builder.append("} );\r\n");
builder.append("}\r\n");
builder.append("$(document).ready(function(){\r\n");
builder.append("setInterval(function(){updateCarsData();}, 60000)\r\n");
builder.append("});\r\n");
builder.append("</script>\r\n");
builder.append("<div id='shuttle-cont'>\r\n");
builder.append("<p>Macro POC + AJS.ajax</p>\r\n");
builder.append("</div>\r\n");
/*if (!macros.isEmpty())
{
builder.append("<table width=\"50%\">");
builder.append("<tr><th>Macro Name</th><th>Has Body?</th></tr>");
for (MacroDefinition defn : macros)
{
builder.append("<tr>");
builder.append("<td>").append(defn.getName()).append("</td><td>").append(defn.hasBody()).append("</td>");
builder.append("</tr>");
}
builder.append("</table>");
}
else
{
builder.append("You've done built yourself a macro! Nice work.");
}*/
builder.append("</p>");
return builder.toString();
}
public boolean hasBody()
{
return true;
}
public RenderMode getBodyRenderMode()
{
return RenderMode.ALL;
}
@Override
public BodyType getBodyType() {
return BodyType.RICH_TEXT;
}
@Override
public OutputType getOutputType() {
return OutputType.BLOCK;
}
}
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.