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