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  import java.util.List;
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 SubReportTest extends BaseDjReportTest {
43  
44  	public DynamicReport buildReport() throws Exception {
45  
46  
47  		FastReportBuilder drb = new FastReportBuilder();
48  		drb.addColumn("State", "state", String.class.getName(),30)
49  			.addColumn("Branch", "branch", String.class.getName(),30)
50  			.addColumn("Product Line", "productLine", String.class.getName(),50)
51  			.addColumn("Item", "item", String.class.getName(),50)
52  			.addColumn("Item Code", "id", Long.class.getName(),30,true)
53  			.addColumn("Quantity", "quantity", Long.class.getName(),60,true)
54  			.addColumn("Amount", "amount", Float.class.getName(),70,true)
55  			.addGroups(2)
56  			.setMargins(5, 5, 20, 20)
57  			.setTitle("November 2006 sales report")
58  			.setSubtitle("This report was generated at " + new Date())
59  			.setUseFullPageWidth(true);
60  
61  		drb.addField("statistics", List.class.getName());
62  		drb.addField("emptyStatistics", List.class.getName());
63  
64  		DynamicReport drHeaderSubreport = createHeaderSubreport();
65  		drb.addSubreportInGroupHeader(2, drHeaderSubreport, new ClassicLayoutManager(),
66  				"emptyStatistics", DJConstants.DATA_SOURCE_ORIGIN_FIELD, DJConstants.DATA_SOURCE_TYPE_COLLECTION);
67  
68  		 DynamicReport drFooterSubreport = createFooterSubreport();
69  		 drb.addSubreportInGroupHeader(2, drFooterSubreport,  new ClassicLayoutManager(),
70  				 "statistics", DJConstants.DATA_SOURCE_ORIGIN_FIELD, DJConstants.DATA_SOURCE_TYPE_COLLECTION);
71  
72  		drb.setUseFullPageWidth(true);
73  
74  		DynamicReport dr = drb.build();
75  		return dr;
76  	}
77  
78  	private DynamicReport createHeaderSubreport() throws Exception {
79  		FastReportBuilder rb = new FastReportBuilder();
80  		DynamicReport dr = rb
81  			.addColumn("Date", "date", Date.class.getName(), 100)
82  			.addColumn("Average", "average", Float.class.getName(), 50)
83  			.addColumn("%", "percentage", Float.class.getName(), 50)
84  			.addColumn("Amount", "amount", Float.class.getName(), 50)
85  			.setMargins(5, 5, 20, 20)
86  			.setUseFullPageWidth(true)
87  			.setWhenNoDataNoPages()
88  			.setTitle("Header Subreport for this group")
89  			.build();
90  		return dr;
91  	}
92  
93  	private DynamicReport createFooterSubreport() throws Exception {
94  		FastReportBuilder rb = new FastReportBuilder();
95  		DynamicReport dr = rb
96  		.addColumn("Area", "name", String.class.getName(), 100)
97  		.addColumn("Average", "average", Float.class.getName(), 50)
98  		.addColumn("%", "percentage", Float.class.getName(), 50)
99  		.addColumn("Amount", "amount", Float.class.getName(), 50)
100 		.addGroups(1)
101 		.setMargins(5, 5, 20, 20)
102 		.setUseFullPageWidth(true)
103 		.setTitle("Footer Subreport for this group")
104 		.build();
105 		return dr;
106 	}
107 
108 
109 	public static void main(String[] args) throws Exception {
110 		SubReportTest test = new SubReportTest();
111 		test.testReport();
112 		JasperViewer.viewReport(test.jp);
113 	}
114 
115 }