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.VerticalAlign;
55  import ar.com.fdvs.dj.test.BaseDjReportTest;
56  import ar.com.fdvs.dj.test.TestRepositoryProducts;
57  import ar.com.fdvs.dj.util.SortUtils;
58  
59  public class CrosstabReportTest extends BaseDjReportTest {
60  
61  	private DJCrosstab djcross;
62  	private Style totalHeader;
63  	private Style colAndRowHeaderStyle;
64  	private Style mainHeaderStyle;
65  	private Style totalStyle;
66  	private Style measureStyle;
67  
68  	public DynamicReport buildReport() throws Exception {
69  
70  
71  		/**
72  		 * Creates the DynamicReportBuilder and sets the basic options for
73  		 * the report
74  		 */
75  		FastReportBuilder drb = new FastReportBuilder();
76  		drb.addColumn("State", "state", String.class.getName(),30)
77  			.addColumn("Branch", "branch", String.class.getName(),30)
78  			.addColumn("Product Line", "productLine", String.class.getName(),50)
79  			.addColumn("Item", "item", String.class.getName(),50)
80  			.addColumn("Item Code", "id", Long.class.getName(),30,true)
81  			.addColumn("Quantity", "quantity", Long.class.getName(),60,true)
82  			.addColumn("Amount", "amount", Float.class.getName(),70,true)
83  			.addGroups(1)
84  			.addFooterVariable(1, 7, DJCalculation.SUM, null)
85  			.addFooterVariable(1, 6, DJCalculation.SUM, null)
86  			.setTitle("November " + getYear() +" sales report")
87  			.setSubtitle("This report was generated at " + new Date())
88  			.setPageSizeAndOrientation(Page.Page_A4_Landscape())
89  			.setUseFullPageWidth(true);
90  //			drb.setTemplateFile("templates/crosstab2-test.jrxml");
91  
92  
93  		initStyles();
94  
95  		CrosstabBuilder cb = new CrosstabBuilder();
96  
97  		cb.setHeight(200)
98  			.setWidth(500)
99  			.setHeaderStyle(mainHeaderStyle)
100 			.setDatasource("sr",DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_COLLECTION)
101 			.setUseFullWidth(true)
102 			.setColorScheme(4)
103 			.setAutomaticTitle(true)
104 			.setCellBorder(Border.PEN_1_POINT());
105 
106 		cb.addMeasure("amount",Float.class.getName(), DJCalculation.SUM , "Amount",measureStyle);
107 
108 		DJCrosstabRow row = new CrosstabRowBuilder().setProperty("productLine",String.class.getName())
109 			.setHeaderWidth(100).setHeight(0)
110 			.setTitle("Product Line my mother teressa")
111 			.setShowTotals(true).setTotalStyle(totalStyle)
112 			.setTotalHeaderStyle(totalHeader).setHeaderStyle(colAndRowHeaderStyle)
113 			.build();
114 
115 		cb.addRow(row);
116 
117 		row = new CrosstabRowBuilder().setProperty("item",String.class.getName())
118 			.setHeaderWidth(100).setHeight(20)
119 			.setTitle("Item").setShowTotals(true)
120 			.setTotalStyle(totalStyle).setTotalHeaderStyle(totalHeader)
121 			.setHeaderStyle(colAndRowHeaderStyle)
122 			.build();
123 
124 		row.setTotalHeaderHeight(100);
125 
126 		cb.addRow(row);
127 
128 //		row = new CrosstabRowBuilder().setProperty("id",Long.class.getName())
129 //			.setHeaderWidth(100).setHeight(30)
130 //			.setTitle("ID").setShowTotals(true)
131 //			.setTotalStyle(totalStyle).setTotalHeaderStyle(totalHeader)
132 //			.setHeaderStyle(colAndRowHeaderStyle)
133 //			.build();
134 
135 //		cb.addRow(row);
136 
137 		DJCrosstabColumn col = new CrosstabColumnBuilder().setProperty("state",String.class.getName())
138 			.setHeaderHeight(60).setWidth(50)
139 			.setTitle("State").setShowTotals(true)
140 			.setTotalStyle(totalStyle).setTotalHeaderStyle(totalHeader)
141 			.setHeaderStyle(colAndRowHeaderStyle)
142 			.build();
143 
144 
145 		cb.addColumn(col);
146 
147 		col = new CrosstabColumnBuilder().setProperty("branch",String.class.getName())
148 			.setHeaderHeight(30).setWidth(60)
149 			.setShowTotals(true).setTitle("Branch")
150 			.setTotalStyle(totalStyle).setTotalHeaderStyle(totalHeader).setTotalLegend("Total for \"+ $V{branch}+ \"")
151 			.setHeaderStyle(colAndRowHeaderStyle)
152 			.build();
153 
154 		cb.addColumn(col);
155 
156 		col = new CrosstabColumnBuilder().setProperty("id",Long.class.getName())
157 		.setHeaderHeight(40)
158 		.setWidth(70)
159 		.setShowTotals(true)
160 		.setTitle("ID")
161 		.setTotalStyle(totalStyle)
162 		.setTotalHeaderStyle(totalHeader)
163 		.setHeaderStyle(colAndRowHeaderStyle)
164 		.build();
165 
166 //		cb.addColumn(col);
167 
168 		djcross = cb.build();
169 
170 		drb.addHeaderCrosstab(djcross);
171 
172 		DynamicReport dr = drb.build();
173 
174 		params.put("sr", SortUtils.sortCollection(TestRepositoryProducts.getDummyCollection(),djcross));
175 		return dr;
176 	}
177 
178 	/**
179 	 *
180 	 */
181 	private void initStyles() {
182 		totalHeader = new StyleBuilder(false)
183 			.setHorizontalAlign(HorizontalAlign.CENTER)
184 			.setVerticalAlign(VerticalAlign.MIDDLE)
185 			.setFont(Font.ARIAL_MEDIUM_BOLD)
186 			.setTextColor(Color.BLUE)
187 			.build();
188 		colAndRowHeaderStyle = new StyleBuilder(false)
189 			.setHorizontalAlign(HorizontalAlign.LEFT)
190 			.setVerticalAlign(VerticalAlign.TOP)
191 			.setFont(Font.ARIAL_MEDIUM_BOLD)
192 			.build();
193 		mainHeaderStyle = new StyleBuilder(false)
194 			.setHorizontalAlign(HorizontalAlign.CENTER)
195 			.setVerticalAlign(VerticalAlign.MIDDLE)
196 			.setFont(Font.ARIAL_BIG_BOLD)
197 			.setTextColor(Color.BLACK)
198 			.build();
199 		totalStyle = new StyleBuilder(false).setPattern("#,###.##")
200 			.setHorizontalAlign(HorizontalAlign.RIGHT)
201 			.setFont(Font.ARIAL_MEDIUM_BOLD)
202 			.build();
203 		measureStyle = new StyleBuilder(false).setPattern("#,###.##")
204 			.setHorizontalAlign(HorizontalAlign.RIGHT)
205 			.setFont(Font.ARIAL_MEDIUM)
206 			.build();
207 	}
208 
209 
210 	public static void main(String[] args) throws Exception {
211 		CrosstabReportTest test = new CrosstabReportTest();
212 		test.testReport();
213 		JasperViewer.viewReport(test.jp);	//finally display the report report
214 		JasperDesignViewer.viewReportDesign(test.jr);
215 	}
216 
217 }