2013-03-22 5 views
5

का उपयोग कर रिपोर्ट उत्पन्न करने के लिए जावा कोड मेरे पास XML के रूप में कुछ डेटा है जिसमें कुछ रिकॉर्ड हैं और एक आरपीटी फ़ाइल भी बनाई गई है।बीआईआरटी

तो अब, मैं इनपुट पैरामीटर के रूप में एक्सएमएल और आरपीटी पास करके रिपोर्ट उत्पन्न करने के लिए बिर्ट कैसे कॉल कर सकता हूं।

मैं बीआईआरटी के लिए बिल्कुल नया हूं। किसी भी कोड उदाहरण की सराहना की जाएगी। BirtEngine.java:

+0

रहे हैं अच्छा ट्यूटोरिया बीआईआरटी सॉफ्टवेयर की मदद सामग्री में ls। इसके अलावा आप कोड लिखते हैं (अन्य तब एसक्यूएल) आप जावा या जावास्क्रिप्ट लिख सकते हैं। –

+0

एक ऐसा एपीआई है जो सीधे जावा से बीआईआरटी रिपोर्ट तैयार करने में मदद करता है: https://mvnrepository.com/artifact/net.sf.automatic-report-generator/birt- जनरेटर। Log4j और v1.1 के साथ v1.0 काम करता है Log4J2 के साथ काम करता है। साथ ही, स्रोत फोर्ज पेज यहां देखें: https://sourceforge.net/projects/automatic-report-generator/ –

उत्तर

6

इस कोशिश

public class BirtEngine { 
    private static IReportEngine birtEngine = null; 
    private static Properties configProps = new Properties(); 
    private final static String configFile = "BirtConfig.properties"; 
    public static synchronized void initBirtConfig() { 
     loadEngineProps(); 
    } 
    public static synchronized IReportEngine getBirtEngine(ServletContext sc) { 
     if (birtEngine == null) 
     { 
      EngineConfig config = new EngineConfig(); 
      if(configProps != null){ 
       String logLevel = configProps.getProperty("logLevel"); 
       Level level = Level.OFF; 
       if ("SEVERE".equalsIgnoreCase(logLevel)) 
       { 
        level = Level.SEVERE; 
       } else if ("WARNING".equalsIgnoreCase(logLevel)) 
       { 
        level = Level.WARNING; 
       } else if ("INFO".equalsIgnoreCase(logLevel)) 
       { 
        level = Level.INFO; 
       } else if ("CONFIG".equalsIgnoreCase(logLevel)) 
       { 
        level = Level.CONFIG; 
       } else if ("FINE".equalsIgnoreCase(logLevel)) 
       { 
        level = Level.FINE; 
       } else if ("FINER".equalsIgnoreCase(logLevel)) 
       { 
        level = Level.FINER; 
       } else if ("FINEST".equalsIgnoreCase(logLevel)) 
       { 
        level = Level.FINEST; 
       } else if ("OFF".equalsIgnoreCase(logLevel)) 
       { 
        level = Level.OFF; 
       } 

       config.setLogConfig(configProps.getProperty("logDirectory"), level); 
      } 

      config.setEngineHome(""); 
      IPlatformContext context = new PlatformServletContext(sc); 
      //IPlatformContext context = new PlatformFileContext(); 
      config.setPlatformContext(context); 
      //Create the report engine 
      //birtEngine = new ReportEngine(config); 
      //ReportEngine engine = new ReportEngine(null); 
      try 
      { 
       Platform.startup(config); 
      } 
      catch (BirtException e) 
      { 
       e.printStackTrace(); 
      } 
      IReportEngineFactory factory = (IReportEngineFactory) Platform 
      .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY); 
      birtEngine = factory.createReportEngine(config); 
     } 
     return birtEngine; 
    } 
    public static synchronized void destroyBirtEngine() { 
     if (birtEngine == null) { 
      return; 
     }  
     birtEngine.destroy(); 
     Platform.shutdown(); 
     birtEngine = null; 
    } 
    public Object clone() throws CloneNotSupportedException { 
     throw new CloneNotSupportedException(); 
    } 
    private static void loadEngineProps() { 
     try { 
      //Config File must be in classpath 
      ClassLoader cl = Thread.currentThread().getContextClassLoader(); 
      InputStream in = null; 
      in = cl.getResourceAsStream (configFile); 
      configProps.load(in); 
      in.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

webreport.java:

public class WebReport extends HttpServlet { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    /** 
    * Constructor of the object. 
    */ 
    private IReportEngine birtReportEngine = null; 
    protected static Logger logger = Logger.getLogger("org.eclipse.birt"); 
    public WebReport() { 
     super(); 
    } 
    /** 
    * Destruction of the servlet. <br> 
    */ 
    public void destroy() { 
     super.destroy(); 
     BirtEngine.destroyBirtEngine(); 
    } 
    /** 
    * The doGet method of the servlet. <br> 
    * 
    */ 
    public void doGet(HttpServletRequest req, HttpServletResponse resp) 
      throws ServletException, IOException { 
     //get report name and launch the engine 
     //resp.setContentType("text/html"); 
     //resp.setContentType("application/pdf"); 
     resp.setContentType("application/msword"); 
     //resp.setHeader ("Content-Disposition","inline; filename=test.pdf"); 
     resp.setHeader("Content-disposition","attachment; filename=\"" +"test.doc" +"\""); 
     String reportName = req.getParameter("ReportName"); 
     ServletContext sc = req.getSession().getServletContext(); 
     this.birtReportEngine = BirtEngine.getBirtEngine(sc); 
     IReportRunnable design; 
     try 
     { 
      //Open report design 
      design = birtReportEngine.openReportDesign(sc.getRealPath("/Reports")+"/"+reportName); 
      //create task to run and render report 
      IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask(design);  
      task.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, WebReport.class.getClassLoader()); 
      task.getAppContext().put("BIRT_VIEWER_HTTPSERVLET_REQUEST", req);   
      //set output options 
      //HTMLRenderOption options = new HTMLRenderOption(); 
      //options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML); 
      //options.setOutputStream(resp.getOutputStream()); 
      //options.setImageHandler(new HTMLServerImageHandler()); 
      //options.setBaseImageURL(req.getContextPath()+"/images"); 
      //options.setImageDirectory(sc.getRealPath("/images")); 
      /*PDFRenderOption options = new PDFRenderOption(); 
      options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);  
      options.setOutputStream(resp.getOutputStream()); 
      */ 
      RenderOption options = new RenderOption(); 
      options.setOutputFormat("doc"); 
      options.setOutputStream(resp.getOutputStream()); 
      //options.setEnableAgentStyleEngine(true); 
      //options.setEnableInlineStyle(true); 
      task.setRenderOption(options); 
      //run report 
      task.run(); 
      task.close(); 
     }catch (Exception e){ 
     e.printStackTrace(); 
      throw new ServletException(e); 
     } 
    } 
    /** 
    * The doPost method of the servlet. <br> 
    * 
    */ 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html"); 
     PrintWriter out = response.getWriter(); 
     out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 
     out.println("<HTML>"); 
     out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 
     out.println(" <BODY>"); 
     out.println(" Post Not Supported"); 
     out.println(" </BODY>"); 
     out.println("</HTML>"); 
     out.flush(); 
     out.close(); 
    } 
    /** 
    * Initialization of the servlet. <br> 
    * 
    * @throws ServletException if an error occure 
    */ 
    public void init(ServletConfig sc) throws ServletException { 
     BirtEngine.initBirtConfig(); 
     this.birtReportEngine = BirtEngine.getBirtEngine(sc.getServletContext()); 
    } 
} 

birtconfigproperty:

ogDirectory=c:/temp 
logLevel=FINEST 
+0

कोई विवरण बिल्कुल नहीं। क्या आप कुछ विवरण नहीं देते हैं। – vigamage

संबंधित मुद्दे