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.Collection;
33  import java.util.Date;
34  
35  import net.sf.jasperreports.view.JasperViewer;
36  import ar.com.fdvs.dj.core.DJConstants;
37  import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
38  import ar.com.fdvs.dj.domain.DynamicReport;
39  import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
40  import ar.com.fdvs.dj.test.BaseDjReportTest;
41  
42  public class SubReportRecursiveTest extends BaseDjReportTest {
43  
44  	public DynamicReport buildReport() throws Exception {
45  
46  
47  
48  		FastReportBuilder drb = new FastReportBuilder();
49  		drb.addColumn("State", "state", String.class.getName(),30)
50  			.addColumn("Branch", "branch", String.class.getName(),30)
51  			.addGroups(2)
52  			.setMargins(5, 5, 20, 20)
53  			.addField("statistics", Collection.class.getName())
54  			.setTitle("November " + getYear() +" sales report")
55  			.setSubtitle("This report was generated at " + new Date())
56  			.setUseFullPageWidth(true);
57  
58  		//Create level 2 sub-report
59  		DynamicReport drLevel2 = createLevel2Subreport();
60  
61  		//now create and put level2 subreport in the main subreport
62  		drb.addSubreportInGroupFooter(2, drLevel2, new ClassicLayoutManager(),
63  				"statistics", DJConstants.DATA_SOURCE_ORIGIN_FIELD, DJConstants.DATA_SOURCE_TYPE_COLLECTION);
64  
65  		DynamicReport mainReport = drb.build();
66  
67  
68  		return mainReport;
69  	}
70  
71  	private DynamicReport createLevel2Subreport() throws Exception {
72  		FastReportBuilder rb = new FastReportBuilder();
73  		DynamicReport dr = rb
74  			.addColumn("Date", "date", Date.class.getName(), 100)
75  			.addColumn("Average", "average", Float.class.getName(), 50)
76  			.addColumn("%", "percentage", Float.class.getName(), 50)
77  			.addColumn("Amount", "amount", Float.class.getName(), 50)
78  			.addGroups(1)
79  			.addField("dummy3", Collection.class.getName())
80  			.setMargins(5, 5, 20, 20)
81  			.setUseFullPageWidth(true)
82  			.setTitle("Level 2 Subreport")
83  			.addSubreportInGroupFooter(1, createLevel3Subreport(), new ClassicLayoutManager(),
84  				"dummy3", DJConstants.DATA_SOURCE_ORIGIN_FIELD, DJConstants.DATA_SOURCE_TYPE_COLLECTION)
85  			.build();
86  		return dr;
87  	}
88  
89  	private DynamicReport createLevel3Subreport() throws Exception {
90  		FastReportBuilder rb = new FastReportBuilder();
91  		DynamicReport dr = rb
92  		.addColumn("Name", "name", String.class.getName(), 100)
93  		.addColumn("Number", "number", Long.class.getName(), 50)
94  		.setMargins(5, 5, 20, 20)
95  		.setUseFullPageWidth(false)
96  		.setTitle("Level 3 Subreport")
97  		.build();
98  		return dr;
99  	}
100 
101 	public static void main(String[] args) throws Exception {
102 		SubReportRecursiveTest test = new SubReportRecursiveTest();
103 		test.testReport();
104 		JasperViewer.viewReport(test.jp);
105 	}
106 
107 }