Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

JasperReport sureport inside subreport

 
Greenhorn
Posts: 16
Eclipse IDE Chrome Java
  • 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.
 
Ranch Hand
Posts: 198
  • 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
Eclipse IDE Chrome Java
  • 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
  • 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
Eclipse IDE Chrome Java
  • 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
  • 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
Eclipse IDE Chrome Java
  • 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
Eclipse IDE Chrome Java
  • 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
  • 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
Eclipse IDE Chrome Java
  • 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
  • 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
Eclipse IDE Chrome Java
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im trying something like ,
javacode:
fillreport(report,params,new EmptyDataSource())
params = {"master",{"name":"abc","addr":"zzz"}} - params.put("master",map);

in mastre jrxml
<subreport>
<parameterMapExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.get("master")]]></parameterMapExpressoin>
<subparameter name="sub">
<subparamexpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.get("master")]]></subparamexpression>
</subparameter>
</subreport>

in subreport
<parameter name="sub" class-"java.util.Map">

<textField>
<textfieldexpression><![CDATA[$P{sub}.get("name")]]></textfieldExpression>
</textfield>


this is not working ; getting empty report .
can someone help me how to populate andfetch in subreport please?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic