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