Search in sources :

Example 1 with ReportCellCSVExporter

use of com.adobe.acs.commons.reports.api.ReportCellCSVExporter in project acs-aem-commons by Adobe-Consulting-Services.

the class ReportCSVExportServlet method doGet.

protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws ServletException, IOException {
    log.trace("doGet");
    // set response parameters
    response.setContentType("text/csv");
    response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(request.getResource().getValueMap().get(JcrConstants.JCR_TITLE, "report"), "UTF-8") + ".csv");
    Writer writer = null;
    try {
        writer = response.getWriter();
        // initialize the csv
        final Csv csv = new Csv();
        csv.writeInit(writer);
        // write the headers
        List<ReportCellCSVExporter> exporters = writeHeaders(request, csv);
        Resource configCtr = request.getResource().getChild("config");
        if (configCtr != null && configCtr.listChildren().hasNext()) {
            Iterator<Resource> children = configCtr.listChildren();
            while (children.hasNext()) {
                Resource config = children.next();
                if (config != null) {
                    updateCSV(config, request, exporters, csv, writer);
                    log.debug("Successfully export report with configuration: {}", config);
                    break;
                } else {
                    log.warn("Unable to export report for configuration: {}", config);
                }
            }
            csv.close();
        } else {
            throw new IOException("No configurations found for " + request.getResource());
        }
    } catch (ReportException e) {
        log.error("Exception extracting report to CSV", e);
        throw new ServletException("Exception extracting report to CSV", e);
    } finally {
        IOUtils.closeQuietly(writer);
    }
}
Also used : ServletException(javax.servlet.ServletException) Csv(com.day.text.csv.Csv) Resource(org.apache.sling.api.resource.Resource) ReportException(com.adobe.acs.commons.reports.api.ReportException) IOException(java.io.IOException) ReportCellCSVExporter(com.adobe.acs.commons.reports.api.ReportCellCSVExporter) Writer(java.io.Writer)

Example 2 with ReportCellCSVExporter

use of com.adobe.acs.commons.reports.api.ReportCellCSVExporter in project acs-aem-commons by Adobe-Consulting-Services.

the class ReportCSVExportServlet method updateCSV.

private void updateCSV(Resource config, SlingHttpServletRequest request, List<ReportCellCSVExporter> exporters, Csv csv, Writer writer) throws ReportException {
    QueryReportExecutor executor = request.adaptTo(QueryReportExecutor.class);
    executor.setConfiguration(config);
    log.debug("Retrieved executor {}", executor);
    ResultsPage queryResult = executor.getAllResults();
    List<? extends Object> results = queryResult.getResults();
    log.debug("Retrieved {} results", results.size());
    for (Object result : results) {
        List<String> row = new ArrayList<String>();
        try {
            for (ReportCellCSVExporter exporter : exporters) {
                row.add(exporter.getValue(result));
            }
            csv.writeRow(row.toArray(new String[row.size()]));
            writer.flush();
        } catch (Exception e) {
            log.warn("Exception writing row: " + row, e);
        }
    }
    log.debug("Results written successfully");
}
Also used : QueryReportExecutor(com.adobe.acs.commons.reports.models.QueryReportExecutor) ArrayList(java.util.ArrayList) ResultsPage(com.adobe.acs.commons.reports.api.ResultsPage) ReportCellCSVExporter(com.adobe.acs.commons.reports.api.ReportCellCSVExporter) ReportException(com.adobe.acs.commons.reports.api.ReportException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 3 with ReportCellCSVExporter

use of com.adobe.acs.commons.reports.api.ReportCellCSVExporter in project acs-aem-commons by Adobe-Consulting-Services.

the class ReportCSVExportServlet method writeHeaders.

private List<ReportCellCSVExporter> writeHeaders(SlingHttpServletRequest request, final Csv csv) throws IOException {
    List<String> row = new ArrayList();
    List<ReportCellCSVExporter> exporters = new ArrayList();
    for (Resource column : request.getResource().getChild("columns").getChildren()) {
        String className = column.getValueMap().get("exporter", String.class);
        if (!StringUtils.isEmpty(className)) {
            try {
                log.debug("Finding ReportCellCSVExporter for {}", className);
                @SuppressWarnings("unchecked") Class<ReportCellCSVExporter> clazz = (Class<ReportCellCSVExporter>) getClass().getClassLoader().loadClass(className);
                ReportCellCSVExporter exporter = column.adaptTo(clazz);
                log.debug("Loaded ReportCellCSVExporter {}", exporter);
                if (exporter != null) {
                    exporters.add(exporter);
                    row.add(column.getValueMap().get("heading", String.class));
                } else {
                    log.warn("Retrieved null ReportCellCSVExporter for {}", className);
                }
            } catch (Exception e) {
                log.warn("Unable to render column due to issue fetching ReportCellCSVExporter " + className, e);
            }
        }
    }
    csv.writeRow(row.toArray(new String[row.size()]));
    return exporters;
}
Also used : ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) ReportCellCSVExporter(com.adobe.acs.commons.reports.api.ReportCellCSVExporter) ReportException(com.adobe.acs.commons.reports.api.ReportException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

ReportCellCSVExporter (com.adobe.acs.commons.reports.api.ReportCellCSVExporter)3 ReportException (com.adobe.acs.commons.reports.api.ReportException)3 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)3 ArrayList (java.util.ArrayList)2 Resource (org.apache.sling.api.resource.Resource)2 ResultsPage (com.adobe.acs.commons.reports.api.ResultsPage)1 QueryReportExecutor (com.adobe.acs.commons.reports.models.QueryReportExecutor)1 Csv (com.day.text.csv.Csv)1 Writer (java.io.Writer)1