views:

4313

answers:

1

Hi,

I have a requirement that needs to have a subreport within a subreport. Is there a sample code which I can refer?

Thanks in advance.

+3  A: 

You don't actually need any code at all to generate a subreport within a subreport. This can be done with reports that have no dynamic components (nothing in the detail band). Of course the resulting report won't be much use for anything interesting.

If you wanted a more interesting report than this, you'll need to provide data for the report and / or subreport. At that point the code will vary depending on where your data is coming from. If you can provide more information on what you are trying to do, we can perhaps be more help.

If the subreport has dynamic content, you will need to pass in to it access to an object which implements JRDataSource.

For example, I recently created a one page report that had multiple "clauses" in it. To make my life simpler, I stored the clauses in a Map and derived the JRDataSource object using the following code. The JRDataSource objects were then passed in as a field for the main report.

private static class ListMapDataSource implements JRRewindableDataSource {

 private Map currentMap = null;
 private int currentRow;
 private int numberOfMoveFirsts = 0;
 private List<Map<String, ? extends Object>> rowList;

 ListMapDataSource(List<Map<String, ? extends Object>> rowList) {
  this.rowList = rowList;
  moveFirst();
 }

 ListMapDataSource(Map<String, ? extends Object> singleRow) {
  this.rowList = new ArrayList<Map<String, ? extends Object>>(1);
  this.rowList.add(singleRow);
  moveFirst();
 }

 public boolean next() throws JRException {
  if (currentRow >= rowList.size() - 1) {
   return false;
  }

  currentRow++;
  currentMap = rowList.get(currentRow);

  return true;
 }

 public Object getFieldValue(JRField jrField) throws JRException {
  String name = jrField.getName();
  Class valueClass = jrField.getValueClass();

  if (JasperReport.class.isAssignableFrom(valueClass)) {
  }

  return currentMap.get(name);
 }

 public void moveFirst() {
  numberOfMoveFirsts++;

  if (numberOfMoveFirsts > 10) {
   System.out.println("Exceeded 10 moveFirst() calls.  Aborting.");
   System.exit(1);
  }

  currentRow = - 1;
  currentMap = null;
 }
}
kwutchak