HOW-TO Create Custom Expressions

Custom Expressions is the way we provide to let the developer make complex arranging of the data to be displayed in the report such as a concatenation of 2 or more fields from the data source, a math calculation, etc. Custom Expressions classes must implement the ar.com.fdvs.dj.domain.CustomExpression interface, which has a single method: evaluate(Object object) , the parameter is a java.util.Map which contains the elements of the current row.

Custom Expression may be defined in-line (they are like closures) Lets see an example.

AbstractColumn columnaCustomExpression = ColumnBuilder.getNew()
.setCustomExpression(
                return new CustomExpression() {

                        public Object evaluate(Map fields, Map variables, Map parameters) {
                                String state = (String) fields.get("state");
                                String branch = (String) fields.get("branch");
                                String productLine = (String) fields.get("productLine");
                                Integer count = (Integer) variables.get("REPORT_COUNT");
                                return count + ": " +state.toUpperCase() + " / " + branch.toUpperCase() + " / " + productLine;
                        }

                        public String getClassName() {
                                return String.class.getName();
                        }

                }
).build();

As you can see, the keys of the map are the properties defined at the moment of the creation of the columns.

If we need to use fields from the data source that are not directly shown on any column, we must register them like this:

drb.addField("productLine", String.class.getName());
drb.addField("branch", String.class.getName());

The resulting report would be something like this

custom-expression

Out of the box CustomExpression

DJ already provides these implementations for common usages. They are in the package ar.com.fdvs.dj.util.customexpression

RecordsInPageCustomExpression returns the record number for the current page
RecordsInReportCustomExpression returns the record number for the whole report
PageNumberCustomExpression returns the page number
Fork me on GitHub