aspose file tools*
The moose likes Other Open Source Projects and the fly likes JasperReport sureport inside subreport Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "JasperReport sureport inside subreport" Watch "JasperReport sureport inside subreport" New topic
Author

JasperReport sureport inside subreport

Cow Yeung
Greenhorn

Joined: Oct 07, 2008
Posts: 16

I would like to ask if jasperReport is support subreport inside subreport?
My situation is 3 level of report.
Main report is a paper, second level is a list of questions, the third level is a list of answers.
Therefore, each question have a list of answers.

In Main report + the subreport of question, I can use JRBeanCollectionDataSource for passing the list of question.
However, I don't know how to merge the subreport of question with the subreport of answer.


SCJP 5 72%
OCEJPA 6 86%
OCEEJB 6 91%
Vinod K Singh
Ranch Hand

Joined: Sep 30, 2008
Posts: 198
Yes, it is possible. You can pass the DataSource from sub-report to further sub-report in same way as you do from main report to sub-report.


My Blog
Cow Yeung
Greenhorn

Joined: Oct 07, 2008
Posts: 16

However, I don't know how to do that. Because of the list of answer is inside the list of question. The structure just like the following code:

// Paper
public class Paper{

public String name;

public List<Question> questions;

}

// Question
public class Question{

public String question;

public List<Answer> answers;

}

// Answer
public class Answer{

public String answer;
}

// Generate the pdf of Paper + Question subReport

InputStream mainInStream = getClass().getResourceAsStream("../paper.jasper");
InputStream questionInStream = getClass().getResourceAsStream("../question.jasper");


JasperReport jRpt = (JasperReport) JRLoader.loadObject(mainInStream);
JasperReport question= (JasperReport)JRLoader.loadObject(questionInStream);

JRBeanCollectionDataSource questionDS = new JRBeanCollectionDataSource(this.paperBean.paperQuestions);

Map<String, Object> main = new HashMap();
main.put("name", this.paper.name);
main.put("subReport", question);
main.put("questionDatasource", questionDS);

JasperPrint jPrint = JasperFillManager.fillReport(jRpt, main, new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jPrint, "D:/test.pdf");

So I don't know how to put the list of answer to be the datasource inside the datasource of questionDS.
Vinod K Singh
Ranch Hand

Joined: Sep 30, 2008
Posts: 198
You can pass JRDataSources as collection e.g. Map<QuestionID, JRDataSource> and inside the report you can pass the relevant datasource to the sub-report as you must have access to QuestionID there.
Cow Yeung
Greenhorn

Joined: Oct 07, 2008
Posts: 16

Sorry, I am not so understand about it.

Do you mean that I pass the map of JRBeanCollectionDatasource of answer to the main report?
Vinod K Singh
Ranch Hand

Joined: Sep 30, 2008
Posts: 198
Yes, and there should be some common ID for matching questions and answers, based on that you can get correct values and display them in report.
Cow Yeung
Greenhorn

Joined: Oct 07, 2008
Posts: 16

Ok Thz~

However, I am not so sure that how can it get datasource from map by the id.
Cow Yeung
Greenhorn

Joined: Oct 07, 2008
Posts: 16

Using the above example, I should do like the below?

// Generate the pdf of Paper + Question subReport

InputStream mainInStream = getClass().getResourceAsStream("../paper.jasper");
InputStream questionInStream = getClass().getResourceAsStream("../question.jasper");


JasperReport jRpt = (JasperReport) JRLoader.loadObject(mainInStream);
JasperReport question= (JasperReport)JRLoader.loadObject(questionInStream);

JRBeanCollectionDataSource questionDS = new JRBeanCollectionDataSource(this.paperBean.paperQuestions);

Map answerMap = new HashMap();
for (Question question:this.paperBean.paperQuestions){
answer.put(question.id, new JRBeanCollectionDataSource(question.Answers));
}


Map<String, Object> main = new HashMap();
main.put("name", this.paper.name);
main.put("subReport", question);
main.put("questionDatasource", questionDS);
main.put("answerMap", answerMap );


JasperPrint jPrint = JasperFillManager.fillReport(jRpt, main, new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jPrint, "D:/test.pdf");


Moreover, how can I get the datasource of answer from map in the Report?
Vinod K Singh
Ranch Hand

Joined: Sep 30, 2008
Posts: 198


Using <parametersMapExpression> and <subreportParameter> you can pass whatever you want.
Cow Yeung
Greenhorn

Joined: Oct 07, 2008
Posts: 16

If it is the code in the main report or question report?
Vinod K Singh
Ranch Hand

Joined: Sep 30, 2008
Posts: 198
Main report. In sub-report in your field expression you will do something like-
$P{QUESTION_MAP}.get( key)
Cow Yeung
Greenhorn

Joined: Oct 07, 2008
Posts: 16

In fact, in my code, I pass value for the first subReport "Question" by questionDatasource which is a JRBeanCollectionDataSource. The key is from that qestionDatasource. How can I get value from the JRBeanCollectionDataSource? If not, I can't get the value of answer by the key.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JasperReport sureport inside subreport