Jump to content

Creating a table structure with assorted java bean structure( one field and a list) as datasource


mohanbelaguli

Recommended Posts

Hi All

I am new to jasper reports but I am required to develop a fairly complex report(w.r.t organization of the datasource). I have to use javabean as the datasource.

I have datasource bean as shown below:

pulic class A{
Subscriber subscriber;
List<Dependent> dependentList;
}


I have to create a table where each bean in the Collection corresponds to one row in the table. But after displaying subscriber as one row.
I have to iterate through a collection of dependentList and display as subsequent rows. Here the properties in both Subscriber and Dependent are same an I have list of A type objects.


If I represent the same in jsp its translates to something like this:
<table>
<%
for(int i=0;i<alist.size();i++) {
A a = (A)alist.get(i);
Subscriber subscriber =  a.getSubscriber;
List<Dependent> dependent = a.getDependentList();
%>
<tr>
<td>
<%=subscriber.getData()%>
</td>
</tr>
<%
for(int i=0;i<dependent.size();i++) {
Dependent dependent = (Dependent)dependent.get(i);
%>
<tr>
<td>
<%=dependent.getData()%>
</td>
</tr>
<%
}
}
%>
</table>

1. Iterate through a list to get subscriber and List<Dependent> objects
3. Create a row with  subscriber data
4. Iterate through List<Dependent> and create rows which each data

I browsed through the net,e- books and demos for past two days. I found <jr:table but it just takes collection of similar beans and not assorted data like this.
I went through subreports but we cannot arrange that in a table structure with each row comming from different datasource. I am goind mad. Can somebody pls guide me how
can I implement  the above structure. What feature in jreports I can use to implement this. I am running out of time can somebody pls guide me on this


 

Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

If i undertand correct, you need display data like this:

 

subscriber1.field1 subscriber1.field2 ...

dependent11.field1 dependent11.field2...

dependent12.field1 dependent12.field2...

subscriber2.field1 subscriber2.field2 ...

dependent21.field1 dependent21.field2...

dependent22.field1 dependent22.field2...

 

The simplest way (imho) is "usual" report without <jt:table

 

1. Make main report with two fields (field name, field type):

subscriber my.full.qualified.classname.Subscriber

dependent java.util.List

2. Make subreport (based on Dependend class), embedded into main report

3. Use field $F{dependent} (java.util.List) of main report in DataSource expression for subreport. For example: new MyClassName($F{dependent})

4. Write myClassName which implements JRDataSource interface for correct work of subreport

Link to comment
Share on other sites

Hi Sanbex

Thaks for the reply. I haved started implementing your suggestion and subscriber data is comming properly.I tried embedding the subreport for dependents but there were some issues and data was not getting displayed. Since even in subreport we have to iterate through the collection of dependent objects, I thought  I will achve this in main report and then transfer to sub report. While iterating I am not getting the individual values during iteration. I have tried to achive similar structure with diffent objects

Class A{

private List<Employee> empList;

private Employee emps;

 
}

 I am able to display Employee emps. But not able to display the elements in empList;

Pls have a look at the code below:

I have declared fields as shown below

<subDataset name="dataset1" uuid="09015d96-ad5a-4fed-aa9e-19d25e02e205">
 <field name="empList" class="java.util.List">
  <fieldDescription><![CDATA[empList]]></fieldDescription>
 </field>
</subDataset>

<field name="emps" class="Employee">
 <fieldDescription><![CDATA[emps]]></fieldDescription>
</field>

I am displaying data as show below:

<detail>
  <band height="224" splitType="Stretch">
   <textField>
    <reportElement uuid="5a1f7a60-468e-4c32-a55b-d095276f0fc2" x="0" y="0" width="100" height="20"/>
    <textElement/>
    <textFieldExpression><![CDATA[((Employee)$F{emps}).getName()]]></textFieldExpression>
   </textField>
   <textField>
    <reportElement uuid="1fafdabd-3f1b-4175-90bf-fc807d81a42e" x="100" y="0" width="100" height="20"/>
    <textElement/>
    <textFieldExpression><![CDATA[((Employee)$F{emps}).getId()]]></textFieldExpression>
   </textField>
   <componentElement>
    <reportElement uuid="66786f07-dbf9-4c05-a769-f76d07261bca" key="table" x="0" y="20" width="802" height="204"/>
   
 <jr:list xmlns:jr="" xsi:schemaLocation="" printOrder="Vertical">
     <datasetRun subDataset="dataset1" uuid="a669f047-7641-464d-9560-559b865a2af5">
      <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{empList})]]></dataSourceExpression>
     </datasetRun>
     
    
     <jr:listContents height="204" width="802">
      <textField>
       <reportElement uuid="30f2b194-b444-4c09-aed2-c99023ecf505" x="130" y="77" width="100" height="20"/>
       <textElement/>
       <textFieldExpression><![CDATA[name]]></textFieldExpression>
      </textField>
http://jasperreports.sourceforge.net/xsd/components.xsdhttp://jasperreports.sourceforge.net/jasperreports/componentshttp://jasperreports.sourceforge.net/jasperreports/components

                        <textField>
       <reportElement uuid="30f2b194-b444-4c09-aed2-c99023ecf505" x="130" y="77" width="100" height="20"/>
       <textElement/>
       <textFieldExpression><![CDATA[id]]></textFieldExpression>
      </textField>
     </jr:listContents>
    </jr:list>
   </componentElement>
  </band>
 </detail>

 In the above code I am able to display 


 <textFieldExpression><![CDATA[((Employee)$F{emps}).getName()]]></textFieldExpression>
 and
 <textFieldExpression><![CDATA[((Employee)$F{emps}).getId()]]></textFieldExpression>
 
 But in I have to iterate through the empList and display the id and name inside the list. I am not getting the id and name at all while iterating through the list
 Can u pls suggest me a way how I can iterate through the list where datasource comes from a bean like <field name="empList" class="java.util.List">
 

My jrxml

and data I am feeding like this      ArrayList employeeList = new ArrayList();
  Bean bean1 = new Bean();
  Employee emp = new Employee();
  emp.setId("1");
  emp.setName("abcdedf");
  bean1.setEmps(emp);
  bean1.setEmpList(Arrays.asList(new Employee[]{emp}));
  
  Bean bean2 = new Bean();
  Employee emp1 = new Employee();
  emp1.setId("2");
  emp1.setName("second");
  bean2.setEmps(emp1);
  bean2.setEmpList(Arrays.asList(new Employee[]{emp1}));
  
  students.add(bean1);
  students.add(bean2); JRBeanCollectionDataSource(Employee.getEmlList()))

new



Post Edited by mohanbelaguli at 07/10/2012 16:07
Link to comment
Share on other sites

I am don't use subdataset. I am using subreport.
Example attached

See how i am embedded subreport in main report. Parameter for DataSource is collection (field of main report $F{equipOfPump})
In your case use field $F{empList}.

class R1020DataSource implements JRDataSource   (see net.sf.jasperreports.engine.JRDataSource   interface)
and this class is provides iteration for collection in subreport.

 

Link to comment
Share on other sites

Hi Sanbez

I thank you profusely for your help in this regard. I implemented subreports. I am exported the files to csv but the the subreport records
are not comming exactly below the other. Some extra commas are getting added

Eg:
Field1,,,Field2
abcdedf,,,1
,insideName,insideId1,
second,,,2
,insideName,insideId2,

In csv it is getting added like this if I add subreport data. Here the data under Field1 and Field2 are comming form Employee and insideName and insideId1 are

comming from subreports

Field1     Field2
abcdedf     1
  insideName insideId1  
second     2
  insideName insideId2  

The data i am feeding like this

 

  Bean bean1 = new Bean();
  Employee emp = new Employee();
  emp.setId("1");
  emp.setName("abcdedf");
  bean1.setEmps(emp);
  Employee empIns = new Employee();
  empIns.setId("insideId1");
  empIns.setName("insideName1");
  bean1.setEmpList(Arrays.asList(new Employee[]{empIns}));
 
  Bean bean2 = new Bean();
  Employee emp1 = new Employee();
  emp1.setId("2");
  emp1.setName("second");
  bean2.setEmps(emp1);
  Employee empIns1 = new Employee();
  empIns1.setId("insideId2");
  empIns1.setName("insideName2");
  bean2.setEmpList(Arrays.asList(new Employee[]{empIns1}));

Can you pls let me know if we can arrange the data to come one below the other if you have come across

 

My Main jrxml

 

 

Code:

My Subreport jrxml

Code:



Post Edited by mohanbelaguli at 07/11/2012 08:08



Post Edited by mohanbelaguli at 07/11/2012 08:10
Link to comment
Share on other sites

I am don't use export into csv. :(

 

But i am suppose that extra comma will disappear, if you set length (and position if this needed) $F{name} in subreport equals length ((Employee)$F{emps}).getName() in main subreport.

And also set length $F{id} in subreport equals length ((Employee)$F{emps}).getId() in main report

 

HTH

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...