Search in sources :

Example 1 with RendererFactory

use of org.activityinfo.server.report.renderer.RendererFactory in project activityinfo by bedatadriven.

the class ReportServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // first, authenticate the response
    AuthenticationDAO authDAO = injector.getInstance(AuthenticationDAO.class);
    Authentication auth = authDAO.findById(req.getParameter("auth"));
    if (auth == null) {
        resp.setStatus(500);
        return;
    }
    // now apply the user filters
    EntityManager em = injector.getInstance(EntityManager.class);
    DomainFilters.applyUserFilter(auth.getUser(), em);
    // load the report definition by id
    ReportDefinition template = em.find(ReportDefinition.class, Integer.parseInt(req.getParameter("id")));
    Report report = null;
    try {
        report = ReportParserJaxb.parseXml(template.getXml());
    } catch (Exception e) {
        e.printStackTrace();
        resp.setStatus(500);
    }
    // parse parameters
    DateRange dateRange = new DateRange();
    if (req.getParameter("minDate") != null) {
        dateRange.setMinDate(new Date(Long.parseLong(req.getParameter("minDate"))));
    }
    if (req.getParameter("maxDate") != null) {
        dateRange.setMaxDate(new Date(Long.parseLong(req.getParameter("maxDate"))));
    }
    // generate the report content
    ReportGenerator gtor = injector.getInstance(ReportGenerator.class);
    gtor.generate(auth.getUser(), report, null, dateRange);
    // render in the requested format
    RenderElement.Format format = RenderElement.Format.valueOf(req.getParameter("format"));
    RendererFactory factory = injector.getInstance(RendererFactory.class);
    Renderer renderer = factory.get(format);
    // composefile name
    String filename = report.getContent().getFileName() + renderer.getFileSuffix();
    resp.setContentType(renderer.getMimeType());
    resp.addHeader("Content-Disposition", "attachment; filename=" + filename);
    try {
        renderer.render(report, resp.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
        resp.setStatus(500);
    }
}
Also used : RenderElement(org.activityinfo.shared.command.RenderElement) Report(org.activityinfo.shared.report.model.Report) AuthenticationDAO(org.activityinfo.server.database.hibernate.dao.AuthenticationDAO) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Date(java.util.Date) EntityManager(javax.persistence.EntityManager) DateRange(org.activityinfo.shared.report.model.DateRange) ReportGenerator(org.activityinfo.server.report.generator.ReportGenerator) RendererFactory(org.activityinfo.server.report.renderer.RendererFactory) Authentication(org.activityinfo.server.database.hibernate.entity.Authentication) Renderer(org.activityinfo.server.report.renderer.Renderer) ReportDefinition(org.activityinfo.server.database.hibernate.entity.ReportDefinition)

Aggregations

IOException (java.io.IOException)1 Date (java.util.Date)1 EntityManager (javax.persistence.EntityManager)1 ServletException (javax.servlet.ServletException)1 AuthenticationDAO (org.activityinfo.server.database.hibernate.dao.AuthenticationDAO)1 Authentication (org.activityinfo.server.database.hibernate.entity.Authentication)1 ReportDefinition (org.activityinfo.server.database.hibernate.entity.ReportDefinition)1 ReportGenerator (org.activityinfo.server.report.generator.ReportGenerator)1 Renderer (org.activityinfo.server.report.renderer.Renderer)1 RendererFactory (org.activityinfo.server.report.renderer.RendererFactory)1 RenderElement (org.activityinfo.shared.command.RenderElement)1 DateRange (org.activityinfo.shared.report.model.DateRange)1 Report (org.activityinfo.shared.report.model.Report)1