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.CustomExpression;
37  import ar.com.fdvs.dj.domain.DJCalculation;
38  import ar.com.fdvs.dj.domain.DynamicReport;
39  import ar.com.fdvs.dj.domain.ExpressionHelper;
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.DJGroupVariable;
52  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
53  import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;
54  
55  public class CustomExpressionReportTest3 extends BaseDjReportTest {
56  
57  	public DynamicReport buildReport() throws Exception {
58  
59  		Style detailStyle = new Style();
60  		Style headerStyle = new Style();
61  		headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD); headerStyle.setBorder(Border.PEN_2_POINT());
62  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER); headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
63  
64  		Style titleStyle = new Style();
65  		titleStyle.setFont(new Font(18,Font._FONT_VERDANA,true));
66  		Style amountStyle = new Style();
67  		amountStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
68  		Style oddRowStyle = new Style();
69  		oddRowStyle.setBorder(Border.NO_BORDER());
70  		Color veryLightGrey = new Color(230,230,230);
71  		oddRowStyle.setBackgroundColor(veryLightGrey);oddRowStyle.setTransparency(Transparency.OPAQUE);
72  
73  		DynamicReportBuilder drb = new DynamicReportBuilder();
74  		Integer margin = new Integer(20);
75  		drb
76  			.setTitle("November " + getYear() +" sales report")					//defines the title of the report
77  			.setSubtitle("The items in this report correspond "
78  					+"to the main products: DVDs, Books, Foods and Magazines")
79  					.setTitleStyle(titleStyle).setTitleHeight(new Integer(30))
80  			.setSubtitleHeight(new Integer(20))
81  			.setDetailHeight(new Integer(15))
82  			.setLeftMargin(margin)
83  			.setRightMargin(margin)
84  			.setTopMargin(margin)
85  			.setBottomMargin(margin)
86  			.setPrintBackgroundOnOddRows(true)
87  			.setOddRowBackgroundStyle(oddRowStyle)
88  			.setColumnsPerPage(new Integer(1))
89  			.setColumnSpace(new Integer(5));
90  
91  		AbstractColumn columnState = ColumnBuilder.getNew().setColumnProperty("state", String.class.getName())
92  			.setTitle("State").setWidth(new Integer(85))
93  			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
94  
95  //		AbstractColumn columnBranch = ColumnBuilder.getNew().setColumnProperty("branch", String.class.getName())
96  //			.setTitle("Branch").setWidth(new Integer(85))
97  //			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
98  
99  //		AbstractColumn columnaProductLine = ColumnBuilder.getNew().setColumnProperty("productLine", String.class.getName())
100 //			.setTitle("Product Line").setWidth(new Integer(85))
101 //			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
102 
103 		AbstractColumn columnaItem = ColumnBuilder.getNew().setColumnProperty("item", String.class.getName())
104 			.setTitle("item").setWidth(new Integer(85))
105 			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
106 
107 		AbstractColumn columnCode = ColumnBuilder.getNew().setColumnProperty("id", Long.class.getName())
108 			.setTitle("ID").setWidth(new Integer(40))
109 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
110 
111 		AbstractColumn columnaCantidad = ColumnBuilder.getNew().setColumnProperty("quantity", Long.class.getName())
112 			.setTitle("Quantity").setWidth(new Integer(80))
113 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
114 
115 		AbstractColumn columnAmount = ColumnBuilder.getNew().setColumnProperty("amount", Float.class.getName())
116 			.setTitle("Amount").setWidth(new Integer(90)).setPattern("$ 0.00")
117 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
118 
119 		AbstractColumn columnaCustomExpression = ColumnBuilder.getNew()
120 		.setCustomExpression(getRatioExpression())
121 		//.setCustomExpressionForCalculation(getCustomExpression3())
122 		//.setColumnProperty("item", String.class.getName())
123 		.setTitle("CustomExp").setWidth(new Integer(90)).setPattern("#,##0.0")
124 		.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
125 
126 		drb.addColumn(columnState);
127 //		drb.addColumn(columnBranch);
128 //		drb.addColumn(columnaProductLine);
129 		drb.addColumn(columnaItem);
130 		drb.addColumn(columnCode);
131 		drb.addColumn(columnaCantidad);
132 		drb.addColumn(columnAmount);
133 		drb.addColumn(columnaCustomExpression);
134 		
135 		drb.addGlobalVariable("quantity_sum", columnaCantidad, DJCalculation.SUM);
136 		drb.addGlobalVariable("amount_sum", columnAmount, DJCalculation.SUM);
137 		drb.addGlobalFooterVariable(columnaCustomExpression, getGlobalRatioExpression());
138 		
139 		GroupBuilder gb1 = new GroupBuilder("g1");
140 		DJGroupVariable var = new DJGroupVariable(columnaCantidad, DJCalculation.SUM);		
141 		var.setPrintWhenExpression(ExpressionHelper.printWhenGroupHasMoreThanOneRecord("g1"));
142 		
143 //	 define the criteria column to group by (columnState)
144 	DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState)
145 			.addFooterVariable(var)
146 			.addVariable("quantity_sum_g1", columnaCantidad,DJCalculation.SUM)
147 			.addVariable("amount_sum_g1", columnAmount,DJCalculation.SUM)
148 			.addFooterVariable(columnaCustomExpression, getGroupRatioExpression())
149 			.setGroupLayout(GroupLayout.VALUE_IN_HEADER) // tells the group how to be shown, there are manyposibilities, see the GroupLayout for more.
150 			.setFooterVariablesHeight(new Integer(20))
151 			.setFooterHeight(new Integer(50),true)
152 			.setHeaderVariablesHeight(new Integer(35))
153 			.build();	
154 	
155 	drb.addGroup(g1);
156 		drb.setUseFullPageWidth(true);
157 
158 		drb.addField("productLine", String.class.getName());
159 		drb.addField("branch", String.class.getName());
160 
161 		DynamicReport dr = drb.build();
162 		return dr;
163 	}
164 	
165 	private CustomExpression getRatioExpression() {
166 		return new CustomExpression() {
167 
168 			public Object evaluate(Map fields, Map variables, Map parameters) {
169 				Long quantity = (Long) fields.get("quantity");
170 				Float amount = (Float) fields.get("amount");
171 				return new Double(amount.doubleValue() / quantity.doubleValue());
172 			}
173 
174 			public String getClassName() {
175 				return Double.class.getName();
176 			}
177 
178 		};
179 	}
180 
181 	private CustomExpression getGlobalRatioExpression() {
182 		return new CustomExpression() {
183 
184 			public Object evaluate(Map fields, Map variables, Map parameters) {
185 				Long quantity = (Long) variables.get("quantity_sum");
186 				Float amount = (Float) variables.get("amount_sum");
187 				return new Double(amount.doubleValue() / quantity.doubleValue());
188 			}
189 
190 			public String getClassName() {
191 				return Double.class.getName();
192 			}
193 
194 		};
195 	}
196 	
197 	private CustomExpression getGroupRatioExpression() {
198 		return new CustomExpression() {
199 
200 			public Object evaluate(Map fields, Map variables, Map parameters) {
201 				Long quantity = (Long) variables.get("quantity_sum_g1");
202 				Float amount = (Float) variables.get("amount_sum_g1");
203 				return new Double(amount.doubleValue() / quantity.doubleValue());
204 			}
205 
206 			public String getClassName() {
207 				return Double.class.getName();
208 			}
209 
210 		};
211 	}
212 	
213 	/*private BooleanExpression getPrintWhenExpression() {
214 		return new BooleanExpression() {
215 
216 			public Object evaluate(Map fields, Map variables, Map parameters) {
217 				Long quantity = (Long) variables.get("quantity_sum_g1");
218 				return new Boolean(quantity.doubleValue() > 20000);
219 			}
220 
221 		};
222 	}*/
223 	
224 	public static void main(String[] args) throws Exception {
225 		CustomExpressionReportTest3 test = new CustomExpressionReportTest3();
226 
227 		test.testReport();
228 		JasperViewer.viewReport(test.jp);
229 	}
230 
231 }