• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JasperReport sureport inside subreport

 
Cow Yeung
Greenhorn
Posts: 16
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vinod K Singh
Ranch Hand
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cow Yeung
Greenhorn
Posts: 16
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Thz~

However, I am not so sure that how can it get datasource from map by the id.
 
Cow Yeung
Greenhorn
Posts: 16
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Using <parametersMapExpression> and <subreportParameter> you can pass whatever you want.
 
Cow Yeung
Greenhorn
Posts: 16
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is the code in the main report or question report?
 
Vinod K Singh
Ranch Hand
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Main report. In sub-report in your field expression you will do something like-
$P{QUESTION_MAP}.get( key)
 
Cow Yeung
Greenhorn
Posts: 16
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic