Search code examples
javajasper-reports

Don't view the data from second JasperPrint


I want to create one report from many other JasperReport. But i don't see the data, which following the first JasperPrint. If i will change places jasperPrintTracking and jasperPrintDeparture. You will see the data from JasperPrint which I wrote first, but second JasperPrint don't have any data. If I wrote jasperPrintTracking at first - I will see data from jasperPrintTracking, but a can't see data from jasperPrintDeparture they don't downloaded. Why this happened?

public class RoadReport {
    JasperPrint jasperPrintDeparture;
    JasperPrint jasperPrintTracking;
    CardFile cardFile = new CardFile();

    public void createRoadReport (List <StatisticsOnPassengerTrainByMonth> passsenger, List<StatisticsOnCommuterTrainByMonth> commuter, List<StatisticsOnFreightTrainByMonth> freight){
        JRBeanCollectionDataSource passengerDataSource = new JRBeanCollectionDataSource(passsenger);
        JRBeanCollectionDataSource commuterDataSource = new JRBeanCollectionDataSource(commuter);
        JRBeanCollectionDataSource freightDataSource  = new JRBeanCollectionDataSource(freight);

        Map <String, Object> parametr = new HashMap<String, Object>();
        parametr.put("PassengerDataSource", passengerDataSource);
        parametr.put("CommuterDataSource", commuterDataSource);
        parametr.put("FreightDataSource", freightDataSource);

        try {
            jasperPrintDeparture = JasperFillManager.fillReport((FacesContext.getCurrentInstance().getExternalContext().getRealPath("/ReportRoadDeparture.jasper")), parametr, new JREmptyDataSource());
            jasperPrintTracking = JasperFillManager.fillReport((FacesContext.getCurrentInstance().getExternalContext().getRealPath("/ReportRoadTracking.jasper")), parametr, new JREmptyDataSource());
            multipageLinking(jasperPrintDeparture, jasperPrintTracking);
            JasperExportManager.exportReportToPdfFile(jasperPrintDeparture, FacesContext.getCurrentInstance().getExternalContext().getRealPath("/report1.pdf"));
        } catch (JRException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("rawtypes")
    private JasperPrint multipageLinking(JasperPrint file1, JasperPrint file2) {
        List pages = file2.getPages();
        for (int count = 0; count <
                pages.size(); count++) {
            file1.addPage((JRPrintPage) pages.get(count));
        }

        return file1;
    }
}

The image of report is below

enter image description here


Solution

  • Beckyang, you method doesn't helped. The result was the same. I found the solution my problem. For each jasperPrint variable you must create new JRBeanCollectionDataSource. My code is below.

    public void createRoadReport (List <StatisticsOnPassengerTrainByMonth> passsenger, List<StatisticsOnCommuterTrainByMonth> commuter, List<StatisticsOnFreightTrainByMonth> freight, String dateReport) throws JRException{
        List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();
    
        for (int i = 0; i < 3; i++) {
            JRBeanCollectionDataSource passengerDataSource = new JRBeanCollectionDataSource(passsenger);
            JRBeanCollectionDataSource commuterDataSource = new JRBeanCollectionDataSource(commuter);
            JRBeanCollectionDataSource freightDataSource  = new JRBeanCollectionDataSource(freight);
    
            Map <String, Object> parametr = new HashMap<String, Object>();
            parametr.put("PassengerDataSource", passengerDataSource);
            parametr.put("CommuterDataSource", commuterDataSource);
            parametr.put("FreightDataSource", freightDataSource);
            parametr.put("date", dateReport);
            switch (i) {
            case 0:
                jasperPrintDeparture = JasperFillManager.fillReport((FacesContext.getCurrentInstance().getExternalContext().getRealPath("/ReportRoadDeparture.jasper")), parametr, new JREmptyDataSource());
                jasperPrintList.add(jasperPrintDeparture);
            break;
            case 1:
                jasperPrintTracking = JasperFillManager.fillReport((FacesContext.getCurrentInstance().getExternalContext().getRealPath("/ReportRoadTracking.jasper")), parametr, new JREmptyDataSource());
                jasperPrintList.add(jasperPrintTracking);
            break;
            case 2:
                jasperPrintArrival = JasperFillManager.fillReport((FacesContext.getCurrentInstance().getExternalContext().getRealPath("/ReportRoadArrival.jasper")), parametr, new JREmptyDataSource());
                jasperPrintList.add(jasperPrintArrival);
            break;
            }
        }
    
            //multipageLinking(jasperPrintDeparture, jasperPrintTracking);
            //JasperExportManager.exportReportToPdfFile(jasperPrintArrival, FacesContext.getCurrentInstance().getExternalContext().getRealPath("/report.pdf"));
            JRPdfExporter exporter = new JRPdfExporter();
            exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList)); //Set as export input my list with JasperPrint s
            exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(FacesContext.getCurrentInstance().getExternalContext().getRealPath("/report.pdf"))); //or any other out streaam
            SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
            configuration.setCreatingBatchModeBookmarks(true); //add this so your bookmarks work, you may set other parameters
            exporter.setConfiguration(configuration);
            exporter.exportReport();
    
    }