KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HibernateApp


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.ArrayList JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import org.hibernate.Session;
38 import org.hibernate.SessionFactory;
39 import org.hibernate.Transaction;
40 import org.hibernate.cfg.Configuration;
41
42 import net.sf.jasperreports.engine.JRException;
43 import net.sf.jasperreports.engine.JRExporterParameter;
44 import net.sf.jasperreports.engine.JRParameter;
45 import net.sf.jasperreports.engine.JasperCompileManager;
46 import net.sf.jasperreports.engine.JasperExportManager;
47 import net.sf.jasperreports.engine.JasperFillManager;
48 import net.sf.jasperreports.engine.JasperPrint;
49 import net.sf.jasperreports.engine.JasperPrintManager;
50 import net.sf.jasperreports.engine.JasperRunManager;
51 import net.sf.jasperreports.engine.export.JExcelApiExporter;
52 import net.sf.jasperreports.engine.export.JRCsvExporter;
53 import net.sf.jasperreports.engine.export.JRRtfExporter;
54 import net.sf.jasperreports.engine.export.JRXlsExporter;
55 import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
56 import net.sf.jasperreports.engine.query.JRHibernateQueryExecuterFactory;
57 import net.sf.jasperreports.engine.util.JRLoader;
58
59
60 /**
61  * @author Teodor Danciu (teodord@users.sourceforge.net)
62  * @version $Id: HibernateApp.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
63  */

64 public class HibernateApp
65 {
66
67
68     private static final String JavaDoc TASK_FILL = "fill";
69     private static final String JavaDoc TASK_PRINT = "print";
70     private static final String JavaDoc TASK_PDF = "pdf";
71     private static final String JavaDoc TASK_RTF = "rtf";
72     private static final String JavaDoc TASK_XML = "xml";
73     private static final String JavaDoc TASK_XML_EMBED = "xmlEmbed";
74     private static final String JavaDoc TASK_HTML = "html";
75     private static final String JavaDoc TASK_XLS = "xls";
76     private static final String JavaDoc TASK_JXL = "jxl";
77     private static final String JavaDoc TASK_CSV = "csv";
78     private static final String JavaDoc TASK_RUN = "run";
79     
80     
81     private static final String JavaDoc[] reportNames = {
82         "AddressesReport",
83         "HibernateQueryReport"
84     };
85     
86     public static void main(String JavaDoc[] args)
87     {
88         String JavaDoc fileName = null;
89         String JavaDoc taskName = null;
90
91         if(args.length == 0)
92         {
93             usage();
94             return;
95         }
96                 
97         int k = 0;
98         while ( args.length > k )
99         {
100             if ( args[k].startsWith("-T") )
101                 taskName = args[k].substring(2);
102             if ( args[k].startsWith("-F") )
103                 fileName = args[k].substring(2);
104             
105             k++;
106         }
107
108         try
109         {
110             if (TASK_FILL.equals(taskName))
111             {
112                 Session session = createSession();
113                 Transaction transaction = session.beginTransaction();
114
115                 Map JavaDoc params = getParameters(session);
116                 
117                 for(int i = 0; i < reportNames.length; i++)
118                 {
119                     long start = System.currentTimeMillis();
120                     JasperFillManager.fillReportToFile(reportNames[i] + ".jasper", params);
121                     System.err.println("Report : " + reportNames[i] + ". Filling time : " + (System.currentTimeMillis() - start));
122                 }
123                 
124                 transaction.rollback();
125                 session.close();
126                 System.exit(0);
127             }
128             else if (TASK_PRINT.equals(taskName))
129             {
130                 for(int i = 0; i < reportNames.length; i++)
131                 {
132                     long start = System.currentTimeMillis();
133                     JasperPrintManager.printReport(reportNames[i] + ".jrprint", true);
134                     System.err.println("Report : " + reportNames[i] + ". Printing time : " + (System.currentTimeMillis() - start));
135                 }
136                 System.exit(0);
137             }
138             else if (TASK_PDF.equals(taskName))
139             {
140                 for(int i = 0; i < reportNames.length; i++)
141                 {
142                     long start = System.currentTimeMillis();
143                     JasperExportManager.exportReportToPdfFile(reportNames[i] + ".jrprint");
144                     System.err.println("Report : " + reportNames[i] + ". PDF creation time : " + (System.currentTimeMillis() - start));
145                 }
146                 System.exit(0);
147             }
148             else if (TASK_RTF.equals(taskName))
149             {
150                 for(int i = 0; i < reportNames.length; i++)
151                 {
152                     long start = System.currentTimeMillis();
153                     File JavaDoc sourceFile = new File JavaDoc(reportNames[i] + ".jrprint");
154         
155                     JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
156         
157                     File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".rtf");
158                 
159                     JRRtfExporter exporter = new JRRtfExporter();
160                 
161                     exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
162                     exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
163                 
164                     exporter.exportReport();
165
166                     System.err.println("Report : " + reportNames[i] + ". RTF creation time : " + (System.currentTimeMillis() - start));
167                 }
168                 System.exit(0);
169             }
170             else if (TASK_XML.equals(taskName))
171             {
172                 for(int i = 0; i < reportNames.length; i++)
173                 {
174                     long start = System.currentTimeMillis();
175                     JasperExportManager.exportReportToXmlFile(reportNames[i] + ".jrprint", false);
176                     System.err.println("Report : " + reportNames[i] + ". XML creation time : " + (System.currentTimeMillis() - start));
177                 }
178                 System.exit(0);
179             }
180             else if (TASK_XML_EMBED.equals(taskName))
181             {
182                 for(int i = 0; i < reportNames.length; i++)
183                 {
184                     long start = System.currentTimeMillis();
185                     JasperExportManager.exportReportToXmlFile(reportNames[i] + ".jrprint", true);
186                     System.err.println("Report : " + reportNames[i] + ". XML creation time : " + (System.currentTimeMillis() - start));
187                 }
188                 System.exit(0);
189             }
190             else if (TASK_HTML.equals(taskName))
191             {
192                 for(int i = 0; i < reportNames.length; i++)
193                 {
194                     long start = System.currentTimeMillis();
195                     JasperExportManager.exportReportToHtmlFile(reportNames[i] + ".jrprint");
196                     System.err.println("Report : " + reportNames[i] + ". HTML creation time : " + (System.currentTimeMillis() - start));
197                 }
198                 System.exit(0);
199             }
200             else if (TASK_XLS.equals(taskName))
201             {
202                 for(int i = 0; i < reportNames.length; i++)
203                 {
204                     long start = System.currentTimeMillis();
205                     File JavaDoc sourceFile = new File JavaDoc(reportNames[i] + ".jrprint");
206         
207                     JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
208         
209                     File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".xls");
210                 
211                     JRXlsExporter exporter = new JRXlsExporter();
212                 
213                     exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
214                     exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
215                     exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
216                 
217                     exporter.exportReport();
218
219                     System.err.println("Report : " + reportNames[i] + ". XLS creation time : " + (System.currentTimeMillis() - start));
220                 }
221                 System.exit(0);
222             }
223             else if (TASK_JXL.equals(taskName))
224             {
225                 for(int i = 0; i < reportNames.length; i++)
226                 {
227                     long start = System.currentTimeMillis();
228                     File JavaDoc sourceFile = new File JavaDoc(fileName);
229
230                     JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
231
232                     File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".jxl.xls");
233
234                     JExcelApiExporter exporter = new JExcelApiExporter();
235
236                     exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
237                     exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
238                     exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
239
240                     exporter.exportReport();
241
242                     System.err.println("Report : " + reportNames[i] + ". XLS creation time : " + (System.currentTimeMillis() - start));
243                 }
244                 System.exit(0);
245             }
246             else if (TASK_CSV.equals(taskName))
247             {
248                 for(int i = 0; i < reportNames.length; i++)
249                 {
250                     long start = System.currentTimeMillis();
251                     File JavaDoc sourceFile = new File JavaDoc(reportNames[i] + ".jrprint");
252         
253                     JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
254         
255                     File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".csv");
256                 
257                     JRCsvExporter exporter = new JRCsvExporter();
258                 
259                     exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
260                     exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
261                 
262                     exporter.exportReport();
263
264                     System.err.println("Report : " + reportNames[i] + ". CSV creation time : " + (System.currentTimeMillis() - start));
265                 }
266                 System.exit(0);
267             }
268             else if (TASK_RUN.equals(taskName))
269             {
270                 Session session = createSession();
271                 Transaction transaction = session.beginTransaction();
272                 
273                 Map JavaDoc params = getParameters(session);
274                 for(int i = 0; i< reportNames.length; i++)
275                 {
276                     long start = System.currentTimeMillis();
277                     JasperRunManager.runReportToPdfFile(reportNames[i] + ".jrprint", params);
278                     System.err.println("Report : " + reportNames[i] + ". PDF running time : " + (System.currentTimeMillis() - start));
279                 }
280
281                 transaction.rollback();
282                 session.close();
283                 System.exit(0);
284             }
285             else
286             {
287                 usage();
288                 System.exit(0);
289             }
290         }
291         catch (Exception JavaDoc e)
292         {
293             e.printStackTrace();
294             System.exit(1);
295         }
296     }
297
298     private static Map JavaDoc getParameters(Session session)
299     {
300         Map JavaDoc parameters = new HashMap JavaDoc();
301         parameters.put(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, session);
302         parameters.put("ReportTitle", "Address Report");
303         List JavaDoc cityFilter = new ArrayList JavaDoc(3);
304         cityFilter.add("Boston");
305         cityFilter.add("Chicago");
306         cityFilter.add("Oslo");
307         parameters.put("CityFilter", cityFilter);
308         parameters.put("OrderClause", "city");
309         return parameters;
310     }
311
312     private static Session createSession()
313     {
314         SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
315         
316         return sessionFactory.openSession();
317     }
318
319     private static void usage()
320     {
321         System.out.println( "HibernateApp usage:" );
322         System.out.println( "\tjava HibernateApp -Ttask -Ffile" );
323         System.out.println( "\tTasks : compile | fill | fillIgnorePagination | print | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv | run" );
324     }
325 }
326
Popular Tags