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.subreport;
31  
32  import java.util.Date;
33  
34  import net.sf.jasperreports.view.JasperViewer;
35  import ar.com.fdvs.dj.core.DJConstants;
36  import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
37  import ar.com.fdvs.dj.domain.DynamicReport;
38  import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
39  import ar.com.fdvs.dj.domain.builders.SubReportBuilder;
40  import ar.com.fdvs.dj.domain.entities.Subreport;
41  import ar.com.fdvs.dj.test.BaseDjReportTest;
42  import ar.com.fdvs.dj.test.domain.Product;
43  
44  public class SubReportBuilderTest extends BaseDjReportTest {
45  
46  	public DynamicReport buildReport() throws Exception {
47  
48  		FastReportBuilder drb = new FastReportBuilder();
49  		drb.addColumn("State", "state", String.class.getName(),30)
50  			.addColumn("Branch", "branch", String.class.getName(),30)
51  			.addColumn("Product Line", "productLine", String.class.getName(),50)
52  			.addColumn("Item", "item", String.class.getName(),50)
53  			.addColumn("Item Code", "id", Long.class.getName(),30,true)
54  			.addColumn("Quantity", "quantity", Long.class.getName(),60,true)
55  			.addColumn("Amount", "amount", Float.class.getName(),70,true)
56  			.addGroups(2)
57  			.setTitle("November 2006 sales report")
58  			.setSubtitle("This report was generated at " + new Date())
59  			.setUseFullPageWidth(true);
60  
61  		/***
62  		 * Create the subreport. Note that the "subreport" object is then passed
63  		 * as parameter to the GroupBuilder
64  		 */
65  		Subreport subreport = new SubReportBuilder()
66  						.setDataSource(	DJConstants.DATA_SOURCE_ORIGIN_PARAMETER,
67  										DJConstants.DATA_SOURCE_TYPE_COLLECTION,
68  										"statistics")
69  						.setDynamicReport(createFooterSubreport(), new ClassicLayoutManager())
70  						.build();
71  
72  		drb.addSubreportInGroupFooter(1, subreport);
73  
74  		/***
75  		 * add in a map the paramter with the data source to use in the subreport.
76  		 * The "params" map is later passed to the DynamicJasperHelper.generateJasperPrint(...)
77  		 */
78  		params.put("statistics", Product.statistics_  ); // the 2nd param is a static Collection
79  
80  		/***
81  		 * Create the group and add the subreport (as a Fotter subreport)
82  		 */
83  		drb.setUseFullPageWidth(true);
84  
85  		DynamicReport dr = drb.build();
86  
87  		return dr;
88  	}
89  
90  	/***
91  	 * Created and compiles dynamically a report to be used as subreportr
92  	 * @return
93  	 * @throws Exception
94  	 */
95  	private DynamicReport createFooterSubreport() throws Exception {
96  		FastReportBuilder rb = new FastReportBuilder();
97  		DynamicReport dr = rb
98  		.addColumn("Area", "name", String.class.getName(), 100)
99  		.addColumn("Average", "average", Float.class.getName(), 50)
100 		.addColumn("%", "percentage", Float.class.getName(), 50)
101 		.addColumn("Amount", "amount", Float.class.getName(), 50)
102 		.addGroups(1)
103 		.setMargins(5, 5, 20, 20)
104 		.setUseFullPageWidth(true)
105 		.setTitle("Subreport for this group")
106 		.build();
107 		return dr;
108 	}
109 
110 
111 	public static void main(String[] args) throws Exception {
112 		SubReportBuilderTest test = new SubReportBuilderTest();
113 		test.testReport();
114 		JasperViewer.viewReport(test.jp);
115 	}
116 
117 }