Search in sources :

Example 1 with JasperReport

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

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

use of net.sf.jasperreports.engine.JasperReport 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 4 with JasperReport

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

the class DefaultReportService method runAndRender.

private JasperPrint runAndRender(Report report) throws Exception, JRException {
    JasperPrint jasperPrint = new JasperPrint();
    JasperReport jasperReport = JasperCompileManager.compileReport(System.getProperty("opennms.home") + File.separator + "etc" + File.separator + "report-templates" + File.separator + report.getReportTemplate());
    if (report.getReportEngine().equals("jdbc")) {
        Connection connection = DataSourceFactory.getInstance().getConnection();
        jasperPrint = JasperFillManager.fillReport(jasperReport, paramListToMap(report.getParameters()), connection);
        connection.close();
    } else if (report.getReportEngine().equals("opennms")) {
        LOG.error("Sorry the OpenNMS Data source engine is not yet available");
        jasperPrint = null;
    } else {
        LOG.error("Unknown report engine: {} ", report.getReportEngine());
        jasperPrint = null;
    }
    return jasperPrint;
}
Also used : JasperPrint(net.sf.jasperreports.engine.JasperPrint) Connection(java.sql.Connection) JasperReport(net.sf.jasperreports.engine.JasperReport)

Example 5 with JasperReport

use of net.sf.jasperreports.engine.JasperReport in project adempiere by adempiere.

the class ReportInfo method processReport.

protected File processReport(String reportFile) {
    log.finest("ReportInfo.processReport - " + reportFile);
    File JasperDesignFile = new File(reportFile);
    String JasperReportFile = reportFile.replaceAll(".jrxml", ".jasper").replaceAll(".xml", ".jasper");
    File jasperFile = new File(JasperReportFile);
    // Marco LOMBARDO: this is the local one, there is the same 
    // field at class level. There was a bug on it about subreports.
    JasperReport jasperReport = null;
    if (jasperFile.exists()) {
        // test time
        if (JasperDesignFile.lastModified() <= jasperFile.lastModified()) {
            try {
                // Marco LOMBARDO: should refer to local.
                jasperReport = (JasperReport) JRLoader.loadObject(jasperFile.getAbsolutePath());
            } catch (JRException e) {
                // Marco LOMBARDO: on error reset global report.
                this.jasperReport = null;
                log.saveError("ReportServer.processReport: Can not load report - ", e);
            }
        } else {
            // Marco LOMBARDO: should refer to local.
            jasperReport = compileReport(JasperDesignFile, jasperFile);
        }
    } else {
        // create new jasper file
        // Marco LOMBARDO: should refer to local.
        jasperReport = compileReport(JasperDesignFile, jasperFile);
    }
    if (hasError)
        return null;
    // Marco LOMBARDO: this should happen only after compile the main report.
    if (this.jasperReport == null)
        this.jasperReport = jasperReport;
    if (// Marco LOMBARDO: should refer to local.
    jasperReport != null) {
        String[] extension = { ".xml", "jrxml" };
        File[] subreports = JasperDesignFile.getParentFile().listFiles(new FileFilter(JasperReportFile.replaceAll(".jasper", "") + "Subreport", JasperDesignFile.getParentFile(), extension));
        for (int i = 0; i < subreports.length; i++) {
            log.finest("The subreport file @ " + subreports[i].getAbsolutePath());
            File sub = processReport(subreports[i].getAbsolutePath());
            String subName = sub.getName();
            int pos = sub.getName().indexOf('.');
            if (pos != -1)
                subName = subName.substring(0, pos);
            subReport.put(subName, sub.getAbsolutePath());
            this.hasSubReport = true;
        }
    }
    if (hasError)
        return null;
    return jasperFile;
}
Also used : JRException(net.sf.jasperreports.engine.JRException) JasperReport(net.sf.jasperreports.engine.JasperReport) File(java.io.File)

Aggregations

JasperReport (net.sf.jasperreports.engine.JasperReport)18 JRException (net.sf.jasperreports.engine.JRException)11 JasperPrint (net.sf.jasperreports.engine.JasperPrint)10 HashMap (java.util.HashMap)6 PrismObject (com.evolveum.midpoint.prism.PrismObject)4 File (java.io.File)4 Connection (java.sql.Connection)4 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)3 JasperDesign (net.sf.jasperreports.engine.design.JasperDesign)3 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 ReportType (com.evolveum.midpoint.xml.ns._public.common.common_3.ReportType)2 SQLException (java.sql.SQLException)2 JRDesignExpression (net.sf.jasperreports.engine.design.JRDesignExpression)2 DigestOfFile (org.compiere.util.DigestOfFile)2 ReportException (org.opennms.api.reporting.ReportException)2 ModelState (com.evolveum.midpoint.model.api.context.ModelState)1 AbstractModelIntegrationTest (com.evolveum.midpoint.model.test.AbstractModelIntegrationTest)1 Item (com.evolveum.midpoint.prism.Item)1 PrismContainer (com.evolveum.midpoint.prism.PrismContainer)1 PrismProperty (com.evolveum.midpoint.prism.PrismProperty)1