Skip to content

Instantly share code, notes, and snippets.

@peteroyle
Created May 13, 2011 04:28
Show Gist options
  • Save peteroyle/969969 to your computer and use it in GitHub Desktop.
Save peteroyle/969969 to your computer and use it in GitHub Desktop.
SeamReports Example Usage
@Qualifier
public @interface Selected {
}
@RequestScoped
public class ReportOptions {
public String reportName;
public String format;
}
@ApplicationScoped
public class ReportInitialiser {
@Inject EntityManager em;
@Inject @JasperReports SeamReportLoader reportLoader;
public Map<String, SeamReport> compiledReports = new HashMap<String, SeamReport>();
// can non-extentions do this?
public void loadMappings(@Observes AfterBeanValidation e) {
List<ReportDetail> allReportDetails = em.findAll(ReportDetail.class);
for (ReportDetail reportDetail : allReportDetails) {
compileReport(reportDetail);
}
}
public void compileReport(@Observes @Modified ReportDetail reportDetail) {
SeamReport report = reportLoader.compile(reportDetail.getPath());
compiledReports.put(reportDetail.getName(), report);
}
@Produces @ApplicationScoped
public SeamReportDataSource produceDataSource(DataSource defaultDataSource) {
return new SeamReportDataSource(defaultDataSource);
}
@Produces @RequestScoped @Selected
public SeamReportInstance produceAppropriateReport(ReportOptions reportOptions, SeamReportDataSource reportDataSource) {
SeamReport report = compiledReports.get(reportOptions.reportName);
return report.fill(reportDataSource);
}
}
public class ReportServlet {
@Inject @Selected SeamReportInstance reportInstance;
@Inject ReportOptions reportOptions;
public void execute(Request req, Response resp) {
SeamReportOutputType outputType = SeamReportOutputType.forName(reportOptions.reportName);
if (reportInstance.isOutputTypeSupported(outputType)) {
reportInstance.render(outputType, resp.getOutputStream());
} else {
throw new Wobbly();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment