KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SubreportApp


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.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.JasperReport;
42 import net.sf.jasperreports.engine.JasperRunManager;
43 import net.sf.jasperreports.engine.export.JExcelApiExporter;
44 import net.sf.jasperreports.engine.export.JRCsvExporter;
45 import net.sf.jasperreports.engine.export.JRRtfExporter;
46 import net.sf.jasperreports.engine.export.JRXlsExporter;
47 import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
48 import net.sf.jasperreports.engine.util.JRLoader;
49
50
51 /**
52  * @author Teodor Danciu (teodord@users.sourceforge.net)
53  * @version $Id: SubreportApp.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
54  */

55 public class SubreportApp
56 {
57
58
59     /**
60      *
61      */

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

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

253     private static void usage()
254     {
255         System.out.println( "SubreportApp usage:" );
256         System.out.println( "\tjava SubreportApp -Ttask -Ffile" );
257         System.out.println( "\tTasks : fill | print | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv | run" );
258     }
259
260
261     /**
262      *
263      */

264     private static Connection JavaDoc getConnection() throws ClassNotFoundException JavaDoc, SQLException JavaDoc
265     {
266         //Change these settings according to your local configuration
267
String JavaDoc driver = "org.hsqldb.jdbcDriver";
268         String JavaDoc connectString = "jdbc:hsqldb:hsql://localhost";
269         String JavaDoc user = "sa";
270         String JavaDoc password = "";
271
272
273         Class.forName(driver);
274         Connection JavaDoc conn = DriverManager.getConnection(connectString, user, password);
275         return conn;
276     }
277
278
279 }
280
Popular Tags