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  import java.util.Map;
34  
35  import net.sf.jasperreports.view.JasperViewer;
36  import ar.com.fdvs.dj.domain.AutoText;
37  import ar.com.fdvs.dj.domain.CustomExpression;
38  import ar.com.fdvs.dj.domain.DJCalculation;
39  import ar.com.fdvs.dj.domain.DynamicReport;
40  import ar.com.fdvs.dj.domain.Style;
41  import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
42  import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
43  import ar.com.fdvs.dj.domain.builders.GroupBuilder;
44  import ar.com.fdvs.dj.domain.constants.Border;
45  import ar.com.fdvs.dj.domain.constants.Font;
46  import ar.com.fdvs.dj.domain.constants.GroupLayout;
47  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
48  import ar.com.fdvs.dj.domain.constants.Transparency;
49  import ar.com.fdvs.dj.domain.constants.VerticalAlign;
50  import ar.com.fdvs.dj.domain.entities.DJGroup;
51  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
52  import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;
53  
54  public class GroupsAndExpressionReportTest 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_SMALL_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  			.setOddRowBackgroundStyle(oddRowStyle);
97  
98  
99  		AbstractColumn columnState = ColumnBuilder.getNew()
100 				.setColumnProperty("state", String.class.getName()).setTitle(
101 						"State").setWidth(new Integer(100))
102 				.setStyle(detailStyle).setHeaderStyle(titleStyle).build();
103 
104 		AbstractColumn columnBranch = ColumnBuilder.getNew()
105 				.setColumnProperty("branch", String.class.getName()).setTitle(
106 						"Branch").setWidth(new Integer(85)).setStyle(
107 						detailStyle).setHeaderStyle(headerStyle).build();
108 
109 		AbstractColumn columnaProductLine = ColumnBuilder.getNew()
110 				.setColumnProperty("productLine", String.class.getName())
111 				.setTitle("Product Line").setWidth(new Integer(85)).setStyle(
112 						detailStyle).setHeaderStyle(headerStyle).build();
113 
114 		AbstractColumn columnaItem = ColumnBuilder.getNew()
115 				.setColumnProperty("item", String.class.getName()).setTitle(
116 						"Item").setWidth(new Integer(85)).setStyle(detailStyle)
117 				.setHeaderStyle(headerStyle).build();
118 
119 		AbstractColumn columnCode = ColumnBuilder.getNew()
120 				.setColumnProperty("id", Long.class.getName()).setTitle("ID")
121 				.setWidth(new Integer(40)).setStyle(importeStyle)
122 				.setHeaderStyle(headerStyle).build();
123 
124 		AbstractColumn columnaQuantity = ColumnBuilder.getNew()
125 				.setColumnProperty("quantity", Long.class.getName()).setTitle(
126 						"Quantity").setWidth(new Integer(80)).setStyle(
127 						importeStyle).setHeaderStyle(headerStyle).build();
128 
129 		AbstractColumn columnAmount = ColumnBuilder.getNew()
130 				.setColumnProperty("amount", Float.class.getName()).setTitle(
131 						"Amount").setWidth(new Integer(90))
132 				.setPattern("$ 0.00").setStyle(importeStyle).setHeaderStyle(
133 						headerStyle).build();
134 
135 		drb.addGlobalHeaderVariable(columnAmount, DJCalculation.SUM,headerVariables);
136 		drb.addGlobalHeaderVariable(columnaQuantity, DJCalculation.SUM,headerVariables);
137 		drb.addGlobalFooterVariable(columnAmount, DJCalculation.SUM,headerVariables);
138 		drb.addGlobalFooterVariable(columnaQuantity, DJCalculation.SUM,headerVariables);
139 
140 		CustomExpression customExpression = new CustomExpression(){
141 			public Object evaluate(Map fields, Map variables, Map parameters) {
142 				return variables.get("PAGE_NUMBER");
143 			}
144 			public String getClassName() {
145 				return Integer.class.getName();
146 			}
147 
148 		};
149 		AbstractColumn exprCol = ColumnBuilder.getNew().setCustomExpression(customExpression)
150 		.build();
151 
152 
153 		GroupBuilder gb1 = new GroupBuilder();
154 
155 //		 define the criteria column to group by (columnState)
156 		DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) exprCol)
157 				.setGroupLayout(GroupLayout.EMPTY)
158 				.setStartInNewPage(true)
159 				.build();
160 
161 		GroupBuilder gb2 = new GroupBuilder(); // Create another group (using another column as criteria)
162 
163 		drb.addColumn(exprCol);
164 		drb.addColumn(columnState);
165 		drb.addColumn(columnBranch);
166 		drb.addColumn(columnaProductLine);
167 		drb.addColumn(columnaItem);
168 		drb.addColumn(columnCode);
169 		drb.addColumn(columnaQuantity);
170 		drb.addColumn(columnAmount);
171 
172 		drb.addGroup(g1); // add group g1
173 
174 		drb.setUseFullPageWidth(true);
175 		drb.addAutoText(AutoText.AUTOTEXT_PAGE_X_OF_Y, AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_RIGHT);
176 
177 		DynamicReport dr = drb.build();
178 		return dr;
179 	}
180 
181 	public static void main(String[] args) throws Exception {
182 		GroupsAndExpressionReportTest test = new GroupsAndExpressionReportTest();
183 		test.testReport();
184 		JasperViewer.viewReport(test.jp);
185 	}
186 	
187 	public void testReport(){
188 		log.warn("Skipping this test");
189 	}
190 
191 }