Solved

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

Posted on 2006-06-13
13
24,000 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
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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 500 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumDigits  challenge 7 60
Updating Java 9 87
Free Alternative to JIRA 4 58
Java Jpanels and Jframe 8 20
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now