Welcome!

Java IoT Authors: Yeshim Deniz, Pat Romanski, Liz McMillan, Zakia Bouachraoui, Carmen Gonzalez

Related Topics: Java IoT

Java IoT: Blog Feed Post

BIRT Crosstab Scripting

The BIRT crosstab element offers onPrepare, onCreate and onRender hooks as well

BIRT supplies scripting hooks for just about every report element in the palette. You can generally implement an onPrepare, onCreate, and onRender event handler for each of these report items. The onPrepare event fires before data is retrieved and allows you to change the design for a specific report item. The onCreate event fires when the report item is being created by the report engine’s generation task. The onRender event fires when the report item is being rendered by the report engine’s render task. These events and example are described on the BIRT website.

Some report items offer more event hooks. For example a chart item actually has over thirty event hooks that allow total customization of the chart generation and presentation phases. As a side note, all chart scripts fire during the report engine’s render task. This does not mean the database is hit at render time though. The report engine will cache the data for a chart in memory or in a report document, depending on what task type is being used to run and render the report.

The BIRT crosstab element offers onPrepare, onCreate and onRender hooks as well. These events are fired both for the crosstab as a whole and for each individual cell in the crosstab. When firing events for cells, these are processed top to bottom and left to right. Handlers can be written using the script tab at the bottom of the report design view. BIRT also supplies an event adapter to allow you to write these events in Java as well.

As an example script, you can modify the crosstab in the onPrepare like:

function onPrepareCrosstab( crosstab, reportContext )
{
var coldim = crosstab.getColumnLevels().get(0);
coldim.removeAllFilterConditions();
crosstab.getStyle().setFontFamily("Arial");
crosstab.getStyle().setFontSize("8");

}

This script removes filters from the first column level and sets some font information. Some of the more interesting capabilities are available when using the onCreateCell event hook. For example consider the following script.

function onCreateCell( cellInst, reportContext )
{

//Can reference cells by type or id - valid types "header" or "aggregation"
if( cellInst.getCellType() == "header" ) {
//Get data values see binding tab on crosstab
//if( cellInst.getDataValue("PRODUCTLINE") == "Planes" ){
if( reportContext.evaluate("dimension['ProductGroup']['PRODUCTLINE']") == "Planes" ){
cellInst.getStyle().setBackgroundColor("#FF0000");
cellInst.getStyle().setFontSize("12");
}else{
//Set the rest to yellow
cellInst.getStyle().setBackgroundColor("#FFFF00");
cellInst.getStyle().setFontSize("12");
}

}
//refer to crosstab header
if( cellInst.getCellID() == 167){
cellInst.getStyle().setBackgroundColor("Orange");
}
if( cellInst.getCellType() == "aggregation"){
//Can refernce value using getDataValue or
if( cellInst.getDataValue("PRODUCTLINE") == "Planes" ){
//set color to bluegray
cellInst.getStyle().setBackgroundColor("RGB(169,170,226)");
}
//by using reportContext.evaluate
//if( reportContext.evaluate("measure['amount']") > 50000 ){
if( cellInst.getDataValue("amount_DateGroup/quarter_ProductGroup/PRODUCTLINE") > 50000 ){
cellInst.getStyle().setBackgroundColor("Green");
cellInst.getStyle().setColor("White");
}
}
}

The onCreateCell script is passed the reportContext and an instance of the cell. The cellInst object has several methods that can be used to determine which cell is currently being processed. You can call getCellType() which will return header or aggregation. In the following image all cells in the red box will return aggregation and all others will return header.

You can also call getCellID() which will return the specific element id that your crosstab uses for that cell.


In this example the getCellID call will return 160 every time this cell is created. This can be useful when you are trying to determine if you have a new row or column in your crosstab. If this is the innermost header element then this cell will be created for every new row/column. The only drawback to this approach is that if the crosstab is in a library or you copy and paste it, the cell ids will change.

Once you know the cell you are currently processing you generally want to access the data, to make some script decision. To do this you have a couple of choices. You can call the getDataValue method on the cell instance or use the reportContext.evaluate method. When using the getDataValue method, the value you need to refer to is the data binding column name set on the crosstab item. For example:

cellInst.getDataValue("PRODUCTLINE") == "Planes"

refers to the PRODUCTLINE data binding. Also note that this is the value of the column as this particular cell is being processed.

You can also use the reportContext.evaluate method, which allows you to build an expression and bypass the binding. For example:
if( reportContext.evaluate("dimension['ProductGroup']['PRODUCTLINE']") == "Planes" )

Essentially returns the same value as the previous getDataValue(“PRODUCTLINE”) method. This is because the PRODUCTLINE data binding has:

dimension['ProductGroup']['PRODUCTLINE']

As it’s expression. This example references a particular dimension of the cube. You can reference cube measures as well.

reportContext.evaluate("measure['amount']")

As with the column binding this will return the value for the specific cell you are currently processing.

In the script posted at the top of this post, we use the cell instance object to set specific styling based on the values of various column bindings. This is done using the getStyle method.

cellInst.getStyle().setBackgroundColor("Green");
cellInst.getStyle().setColor("White");

Using this method is no different than other BIRT report items.

Using these methods and the reportContext you can write some very sophisticated scripts. For example you can store all the quantities ordered where the product line is ships to a global variable using a script similar to this:

if( cellInst.getCellType() == "aggregation" && cellInst.getCellID() == 151){
if( cellInst.getDataValue("PRODUCTLINE") == "Ships" ){
importPackage(Packages.java.lang );
var cur = cellInst.getDataValue("amount_DateGroup/quarter_ProductGroup/PRODUCTLINE");
if( reportContext.getGlobalVariable("totalplanes")==null){
reportContext.setGlobalVariable("totalplanes", new Double(Double.parseDouble(cur)) );
}else{
var oldcnt = reportContext.getGlobalVariable("totalplanes");
reportContext.setGlobalVariable("totalplanes", new Double(Double.parseDouble(oldcnt) + Double.parseDouble(cur)));
}
}
}


This value can then be referenced within the same crosstab or later in the report. For example the following expression for a text item can be used.

<VALUE-OF format="#,##0.00">reportContext.getGlobalVariable("totalplanes");</VALUE-OF>

This example can also be implemented in Java. In the attached link the report will contain two crosstabs. One where the code is done in JavaScript and the other uses an event handler written in Java, which is also attached.

The example report output is presented below.



The examples are available at Birt-Exchange.

More Stories By Jason Weathersby

Jason Weathersby is a member of the extended BIRT development team at Actuate Corporation and has backgrounds in both computer science and technical writing. He has many years experience in technical consulting, training, writing, and publishing about reporting, business intelligence tools, and database technologies.

IoT & Smart Cities Stories
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.
Digital Transformation is much more than a buzzword. The radical shift to digital mechanisms for almost every process is evident across all industries and verticals. This is often especially true in financial services, where the legacy environment is many times unable to keep up with the rapidly shifting demands of the consumer. The constant pressure to provide complete, omnichannel delivery of customer-facing solutions to meet both regulatory and customer demands is putting enormous pressure on...
IoT is rapidly becoming mainstream as more and more investments are made into the platforms and technology. As this movement continues to expand and gain momentum it creates a massive wall of noise that can be difficult to sift through. Unfortunately, this inevitably makes IoT less approachable for people to get started with and can hamper efforts to integrate this key technology into your own portfolio. There are so many connected products already in place today with many hundreds more on the h...
The standardization of container runtimes and images has sparked the creation of an almost overwhelming number of new open source projects that build on and otherwise work with these specifications. Of course, there's Kubernetes, which orchestrates and manages collections of containers. It was one of the first and best-known examples of projects that make containers truly useful for production use. However, more recently, the container ecosystem has truly exploded. A service mesh like Istio addr...
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As automation and artificial intelligence (AI) power solution development and delivery, many businesses need to build backend cloud capabilities. Well-poised organizations, marketing smart devices with AI and BlockChain capabilities prepare to refine compliance and regulatory capabilities in 2018. Volumes of health, financial, technical and privacy data, along with tightening compliance requirements by...
Charles Araujo is an industry analyst, internationally recognized authority on the Digital Enterprise and author of The Quantum Age of IT: Why Everything You Know About IT is About to Change. As Principal Analyst with Intellyx, he writes, speaks and advises organizations on how to navigate through this time of disruption. He is also the founder of The Institute for Digital Transformation and a sought after keynote speaker. He has been a regular contributor to both InformationWeek and CIO Insight...
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
To Really Work for Enterprises, MultiCloud Adoption Requires Far Better and Inclusive Cloud Monitoring and Cost Management … But How? Overwhelmingly, even as enterprises have adopted cloud computing and are expanding to multi-cloud computing, IT leaders remain concerned about how to monitor, manage and control costs across hybrid and multi-cloud deployments. It’s clear that traditional IT monitoring and management approaches, designed after all for on-premises data centers, are falling short in ...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...