KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > QueryApp


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.sql.Connection JavaDoc;
30 import java.sql.DriverManager JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import net.sf.jasperreports.engine.JRException;
36 import net.sf.jasperreports.engine.JRExporterParameter;
37 import net.sf.jasperreports.engine.JRParameter;
38 import net.sf.jasperreports.engine.JasperCompileManager;
39 import net.sf.jasperreports.engine.JasperExportManager;
40 import net.sf.jasperreports.engine.JasperFillManager;
41 import net.sf.jasperreports.engine.JasperPrint;
42 import net.sf.jasperreports.engine.JasperPrintManager;
43 import net.sf.jasperreports.engine.JasperRunManager;
44 import net.sf.jasperreports.engine.export.JExcelApiExporter;
45 import net.sf.jasperreports.engine.export.JRCsvExporter;
46 import net.sf.jasperreports.engine.export.JRRtfExporter;
47 import net.sf.jasperreports.engine.export.JRXlsExporter;
48 import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
49 import net.sf.jasperreports.engine.util.JRLoader;
50
51
52 /**
53  * @author Teodor Danciu (teodord@users.sourceforge.net)
54  * @version $Id: QueryApp.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
55  */

56 public class QueryApp
57 {
58
59
60     /**
61      *
62      */

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

267     private static void usage()
268     {
269         System.out.println( "QueryApp usage:" );
270         System.out.println( "\tjava QueryApp -Ttask -Ffile" );
271         System.out.println( "\tTasks : compile | fill | fillIgnorePagination | print | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv | run" );
272     }
273
274
275     /**
276      *
277      */

278     private static Connection JavaDoc getConnection() throws ClassNotFoundException JavaDoc, SQLException JavaDoc
279     {
280         //Change these settings according to your local configuration
281
String JavaDoc driver = "org.hsqldb.jdbcDriver";
282         String JavaDoc connectString = "jdbc:hsqldb:hsql://localhost";
283         String JavaDoc user = "sa";
284         String JavaDoc password = "";
285
286
287         Class.forName(driver);
288         Connection JavaDoc conn = DriverManager.getConnection(connectString, user, password);
289         return conn;
290     }
291
292
293 }
294
Popular Tags