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.groups;
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.DJCalculation;
37  import ar.com.fdvs.dj.domain.DynamicReport;
38  import ar.com.fdvs.dj.domain.ImageBanner;
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  import ar.com.fdvs.dj.test.BaseDjReportTest;
53  
54  public class GroupsReportTestG1T7 extends BaseDjReportTest {
55  
56  	public DynamicReport buildReport() throws Exception {
57  
58  		Style detailStyle = new Style();
59  		detailStyle.setVerticalAlign(VerticalAlign.TOP);
60  
61  		Style headerStyle = new Style();
62  		headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD);
63  		headerStyle.setBorderBottom(Border.PEN_1_POINT());
64  		headerStyle.setBackgroundColor(Color.gray);
65  		headerStyle.setTextColor(Color.white);
66  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
67  		headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
68  		headerStyle.setTransparency(Transparency.OPAQUE);
69  
70  		Style headerVariables = new Style();
71  		headerVariables.setFont(Font.ARIAL_MEDIUM_BOLD);
72  //		headerVariables.setBorderBottom(Border.THIN());
73  		headerVariables.setHorizontalAlign(HorizontalAlign.RIGHT);
74  		headerVariables.setVerticalAlign(VerticalAlign.MIDDLE);
75  
76  		Style titleStyle = new Style();
77  		titleStyle.setFont(new Font(18, Font._FONT_VERDANA, true));
78  		Style importeStyle = new Style();
79  		importeStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
80  		Style oddRowStyle = new Style();
81  		oddRowStyle.setBorder(Border.NO_BORDER());
82  		oddRowStyle.setBackgroundColor(Color.LIGHT_GRAY);
83  		oddRowStyle.setTransparency(Transparency.OPAQUE);
84  
85  		DynamicReportBuilder drb = new DynamicReportBuilder();
86  		Integer margin = new Integer(20);
87  		drb
88  			.setTitleStyle(titleStyle)
89  			.setTitle("November " + getYear() +" sales report")					//defines the title of the report
90  			.setSubtitle("The items in this report correspond "
91  					+"to the main products: DVDs, Books, Foods and Magazines")
92  			.setDetailHeight(new Integer(15)).setLeftMargin(margin)
93  			.setRightMargin(margin).setTopMargin(margin).setBottomMargin(margin)
94  			.setPrintBackgroundOnOddRows(false)
95  			.setGrandTotalLegend("Grand Total")
96  			.setGrandTotalLegendStyle(headerVariables)
97  			.setDefaultStyles(titleStyle, null, headerStyle, detailStyle)
98  			.setPrintColumnNames(false)
99  			.addImageBanner(System.getProperty("user.dir") +"/target/test-classes/images/logo_fdv_solutions_60.jpg", new Integer(100), new Integer(30), ImageBanner.ALIGN_RIGHT)
100 			.setOddRowBackgroundStyle(oddRowStyle);
101 
102 
103 		AbstractColumn columnState = ColumnBuilder.getNew()
104 				.setColumnProperty("state", String.class.getName())
105 				.setTitle("State").setWidth(new Integer(85))
106 				.setStyle(titleStyle).setHeaderStyle(titleStyle)
107 				.build();
108 
109 		AbstractColumn columnBranch = ColumnBuilder.getNew()
110 				.setColumnProperty("branch", String.class.getName())
111 				.setTitle("Branch").setWidth(new Integer(85))
112 				.setStyle(detailStyle).setHeaderStyle(headerStyle)
113 				.build();
114 
115 		AbstractColumn columnaProductLine = ColumnBuilder.getNew()
116 				.setColumnProperty("productLine", String.class.getName())
117 				.setTitle("Product Line").setWidth(new Integer(85))
118 				.setStyle(detailStyle).setHeaderStyle(headerStyle)
119 				.build();
120 
121 		AbstractColumn columnaItem = ColumnBuilder.getNew()
122 				.setColumnProperty("item", String.class.getName())
123 				.setTitle("Item").setWidth(new Integer(85))
124 				.setStyle(detailStyle).setHeaderStyle(headerStyle)
125 				.build();
126 
127 		AbstractColumn columnCode = ColumnBuilder.getNew()
128 				.setColumnProperty("id", Long.class.getName())
129 				.setTitle("ID").setWidth(new Integer(40))
130 				.setStyle(importeStyle).setHeaderStyle(headerStyle)
131 				.build();
132 
133 		AbstractColumn columnaQuantity = ColumnBuilder.getNew()
134 				.setColumnProperty("quantity", Long.class.getName())
135 				.setTitle("Quantity").setWidth(new Integer(80))
136 				.setStyle(importeStyle).setHeaderStyle(headerStyle)
137 				.build();
138 
139 		AbstractColumn columnAmount = ColumnBuilder.getNew()
140 				.setColumnProperty("amount", Float.class.getName())
141 				.setTitle("Amount").setWidth(new Integer(90)).setPattern("$ 0.00")
142 				.setStyle(importeStyle).setHeaderStyle(headerStyle)
143 				.build();
144 
145 // drb.addGlobalHeaderVariable(columnAmount,
146 // ColumnsGroupVariableOperation.SUM,headerVariables);
147 //		drb.addGlobalHeaderVariable(columnaQuantity, ColumnsGroupVariableOperation.SUM,headerVariables);
148 //		drb.addGlobalFooterVariable(columnAmount, ColumnsGroupVariableOperation.SUM,headerVariables);
149 //		drb.addGlobalFooterVariable(columnaQuantity, ColumnsGroupVariableOperation.SUM,headerVariables);
150 
151 		GroupBuilder gb1 = new GroupBuilder();
152 
153 //		 define the criteria column to group by (columnState)
154 		DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState)
155 				.addFooterVariable(columnAmount,DJCalculation.SUM,headerVariables) 
156 				.addFooterVariable(columnaQuantity,DJCalculation.SUM,headerVariables) 
157 				.setGroupLayout(GroupLayout.VALUE_IN_HEADER_WITH_HEADERS) 
158 				.build();
159 
160 		GroupBuilder gb2 = new GroupBuilder(); // Create another group (using another column as criteria)
161 		DJGroup g2 = gb2.setCriteriaColumn((PropertyColumn) columnBranch) // and we add the same operations for the columnAmount and
162 				.addFooterVariable(columnAmount,DJCalculation.SUM) // columnaQuantity columns
163 				.addFooterVariable(columnaQuantity,	DJCalculation.SUM)
164 				.build();
165 
166 		drb.addColumn(columnState);
167 		drb.addColumn(columnBranch);
168 		drb.addColumn(columnaProductLine);
169 		drb.addColumn(columnaItem);
170 		drb.addColumn(columnCode);
171 		drb.addColumn(columnaQuantity);
172 		drb.addColumn(columnAmount);
173 
174 		drb.addGroup(g1); // add group g1
175 //		drb.addGroup(g2); // add group g2
176 
177 		drb.setUseFullPageWidth(true);
178 		drb.addAutoText(AutoText.AUTOTEXT_PAGE_X_SLASH_Y, AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_RIGHT);
179 
180 		DynamicReport dr = drb.build();
181 		return dr;
182 	}
183 
184 	public static void main(String[] args) throws Exception {
185 		GroupsReportTestG1T7 test = new GroupsReportTestG1T7();
186 		test.testReport();
187 		JasperViewer.viewReport(test.jp);
188 	}
189 
190 }