View Javadoc

1   /*
2    * DynamicJasper: A library for creating reports dynamically by specifying
3    * columns, groups, styles, etc. at runtime. It also saves a lot of development
4    * time in many cases! (http://sourceforge.net/projects/dynamicjasper)
5    *
6    * Copyright (C) 2008  FDV Solutions (http://www.fdvsolutions.com)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   *
11   * License as published by the Free Software Foundation; either
12   *
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19   *
20   * Lesser General Public License for more details.
21   *
22   * You should have received a copy of the GNU Lesser General Public
23   * License along with this library; if not, write to the Free Software
24   *
25   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26   *
27   *
28   */
29  
30  package ar.com.fdvs.dj.test;
31  
32  
33  import java.sql.Connection;
34  
35  import net.sf.jasperreports.engine.xml.JRXmlWriter;
36  import net.sf.jasperreports.view.JasperViewer;
37  import ar.com.fdvs.dj.core.DJConstants;
38  import ar.com.fdvs.dj.core.DynamicJasperHelper;
39  import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
40  import ar.com.fdvs.dj.domain.DynamicReport;
41  import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
42  
43  public class QueryReportTest extends BaseDjReportTest {
44  
45  	public DynamicReport buildReport() throws Exception {
46  		/**
47  		 * Creates the DynamicReportBuilder and sets the basic options for
48  		 * the report
49  		 */
50  		FastReportBuilder drb = new FastReportBuilder();
51  		drb
52  			.addColumn("Id", "id", Integer.class.getName(),30)
53  			.addColumn("First Name", "firstname", String.class.getName(),30)
54  			.addColumn("Last Name", "lastname", String.class.getName(),50)
55  			.addColumn("Street", "street", String.class.getName(),50)
56  			.addColumn("City", "city", String.class.getName(),50)
57  			.setTitle("Customers")
58  			.setQuery("select * from customer where firstname like $P{start}", DJConstants.QUERY_LANGUAGE_SQL)
59  			//.setQuery("select * from customer", DJConstants.QUERY_LANGUAGE_SQL)
60  			.setTemplateFile("templates/TemplateReportTest.jrxml")
61  			.setUseFullPageWidth(true);
62  
63  		DynamicReport dr = drb.build();
64  
65  		//Note that the query has a parameter, by putting in the map
66  		//an item with the proper key, it will be automatically registered as a parameter
67  		params.put("start", "A%");
68  
69  		return dr;
70  	}
71  
72  	public static void main(String[] args) throws Exception {
73  		BaseDjReportTest test = new QueryReportTest();
74  		test.testReport();
75  		JasperViewer.viewReport(test.jp);	//finally display the report report
76  //			JasperDesignViewer.viewReportDesign(jr);
77  
78  		String jrxml = JRXmlWriter.writeReport(test.jr, "UTF-8");
79  //		System.out.println(jrxml);
80  	}
81  
82  	public void testReport() throws Exception {
83  		Connection con = null;
84  		try {
85  			dr = buildReport();
86  			con = createSQLConnection();
87  			jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), con,params );
88  			ReportExporter.exportReport(jp, System.getProperty("user.dir")+ "/target/"+this.getClass().getName()+".pdf");
89  			jr = DynamicJasperHelper.generateJasperReport(dr,  new ClassicLayoutManager(),params);
90  		} catch (Exception e) {
91  			throw e;
92  		} finally {
93  			try {
94  				con.close();
95  			} catch (Exception e1) { }
96  		}
97  	}
98  
99  
100 }