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.crosstab;
31  
32  
33  import java.awt.Color;
34  import java.util.Date;
35  
36  import net.sf.jasperreports.view.JasperDesignViewer;
37  import net.sf.jasperreports.view.JasperViewer;
38  import ar.com.fdvs.dj.core.DJConstants;
39  import ar.com.fdvs.dj.domain.DJCalculation;
40  import ar.com.fdvs.dj.domain.DJCrosstab;
41  import ar.com.fdvs.dj.domain.DJCrosstabColumn;
42  import ar.com.fdvs.dj.domain.DJCrosstabRow;
43  import ar.com.fdvs.dj.domain.DynamicReport;
44  import ar.com.fdvs.dj.domain.Style;
45  import ar.com.fdvs.dj.domain.builders.CrosstabBuilder;
46  import ar.com.fdvs.dj.domain.builders.CrosstabColumnBuilder;
47  import ar.com.fdvs.dj.domain.builders.CrosstabRowBuilder;
48  import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
49  import ar.com.fdvs.dj.domain.builders.StyleBuilder;
50  import ar.com.fdvs.dj.domain.constants.Border;
51  import ar.com.fdvs.dj.domain.constants.Font;
52  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
53  import ar.com.fdvs.dj.domain.constants.Page;
54  import ar.com.fdvs.dj.domain.constants.Transparency;
55  import ar.com.fdvs.dj.domain.constants.VerticalAlign;
56  import ar.com.fdvs.dj.test.BaseDjReportTest;
57  import ar.com.fdvs.dj.test.TestRepositoryProducts;
58  import ar.com.fdvs.dj.util.SortUtils;
59  
60  public class CrosstabReportTest2 extends BaseDjReportTest {
61  
62  	private Style totalHeader;
63  	private Style colAndRowHeaderStyle;
64  	private Style mainHeaderStyle;
65  	private Style totalStyle;
66  	private Style measureStyle;
67  	private Style titleStyle;
68  
69  	public DynamicReport buildReport() throws Exception {
70  		initStyles(); //init some styles to be used
71  
72  
73  	/**
74  	 * Create an empty report (no columns)!
75  	 */
76  		FastReportBuilder drb = new FastReportBuilder();
77  			drb
78  			.setTitle("November " + getYear() +" sales report")
79  			.setSubtitle("This report was generated at " + new Date())
80  			.setPageSizeAndOrientation(Page.Page_A4_Landscape())
81  			.setPrintColumnNames(false)
82  			.setUseFullPageWidth(true)
83  			.setDefaultStyles(titleStyle, null, null, null);
84  
85  		DJCrosstab djcross = createCrosstab();
86  
87  		drb.addHeaderCrosstab(djcross); //add the crosstab in the header band of the report
88  
89  		DynamicReport dr = drb.build();
90  
91  		//put a collection in the parameters map to be used by the crosstab
92  		params.put("sr", SortUtils.sortCollection(TestRepositoryProducts.getDummyCollection(),djcross));
93  
94  		return dr;
95  	}
96  
97  	/**
98  	 * Creates s DJCrosstab object, ready to be inserted in the main report
99  	 * @return
100 	 *
101 	 */
102 	private DJCrosstab createCrosstab() {
103 
104 		CrosstabBuilder cb = new CrosstabBuilder();
105 
106 		cb.setHeight(200)
107 			.setWidth(500)
108 			.setHeaderStyle(mainHeaderStyle)
109 			.setDatasource("sr",DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_COLLECTION)
110 			.setUseFullWidth(true)
111 			.setColorScheme(4)
112 			.setAutomaticTitle(true)
113 			.setCellBorder(Border.THIN());
114 
115 		cb.addMeasure("amount",Float.class.getName(), DJCalculation.SUM , "Amount",measureStyle);
116 
117 		DJCrosstabRow row = new CrosstabRowBuilder().setProperty("productLine",String.class.getName())
118 			.setHeaderWidth(100).setHeight(20)
119 			.setTitle("Product Line")
120 			.setShowTotals(true).setTotalStyle(totalStyle)
121 			.setTotalHeaderStyle(totalHeader).setHeaderStyle(colAndRowHeaderStyle)
122 			.build();
123 
124 		cb.addRow(row);
125 
126 		row = new CrosstabRowBuilder().setProperty("item",String.class.getName())
127 			.setHeaderWidth(100).setHeight(20)
128 			.setTitle("Item").setShowTotals(true)
129 			.setTotalStyle(totalStyle).setTotalHeaderStyle(totalHeader)
130 			.setHeaderStyle(colAndRowHeaderStyle)
131 			.build();
132 
133 		cb.addRow(row);
134 
135 		row = new CrosstabRowBuilder().setProperty("id",Long.class.getName())
136 			.setHeaderWidth(100).setHeight(30)
137 			.setTitle("ID").setShowTotals(true)
138 			.setTotalStyle(totalStyle).setTotalHeaderStyle(totalHeader)
139 			.setHeaderStyle(colAndRowHeaderStyle)
140 			.build();
141 
142 		cb.addRow(row);
143 
144 		DJCrosstabColumn col = new CrosstabColumnBuilder().setProperty("state",String.class.getName())
145 			.setHeaderHeight(60).setWidth(80)
146 			.setTitle("State").setShowTotals(true)
147 			.setTotalStyle(totalStyle).setTotalHeaderStyle(totalHeader)
148 			.setHeaderStyle(colAndRowHeaderStyle)
149 			.build();
150 
151 
152 		cb.addColumn(col);
153 
154 		col = new CrosstabColumnBuilder().setProperty("branch",String.class.getName())
155 			.setHeaderHeight(30).setWidth(70)
156 			.setShowTotals(true).setTitle("Branch")
157 			.setTotalStyle(totalStyle).setTotalHeaderStyle(totalHeader)
158 			.setHeaderStyle(colAndRowHeaderStyle)
159 			.build();
160 
161 		cb.addColumn(col);
162 
163 //		col = new CrosstabColumnBuilder().setProperty("id",Long.class.getName())
164 //			.setHeaderHeight(40).setWidth(70)
165 //			.setShowTotals(true).setTitle("ID")
166 //			.setTotalStyle(totalStyle).setTotalHeaderStyle(totalHeader)
167 //			.setHeaderStyle(colAndRowHeaderStyle)
168 //			.build();
169 
170 //		cb.addColumn(col);
171 
172 		return cb.build();
173 	}
174 
175 	/**
176 	 *
177 	 */
178 	private void initStyles() {
179 		titleStyle =  new StyleBuilder(false)
180 			.setFont(Font.ARIAL_BIG_BOLD)
181 			.setHorizontalAlign(HorizontalAlign.LEFT)
182 			.setVerticalAlign(VerticalAlign.MIDDLE)
183 			.setTransparency(Transparency.OPAQUE)
184 			.setBorderBottom(Border.PEN_2_POINT())
185 			.build();
186 
187 		totalHeader = new StyleBuilder(false)
188 			.setHorizontalAlign(HorizontalAlign.CENTER)
189 			.setVerticalAlign(VerticalAlign.MIDDLE)
190 			.setFont(Font.ARIAL_MEDIUM_BOLD)
191 			.setTextColor(Color.BLUE)
192 			.build();
193 		colAndRowHeaderStyle = new StyleBuilder(false)
194 			.setHorizontalAlign(HorizontalAlign.LEFT)
195 			.setVerticalAlign(VerticalAlign.TOP)
196 			.setFont(Font.ARIAL_MEDIUM_BOLD)
197 			.build();
198 		mainHeaderStyle = new StyleBuilder(false)
199 			.setHorizontalAlign(HorizontalAlign.CENTER)
200 			.setVerticalAlign(VerticalAlign.MIDDLE)
201 			.setFont(Font.ARIAL_BIG_BOLD)
202 			.setTextColor(Color.BLACK)
203 			.build();
204 		totalStyle = new StyleBuilder(false).setPattern("#,###.##")
205 			.setHorizontalAlign(HorizontalAlign.RIGHT)
206 			.setFont(Font.ARIAL_MEDIUM_BOLD)
207 			.build();
208 		measureStyle = new StyleBuilder(false).setPattern("#,###.##")
209 			.setHorizontalAlign(HorizontalAlign.RIGHT)
210 			.setFont(Font.ARIAL_MEDIUM)
211 			.build();
212 	}
213 
214 
215 	public static void main(String[] args) throws Exception {
216 		CrosstabReportTest2 test = new CrosstabReportTest2();
217 		test.testReport();
218 		JasperViewer.viewReport(test.jp);	//finally display the report report
219 		JasperDesignViewer.viewReportDesign(test.jr);
220 	}
221 
222 }