Jump to content
Changes to the Jaspersoft community edition download ×

Pass instance of bean to subreport


Recommended Posts

By: Martyn - martyn-hi-ss-nl

Pass instance of bean to subreport

2006-05-24 00:38

Hi All

 

I want to print a list of beans that are in a JRBeanCollectionDataSource. The problem is that the list contains a list of beans of different types and the list isnt sorted by type. The one thing the beans have in common is that they all extend from the same object.

 

ParentObject

|

|

/

/

/

ChildA ChildB

 

I send the collection of beans to a subreport as a datasource. In that sub report I want to do a kind of if statement. I create 2 sub reports and each report has a printWhenExpression. If the current bean is of type ChildA then the ChildA report is loaded and otherwise the ChildB report is loaded.

 

This system works but there is one thing that I cant solve. I need to pass an instance of the current bean in the parent report onto the sub report.

 

I know how to pass on the datasource, and the report connection but I need to pass on the current bean.

 

 

 

 

 

 

By: Martyn - martyn-hi-ss-nl

RE: Pass instance of bean to subreport

2006-05-29 05:09

Ok I've fixed it by creating my own datasource. When field name is currentBean then it returns the current bean.

 

public class ReportCollectionDataSource extends JRAbstractBeanDataSource implements Serializable {

 

private Collection data = null;

private Iterator iterator = null;

private Object currentBean = null;

 

public ReportCollectionDataSource(Object object) {

super(true);

 

// Create an empty list

this.data = new LinkedList();

 

// Add the object to the list

this.data.add(object);

 

// Ensure the iterator is initialized

this.moveFirst();

}

 

public ReportCollectionDataSource(Collection beanCollection) {

super(true);

 

this.data = beanCollection;

 

// Ensure the iterator is initialized

this.moveFirst();

}

 

public ReportCollectionDataSource(Collection beanCollection, boolean isUseFieldDescription) {

super(isUseFieldDescription);

 

this.data = beanCollection;

 

// Ensure the iterator is initialized

this.moveFirst();

}

 

/**

* Retreives the field with a certain name in the current bean. If the name is currentBean

* then the current bean is returned

*/

public Object getFieldValue(JRField field) throws JRException {

Object value = null;

 

String propertyName = field.getName();

 

// If the field name is 'currentBean' then we must return the currentBean

if ("currentBean".equalsIgnoreCase(propertyName)) {

return this.currentBean;

} else {

if (currentBean != null) {

try {

value = PropertyUtils.getProperty(currentBean, propertyName);

} catch (java.lang.IllegalAccessException e) {

throw new JRException("Error retrieving field value from bean : " + propertyName, e);

} catch (java.lang.reflect.InvocationTargetException e) {

throw new JRException("Error retrieving field value from bean : " + propertyName, e);

} catch (java.lang.NoSuchMethodException e) {

throw new JRException("Error retrieving field value from bean : " + propertyName, e);

}

}

}

 

return value;

}

 

/**

* Get the next bean

*/

public boolean next() throws JRException {

boolean hasNext = false;

 

if (this.iterator != null) {

hasNext = this.iterator.hasNext();

 

if (hasNext) {

this.currentBean = this.iterator.next();

}

}

 

return hasNext;

}

 

/**

* Move the pointer to the first bean

*/

public void moveFirst() {

if (this.data != null) {

this.iterator = this.data.iterator();

}

}

}

Link to comment
Share on other sites

  • 10 months later...
  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

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...