Search in sources :

Example 1 with JRException

use of net.sf.jasperreports.engine.JRException in project midpoint by Evolveum.

the class ReportTypeUtil method loadJasperReport.

public static JasperReport loadJasperReport(ReportType reportType) throws SchemaException {
    if (reportType.getTemplate() == null) {
        throw new IllegalStateException("Could not create report. No jasper template defined.");
    }
    try {
        //	    	 	byte[] reportTemplate = Base64.decodeBase64(reportType.getTemplate());
        //	    	 	
        //	    	 	InputStream inputStreamJRXML = new ByteArrayInputStream(reportTemplate);
        //JRXmlLoader.load(inputStreamJRXML);
        JasperDesign jasperDesign = loadJasperDesign(reportType.getTemplate());
        if (reportType.getTemplateStyle() != null) {
            JRDesignReportTemplate templateStyle = new JRDesignReportTemplate(new JRDesignExpression("$P{" + PARAMETER_TEMPLATE_STYLES + "}"));
            jasperDesign.addTemplate(templateStyle);
            JRDesignParameter parameter = new JRDesignParameter();
            parameter.setName(PARAMETER_TEMPLATE_STYLES);
            parameter.setValueClass(JRTemplate.class);
            parameter.setForPrompting(false);
            jasperDesign.addParameter(parameter);
        }
        //			 if (StringUtils.isNotEmpty(finalQuery)){
        JRDesignParameter parameter = new JRDesignParameter();
        parameter.setName("finalQuery");
        parameter.setValueClass(Object.class);
        parameter.setForPrompting(false);
        jasperDesign.addParameter(parameter);
        //			 }
        JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
        return jasperReport;
    } catch (JRException ex) {
        //			 LOGGER.error("Couldn't create jasper report design {}", ex.getMessage());
        throw new SchemaException(ex.getMessage(), ex.getCause());
    }
}
Also used : JRDesignExpression(net.sf.jasperreports.engine.design.JRDesignExpression) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) JasperDesign(net.sf.jasperreports.engine.design.JasperDesign) JRException(net.sf.jasperreports.engine.JRException) JRDesignReportTemplate(net.sf.jasperreports.engine.design.JRDesignReportTemplate) JRDesignParameter(net.sf.jasperreports.engine.design.JRDesignParameter) JasperReport(net.sf.jasperreports.engine.JasperReport)

Example 2 with JRException

use of net.sf.jasperreports.engine.JRException 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 JRException

use of net.sf.jasperreports.engine.JRException in project opennms by OpenNMS.

the class AbstractMeasurementQueryExecutorTest method fill.

protected JasperPrint fill(final JasperReport jasperReport, final ReportFiller filler) throws JRException {
    try {
        Map<String, Object> params = new HashMap<String, Object>();
        filler.fill(params);
        return JasperFillManager.fillReport(jasperReport, params);
    } catch (Exception ex) {
        if (ex instanceof JRException) {
            throw (JRException) ex;
        }
        throw new JRException(ex);
    }
}
Also used : JRException(net.sf.jasperreports.engine.JRException) HashMap(java.util.HashMap) JRException(net.sf.jasperreports.engine.JRException) IOException(java.io.IOException) ValidationException(org.opennms.netmgt.measurements.api.exceptions.ValidationException)

Example 4 with JRException

use of net.sf.jasperreports.engine.JRException in project opennms by OpenNMS.

the class LocalMeasurementDataSourceWrapper method createDataSource.

@Override
public JRRewindableDataSource createDataSource(String query) throws JRException {
    Objects.requireNonNull(query);
    QueryRequest queryRequest = unmarshal(query);
    Objects.requireNonNull(queryRequest);
    // Enforce relaxed mode
    queryRequest.setRelaxed(true);
    try {
        QueryResponse response = fetchService.query(queryRequest);
        return new MeasurementDataSource(response);
    } catch (ResourceNotFoundException rnfe) {
        LOG.warn("A attribute or resource was not found", rnfe);
        return new EmptyJRDataSource();
    } catch (Exception e) {
        LOG.error("An error occurred while fetching the measurement results", e);
        throw new JRException(e);
    }
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) JRException(net.sf.jasperreports.engine.JRException) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) MeasurementDataSource(org.opennms.netmgt.jasper.measurement.MeasurementDataSource) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) JRException(net.sf.jasperreports.engine.JRException) EmptyJRDataSource(org.opennms.netmgt.jasper.measurement.EmptyJRDataSource)

Example 5 with JRException

use of net.sf.jasperreports.engine.JRException in project opennms by OpenNMS.

the class RemoteMeasurementDataSourceWrapper method createDataSource.

@Override
public JRRewindableDataSource createDataSource(String query) throws JRException {
    try {
        QueryRequest queryRequest = unmarshal(query);
        // enforce relaxed mode
        queryRequest.setRelaxed(true);
        query = marshal(queryRequest);
        Result result = connector.execute(useSsl, url, username, password, query);
        // therefore do not allow redirection at all.
        if (result.wasRedirection()) {
            throw new IOException("Request was redirected. This is not supported.");
        }
        // if there is no data
        if (404 == result.getResponseCode()) {
            LOG.warn("Got a 404 (Not Found) response. This might be due to a wrong url or the resource does not exist. Requested URL was: '{}'", url);
            return new EmptyJRDataSource();
        }
        // OK
        if (result.wasSuccessful() && result.getInputStream() != null) {
            return new MeasurementDataSource(result.getInputStream());
        }
        // Error
        ByteArrayOutputStream errorMessageStream = new ByteArrayOutputStream();
        if (result.getErrorStream() != null) {
            ByteStreams.copy(result.getErrorStream(), errorMessageStream);
        }
        throw new JRException("Invalid request. Response was : " + result.getResponseCode() + " (" + result.getResponseMessage() + ")\n" + errorMessageStream.toString());
    } catch (IOException ioException) {
        throw new JRException(ioException);
    }
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) JRException(net.sf.jasperreports.engine.JRException) MeasurementDataSource(org.opennms.netmgt.jasper.measurement.MeasurementDataSource) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EmptyJRDataSource(org.opennms.netmgt.jasper.measurement.EmptyJRDataSource)

Aggregations

JRException (net.sf.jasperreports.engine.JRException)33 JasperReport (net.sf.jasperreports.engine.JasperReport)8 File (java.io.File)7 InputStream (java.io.InputStream)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 JasperPrint (net.sf.jasperreports.engine.JasperPrint)6 PrismObject (com.evolveum.midpoint.prism.PrismObject)5 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)5 ArrayList (java.util.ArrayList)4 JRBeanCollectionDataSource (net.sf.jasperreports.engine.data.JRBeanCollectionDataSource)4 Containerable (com.evolveum.midpoint.prism.Containerable)3 PrismContainerValue (com.evolveum.midpoint.prism.PrismContainerValue)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Map (java.util.Map)3 JasperDesign (net.sf.jasperreports.engine.design.JasperDesign)3 BusinessServiceException (sic.service.BusinessServiceException)3 ServiceException (sic.service.ServiceException)3 Item (com.evolveum.midpoint.prism.Item)2 PrismContainer (com.evolveum.midpoint.prism.PrismContainer)2