KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MondrianApp


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2005 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.FileNotFoundException JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 import mondrian.olap.Connection;
37 import mondrian.olap.DriverManager;
38 import net.sf.jasperreports.engine.JRException;
39 import net.sf.jasperreports.engine.JRExporterParameter;
40 import net.sf.jasperreports.engine.JasperExportManager;
41 import net.sf.jasperreports.engine.JasperFillManager;
42 import net.sf.jasperreports.engine.JasperPrint;
43 import net.sf.jasperreports.engine.JasperPrintManager;
44 import net.sf.jasperreports.engine.JasperRunManager;
45 import net.sf.jasperreports.engine.export.JExcelApiExporter;
46 import net.sf.jasperreports.engine.export.JRCsvExporter;
47 import net.sf.jasperreports.engine.export.JRRtfExporter;
48 import net.sf.jasperreports.engine.export.JRXlsExporter;
49 import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
50 import net.sf.jasperreports.engine.util.JRLoader;
51 import net.sf.jasperreports.olap.JRMondrianQueryExecuterFactory;
52
53
54 /**
55  * @author Teodor Danciu (teodord@users.sourceforge.net)
56  * @version $Id: MondrianApp.java 1247 2006-05-05 19:13:59 +0300 (Fri, 05 May 2006) lucianc $
57  */

58 public class MondrianApp
59 {
60
61
62     /**
63      *
64      */

65     private static final String JavaDoc TASK_FILL = "fill";
66     private static final String JavaDoc TASK_PRINT = "print";
67     private static final String JavaDoc TASK_PDF = "pdf";
68     private static final String JavaDoc TASK_XML = "xml";
69     private static final String JavaDoc TASK_XML_EMBED = "xmlEmbed";
70     private static final String JavaDoc TASK_HTML = "html";
71     private static final String JavaDoc TASK_RTF = "rtf";
72     private static final String JavaDoc TASK_XLS = "xls";
73     private static final String JavaDoc TASK_JXL = "jxl";
74     private static final String JavaDoc TASK_CSV = "csv";
75     private static final String JavaDoc TASK_RUN = "run";
76     
77     
78     /**
79      *
80      */

81     public static void main(String JavaDoc[] args)
82     {
83         String JavaDoc fileName = null;
84         String JavaDoc taskName = null;
85         String JavaDoc propertiesFileName = null;
86
87         if(args.length == 0)
88         {
89             usage();
90             return;
91         }
92                 
93         int k = 0;
94         while ( args.length > k )
95         {
96             if ( args[k].startsWith("-T") )
97                 taskName = args[k].substring(2);
98             if ( args[k].startsWith("-F") )
99                 fileName = args[k].substring(2);
100             if ( args[k].startsWith("-P") )
101                 propertiesFileName = args[k].substring(2);
102             
103             k++;
104         }
105
106         try
107         {
108             long start = System.currentTimeMillis();
109             if (TASK_FILL.equals(taskName))
110             {
111                 Connection conn = getConnection(propertiesFileName);
112                 try
113                 {
114                     Map JavaDoc parameters = new HashMap JavaDoc();
115                     parameters.put(JRMondrianQueryExecuterFactory.PARAMETER_MONDRIAN_CONNECTION, conn);
116                     
117                     JasperFillManager.fillReportToFile(fileName, parameters);
118                     System.err.println("Filling time : " + (System.currentTimeMillis() - start));
119                 }
120                 finally
121                 {
122                     conn.close();
123                 }
124             }
125             else if (TASK_PRINT.equals(taskName))
126             {
127                 JasperPrintManager.printReport(fileName, true);
128                 System.err.println("Printing time : " + (System.currentTimeMillis() - start));
129             }
130             else if (TASK_PDF.equals(taskName))
131             {
132                 JasperExportManager.exportReportToPdfFile(fileName);
133                 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start));
134             }
135             else if (TASK_XML.equals(taskName))
136             {
137                 JasperExportManager.exportReportToXmlFile(fileName, false);
138                 System.err.println("XML creation time : " + (System.currentTimeMillis() - start));
139             }
140             else if (TASK_XML_EMBED.equals(taskName))
141             {
142                 JasperExportManager.exportReportToXmlFile(fileName, true);
143                 System.err.println("XML creation time : " + (System.currentTimeMillis() - start));
144             }
145             else if (TASK_HTML.equals(taskName))
146             {
147                 JasperExportManager.exportReportToHtmlFile(fileName);
148                 System.err.println("HTML creation time : " + (System.currentTimeMillis() - start));
149             }
150             else if (TASK_RTF.equals(taskName))
151             {
152                 File JavaDoc sourceFile = new File JavaDoc(fileName);
153         
154                 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
155         
156                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".rtf");
157                 
158                 JRRtfExporter exporter = new JRRtfExporter();
159                 
160                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
161                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
162                 
163                 exporter.exportReport();
164
165                 System.err.println("RTF creation time : " + (System.currentTimeMillis() - start));
166             }
167             else if (TASK_XLS.equals(taskName))
168             {
169                 File JavaDoc sourceFile = new File JavaDoc(fileName);
170         
171                 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
172         
173                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".xls");
174                 
175                 JRXlsExporter exporter = new JRXlsExporter();
176                 
177                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
178                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
179                 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
180                 
181                 exporter.exportReport();
182
183                 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start));
184             }
185             else if (TASK_JXL.equals(taskName))
186             {
187                 File JavaDoc sourceFile = new File JavaDoc(fileName);
188
189                 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
190
191                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".jxl.xls");
192
193                 JExcelApiExporter exporter = new JExcelApiExporter();
194
195                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
196                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
197                 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
198
199                 exporter.exportReport();
200
201                 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start));
202                 System.exit(0);
203             }
204             else if (TASK_CSV.equals(taskName))
205             {
206                 File JavaDoc sourceFile = new File JavaDoc(fileName);
207         
208                 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
209         
210                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".csv");
211                 
212                 JRCsvExporter exporter = new JRCsvExporter();
213                 
214                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
215                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
216                 
217                 exporter.exportReport();
218
219                 System.err.println("CSV creation time : " + (System.currentTimeMillis() - start));
220             }
221             else if (TASK_RUN.equals(taskName))
222             {
223                 Connection conn = getConnection(propertiesFileName);
224                 try
225                 {
226                     Map JavaDoc parameters = new HashMap JavaDoc();
227                     parameters.put(JRMondrianQueryExecuterFactory.PARAMETER_MONDRIAN_CONNECTION, conn);
228                     
229                     JasperRunManager.runReportToPdfFile(fileName, parameters);
230                     System.err.println("PDF running time : " + (System.currentTimeMillis() - start));
231                 }
232                 finally
233                 {
234                     conn.close();
235                 }
236             }
237             else
238             {
239                 usage();
240             }
241         }
242         catch (JRException e)
243         {
244             e.printStackTrace();
245             System.exit(1);
246         }
247         catch (Exception JavaDoc e)
248         {
249             e.printStackTrace();
250             System.exit(1);
251         }
252     }
253
254
255     /**
256      *
257      */

258     private static void usage()
259     {
260         System.out.println( "MondrianApp usage:" );
261         System.out.println( "\tjava MondrianApp -Ttask -Ffile -Pproperties" );
262         System.out.println( "\tTasks : fill | print | pdf | xml | xmlEmbed | html | rtf | xls | csv | run" );
263         System.out.println( "\tproperties : properties file for Mondrian connection" );
264     }
265
266
267     private static Connection getConnection(String JavaDoc propertiesFileName) throws FileNotFoundException JavaDoc, IOException JavaDoc
268     {
269         if (propertiesFileName == null) {
270             throw new RuntimeException JavaDoc("connection properties file not set");
271         }
272         ConnectionData data = getConnectionData(propertiesFileName);
273         Connection connection =
274             DriverManager.getConnection(
275                     "Provider=mondrian;" +
276                     "JdbcDrivers=" + data.getJdbcDrivers() + ";" +
277                     "Jdbc=" + data.getJdbcUrl() + ";" +
278                     "JdbcUser=" + data.getJdbcUser() + ";" +
279                     "JdbcPassword=" + data.getJdbcPassword() + ";" +
280                     "Catalog=" + data.getCatalogUri() + ";",
281                     null, false);
282         
283         return connection;
284     }
285
286     private static Properties JavaDoc loadConnectionProperties(String JavaDoc path) throws FileNotFoundException JavaDoc, IOException JavaDoc
287     {
288         File JavaDoc connectionFile = new File JavaDoc(path);
289         Properties JavaDoc properties = new Properties JavaDoc();
290         properties.load(new FileInputStream JavaDoc(connectionFile));
291         return properties;
292     }
293
294     private static ConnectionData getConnectionData(String JavaDoc propertiesFileName) throws IOException JavaDoc
295     {
296         Properties JavaDoc properties = loadConnectionProperties(propertiesFileName);
297
298         ConnectionData data = (new MondrianApp()).new ConnectionData();
299         String JavaDoc prop;
300
301         String JavaDoc dataSourceName = properties.getProperty("dataSourceName");
302         if (dataSourceName == null || dataSourceName.length() == 0) {
303             prop = properties.getProperty("jdbcDrivers");
304             if (prop != null && prop.length() > 0)
305                 data.setJdbcDrivers(prop);
306             else
307                 throw new RuntimeException JavaDoc("Invalid JDBC driver");
308
309             prop = properties.getProperty("jdbcUrl");
310             if (prop != null && prop.length() > 0)
311                 data.setJdbcUrl(prop);
312             else
313                 throw new RuntimeException JavaDoc("Invalid JDBC URL");
314
315             prop = properties.getProperty("jdbcUser");
316             if (prop != null && prop.length() > 0)
317                 data.setJdbcUser(prop);
318             else
319                 data.setJdbcUser("");
320
321             prop = properties.getProperty("jdbcPassword");
322             if (prop != null && prop.length() > 0)
323                 data.setJdbcPassword(prop);
324             else
325                 data.setJdbcPassword("");
326         }
327         else
328             data.setDataSourceName(dataSourceName);
329
330         prop = properties.getProperty("catalogUri");
331         if (prop != null && prop.length() > 0) {
332             data.setCatalogUri(prop);
333         } else
334             throw new RuntimeException JavaDoc("Invalid catalog URI");
335
336         return data;
337     }
338
339     
340     /**
341      * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
342      * @version $Id
343      */

344     private class ConnectionData
345     {
346         private String JavaDoc jdbcDrivers;
347         private String JavaDoc jdbcUrl;
348         private String JavaDoc jdbcUser;
349         private String JavaDoc jdbcPassword;
350         private String JavaDoc dataSourceName;
351         private String JavaDoc catalogUri;
352
353         public ConnectionData()
354         {
355         }
356
357         public String JavaDoc getCatalogUri()
358         {
359             return catalogUri;
360         }
361
362         public void setCatalogUri(String JavaDoc catalogUri)
363         {
364             this.catalogUri = catalogUri;
365         }
366
367         public String JavaDoc getDataSourceName()
368         {
369             return dataSourceName;
370         }
371
372         public void setDataSourceName(String JavaDoc dataSourceName)
373         {
374             this.dataSourceName = dataSourceName;
375         }
376
377         public String JavaDoc getJdbcDrivers()
378         {
379             return jdbcDrivers;
380         }
381
382         public void setJdbcDrivers(String JavaDoc jdbcDrivers)
383         {
384             this.jdbcDrivers = jdbcDrivers;
385         }
386
387         public String JavaDoc getJdbcPassword()
388         {
389             return jdbcPassword;
390         }
391
392         public void setJdbcPassword(String JavaDoc jdbcPassword)
393         {
394             this.jdbcPassword = jdbcPassword;
395         }
396
397         public String JavaDoc getJdbcUrl()
398         {
399             return jdbcUrl;
400         }
401
402         public void setJdbcUrl(String JavaDoc jdbcUrl)
403         {
404             this.jdbcUrl = jdbcUrl;
405         }
406
407         public String JavaDoc getJdbcUser()
408         {
409             return jdbcUser;
410         }
411
412         public void setJdbcUser(String JavaDoc jdbcUser)
413         {
414             this.jdbcUser = jdbcUser;
415         }
416     }
417 }
418
Popular Tags