?
Solved

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

Posted on 2006-06-13
13
Medium Priority
?
24,255 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
  • 6
  • 4
11 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 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