use of org.opennms.api.reporting.ReportException in project opennms by OpenNMS.
the class JasperReportService method runAndRender.
/**
* {@inheritDoc}
*/
@Override
public void runAndRender(final Map<String, Object> reportParms, final String reportId, final ReportFormat format, final OutputStream outputStream) throws ReportException {
try {
Logging.withPrefix(LOG4J_CATEGORY, new Callable<Void>() {
@Override
public Void call() throws Exception {
final JasperReport jasperReport = getJasperReport(reportId);
final Map<String, Object> jrReportParms = buildJRparameters(reportParms, jasperReport.getParameters());
jrReportParms.putAll(buildSubreport(reportId, jasperReport));
if ("jdbc".equalsIgnoreCase(m_globalReportRepository.getEngine(reportId))) {
final DBUtils db = new DBUtils();
try {
final Connection connection = DataSourceFactory.getInstance().getConnection();
db.watch(connection);
final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, jrReportParms, connection);
exportReport(format, jasperPrint, outputStream);
} finally {
db.cleanUp();
}
} else if ("null".equalsIgnoreCase(m_globalReportRepository.getEngine(reportId))) {
final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, jrReportParms, new JREmptyDataSource());
exportReport(format, jasperPrint, outputStream);
}
return null;
}
});
} catch (final Exception e) {
if (e instanceof ReportException)
throw (ReportException) e;
throw new ReportException("Failed to run Jasper report " + reportId, e);
}
}
use of org.opennms.api.reporting.ReportException in project opennms by OpenNMS.
the class JasperReportService method compileReport.
/**
* Compiles the given {@link JasperDesign} (*.jrxml) to a {@link JasperReport} (*.jasper)
*
* @param jasperDesign The Design (*.jrxml) to compile
* @return The compiled report (*.jasper)
* @throws ReportException If the design could not be compiled.
*/
private JasperReport compileReport(JasperDesign jasperDesign) throws ReportException {
try {
JasperReport report;
// If the target report is written in Java, use our custom JDT compiler
if (JRReport.LANGUAGE_JAVA.equals(jasperDesign.getLanguage())) {
JasperReportsContext reportsContext = new SimpleJasperReportsContext();
JRPropertiesUtil.getInstance(reportsContext).setProperty(JRCompiler.COMPILER_PREFIX + JRReport.LANGUAGE_JAVA, CustomJRJdtCompiler.class.getCanonicalName());
JasperCompileManager jasperCompilerManager = JasperCompileManager.getInstance(reportsContext);
report = jasperCompilerManager.compile(jasperDesign);
} else {
// Otherwise, use the default that Jasper providers
report = JasperCompileManager.compileReport(jasperDesign);
}
for (Object eachKey : System.getProperties().keySet()) {
String eachStringKey = (String) eachKey;
if (eachStringKey.startsWith("net.sf.jasperreports")) {
report.setProperty(eachStringKey, System.getProperty(eachStringKey));
}
}
compileSubreportsRecursively(report);
return report;
} catch (final JRException e) {
LOG.error("unable to compile jasper report {}", e);
throw new ReportException("unable to compile jasperReport", e);
}
}
use of org.opennms.api.reporting.ReportException in project opennms by OpenNMS.
the class JasperReportService method buildJRparameters.
private Map<String, Object> buildJRparameters(final Map<String, Object> onmsReportParms, final JRParameter[] reportParms) throws ReportException {
final Map<String, Object> jrReportParms = new HashMap<String, Object>();
for (final JRParameter reportParm : reportParms) {
if (apply(getParameterFilters(), reportParm)) {
final String parmName = reportParm.getName();
if (!onmsReportParms.containsKey(parmName)) {
throw new ReportException("Required parameter " + parmName + " not supplied to JasperReports by OpenNMS");
}
if (reportParm.getValueClassName().equals("java.lang.String")) {
jrReportParms.put(parmName, (String) onmsReportParms.get(parmName));
continue;
}
if (reportParm.getValueClassName().equals("java.lang.Integer")) {
jrReportParms.put(parmName, (Integer) onmsReportParms.get(parmName));
continue;
}
if (reportParm.getValueClassName().equals("java.lang.Float")) {
jrReportParms.put(parmName, (Float) onmsReportParms.get(parmName));
continue;
}
if (reportParm.getValueClassName().equals("java.lang.Double")) {
jrReportParms.put(parmName, (Double) onmsReportParms.get(parmName));
continue;
}
if (reportParm.getValueClassName().equals("java.util.Date")) {
jrReportParms.put(parmName, (Date) onmsReportParms.get(parmName));
continue;
}
if (reportParm.getValueClassName().equals("java.sql.Date")) {
final Date date = (Date) onmsReportParms.get(parmName);
jrReportParms.put(parmName, new java.sql.Date(date.getTime()));
continue;
}
if (reportParm.getValueClassName().equals("java.sql.Timestamp")) {
final Date date = (Date) onmsReportParms.get(parmName);
jrReportParms.put(parmName, new java.sql.Timestamp(date.getTime()));
continue;
}
throw new ReportException("Unsupported report parameter type " + reportParm.getValueClassName());
}
}
return jrReportParms;
}
use of org.opennms.api.reporting.ReportException in project opennms by OpenNMS.
the class OnlineReportController method handleSubmit.
@RequestMapping(method = RequestMethod.POST, params = "run")
public String handleSubmit(ModelMap modelMap, HttpServletResponse response, @ModelAttribute("parameters") ReportParameters parameters) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
m_reportWrapperService.runAndRender(parameters, ReportMode.IMMEDIATE, outputStream);
if ((parameters.getFormat() == ReportFormat.PDF) || (parameters.getFormat() == ReportFormat.SVG)) {
response.setContentType("application/pdf;charset=UTF-8");
response.setHeader("Content-disposition", "inline; filename=report.pdf");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "cache");
response.setHeader("Cache-Control", "must-revalidate");
}
if (parameters.getFormat() == ReportFormat.CSV) {
response.setContentType("text/csv;charset=UTF-8");
response.setHeader("Content-disposition", "inline; filename=report.csv");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "cache");
response.setHeader("Cache-Control", "must-revalidate");
}
response.getOutputStream().write(outputStream.toByteArray());
return null;
} catch (ReportException ex) {
// forward to same page, but now show errors
modelMap.addAttribute("errorMessage", ex.getMessage());
modelMap.addAttribute("errorCause", ex.getCause());
// add default view parameters
handleGet(modelMap, parameters.getReportId());
return "report/database/onlineReport";
} finally {
IOUtils.closeQuietly(outputStream);
}
}
Aggregations