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());
}
}
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);
}
}
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);
}
}
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;
}
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;
}
Aggregations