KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CrosstabApp


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 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.util.Calendar JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.sql.Connection JavaDoc;
32 import java.sql.DriverManager JavaDoc;
33 import java.sql.SQLException JavaDoc;
34
35 import net.sf.jasperreports.engine.JRException;
36 import net.sf.jasperreports.engine.JRExporterParameter;
37 import net.sf.jasperreports.engine.JasperExportManager;
38 import net.sf.jasperreports.engine.JasperFillManager;
39 import net.sf.jasperreports.engine.JasperPrint;
40 import net.sf.jasperreports.engine.JasperPrintManager;
41 import net.sf.jasperreports.engine.JasperRunManager;
42 import net.sf.jasperreports.engine.export.JExcelApiExporter;
43 import net.sf.jasperreports.engine.export.JRCsvExporter;
44 import net.sf.jasperreports.engine.export.JRRtfExporter;
45 import net.sf.jasperreports.engine.export.JRXlsExporter;
46 import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
47 import net.sf.jasperreports.engine.util.JRLoader;
48
49
50 /**
51  * @author Lucian Chirita (lucianc@users.sourceforge.net)
52  * @version $Id: CrosstabApp.java,v 1.9 2006/06/30 16:02:41 lucianc Exp $
53  */

54 public class CrosstabApp
55 {
56
57
58     /**
59      *
60      */

61     private static final String JavaDoc TASK_FILL = "fill";
62     private static final String JavaDoc TASK_PRINT = "print";
63     private static final String JavaDoc TASK_PDF = "pdf";
64     private static final String JavaDoc TASK_XML = "xml";
65     private static final String JavaDoc TASK_XML_EMBED = "xmlEmbed";
66     private static final String JavaDoc TASK_HTML = "html";
67     private static final String JavaDoc TASK_RTF = "rtf";
68     private static final String JavaDoc TASK_XLS = "xls";
69     private static final String JavaDoc TASK_JXL = "jxl";
70     private static final String JavaDoc TASK_CSV = "csv";
71     private static final String JavaDoc TASK_RUN = "run";
72     
73     private static final String JavaDoc[] reportNames = {
74         "OrdersReport",
75         "ProductsReport",
76         "ShipmentsReport",
77         "LateOrdersReport"
78     };
79     
80     /**
81      *
82      */

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

279     private static void usage()
280     {
281         System.out.println( "CrosstabApp usage:" );
282         System.out.println( "\tjava CrosstabApp -Ttask" );
283         System.out.println( "\tTasks : fill | print | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv | run" );
284     }
285
286
287     /**
288      *
289      */

290     private static Connection JavaDoc getConnection() throws ClassNotFoundException JavaDoc, SQLException JavaDoc
291     {
292         //Change these settings according to your local configuration
293
String JavaDoc driver = "org.hsqldb.jdbcDriver";
294         String JavaDoc connectString = "jdbc:hsqldb:hsql://localhost";
295         String JavaDoc user = "sa";
296         String JavaDoc password = "";
297
298
299         Class.forName(driver);
300         Connection JavaDoc conn = DriverManager.getConnection(connectString, user, password);
301         return conn;
302     }
303
304     
305     
306     public static final Date JavaDoc truncateToMonth(Date JavaDoc date)
307     {
308         Calendar JavaDoc calendar = Calendar.getInstance();
309         calendar.setTime(date);
310         int year = calendar.get(Calendar.YEAR);
311         int month = calendar.get(Calendar.MONTH);
312         calendar.clear();
313         calendar.set(Calendar.YEAR, year);
314         calendar.set(Calendar.MONTH, month);
315         return calendar.getTime();
316     }
317     
318     
319     public static final Date JavaDoc truncateToYear(Date JavaDoc date)
320     {
321         Calendar JavaDoc calendar = Calendar.getInstance();
322         calendar.setTime(date);
323         int year = calendar.get(Calendar.YEAR);
324         calendar.clear();
325         calendar.set(Calendar.YEAR, year);
326         return calendar.getTime();
327     }
328
329 }
330
Popular Tags