5 Replies Latest reply on May 23, 2007 8:17 AM by Deepak Solasa

    Generation of jasper reports in seam

    Deepak Solasa Newbie

      Hi

      I struck at the point to generate the jasper reports in seam ,ofcourse in jboss too.i even used the component that is given in the jboss site (but that was depricated).And i tried in all the ways that am supposed to generate the jasper reports in java.May be i think i need a jar file or any other configuration to generate jaspe reports in seam..

      plz be help me..So urgent that i need to implement it in my project

        • 1. Re: Generation of jasper reports in seam
          Daniel Young Novice

          Hi,

          I wrote my own JSF component that can view a report inside a div on a given page, and also export a report in a number of output formats. I can't really share the component at this stage, but these snippets should help you get something up and running. Hopefully in the future I can make my component more generic and get permission to contribute it in full.

          To view the report, firstly I create the JasperPrint object which is the result of filling the report (JasperFillManager.fillReport()). The following code is inside my customer JSF component and the context variable you see is the FacesContext... however the export gets triggered directly from a Seam action, where you can inject the FacesContext using @In, so you should be able to do this without having to write a JSF component.

           if (jasperPrint != null) {
           final JRExporter exporter = new JRHtmlExporter();
           exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
           exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, context.getResponseWriter());
           exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "");
           exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "");
          
           // images
           final HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
           request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,
           jasperPrint);
           exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, new HashMap<Object, Object>());
           exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,
           request.getContextPath() + "/jasperimg?image=");
          
           // export
           try {
           context.getResponseWriter().write("<div class=\"reportview\">");
           exporter.exportReport();
           context.getResponseWriter().write("</div>");
           } catch (final JRException e) {
           LOG.error("Report exporting error", e);
           .....
           }
           }
          


          To export the report in a whole new response
           final JRExporter exporter = JRExporterFactory.getExporter(outputType, jasperPrint, context);
           final ByteArrayOutputStream stream = new ByteArrayOutputStream();
           exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, stream);
          
           // export
           try {
           exporter.exportReport();
           } catch (final JRException e) {
           LOG.error("Report exporting error", e);
           ....
           }
           final HttpServletResponse response =
           (HttpServletResponse) context.getExternalContext().getResponse();
           try {
           final OutputStream responseStream = response.getOutputStream();
           response.setContentLength(stream.size());
           response.setContentType(contentTypes.get(outputType));
           if (outputType != ReportOutputType.HTML) {
           response.setHeader("Content-Disposition", "attachment; filename=report." + outputType);
           }
           responseStream.write(stream.toByteArray());
           responseStream.flush();
           responseStream.close();
           response.flushBuffer();
           } catch (final IOException e) {
           LOG.error(e);
           ....
           }
           context.responseComplete();
          
          


          I hope this helps you to get it working,
          Daniel.



          "d.solasa" wrote:
          Hi

          I struck at the point to generate the jasper reports in seam ,ofcourse in jboss too.i even used the component that is given in the jboss site (but that was depricated).And i tried in all the ways that am supposed to generate the jasper reports in java.May be i think i need a jar file or any other configuration to generate jaspe reports in seam..

          plz be help me..So urgent that i need to implement it in my project


          • 2. Re: Generation of jasper reports in seam
            Deepak Solasa Newbie

            Hi Daniel
            Thanks for ur response,i will try the same in my code .

            • 3. Re: Generation of jasper reports in seam
              Deepak Solasa Newbie

              Hi Daniel..
              iTried to implement ur code but I am getting the exception as
              12:18:41,966 INFO [JasperReporter] Exceptionnet.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports
              ngine.JRException: Class "net.sf.jasperreports.engine.query.JRJdbcQueryExecuterFactory" should be compatible with "net.sf.jasperreports.engine.query.JRQue
              ExecuterFactory" even when i tried to compile my report .so even i am unable to get the print object.
              my code snippet is:
              InputStream input = this.getClass().getClassLoader().getResourceAsStream(reportName);
              JasperDesign design = JRXmlLoader.load(input);
              JasperReport report = JasperCompileManager.compileReport(design);
              InitialContext initialContext = new InitialContext();
              DataSource ds = (DataSource) initialContext.lookup("java:/claims");
              conn = ds.getConnection();
              JasperPrint print = JasperFillManager.fillReport(report, parameters, conn);
              String fileName = "D:/downloads/ireport/" + showType;
              OutputStream output = new FileOutputStream(new File(fileName));
              JasperExportManager.exportReportToPdfStream(print, output);

              plz help me

              • 4. Re: Generation of jasper reports in seam
                Daniel Young Novice

                Sorry, I can't be much help with that, I've never seen anything like it. I'm running my query in my own code and using a custom data source, so I've never dealt with these executer factory classes.. This seems to be a pure Jasper question, not really about using Jasper with Seam, so you may have more luck asking elsewhere.

                One thing that you could try is compiling the report first using the jrc ant target instead of compiling it when you use it in your app. You really should be doing that anyway.

                good luck

                "d.solasa" wrote:
                Hi Daniel..
                iTried to implement ur code but I am getting the exception as
                12:18:41,966 INFO [JasperReporter] Exceptionnet.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports
                ngine.JRException: Class "net.sf.jasperreports.engine.query.JRJdbcQueryExecuterFactory" should be compatible with "net.sf.jasperreports.engine.query.JRQue
                ExecuterFactory" even when i tried to compile my report .so even i am unable to get the print object.
                my code snippet is:
                InputStream input = this.getClass().getClassLoader().getResourceAsStream(reportName);
                JasperDesign design = JRXmlLoader.load(input);
                JasperReport report = JasperCompileManager.compileReport(design);
                InitialContext initialContext = new InitialContext();
                DataSource ds = (DataSource) initialContext.lookup("java:/claims");
                conn = ds.getConnection();
                JasperPrint print = JasperFillManager.fillReport(report, parameters, conn);
                String fileName = "D:/downloads/ireport/" + showType;
                OutputStream output = new FileOutputStream(new File(fileName));
                JasperExportManager.exportReportToPdfStream(print, output);

                plz help me


                • 5. Re: Generation of jasper reports in seam
                  Deepak Solasa Newbie

                  Hi Daniel,
                  i think that jboss or seam may internally supports jasper libraries ,due to which i m getting the exception.so plz me the way i follow to use jasper reports in seam as I didnt get clear with u r code and u way of approach. EVen i tried it using this way
                  JasperRunManager.runReportToPdfStream(input,facesCtx.getResponseStream(),parameters,conn);