private void build() {
    List<PrintElement> printElements = new ArrayList<PrintElement>();
    PrintElementsFactory printElementsFactory = new PrintElementsFactory(mappedJasperPrint);
    JasperPrint jasperPrint = mappedJasperPrint.getJasperPrint();
    List<JRPrintPage> pages = jasperPrint.getPages();
    int pageIndex = 0;
    for (JRPrintPage page : pages) {
      List<JRPrintElement> elements = page.getElements();
      for (JRPrintElement jrPrintElement : elements) {
        PrintElement pe = printElementsFactory.createPrintElement(jrPrintElement, pageIndex);
        if (pe != null) {
          printElements.add(pe);
        }
      }
      pageIndex++;
    }
    this.printElements = printElements;

    GroupPrintElements groupBase = new GroupPrintElements();
    ReportPointer pointer =
        new ReportPointer(mappedJasperPrint.getMappedJasperReport().getReportDefinition());
    //		System.out.println(pointer);
    createGroupsForSubReports(pointer, groupBase, printElements);

    this.printElements = groupBase.getPrintElements();

    reorganizeRows(this.printElements);

    //		System.out.println(this.printElements);
  }
  private String searchKeyForSubreport(Subreport subreport) {
    // TODO REFACTOR.. DUPLICATE CODE
    String subreportIndex =
        mappedJasperPrint
            .getMappedJasperReport()
            .getReportDefinition()
            .getSubreportIndex(subreport);
    if (!subreportIndex.equals("-1")) {
      MappedJasperReport mappedJasperReport = mappedJasperPrint.getMappedJasperReport();
      return readKeyFromMappedJasperReport(subreport, mappedJasperReport);
    } else {

      // this is a sub subreport.. search deeper
      List<MappedJasperReport> subreports = mappedJasperPrint.getSubreports();
      for (MappedJasperReport mappedJasperReport : subreports) {
        ReportDefinition parent = mappedJasperReport.getReportDefinition();
        String subreportIndex2 = parent.getSubreportIndex(subreport);
        if (!subreportIndex2.equals("-1")) {
          return readKeyFromMappedJasperReport(subreport, mappedJasperReport);
        }
      }
    }

    throw new IllegalStateException("subreport key not found");
  }