Question

I am currently developing a report using DynamicJasper. This is the first time I'm going to use this. I'm following several tutorial but I can't get my head around this problem. I have a column state which is grouped. This column can only have two values - state1 and state2. Basically, what I'm expecting is something like

state1
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode

state2
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode
branch    productline    columnItem    columnCode

But currently, what I'm having is

state1
branch    productline    columnItem    columnCode

state2
branch    productline    columnItem    columnCode

state1
branch    productline    columnItem    columnCode

state2
branch    productline    columnItem    columnCode

...

Its like they are never grouped at all.

Here is my runnable code :

GroupTest.java

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;
import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
import ar.com.fdvs.dj.domain.builders.GroupBuilder;
import ar.com.fdvs.dj.domain.constants.GroupLayout;
import ar.com.fdvs.dj.domain.constants.VerticalAlign;
import ar.com.fdvs.dj.domain.entities.DJGroup;
import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;

public class GroupTest{

    public DynamicReport buildReport() throws Exception {

        DynamicReportBuilder drb = new DynamicReportBuilder();
        Integer margin = new Integer(20);

        drb.setTitle("November 2006 sales report")
            .setSubtitle("This is a sample report")
            .setDetailHeight(new Integer(15)).setLeftMargin(margin)
            .setRightMargin(margin).setTopMargin(margin).setBottomMargin(margin)
            .setPrintBackgroundOnOddRows(true);


        AbstractColumn columnState = ColumnBuilder.getNew()
                .setColumnProperty("state", String.class.getName())
                .setTitle("State")
                .setWidth(new Integer(85))
                .build();

        AbstractColumn columnBranch = ColumnBuilder.getNew()
                .setColumnProperty("branch", String.class.getName())
                .setTitle("Branch")
                .setWidth(new Integer(85))
                .build();

        AbstractColumn columnaProductLine = ColumnBuilder.getNew()
                .setColumnProperty("productLine", String.class.getName())
                .setTitle("Product Line")
                .setWidth(new Integer(85))
                .build();

        AbstractColumn columnaItem = ColumnBuilder.getNew()
                .setColumnProperty("item", String.class.getName()).setTitle(
                        "Item").setWidth(new Integer(85))
                .build();

        AbstractColumn columnCode = ColumnBuilder.getNew()
                .setColumnProperty("id", Long.class.getName())
                .setTitle("ID")
                .setWidth(new Integer(40))
                .build();

        AbstractColumn columnaQuantity = ColumnBuilder.getNew()
                .setColumnProperty("quantity", Long.class.getName())
                .setTitle("Quantity")
                .setWidth(new Integer(25))
                .build();

        AbstractColumn columnAmount = ColumnBuilder.getNew()
                .setColumnProperty("amount", Float.class.getName())
                .setTitle("Amount")
                .setWidth(new Integer(100))
                .setPattern("$ 0.00")
                .build();

        Style groupLabelStyle = new Style("groupLabel");
        groupLabelStyle.setVerticalAlign(VerticalAlign.BOTTOM);

        GroupBuilder gb1 = new GroupBuilder();
        DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState)
                        .setGroupLayout(GroupLayout.VALUE_IN_HEADER)
                        .build();

        drb.addColumn(columnState);
        drb.addColumn(columnBranch);
        drb.addColumn(columnaProductLine);
        drb.addColumn(columnaItem);
        drb.addColumn(columnCode);
        drb.addColumn(columnaQuantity);
        drb.addColumn(columnAmount);

        drb.addGroup(g1); 

        drb.setUseFullPageWidth(true);

        DynamicReport dr = drb.build();

        return dr;
    }

    public static void main(String[] args) throws Exception {
        GroupTest test = new GroupTest();                                   
        DynamicReport dr = test.buildReport();                                  
        JRDataSource ds = new JRBeanCollectionDataSource(TheBean.getDummyBeans());      
        JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);   
        JasperViewer.viewReport(jp);    
    }

}

TheBean.java

import java.util.ArrayList;
import java.util.List;

public class TheBean{

    private String state;
    private String branch;
    private String productLine;
    private String item;
    private Long id;
    private Long quantity;
    private Float amount;

    public TheBean(String state, String branch, String productLine,
            String item, Long id, Long quantity, Float amount) {
        this.state = state;
        this.branch = branch;
        this.productLine = productLine;
        this.item = item;
        this.id = id;
        this.quantity = quantity;
        this.amount = amount;
    }

    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getBranch() {
        return branch;
    }
    public void setBranch(String branch) {
        this.branch = branch;
    }
    public String getProductLine() {
        return productLine;
    }
    public void setProductLine(String productLine) {
        this.productLine = productLine;
    }
    public String getItem() {
        return item;
    }
    public void setItem(String item) {
        this.item = item;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Long getQuantity() {
        return quantity;
    }
    public void setQuantity(Long quantity) {
        this.quantity = quantity;
    }
    public Float getAmount() {
        return amount;
    }
    public void setAmount(Float amount) {
        this.amount = amount;
    }
    public static List getDummyBeans(){
        List theBeans = new ArrayList();

        for(int i = 0; i < 10; i++){
            String s = "";
            if(i % 2 == 0)
                s = "state1";
            else
                s = "state2";
            TheBean bean = new TheBean(s, "branch", "productLine", "item", new Long(10L), new Long(10L), new Float(10));
            theBeans.add(bean);
        }
        return theBeans;
    }
}
Was it helpful?

Solution

I think that you forgot to sort data by state field.

Try to sort data and everything will be OK.


You can find more info about grouping in Data Grouping topic.

OTHER TIPS

You are actually creating a list of your bean TheBeanobject that's why it coming like

state1
branch    productline    columnItem    columnCode

state2
branch    productline    columnItem    columnCode

First make a bean containing state and TheBean object like

class TheStateBean {
    private String state;
    private TheBean theBean;

    // Getter and Setter

    //Write your code to populate the list of TheStateBean object
}

Then do whatever you want to do with this list. This list will show your desire output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top