KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ChartsApp


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.Calendar JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import net.sf.jasperreports.engine.JRException;
38 import net.sf.jasperreports.engine.JRExporterParameter;
39 import net.sf.jasperreports.engine.JasperCompileManager;
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.util.JRLoader;
44
45
46 /**
47  * @author Teodor Danciu (teodord@users.sourceforge.net)
48  * @version $Id: ChartsApp.java 1381 2006-09-06 00:15:13 +0300 (Wed, 06 Sep 2006) bklawans $
49  */

50 public class ChartsApp
51 {
52
53
54     /**
55      *
56      */

57     private static final String JavaDoc TASK_FILL = "fill";
58     private static final String JavaDoc TASK_PDF = "pdf";
59     private static final String JavaDoc TASK_HTML = "html";
60     private static final String JavaDoc TASK_WRITE_XML = "writeXml";
61
62     private static final String JavaDoc[] reportNames =
63         {
64         "PieChartReport",
65         "Pie3DChartReport",
66         "BarChartReport",
67         "Bar3DChartReport",
68         "StackedBarChartReport",
69         "StackedBar3DChartReport",
70         "XYBarChartTimePeriodReport",
71         "XYBarChartTimeSeriesReport",
72         "XYBarChartReport",
73         "AreaChartReport",
74         "XYAreaChartReport",
75         "ScatterChartReport",
76         "LineChartReport",
77         "XYLineChartReport",
78         "TimeSeriesChartReport",
79         "BubbleChartReport",
80         "HighLowChartReport",
81         "CandlestickChartReport",
82         "SubDatasetChartReport",
83         "MeterChartReport",
84         "MultipleAxisChartReport",
85         "ThermometerChartReport"
86         };
87     
88     /**
89      *
90      */

91     public static void main(String JavaDoc[] args)
92     {
93         String JavaDoc taskName = null;
94
95         if(args.length == 0)
96         {
97             usage();
98             return;
99         }
100                 
101         int k = 0;
102         while ( args.length > k )
103         {
104             if ( args[k].startsWith("-T") )
105                 taskName = args[k].substring(2);
106             
107             k++;
108         }
109
110         try
111         {
112             if (TASK_FILL.equals(taskName))
113             {
114                 Map JavaDoc parameters = new HashMap JavaDoc();
115                 parameters.put("MaxOrderID", new Integer JavaDoc(12500));
116                 
117                 for(int i = 0; i < reportNames.length; i++)
118                 {
119                     long start = System.currentTimeMillis();
120                     JasperFillManager.fillReportToFile(reportNames[i] + ".jasper", parameters, getConnection());
121                     System.err.println("Report : " + reportNames[i] + ". Filling time : " + (System.currentTimeMillis() - start));
122                 }
123
124                 System.exit(0);
125             }
126             else if (TASK_PDF.equals(taskName))
127             {
128                 for(int i = 0; i < reportNames.length; i++)
129                 {
130                     long start = System.currentTimeMillis();
131                     JasperExportManager.exportReportToPdfFile(reportNames[i] + ".jrprint");
132                     System.err.println("Report : " + reportNames[i] + ". PDF export time : " + (System.currentTimeMillis() - start));
133                 }
134
135                 System.exit(0);
136             }
137             else if (TASK_HTML.equals(taskName))
138             {
139                 for(int i = 0; i < reportNames.length; i++)
140                 {
141                     long start = System.currentTimeMillis();
142                     JasperExportManager.exportReportToHtmlFile(reportNames[i] + ".jrprint");
143                     System.err.println("Report : " + reportNames[i] + ". Html export time : " + (System.currentTimeMillis() - start));
144                 }
145
146                 System.exit(0);
147             }
148             else if (TASK_WRITE_XML.equals(taskName))
149             {
150                 for(int i = 0; i < reportNames.length; i++)
151                 {
152                     long start = System.currentTimeMillis();
153                     JasperCompileManager.writeReportToXmlFile(reportNames[i] + ".jasper", reportNames[i] + ".jasper.jrxml");
154                     System.err.println("Report : " + reportNames[i] + ". XML write time : " + (System.currentTimeMillis() - start));
155                 }
156
157                 System.exit(0);
158             }
159             else
160             {
161                 usage();
162                 System.exit(0);
163             }
164         }
165         catch (JRException e)
166         {
167             e.printStackTrace();
168             System.exit(1);
169         }
170         catch (Exception JavaDoc e)
171         {
172             e.printStackTrace();
173             System.exit(1);
174         }
175     }
176
177
178     /**
179      *
180      */

181     private static void usage()
182     {
183         System.out.println( "ChartsApp usage:" );
184         System.out.println( "\tjava ChartsApp -Ttask" );
185         System.out.println( "\tTasks : fill | pdf | html" );
186     }
187
188
189     /**
190      *
191      */

192     private static Connection JavaDoc getConnection() throws ClassNotFoundException JavaDoc, SQLException JavaDoc
193     {
194         //Change these settings according to your local configuration
195
String JavaDoc driver = "org.hsqldb.jdbcDriver";
196         String JavaDoc connectString = "jdbc:hsqldb:hsql://localhost";
197         String JavaDoc user = "sa";
198         String JavaDoc password = "";
199
200
201         Class.forName(driver);
202         Connection JavaDoc conn = DriverManager.getConnection(connectString, user, password);
203         return conn;
204     }
205
206     
207     
208     public static final Date JavaDoc truncateToMonth(Date JavaDoc date)
209     {
210         Calendar JavaDoc calendar = Calendar.getInstance();
211         calendar.setTime(date);
212         int year = calendar.get(Calendar.YEAR);
213         int month = calendar.get(Calendar.MONTH);
214         calendar.clear();
215         calendar.set(Calendar.YEAR, year);
216         calendar.set(Calendar.MONTH, month);
217         return calendar.getTime();
218     }
219
220 }
221
Popular Tags