Question

I want to export a dataTable (with Pagination) having LazyLoad DataModel during Report Generation.

Problem : When I export, the report is getting generated page by page from Database and then getting exported to Excel/PDF that consumes more time. I would like to get it in a single database access by skipping the page by page generation of dataset.

I'm producing my code snippet as follows:

JSF:

<p:dataTable id="dTable" var="dev" value="#{ReportAction.lazyModel}"
    styleClass ="table_paginator" rowKey="#{device.macAddress}" paginatorPosition="bottom"
    paginator="true" rowsPerPageTemplate="10,20,30" rows="10" lazy="true" 
    paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
    emptyMessage="Select appropriate conditions and click 'Generate Report'">

    <f:facet name="header">
        <h:commandLink actionListener="#{ReportAction.doExport}">
            <p:graphicImage value="../../../resources/images/excel.png"
                alt="XLS" style="float:right;width:32px;height:32px" />
            <p:dataExporter type="xls" target="dTable" fileName="#{ReportAction.fileName}"
                            preProcessor="#{ReportAction.preProcess}"
                            postProcessor="#{ReportAction.postProcessXLS}"  />
        </h:commandLink>
    </f:facet>

    <!-- All the columns in Data Table  -->

</p:dataTable>

Managed Bean:

public class ReportAction {


    private ConfigurationReportDataModel mediumConfigModel;
    private List<FieldReportModel> configModelList;
    private String fileName;
    private LazyDataModel<ConfigurationReportModel> lazyModel;
    private boolean export; 

    public ReportAction() {

        configModelList = new ArrayList<ConfigurationReportModel>();
        export = false;
        mediumConfigModel = new ReportDataModel();      

    }

    public void generateFieldReport() {

        lazyFieldModel = new ConfigurationReportDataModel(day, fromDate,
                location,store,engineer, status, toDate, export);

    }

    public void preProcess(Object document)     {

        export = true;

        log.info("preProcess::Lazy model : Page Sizing");
        if(lazyFieldModel != null)  {
            lazyFieldModel.setPageSize(1000000);

        }

        log.info("preProcess::Export All Details");
        mediumConfigModel.setExport(true);

    }

    public void postProcessXLS(Object document) {

        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow header = sheet.getRow(0);

        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillForegroundColor(HSSFColor.GREEN.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) {
            HSSFCell cell = header.getCell(i);

            cell.setCellValue(cell.getStringCellValue().toUpperCase());
            cell.setCellStyle(cellStyle);

            sheet.autoSizeColumn(i);
        }

        export = false;
        mediumConfigModel.setExport(false);
    }


    public List<ConfigurationReportModel> getConfigModelList() {
        return configModelList;
    }

    public void setConfigModelList(
            ArrayList<ConfigurationReportModel> configModelList) {
        this.configModelList = configModelList;
    }


    public String getFileName() {
        SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy_HHmmss");
        fileName = "Config_Report_".concat(formatter.format(new Date()));

        return fileName;
    }


    public void setMediumConfigModel(
            ConfigurationReportDataModel mediumConfigModel) {
        this.mediumConfigModel = mediumConfigModel;
    }


    public void setConfigModelList(
            List<ConfigurationReportModel> configModelList) {
        this.configModelList = configModelList;
    }

    public LazyDataModel<ConfigurationReportModel> getLazyFieldModel() {

        log.info("##########getLazyFieldModel###########");

        if(export)  {
            log.info("getLazyFieldModel::Will get Exported........");
            lazyFieldModel = new ConfigurationReportDataModel(day, fromDate,
                    location, store, engineer, status, toDate, true);
            lazyFieldModel.load(1, 1000000000, null, null, null);
        }

        return lazyFieldModel;
    }

    public void setLazyFieldModel(
            LazyDataModel<ConfigurationReportModel> lazyFieldModel) {
        this.lazyFieldModel = lazyFieldModel;
    }

  }

DataModel:

public class ConfigurationReportDataModel extends
        LazyDataModel<ConfigurationReportModel> {

    private List<ConfigurationReportModel> configReport;

    private boolean export; 

    public ConfigurationReportDataModel() {
        this.export = false; 

    }

    public List<ConfigurationReportModel> load(int first, int pageSize,
            String sortField, SortOrder sortOrder, Map<String, String> filters) {


        UIClient client = new UIClient();
        ReportData data = null;
        // ///////////////////

        if(export)      {
            log.info("Do Export....");
            first = 1;
            pageSize = 1000000000; 
        }

        deviceList = new ArrayList<DeviceGlobal>();

        // Retrieves data from Database with the number of record (page size)
        data = client.generateFieldReport(first, pageSize,
                Integer.parseInt(location), Integer.parseInt(store),
                engineer, getTimeToBeginningOfDay(), getTimeToEndofDay(),
                status);


        log.info("Obtained data : " + data);

        if (data != null) {

            log.info("Got devices : " + data.getRecords().size());

            deviceList = (ArrayList<DeviceGlobal>) data.getRecords();

            // ///////////////////
            int record = first + 1;
            ConfigurationReportModel storeModel = null;
            DeviceGlobal deviceGlobal = null;

            configReport = new ArrayList<ConfigurationReportModel>();

            for (Iterator<DeviceGlobal> iterator = deviceList.iterator(); iterator
                    .hasNext();) {

                deviceGlobal = (DeviceGlobal) iterator.next();
                storeModel = new ConfigurationReportModel(deviceGlobal,
                        record++);

                configReport.add(storeModel);
            }

            log.info("Total Config Report : " + configReport.size());

            // rowCount
            int dataSize = data.getReportCount();
            this.setRowCount(dataSize);

            log.info("Report Count: " + data.getReportCount());

            if(export)  {

                return configReport;
            }
            else    {
                // paginate
                if (dataSize > pageSize) {
                    try {
                        return configReport;
                    } catch (IndexOutOfBoundsException e) {
                        return configReport;
                    }
                } else {
                    return configReport;
                }
            }

        } else {

            log.info("Got no devices");

            deviceList = new ArrayList<DeviceGlobal>();
            configReport = new ArrayList<ConfigurationReportModel>();

            this.setRowCount(0);

            return configReport;
        }
    }
}

Note:

  • There's no syntax error.
  • All the custom type classes are defined.

How do I get the Excel report by skipping the page by page generation of records?

Was it helpful?

Solution 2

for zero devision issue just override an extra LaztDataModel method as given below.

        @Override
        public void setRowIndex(int rowIndex) {
            // TODO Auto-generated method stub
            if (rowIndex == -1 || getPageSize() == 0) {
                super.setRowIndex(-1);
            }
            else
                super.setRowIndex(rowIndex % getPageSize());
        }

OTHER TIPS

You have to add pageOnly="false" to dataExporter

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top