View Javadoc
1   package fr.ifremer.tutti.report;
2   
3   /*
4    * #%L
5    * Tutti :: Report Generator
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import org.eclipse.birt.core.exception.BirtException;
28  import org.eclipse.birt.core.framework.Platform;
29  import org.eclipse.birt.report.engine.api.EngineConfig;
30  import org.eclipse.birt.report.engine.api.EngineConstants;
31  import org.eclipse.birt.report.engine.api.EngineException;
32  import org.eclipse.birt.report.engine.api.IRenderOption;
33  import org.eclipse.birt.report.engine.api.IReportEngine;
34  import org.eclipse.birt.report.engine.api.IReportEngineFactory;
35  import org.eclipse.birt.report.engine.api.IReportRunnable;
36  import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
37  import org.eclipse.birt.report.engine.api.RenderOption;
38  
39  import java.io.Closeable;
40  import java.io.File;
41  import java.util.HashMap;
42  import java.util.Map;
43  import java.util.logging.Level;
44  
45  /**
46   * Created on 3/10/15.
47   *
48   * @author Tony Chemit - chemit@codelutin.com
49   * @since 3.13.2
50   */
51  public class ReportEngine implements Closeable {
52  
53      private static final int RENDER_DPI = 300;
54  
55      private final IReportEngine engine;
56  
57      public ReportEngine(File reportHomeDirectory, File reportLogDirectory) {
58  
59          EngineConfig engineConfig = new EngineConfig();
60          engineConfig.setBIRTHome(reportHomeDirectory.getAbsolutePath());
61          engineConfig.setResourcePath(reportHomeDirectory.getAbsolutePath());
62          engineConfig.setLogConfig(reportLogDirectory.getAbsolutePath(), Level.ALL);
63  
64          try {
65              Platform.startup(engineConfig);
66          } catch (BirtException e) {
67              throw new RuntimeException("Could not init birt", e);
68          }
69  
70          IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
71  
72          engine = factory.createReportEngine(engineConfig);
73  
74      }
75  
76      public void generate(ReportConfig reportConfig) {
77  
78          IReportRunnable rptDoc;
79  
80          try {
81              rptDoc = engine.openReportDesign(reportConfig.getReportFile().getAbsolutePath());
82          } catch (EngineException e) {
83  
84              throw new RuntimeException("Could not get report " + reportConfig.getReportFile(), e);
85  
86          }
87  
88          /**
89           * rpt-param_dirfile : répertoire contant les fichiers
90           + rpt-param_codestation : code de la station (ex: G101)
91           + rpt-param_numerotrait : code du trait (ex: 53)
92           */
93  
94          // Create the render task
95          IRunAndRenderTask task = engine.createRunAndRenderTask(rptDoc);
96  
97          try {
98  
99              // Configure the render options
100             IRenderOption renderOption = new RenderOption();
101             renderOption.setOutputFileName(reportConfig.getOutputFile().getAbsolutePath());
102             renderOption.setOutputFormat(IRenderOption.OUTPUT_FORMAT_PDF);
103 
104             task.setRenderOption(renderOption);
105 
106             // Report context
107             Map<String, Object> context = new HashMap<>();
108             context.put(EngineConstants.APPCONTEXT_CHART_RESOLUTION, RENDER_DPI);
109 
110             task.setAppContext(context);
111             task.setParameterValue("rpt-param_dirfile", reportConfig.getDataDirectory().getAbsolutePath());
112             task.setParameterValue("rpt-param_codestation", reportConfig.getStationNumber());
113             task.setParameterValue("rpt-param_numerotrait", reportConfig.getFishingOperationNumber());
114 
115             task.run();
116 
117         } catch (EngineException e) {
118 
119             throw new RuntimeException("Could not execute report", e);
120 
121         } finally {
122 
123             // close task
124             task.close();
125 
126         }
127 
128     }
129 
130     @Override
131     public void close() {
132 
133         try {
134             engine.destroy();
135         } finally {
136 
137             Platform.shutdown();
138 
139         }
140 
141     }
142 }