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