Search in sources :

Example 1 with ReportException

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);
    }
}
Also used : JREmptyDataSource(net.sf.jasperreports.engine.JREmptyDataSource) JasperPrint(net.sf.jasperreports.engine.JasperPrint) DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) ReportException(org.opennms.api.reporting.ReportException) JasperReport(net.sf.jasperreports.engine.JasperReport) Map(java.util.Map) HashMap(java.util.HashMap) ReportException(org.opennms.api.reporting.ReportException) JRException(net.sf.jasperreports.engine.JRException)

Example 2 with ReportException

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);
    }
}
Also used : CustomJRJdtCompiler(org.opennms.reporting.jasperreports.compiler.CustomJRJdtCompiler) JRException(net.sf.jasperreports.engine.JRException) SimpleJasperReportsContext(net.sf.jasperreports.engine.SimpleJasperReportsContext) ReportException(org.opennms.api.reporting.ReportException) JasperReport(net.sf.jasperreports.engine.JasperReport) JasperCompileManager(net.sf.jasperreports.engine.JasperCompileManager) SimpleJasperReportsContext(net.sf.jasperreports.engine.SimpleJasperReportsContext) JasperReportsContext(net.sf.jasperreports.engine.JasperReportsContext)

Example 3 with ReportException

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;
}
Also used : JRParameter(net.sf.jasperreports.engine.JRParameter) HashMap(java.util.HashMap) ReportException(org.opennms.api.reporting.ReportException) Date(java.util.Date)

Example 4 with ReportException

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);
    }
}
Also used : ReportException(org.opennms.api.reporting.ReportException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ReportException (org.opennms.api.reporting.ReportException)4 HashMap (java.util.HashMap)2 JRException (net.sf.jasperreports.engine.JRException)2 JasperReport (net.sf.jasperreports.engine.JasperReport)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Connection (java.sql.Connection)1 Date (java.util.Date)1 Map (java.util.Map)1 JREmptyDataSource (net.sf.jasperreports.engine.JREmptyDataSource)1 JRParameter (net.sf.jasperreports.engine.JRParameter)1 JasperCompileManager (net.sf.jasperreports.engine.JasperCompileManager)1 JasperPrint (net.sf.jasperreports.engine.JasperPrint)1 JasperReportsContext (net.sf.jasperreports.engine.JasperReportsContext)1 SimpleJasperReportsContext (net.sf.jasperreports.engine.SimpleJasperReportsContext)1 DBUtils (org.opennms.core.utils.DBUtils)1 CustomJRJdtCompiler (org.opennms.reporting.jasperreports.compiler.CustomJRJdtCompiler)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1