Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass ArrayList to JasperReports?

I'm new to JasperReports. I want to pass ArrayList to subreport of subreport.

I have master report which contains one subreport1 and this subreport1 encapsulate one
subreport2 inside.

Now how to pass arrayList to subreport2?

Like MasterReport -> SubReport -> SubReport

Note: I am using iReport to create jasper template and passing datalist to jasper from my DAO java class.

Below is my Form bean Class

public class CollatReportData extends BaseItem {

    private List<CusipData> listCusipData = null;
    private String dealerID = null;
    private String tripID = null;
    private String loanNo = null;
    private String dealerName = null;
    private String tripDealerLoan = null;

    public CollatReportData() {
        super();
    }

    public List<CusipData> getListCusipData() {
        return listCusipData;
    }

    public void setListCusipData(List<CusipData> listCusipData) {
        this.listCusipData = listCusipData;
    }

    public String getDealerID() {
        return dealerID;
    }

    public void setDealerID(String dealerID) {
        this.dealerID = dealerID;
    }

    public String getTripID() {
        return tripID;
    }

    public void setTripID(String tripID) {
        this.tripID = tripID;
    }
} // and so on for other variables..

My DAO java class is below

public List<Object> getCollatData(String custName, String ctripid, 
        String dealerid, String userID) {

    final Connection conn = super.getCurrentConnection();
    String sqlQueryTrip = null;
    ResultSet rsCollat = null;
    String tripID = null;
    String dealerID = null;
    String cusSysID = null;
    String loanNo = null;
    String txnNo = null;
    String cusipNo = null;
    String cusipStatus = null;
    String parVal = null;
    String tripDealerLoan = null;
    String OldtripDealerLoan = "";
    String NewtripDealerLoan = "";

    CollatReportData reportData = null;
    CusipData cusipData = null;
    List listCusip = new ArrayList<Object>();
    List CollatList = new ArrayList<Object>();
    try {
        PreparedStatement pstmtTrip;
        sqlQueryTrip = "SELECT iscl_sys_id, iscl_trip_id, iscl_trip_dealer_id, "
                + "iscl_cus_sys_id, iscl_bankref_cd, iscl_bdas_db_cd, "
                + "iscl_loan_no, iscl_txn_no, iscl_cusip_no, iscl_status_cd, "
                + "iscl_sec_de_tx, iscl_par_val_am, iscl_market_val_am, "
                + "scl_pri_source_cd, iscl_colla_mar_val_am, iscl_mature_dt, "
                + "iscl_sec_ty, iscl_sec_rt, iscl_sec_price_am, iscl_sec_factor, "
                + "iscl_sec_margin, iscl_sec_accrued_am "
                + "FROM BDS_DBA.INVCONF_SHELL_COLLAT "
                + "WHERE iscl_cus_sys_id='" + custName + "'";
        pstmtTrip = conn.prepareStatement(sqlQueryTrip);
        rsCollat = pstmtTrip.executeQuery();
        if (rsCollat != null) {
            while (rsCollat.next()) {
                tripID = rsCollat.getString("iscl_trip_id");
                dealerID = rsCollat.getString("iscl_trip_dealer_id");
                loanNo = rsCollat.getString("iscl_loan_no");

                tripDealerLoan = tripID + dealerID + loanNo;
                cusipData = new CusipData();
                cusipData.setTripID(tripID);
                cusipData.setCusipNo(cusipNo);
                cusipData.setTripDealerLoan(tripDealerLoan);

                listCusip.add(cusipData);
            } // end rsCollat
        } // end if
        CusipData cusipData1 = new CusipData();
        List CusipList = new ArrayList<Object>();

        for (int io = 0; io < listCusip.size(); io++) {
            cusipData1 = (CusipData) listCusip.get(io);
            NewtripDealerLoan = cusipData1.getTripDealerLoan();
            tripID = cusipData1.getTripID();
            dealerID = cusipData1.getDealerID();
            loanNo = cusipData1.getLoanNo();

            if (NewtripDealerLoan.equalsIgnoreCase(OldtripDealerLoan) || OldtripDealerLoan.equalsIgnoreCase("")) {
                CusipList.add(cusipData1);

            }
            if (!NewtripDealerLoan.equalsIgnoreCase(OldtripDealerLoan)) {
                reportData = new CollatReportData();
                reportData.setTripID(tripID);
                reportData.setTripDealerLoan(NewtripDealerLoan);
                reportData.setListCusipData(CusipList);
                //and so on
                //........
                CollatList.add(reportData);

                CusipList = null;
            }
            OldtripDealerLoan = NewtripDealerLoan;
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("Inside LoanDetailDAO:strTripQuery:Exception" + sqlQueryTrip + e);
    }

    return CollatList;
}

the above DAO class returns List and passing this Arraylist to Jasper template through Hashmap Param.

Below is my sample Master jasper template, from here i am passing ArrayList to subReport like

     <parameter name="list" isForPrompting="false" class="java.util.List"/>

     <detail>
        <band height="100"  isSplitAllowed="true" >
            <subreport  isUsingCache="true">
                <reportElement
                    x="30"
                    y="20"
                    width="170"
                    height="40"
                    key="subreport-1"/>
                <dataSourceExpression><![CDATA[new JRBeanCollectionDataSource( $P{list})]]></dataSourceExpression>
                <subreportExpression  class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + "Collateral_SubReport1.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>

where $p{list} is ArrayList. and use this list to print in subReport1 asusual

while running the code, i am getting the below error:

Error retrieving field value from bean : tripID

Thanks for your help in advance.

like image 580
Manu Avatar asked Sep 02 '10 07:09

Manu


2 Answers

It's done by passing a collection data-source: new BeanCollectionDataSource(yourArrayList);

And then to obtain the JasperPrint object:

JasperPrint jasperPrint = 
      JasperFillManager.fillReport(jasperReport, params, dataSource);

To pass to a subreport, you have two options:

  • If it is a subreport-per-row, simply have the array as a property of the bean. I.e. List<Something> where Something has a property of type List<AnotherThing>
  • If it is one for the whole report, pass it as a parameter (params above).
like image 196
Bozho Avatar answered Nov 12 '22 07:11

Bozho


Step:1 define a field like
field name="listCusipData" and class="java.util.ArrayList"

Step:2 use this expression inside your sub report dataSourceExpression
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{listCusipData},false)

like image 34
Monu Agrawal Avatar answered Nov 12 '22 08:11

Monu Agrawal