views:

53

answers:

1

Hi Experts,

i new to jasper reports,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

post some example.

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

------------------------edited--

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[Returning List]

    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,iscl_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.

+3  A: 

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 suberport-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).
Bozho
yes i know it. but how to pass it. can you provide me some sample example code briefly.i am able to pass arrayList to first subreport but not able to pass to supreport2 from subreport1(master-sureport1-subreport2).subreport2 is inside subreport1
Manu
well, you show some code first - how are you obtaining your report in Java code.
Bozho
@Bozho.. my coding is huge, so i couldn't post it here.sorry for that. could you please post some sample code which is used to pass Arraylist to subreport of subreport.
Manu
@Bozho.. see my edited detail section information
Manu
well, it doesn't make it more clear. Perhaps you should identify the relvent pieces. Anyway, I added one line which I'm using to pass the data source to the report and get the result.
Bozho
the same code using in my code also, but the thing is not able to pass the list to nested subreport.Let me explain here simply.1) i am having master report - master.jrxml2) subreport - subreport1.jrxml3) subreport - subreport2.jrxml-The master.jrxml contains subreport1.jrxml, subreport1.jrxml contains subreport2.jrxml,as per the above code you posted i am able to pass arrayList to subreport1.jrxml but how to pass the same or different arrayList to subreport2.jrxml
Manu
I have used "JRBeanCollectionDataSource" inside subreport1.jrxml to pass list to subreport2.jrxml, here its throwing error.Liek-Error retrieving field value from bean : subList(subList is arraylist )
Manu
@Bozho....if you dont mind could you please post some code like how to pass list to the 2nd subreport.
Manu
see updated....
Bozho
@Bozho.. if i want to pass ArrayList parameter to subreport.. what need to be done? please help me.
Manu
which part you didn't understand?
Bozho
i am passing arraylist in "params"(hasmap paramter) to master report and in master report i am passin the arralylist by converting to "JRBeanCollectionDataSource" to subreport. but now it throwing error like below.. Error retrieving field value from bean : tripID.. where trip id is deaclared variable in formbean and having Setter(),Getter() methods
Manu
Show that pieces of the code (add them to your question)
Bozho
@Bozho.. See my edited part
Manu