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.HashMap;
34  
35  import net.sf.jasperreports.engine.JasperFillManager;
36  import net.sf.jasperreports.engine.JasperReport;
37  import net.sf.jasperreports.view.JasperDesignViewer;
38  import net.sf.jasperreports.view.JasperViewer;
39  import ar.com.fdvs.dj.core.DJConstants;
40  import ar.com.fdvs.dj.core.DynamicJasperHelper;
41  import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
42  import ar.com.fdvs.dj.domain.AutoText;
43  import ar.com.fdvs.dj.domain.DynamicReport;
44  import ar.com.fdvs.dj.domain.Style;
45  import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
46  import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
47  import ar.com.fdvs.dj.domain.constants.Font;
48  import ar.com.fdvs.dj.test.BaseDjReportTest;
49  import ar.com.fdvs.dj.test.ChartReportTest;
50  import ar.com.fdvs.dj.test.ReportExporter;
51  import ar.com.fdvs.dj.test.TestRepositoryProducts;
52  import ar.com.fdvs.dj.test.domain.Product;
53  
54  public class ConcatenatedReportTest extends BaseDjReportTest {
55  
56  	public DynamicReport buildReport() throws Exception {
57  
58  		Style titleStyle = new Style();
59  		titleStyle.setFont(new Font(24, Font._FONT_VERDANA, true));
60  
61  		/**
62  		 * 1st) Whe create an empty report (just title) and add 2 sub-reports
63  		 */
64  		DynamicReportBuilder drb = new DynamicReportBuilder();
65  		Integer margin = new Integer(20);
66  		drb
67  			.setTitleStyle(titleStyle)
68  			.setTitle("Concatenated reports")					//defines the title of the report
69  			.setSubtitle("All the reports shown here are concatenated as sub reports")
70  			.setDetailHeight(new Integer(15)).setLeftMargin(margin)
71  			.setRightMargin(margin).setTopMargin(margin).setBottomMargin(margin)
72  			.setUseFullPageWidth(true)
73  			.setWhenNoDataAllSectionNoDetail()
74  			.addAutoText(AutoText.AUTOTEXT_PAGE_X_OF_Y, AutoText.POSITION_FOOTER,AutoText.ALIGNMENT_CENTER)
75  			.addConcatenatedReport(createSubreport1("Sub report 1"), new ClassicLayoutManager(), "statistics",
76  									DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_COLLECTION,false)
77  			.addConcatenatedReport(createSubreport2("Sub report 2"), "statistics",
78  									DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_COLLECTION,true)
79  			.addConcatenatedReport(createSubreport2("Sub report 3"), "statistics",
80  											DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_COLLECTION,true)
81  		.addConcatenatedReport(createSubreport2("Sub report 4"), "statistics",
82  				DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_COLLECTION,true);
83  
84  
85  		//Add the data source of the sub-report as a parameter
86  		params.put("statistics", Product.statistics_ );
87  		//add the subreport to the main report builder
88  
89  		//Add the data source of the sub-report as a parameter
90  		params.put("statisticsArray", Product.statistics_.toArray() );
91  
92  		/**
93  		 * Add one more report (from another test)
94  		 */
95  		//Create a subreport
96  		ChartReportTest crt = new ChartReportTest();
97  		JasperReport chartJr = DynamicJasperHelper.generateJasperReport(crt.buildReport(), new ClassicLayoutManager(),new HashMap());
98  //		drb.addConcatenatedReport(chartJr, "subreportsDataSource",
99  //									DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_COLLECTION,true);
100 		//Add the data source of the sub-report as a parameter
101 		params.put("subreportsDataSource", TestRepositoryProducts.getDummyCollection()  );
102 
103 		//thats it!!!!
104 //		DynamicReport dr = drb.build();
105 		dr = drb.build();
106 
107 		return dr;
108 	}
109 
110 	public void testReport() throws Exception {
111 			dr = buildReport();
112 
113 			jr = DynamicJasperHelper.generateJasperReport(dr, getLayoutManager(), params);
114 
115 			/**
116 			 * Creates the JasperPrint object, we pass as a Parameter
117 			 * the JasperReport object, and the JRDataSource
118 			 */
119 			jp = JasperFillManager.fillReport(jr, params);			
120 			ReportExporter.exportReport(jp, System.getProperty("user.dir") + "/target/ConcatenatedReportTest.pdf");
121 
122 	}
123 
124 	/**
125 	 * Creates a dynamic reports and compiles it in order to be used
126 	 * as a subreport
127 	 * @return
128 	 * @throws Exception
129 	 */
130 	private DynamicReport createSubreport1(String title) throws Exception {
131 		FastReportBuilder rb = new FastReportBuilder();
132 		DynamicReport dr = rb
133 			.addColumn("Date", "date", Date.class.getName(), 100)
134 			.addColumn("Average", "average", Float.class.getName(), 50)
135 			.addColumn("%", "percentage", Float.class.getName(), 50)
136 			.addColumn("Amount", "amount", Float.class.getName(), 50)
137 			.setMargins(5, 5, 20, 20)
138 			.setUseFullPageWidth(true)
139 			.setTitle(title)
140 			.build();
141 		return dr;
142 	}
143 
144 	/**
145 	 * Creates a dynamic reports and compiles it in order to be used
146 	 * as a subreport
147 	 * @return
148 	 * @throws Exception
149 	 */
150 	private JasperReport createSubreport2(String title) throws Exception {
151 		FastReportBuilder rb = new FastReportBuilder();
152 		DynamicReport dr = rb
153 		.addColumn("Area", "name", String.class.getName(), 100)
154 		.addColumn("Average", "average", Float.class.getName(), 50)
155 		.addColumn("%", "percentage", Float.class.getName(), 50)
156 		.addColumn("Amount", "amount", Float.class.getName(), 50)
157 		.setMargins(5, 5, 20, 20)
158 		.setUseFullPageWidth(true)
159 		.setTitle(title)
160 		.build();
161 		return DynamicJasperHelper.generateJasperReport(dr, new ClassicLayoutManager(),null);
162 	}
163 
164 	public static void main(String[] args) throws Exception {
165 		ConcatenatedReportTest test = new ConcatenatedReportTest();
166 		test.testReport();
167 		JasperViewer.viewReport(test.jp);
168 		JasperDesignViewer.viewReportDesign(test.jr);
169 		test.exportToJRXML();
170 	}
171 
172 }
173