Multiple Object in DataSource Collection

View: New views
4 Messages — Rating Filter:   Alert me  

Multiple Object in DataSource Collection

by harishsyndrome :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello All,

I need to use two objects for generating reports with JRXML

dataCollection.add(project);
dataCollection.add(coverage)
I use

JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(dataCollection);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds);

this works fine when i use only one object.
Say Project.

If i use two objects. I get error in the JRXML (As field not found)

How to solve this problem. I need to get values from both the objects

Some help will be declared great

Re: Multiple Object in DataSource Collection

by harishsyndrome :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Without knowing much about your code or system I would say you need a subreport to use 2 datasources. However if the data you need is in 2 separate databases then why not use a join sql statement?
The Query is

session.createQuery(from jasperReport.Project P,jasperReport.Coverage C where P.projectId = 1 and C.coverageId = 1)

Corresponding SQL Query:select project0_.PROJECT_ID as PROJECT1_1_0_, coverage1_.COVERAGE_ID as COVERAGE1_0_1_, project0_.PROJECT_NAME as PROJECT2_1_0_, coverage1_.COVERAGE_NAME as COVERAGE2_0_1_ from PROJECT project0_, COVERAGE coverage1_ where project0_.PROJECT_ID=1 and coverage1_.COVERAGE_ID=1

This  returns two objects. Project and Coverage.
Project Attributes:
projectId
projectName
Coverage Attributes:
coverageId
coverageName
projectFk(Foreign key - project.projectId)

Now in the JRXML

<field name = "projectId" ...> is accepted
<field name = "coverageId"...> is showing error as undefined property

Is there any wat to call the attributes separately like
project.ProjectId
coverage.coverageId??

Re: Multiple Object in DataSource Collection

by harishsyndrome :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

private JRBeanCollectionDataSource dataSource;
private Map<String, Object> parameters = new HashMap<String, Object>();
Collection<ReportBean> BeanList = createBeans();
dataSource = new JRBeanCollectionDataSource(BeanList);
 
       //use parameters if the value doesn't change i.e.customer name,account number,etc.
        parameters.put("PROJECT_ID", project.projectID);
 
 Collection<ReportBeans> createBeans(resultSet){
        List<ReportBeanList> result = new ArrayList<ReportBeanList>();
        foreach (value in the resultSet ) {
              ResultsReportBean Bean = new ResultsReportBean();
              Bean.setType("Project"); //i.e. prjoect or coverage
              Bean.setId(Id);
              etc, etc, etc
        }    
 
result.addAll(Bean);
 
JasperReport jasperReport = JasperCompileManager.compileReport(templateFilename);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters , dataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, reportFilename);
Thank you very much for your reply.
I dont expect spoon feeding. I try to kick start with the piece of code that you provided.

Re: Multiple Object in DataSource Collection

by harishsyndrome :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Novice requires some more clarifications.

Having Chris's Idea running in parallel. I would like put my question in a different way.

In JRXML

<field name = "projectId" class = "java.lang.Integer">
<field name = "coverageId" class = "java.lang.Integer">

Is there any way that i can mention
projectId belongs to the class Project
coverageId belongs to the class Coverage

??