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;
31  
32  import java.awt.Color;
33  
34  import net.sf.jasperreports.view.JasperViewer;
35  import ar.com.fdvs.dj.domain.AutoText;
36  import ar.com.fdvs.dj.domain.ColumnsGroupVariableOperation;
37  import ar.com.fdvs.dj.domain.DynamicReport;
38  import ar.com.fdvs.dj.domain.Style;
39  import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
40  import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
41  import ar.com.fdvs.dj.domain.builders.GroupBuilder;
42  import ar.com.fdvs.dj.domain.constants.Border;
43  import ar.com.fdvs.dj.domain.constants.Font;
44  import ar.com.fdvs.dj.domain.constants.GroupLayout;
45  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
46  import ar.com.fdvs.dj.domain.constants.Transparency;
47  import ar.com.fdvs.dj.domain.constants.VerticalAlign;
48  import ar.com.fdvs.dj.domain.entities.ColumnsGroup;
49  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
50  import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;
51  
52  public class GroupsReportTest extends BaseDjReportTest {
53  
54  	public DynamicReport buildReport() throws Exception {
55  
56  		Style detailStyle = new Style("detail");
57  
58  		Style headerStyle = new Style("header");
59  		headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD);
60  		headerStyle.setBorderBottom(Border.PEN_1_POINT);
61  		headerStyle.setBackgroundColor(Color.gray);
62  		headerStyle.setTextColor(Color.white);
63  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
64  		headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
65  		headerStyle.setTransparency(Transparency.OPAQUE);
66  
67  		Style headerVariables = new Style("headerVariables");
68  		headerVariables.setFont(Font.ARIAL_SMALL_BOLD);
69  		headerVariables.setBorderBottom(Border.THIN);
70  		headerVariables.setHorizontalAlign(HorizontalAlign.RIGHT);
71  		headerVariables.setVerticalAlign(VerticalAlign.MIDDLE);
72  
73  		Style titleStyle = new Style("titleStyle");
74  		titleStyle.setFont(new Font(18, Font._FONT_VERDANA, true));
75  		Style importeStyle = new Style();
76  		importeStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
77  		Style oddRowStyle = new Style();
78  		oddRowStyle.setBorder(Border.NO_BORDER);
79  		oddRowStyle.setBackgroundColor(Color.LIGHT_GRAY);
80  		oddRowStyle.setTransparency(Transparency.OPAQUE);
81  
82  		DynamicReportBuilder drb = new DynamicReportBuilder();
83  		Integer margin = new Integer(20);
84  		drb
85  			.setTitleStyle(titleStyle)
86  			.setTitle("November 2006 sales report")					//defines the title of the report
87  			.setSubtitle("The items in this report correspond "
88  					+"to the main products: DVDs, Books, Foods and Magazines")
89  			.setDetailHeight(new Integer(15)).setLeftMargin(margin)
90  			.setRightMargin(margin).setTopMargin(margin).setBottomMargin(margin)
91  			.setPrintBackgroundOnOddRows(true)
92  			.setGrandTotalLegend("Grand Total")
93  			.setGrandTotalLegendStyle(headerVariables)
94  			.setOddRowBackgroundStyle(oddRowStyle);
95  
96  
97  		AbstractColumn columnState = ColumnBuilder.getInstance()
98  				.setColumnProperty("state", String.class.getName()).setTitle(
99  						"State").setWidth(new Integer(85))
100 				.setStyle(titleStyle).setHeaderStyle(titleStyle).build();
101 
102 		AbstractColumn columnBranch = ColumnBuilder.getInstance()
103 				.setColumnProperty("branch", String.class.getName()).setTitle(
104 						"Branch").setWidth(new Integer(85)).setStyle(
105 						detailStyle).setHeaderStyle(headerStyle).build();
106 
107 		AbstractColumn columnaProductLine = ColumnBuilder.getInstance()
108 				.setColumnProperty("productLine", String.class.getName())
109 				.setTitle("Product Line").setWidth(new Integer(85)).setStyle(
110 						detailStyle).setHeaderStyle(headerStyle).build();
111 
112 		AbstractColumn columnaItem = ColumnBuilder.getInstance()
113 				.setColumnProperty("item", String.class.getName()).setTitle(
114 						"Item").setWidth(new Integer(85)).setStyle(detailStyle)
115 				.setHeaderStyle(headerStyle).build();
116 
117 		AbstractColumn columnCode = ColumnBuilder.getInstance()
118 				.setColumnProperty("id", Long.class.getName()).setTitle("ID")
119 				.setWidth(new Integer(40)).setStyle(importeStyle)
120 				.setHeaderStyle(headerStyle).build();
121 
122 		AbstractColumn columnaQuantity = ColumnBuilder.getInstance()
123 				.setColumnProperty("quantity", Long.class.getName()).setTitle(
124 						"Quantity").setWidth(new Integer(80)).setStyle(
125 						importeStyle).setHeaderStyle(headerStyle).build();
126 
127 		AbstractColumn columnAmount = ColumnBuilder.getInstance()
128 				.setColumnProperty("amount", Float.class.getName()).setTitle(
129 						"Amount").setWidth(new Integer(90))
130 				.setPattern("$ 0.00").setStyle(importeStyle).setHeaderStyle(
131 						headerStyle).build();
132 
133 		drb.addGlobalHeaderVariable(columnAmount, ColumnsGroupVariableOperation.SUM,headerVariables);
134 		drb.addGlobalHeaderVariable(columnaQuantity, ColumnsGroupVariableOperation.SUM,headerVariables);
135 		drb.addGlobalFooterVariable(columnAmount, ColumnsGroupVariableOperation.SUM,headerVariables);
136 		drb.addGlobalFooterVariable(columnaQuantity, ColumnsGroupVariableOperation.SUM,headerVariables);
137 
138 		GroupBuilder gb1 = new GroupBuilder();
139 
140 //		 define the criteria column to group by (columnState)
141 		ColumnsGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState)
142 				.addHeaderVariable(columnAmount,ColumnsGroupVariableOperation.SUM) // tell the group place a variable footer of the column "columnAmount" with the SUM of allvalues of the columnAmount in this group.
143 				.addFooterVariable(columnAmount,ColumnsGroupVariableOperation.SUM) // tell the group place a variable footer of the column "columnAmount" with the SUM of allvalues of the columnAmount in this group.
144 				.addFooterVariable(columnaQuantity,ColumnsGroupVariableOperation.SUM) // idem for the columnaQuantity column
145 				.setGroupLayout(GroupLayout.VALUE_IN_HEADER) // tells the group how to be shown, there are manyposibilities, see the GroupLayout for more.
146 				.build();
147 
148 		GroupBuilder gb2 = new GroupBuilder(); // Create another group (using another column as criteria)
149 		ColumnsGroup g2 = gb2.setCriteriaColumn((PropertyColumn) columnBranch) // and we add the same operations for the columnAmount and
150 				.addFooterVariable(columnAmount,
151 						ColumnsGroupVariableOperation.SUM) // columnaQuantity columns
152 				.addFooterVariable(columnaQuantity,	ColumnsGroupVariableOperation.SUM)
153 				.build();
154 
155 		drb.addColumn(columnState);
156 		drb.addColumn(columnBranch);
157 		drb.addColumn(columnaProductLine);
158 		drb.addColumn(columnaItem);
159 		drb.addColumn(columnCode);
160 		drb.addColumn(columnaQuantity);
161 		drb.addColumn(columnAmount);
162 
163 		drb.addGroup(g1); // add group g1
164 //		drb.addGroup(g2); // add group g2
165 
166 		drb.setUseFullPageWidth(true);
167 		drb.addAutoText(AutoText.AUTOTEXT_PAGE_X_SLASH_Y, AutoText.POSITION_FOOTER, AutoText.ALIGMENT_RIGHT);
168 
169 		DynamicReport dr = drb.build();
170 		return dr;
171 	}
172 
173 	public static void main(String[] args) throws Exception {
174 		GroupsReportTest test = new GroupsReportTest();
175 		test.testReport();
176 		JasperViewer.viewReport(test.jp);
177 	}
178 
179 }