An addon provides access to a PageComponent instance during page execution, which can be used to access and manipulate a bean on that page. This java snippet demonstrates how a specific bean can be retrieved from the PageComponent, if present, using a recursive loop. The same methodology can be applied for other beans found on the page, returning null if the bean could not be found.
SubscriptionBean:
// Fetch the SubscriptionBean from the page, if present
private SubscriptionBean getSubscriptionBean(WebComponent wc) {
SubscriptionBean result = null;
if (wc instanceof SubscriptionBean) {
System.out.println("wc.getname :"+wc.getName());
result = (SubscriptionBean) wc;
}
else
{
for (int i = 0; i < wc.getChildCount(); i++) {
result = getSubscriptionBean(wc.getChild(i));
if (result != null) break;
}
}
return result;
}
// Fetch the ReportBean from the page, if present
private ReportBean getReportBean(WebComponent wc) {
ReportBean result = null;
if (wc instanceof ReportBean) {
System.out.println("wc.getname :"+wc.getName());
result = (ReportBean) wc;
}
else
{
for (int i = 0; i < wc.getChildCount(); i++) {
result = getReportBean(wc.getChild(i));
if (result != null) break;
}
}
return result;
}
// get the subscription bean from the page and do something with it
public void preCollectData(PageComponent page) {
SubscriptionBean sb;
try {
sb = getSubscriptionBean(page);
if(sb != null){
// do something
System.out.println("Subscription name: "+ sb.getSubscription().getName());
}
} catch (WebBeanException e) {
e.printStackTrace();
} catch (WebObjectsException e) {
e.printStackTrace();
}
}