?
Solved

How to use custom data sources (JavaBeans) for Jasper subreports via iReport?

Posted on 2006-06-13
13
Medium Priority
?
24,179 Views
Last Modified: 2011-08-18
hi
i am using two custom data sources (java bean datasources).
one is masterreport and and other is subreport when i am compiling them and preview them seperatly they working fine

but when i added a subreport tag to masterreport and include "subreport.jasper" sub report data is not displaying


( but in case database data source there is no problem with subreports)

Is there any way to include subreports using java bean id custom datasource ?
0
Comment
Question by:feliciayong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
13 Comments
 
LVL 2

Expert Comment

by:leogc
ID: 16895959
In the subreport icon that u have included in the main report pass the bean name (including the package name)as the parameter to the subreport and in the sub report specify the properties of the bean as the fields of the sub report so that the sub report data gets displayed.
0
 
LVL 1

Author Comment

by:feliciayong
ID: 16899092
I'm using iReport 121. Do u mean right click on the subreport element tat's on the main report, & after that I will see 4 tabs namely "Common", "Subreport", "Subreport (Other)", "All", so do u mean I should click on the Subreport tab & modify the Connection/ Datasource Expression? So should I be choosing "Don't use connection or datasource"? On which tab, should I enter the package name of the JavaBean class? Sorry really need more detailed instructions.
0
 
LVL 2

Expert Comment

by:leogc
ID: 16920815
I want more details regarding how u r passing the Javabeans to the jasper report(weather
u r calling the report from servlet or jsp.If so that part of the code).Right now i have
followed these steps to display the main report with MainReportBean  details and subreport
with the Subreport bean details:

1.>I have created a wrapperbean for the mainreport and subreport beans.i.e i have
created the getter and setter for the mainreportbean and subreportbean.I am passing the
wrapperbean to the report.
2.>In the report i am defining two fields for the mainreportbean and subreportbean.
   main report details are got through the properties of the main report.

For getting the subreport details the following steps are followed:
1>In the subreport icon in the subreport tab use datasource expression is selected and
 the subreportbean along with the complete package is passed.
2>In the subeport(other) tab the path of the subreport jasper file is given.Then in the
subreport u can access the fields as the properties of the subreporbean.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 1

Author Comment

by:feliciayong
ID: 16939294
Hi Leogc,

Thanks for the elaboration. Sorry that I have been brief abt my explanation of the problem tat I'm stuck with earlier on.  My current problem is that I'm not sure how to print the subreport details of each master report's record, let's say I want to achieve the following output:

-----------------------------------
name: Garfield, age: 10
  subject: english, marks:90
  subject: maths, marks:80

name: Ernie, age: 12
  subject: english, marks:88
  subject: maths, marks:77
  subject: science, marks:66
-----------------------------------

Read this in the IReport manual:
"A datasource is in general a consumable object that is usable for feeding a report
only once; so a datasource passed as a parameter will satisfy the needs of only one
subreport. So therefore the parameter technique is not suitable when every record of
the master report has got its own subreport of detail (unless that in the master there
is only one record). When we explain the Datasource we will see how this problem
is easily solvable by using custom datasources." ---> Can u show me by an example how this can be solved by custom datasources.

By the way, are u using Javabean Extended datasources?
Can u paste ur codes here for me to reference? It will be good if u can share ur JRXMLs & javabeans & other useful sources with me.

My code is as follows as per request for ur guidance & correction:

===
package com.ubs.swidFFL.v1.server.service.jasper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;


public class JasperTest {

      public static void main (String args[]) {
       
        String fileName = "C:/development/main.jasper";
        HashMap hm = new HashMap();
       
        try {
           
            JRBeanCollectionDataSource mainRptDs = new JRBeanCollectionDataSource(MainRptBeanFactory.generateCollection());
            JRBeanCollectionDataSource subRptDs = new JRBeanCollectionDataSource(SubRptBeanFactory.generateCollection());
            hm.put("subreport_sub_ds", subRptDs);
           
            JasperPrint print = JasperFillManager.fillReport(fileName, hm, mainRptDs);
           
            OutputStream output=new FileOutputStream(new File("c:/testMainSub.pdf"));
            JasperExportManager.exportReportToPdfStream(print, output);
            System.err.println("export has been done, p/s check file at local drive");
           
        } catch (JRException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }        
    }
}

===
public class SubRptBean {
      private String subject;
    private String marks;
   
      /**
       * @return Returns the marks.
       */
      public String getMarks() {
            return marks;
      }
      /**
       * @param marks The marks to set.
       */
      public void setMarks(String marks) {
            this.marks = marks;
      }
      /**
       * @return Returns the subject.
       */
      public String getSubject() {
            return subject;
      }
      /**
       * @param subject The subject to set.
       */
      public void setSubject(String subject) {
            this.subject = subject;
      }
}
===
public class MainRptBean {

    private String name;
    private String age;
   
      /**
       * @return Returns the age.
       */
      public String getAge() {
            return age;
      }
      /**
       * @param age The age to set.
       */
      public void setAge(String age) {
            this.age = age;
      }
      /**
       * @return Returns the name.
       */
      public String getName() {
            return name;
      }
      /**
       * @param name The name to set.
       */
      public void setName(String name) {
            this.name = name;
      }
}
===
For getting the subreport details the following steps are followed:
In the subreport icon in the subreport tab use datasource expression is selected and expression used:
((net.sf.jasperreports.engine.data.JRBeanCollectionDataSource)$P{subreport_sub_ds})

===

0
 
LVL 2

Expert Comment

by:leogc
ID: 16940958
To achieve u r first output u can place the subreport icon in a group band and group by the main report field.
I am using JRBeanArrayDataSource.I am using a wrapperbean where i write the getter and setter for MainRptBean and
SubreportBean in the wrapperBean and passing the wrapperbean to the JasperFillManager.fillReport so that it contains the
data of both the beans which can be retieved in the jasper report.
i am doing something like this:
pseudo code:
for(int i=0;i<bean.length;i++)
{
   wrapperBean=new WrapperBean();
//populate main rpt bean
   MainRptBean mainRptBean=new MainRptBean();
   wrapperBean.setMainRptBean(mainRptBean);
//populate subreport bean
SubRptBean subRptBean=new SubRptBean();
   wrapperBean.setSubRptBean(subRptBean);

col.add(wrapperBean);
}


and then

jasperPrint = JasperFillManager.fillReport(jasperFileName,
                              parameters, new JRBeanArrayDataSource(col
                                          .toArray(new WrapperBean[0])));
0
 
LVL 1

Author Comment

by:feliciayong
ID: 17041467
Hi Leogc,

Thanks for the useful input, think I'm getting closer to the solution.
Sorry for the late response as I hv been tied up lately.

One Question over here:
How is the subreport bean being passed from the main report to the sub report?
Because if it's passed as a datasource, i.e. as u hv described as above:
"1>In the subreport icon in the subreport tab use datasource expression is selected and
the subreportbean along with the complete package is passed.", ClassCastException will be hit during report compilation.
This is because $F{subBean} is a javabean (POJO class), it's not of a datasource type.

I have specified in the subreport icon in the subreport tab as follows

"Use datasource expression selected": $F{subBean} & thus got the ClassCastException.

Regards.
0
 
LVL 2

Expert Comment

by:leogc
ID: 17043914
try like this
new dori.jasper.engine.data.JRBeanArrayDataSource((Object[]) ((com.iss.fasttrack.standard.reports.regular.form.SubRptBean) $F{subRptBean}).getSubRptBean())
0
 
LVL 1

Author Comment

by:feliciayong
ID: 17079829
Hi, I have tried the suggested & hit the following compilation errors:

Attempted:
new net.sf.jasperreports.engine.data.JRBeanArrayDataSource
(
  (Object[])
      ( (com.abc.appEPC.v1.server.service.jasper.SubBean) $F{subBean} )
          .getSubBean()
)

Errors:
net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
C:\development\iReport-1.2.1\mainSubRptTest_1152608548252_391187.java:187: cannot resolve symbol symbol  : method getSubBean ()
location: class com.abc.appEPC.v1.server.service.jasper.SubBean     ( (com.abc.appEPC.v1.server.service.jasper.SubBean) ((com.abc.appEPC.v1.server.service.jasper.SubBean)field_subBean.getValue()) )         ^ C:\development\iReport-1.2.1\mainSubRptTest_1152608548252_391187.java:290: cannot resolve symbol symbol  : method getSubBean ()
location: class com.abc.appEPC.v1.server.service.jasper.SubBean     ( (com.abc.appEPC.v1.server.service.jasper.SubBean) ((com.abc.appEPC.v1.server.service.jasper.SubBean)field_subBean.getOldValue()) )         ^ C:\development\iReport-1.2.1\mainSubRptTest_1152608548252_391187.java:393: cannot resolve symbol symbol  : method getSubBean ()
location: class com.abc.appEPC.v1.server.service.jasper.SubBean     ( (com.abc.appEPC.v1.server.service.jasper.SubBean) ((com.abc.appEPC.v1.server.service.jasper.SubBean)field_subBean.getValue()) )         ^ 3 errors


I hv verified that there's getSubBean () method at my WrapperBean class, considering that Object[] refers to the WrapperBean[]

Regards.
0
 
LVL 2

Accepted Solution

by:
leogc earned 2000 total points
ID: 17092314
u should have subBean defined as a field in the main report.Also check that
u r subbean,wrapperbean and all the relatedbeans are in classpath i.e createthe
jar of the files containing u r beans i.e com.leogc2006.gmail.subBean etc are in the jar and put the jar in the ireport lib and then try to compile the report xml to jasper file.
0
 
LVL 2

Expert Comment

by:leogc
ID: 17302083
hi there is no response from the person who asked question for a long time.can the volunteers help me in getting the points.
0
 

Expert Comment

by:ylpkumar
ID: 21400069
Can u provide the jrxml files for JasperTest class
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question