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