Sample of an Extension Class |
|
The sample that ships with ReportWeaver contains a extension class which is described in detail here.
The tesschema.rsd file contains the following view:
<view name="Order" class="com.qintsoft.joria.examples.beansapi.Order"
extclass="com.qintsoft.joria.examples.beansapi.OrderExtensions">
<member name="OrderNo" src="orderId"/>
<member name="netValue" src="value"/>
<member name="date" src="date"/>
<member name="calDate" src="calDate"/>
<member name="positions" view="OrderItemArray" src="items"/>
<member name="customer" view="CustomerOrg" src="customer"/>
<member name="boxes">
<extmethod name="getBoxes"/>
</member>
</view>
The class that implements this function is also included:
package com.qintsoft.joria.examples.beansapi;
/**
* This class implements extensions to be used by ReportWeaver when it accesses the
* data classes. It provides an access method that appears as a member of the class Product
* The access methods must be static, and they must have a single parameter, which is the class
* that they are attached to. (Here it is Product)
*/
public class OrderExtensions
{
protected static final int weightUnitsPerBox = 42;
/**
* This adds an extra method to the class view of Order
* The method implements a non-trivial aggregation function that
* computes the required number of boxes for shipment depending
* on the weight.
* In reality such a function would be more complex but here we just want to show the
* principle.
* @param order the Order that we want the reqired number of boxes for
* @return the required number of boxes
*/
public static int getBoxes(Order order)
{
int boxes = 0;
for (int i = 0; i < order.items.length; i++)
{
int itemsPerBox = (int) (weightUnitsPerBox/order.items[i].product.getWeight());
boxes += (order.items[i].units+itemsPerBox-1)/itemsPerBox;
}
return boxes;
}
}
To enable ReportWeaver to use the method some requirements must be observed: