Anda di halaman 1dari 4

/** * el BIRT report engine */ public class BirtEngine { private static IReportEngine birtEngine = null; private static Properties

properties = null; private static String path = null; ... public static synchronized IReportEngine getBirtEngine(ServletContext sc) { if (birtEngine == null) { EngineConfig config = new EngineConfig(); Level logLevel = Level.OFF; String engineHome = new String(); String logDir = new String(); /* mtodo de la clase donde cargaremos desde un * properties las propiedades necesarias para * configurar el Engine, como son: * logLevel=ALL * logDir=C:\\temp\\logs * engineDir=\\birt-runtime-2_6_1\\ReportEngine * reports.dir=\\WEB-INF\\Reports\\ */ loadProperties(); //cargamos la configuracion del motor if( properties != null) { String level = properties.getProperty("logLevel"); if ("SEVERE".equalsIgnoreCase(level)) { logLevel = Level.SEVERE; } else if ("WARNING".equalsIgnoreCase(level)) { logLevel = Level.WARNING; } else if ("INFO".equalsIgnoreCase(level)) { logLevel = Level.INFO; } else if ("CONFIG".equalsIgnoreCase(level)) { logLevel = Level.CONFIG; } else if ("FINE".equalsIgnoreCase(level)) { logLevel = Level.FINE; } else if ("FINER".equalsIgnoreCase(level)) { logLevel = Level.FINER; } else if ("FINEST".equalsIgnoreCase(level))

{ logLevel = Level.FINEST; } else if ("OFF".equalsIgnoreCase(level)) { logLevel = Level.OFF; } else if ("ALL".equalsIgnoreCase(level)) { logLevel = Level.ALL; } logDir = properties.getProperty("logDir"); engineHome = sc.getRealPath(properties. getProperty("engineDir")); path = properties.getProperty("reports.dir")+"\\"; } config.setEngineHome(engineHome); config.setLogConfig(logDir, logLevel); try { Platform.startup( config ); } catch ( BirtException e ) { e.printStackTrace( ); } IReportEngineFactory factory = (IReportEngineFactory) Platform .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FA CTORY ); birtEngine = factory.createReportEngine( config ); } } ... return birtEngine;

/** * el servlet que acepta las peticiones */ public class BirtServlet extends HttpServlet { /** * Define el engine */ private IReportEngine birtReportEngine = null; ... public void init() throws ServletException { logger.info("Iniciando BIRTServlet..."); BirtEngine.initBirtConfig(); logger.info("BIRTServlet iniciado");

} public void destroy() { super.destroy(); BirtEngine.destroyBirtEngine(); } @SuppressWarnings("unchecked") public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //get report name and launch the engine response.setContentType( "application/pdf" ); response.setHeader ("Content-Disposition","inline; filename=test.pdf"); ServletContext sc = request.getSession().getServletContext(); //el servlet recibe los datos leyendo de la sesin ParametrosReport parametros = (ParametrosReport)request.getSession().getAttribute(ConstantesBIRT.PAR AMETROS_BIRT); HashMap<String,Object> context = (HashMap<String,Object>)request.getSession().getAttribute(ConstantesBI RT.DATOS_BIRT); IReportRunnable design; try { //inicializamos el Engine desde nuestra clase BirtEngine this.birtReportEngine = BirtEngine.getBirtEngine(sc); //Open report design design = birtReportEngine.openReportDesign(sc.getRealPath(BirtEngine.getPath() +parametros.getPlantilla())); //create task to run and render report IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask( design ); task.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, BirtServlet.class.getClassLoader()); //Set parameter values and validate if( parametros.getParametros() != null ) { task.setParameterValues(parametros.getParametros()); } task.validateParameters();

//ponemos el contexto task.setAppContext(context); //set output options HTMLRenderOption options = new HTMLRenderOption(); //forzamos el PDF options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF); options.setOutputStream(response.getOutputStream()); task.setRenderOption(options);

//run report task.run(); task.close(); } catch (Exception e){ e.printStackTrace(); throw new ServletException( e ); } finally { } ... } }