Search in sources :

Example 1 with ReportException

use of com.adobe.acs.commons.reports.api.ReportException 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 ReportException

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

the class QueryReportExecutor method getParameters.

@Override
public String getParameters() throws ReportException {
    List<String> params = new ArrayList<String>();
    Enumeration<String> keys = request.getParameterNames();
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        for (String value : request.getParameterValues(key)) {
            try {
                params.add(URLEncoder.encode(key, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                throw new ReportException("UTF-8 encoding available", e);
            }
        }
    }
    return StringUtils.join(params, "&");
}
Also used : ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ReportException(com.adobe.acs.commons.reports.api.ReportException)

Example 3 with ReportException

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

the class QueryReportExecutor method prepareStatement.

private void prepareStatement() throws ReportException {
    try {
        Map<String, String> parameters = new HashMap<String, String>();
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String key = paramNames.nextElement();
            parameters.put(key, StringEscapeUtils.escapeSql(request.getParameter(key)));
        }
        log.trace("Loading parameters from request: {}", parameters);
        Handlebars handlebars = new Handlebars();
        Template template = handlebars.compileInline(config.getQuery());
        statement = template.apply(parameters);
        log.trace("Loaded statement: {}", statement);
    } catch (IOException ioe) {
        throw new ReportException("Exception templating query", ioe);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Handlebars(com.github.jknack.handlebars.Handlebars) ReportException(com.adobe.acs.commons.reports.api.ReportException) IOException(java.io.IOException) Template(com.github.jknack.handlebars.Template)

Example 4 with ReportException

use of com.adobe.acs.commons.reports.api.ReportException 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 5 with ReportException

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

the class QueryReportExecutor method getDetails.

@Override
public String getDetails() throws ReportException {
    Map<String, String> details = new LinkedHashMap<String, String>();
    details.put("Language", config.getQueryLanguage());
    details.put("Page", Integer.toString(page));
    details.put("Page Size", Integer.toString(config.getPageSize()));
    details.put("Query", statement);
    try {
        final QueryManager queryManager = request.getResourceResolver().adaptTo(Session.class).getWorkspace().getQueryManager();
        final Query query = queryManager.createQuery("explain " + statement, config.getQueryLanguage());
        final QueryResult queryResult = query.execute();
        final RowIterator rows = queryResult.getRows();
        while (rows.hasNext()) {
            final Row row = rows.nextRow();
            String[] cols = queryResult.getColumnNames();
            Value[] values = row.getValues();
            for (int i = 0; i < cols.length; i++) {
                details.put(cols[i], values[i].getString());
            }
        }
    } catch (RepositoryException re) {
        log.error("Exception getting details", re);
        throw new ReportException("Exception getting details", re);
    }
    StringBuilder sb = new StringBuilder();
    for (Entry<String, String> entry : details.entrySet()) {
        sb.append("<dt>" + StringEscapeUtils.escapeHtml(entry.getKey()) + "</dt>");
        sb.append("<dd>" + StringEscapeUtils.escapeHtml(entry.getValue()) + "</dd>");
    }
    return "<dl>" + sb.toString() + "</dl>";
}
Also used : Query(javax.jcr.query.Query) RepositoryException(javax.jcr.RepositoryException) LinkedHashMap(java.util.LinkedHashMap) QueryResult(javax.jcr.query.QueryResult) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Value(javax.jcr.Value) ReportException(com.adobe.acs.commons.reports.api.ReportException) Row(javax.jcr.query.Row)

Aggregations

ReportException (com.adobe.acs.commons.reports.api.ReportException)6 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ReportCellCSVExporter (com.adobe.acs.commons.reports.api.ReportCellCSVExporter)2 ResultsPage (com.adobe.acs.commons.reports.api.ResultsPage)2 LinkedHashMap (java.util.LinkedHashMap)2 RepositoryException (javax.jcr.RepositoryException)2 Query (javax.jcr.query.Query)2 QueryManager (javax.jcr.query.QueryManager)2 QueryResult (javax.jcr.query.QueryResult)2 ServletException (javax.servlet.ServletException)2 QueryReportExecutor (com.adobe.acs.commons.reports.models.QueryReportExecutor)1 Csv (com.day.text.csv.Csv)1 Handlebars (com.github.jknack.handlebars.Handlebars)1 Template (com.github.jknack.handlebars.Template)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Writer (java.io.Writer)1 HashMap (java.util.HashMap)1 NodeIterator (javax.jcr.NodeIterator)1 Session (javax.jcr.Session)1