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