MicroStrategy ONE
Add-on Creation
This example demonstrates the creation of a simple add-on that supports a single input parameter passed from the Page Configuration file. The add-on performs limited useful tasks, but it demonstrates the structure of the class and shows how to configure the Page Configuration file to use the add-on.
The following steps are involved:
- Implementing the add-ons interface
- Passing a parameter from the Page Configuration file
- Registering the add-on in the Page Configuration file
- Analyzing what the PageComponent instance represents
Implementing the add-ons interface
To implement an addon, the class needs to implement the add-ons interface. As described in the Add-on Interface section, this interface has three methods— getAddOnDescription(), preCollectData(PageComponent pg) and postCollectData(PageComponent pg).
The getAddOnDescription() method is used to return the description of the class.
public String getAddOnDescription() {
return "Add-on example that demonstrates the creation and registering process for an add-on";
}
The two methods, preCollectData(PageComponent pg) and postCollectData(PageComponent pg), perform the real work for the add-on. When an add-on is registered in a particular page, it is instantiated and called as a part of the data collection process of the page. It is called just prior to the collection of data from Intelligence Server by the beans on the page. An instance of a com.microstrategy.web.app.beans.PageComponent class that represents one of the components on the page, such as a bean, is passed to an add-on. You can check what kind of PageComponent has been passed to the add-on in the analyzing what the PageComponent instance represents section.
In this example, some information is sent to the Java console to demonstrate that the add-on executed correctly.
Code Sample
public voidpreCollectData(PageComponent pg) {
PrintStream output=System.out;
output.println("Page component Information: ");
output.println("\t\tName: "+pg.getName());
output.println("\t\tClass: "+pg.getClass().getName());
output.println("Addon information: ");
output.println("\t\tDescription:"+this.getAddOnDescription());
output.println("\t\tParameter value: "+addonParamValue);
}
Passing a parameter from the Page Configuration file
In the previous code sample, the last line prints the value of a data member addonParamValue. This is a parameter that is passed from the Page Configuration file. To do this, create a property in the add-on having the same name as the parameter that is used in the Page Configuration file. Thus, if you want to have a parameter in the Page Configuration file called AddonParam, then include the following methods in the class:
public void setAddonParam(String value) {
addonParamValue=value;
}
public String getAddonParam() {
return addonParamValue;
}
The above technique automatically makes the parameter available in the Page Configuration file.
Registering the add-on in the Page Configuration file
To actually use this add-on class that was created, you need to perform a number of configuration steps. The first step is to compile the class and insert it into the class path of the Web application. The second step is to add a line in the Page Configuration file to specify a page where the add-on will be used by the application. This is accomplished by adding an additional <addon> node within the desired <page> node, as shown by the code in bold in the code sample below.
<page desc="Report Execution"... ...>
... ...
<addons>
<addon name="com.microstrategy.web.app.addons.ReportSetFlagsAddOn"/>
<addon name="com.microstrategy.sdk.samples.addons.SimpleAddon">
<properties>
<property name="AddonParam" source="const" type="string" value="This string is passed to the addon"/>
</properties>
</addon>
</addons>
... ...
</page>
An add-on was already configured for the Report Execution page so the new one was added below the existing one.
Analyzing what the PageComponent instance represents
In many cases, the add-on acts on a particular Web Component type such as a specific bean. The PageComponent instance represents a number of different object types in the application. The following code demonstrates a way to identify the type of the component. In addition, the component may have a number of child components and so you can also iterate through the children to find a component of interest.
Code Sample
private static ReportBean getReportBean(WebComponent wc) {
ReportBean result = null;
//if the current Web component is a ReportBean then return it
if (wc instanceof ReportBean) {
result = (ReportBean) wc;
//otherwise, try to find a report bean amongst the children
} else {
for (int i = 0; i < wc.getChildCount(); i++) {
//search recursively through all children
result = getReportBean(wc.getChild(i));
if (result != null) break;
}
}
return result;
}