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);
}
}
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, "&");
}
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);
}
}
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");
}
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>";
}
Aggregations