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.DJValueFormatter;
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.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 VariableValueFormatterReportTest extends BaseDjReportTest {
54  
55  	public DynamicReport buildReport() throws Exception {
56  
57  		Style detailStyle = new Style();
58  		Style headerStyle = new Style();
59  		headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD); headerStyle.setBorder(Border.PEN_2_POINT());
60  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER); headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
61  
62  		Style titleStyle = new Style();
63  		titleStyle.setFont(new Font(18,Font._FONT_VERDANA,true));
64  		Style amountStyle = new Style();
65  		amountStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
66  		amountStyle.setStretchWithOverflow(true);
67  		Style oddRowStyle = new Style();
68  		oddRowStyle.setBorder(Border.NO_BORDER());
69  		Color veryLightGrey = new Color(230,230,230);
70  		oddRowStyle.setBackgroundColor(veryLightGrey);oddRowStyle.setTransparency(Transparency.OPAQUE);
71  
72  		DynamicReportBuilder drb = new DynamicReportBuilder();
73  		Integer margin = new Integer(20);
74  		drb
75  			.setTitle("November " + getYear() +" sales report")					//defines the title of the report
76  			.setSubtitle("The items in this report correspond "
77  					+"to the main products: DVDs, Books, Foods and Magazines")
78  					.setTitleStyle(titleStyle).setTitleHeight(new Integer(30))
79  			.setSubtitleHeight(new Integer(20))
80  			.setDetailHeight(new Integer(15))
81  			.setLeftMargin(margin)
82  			.setRightMargin(margin)
83  			.setTopMargin(margin)
84  			.setBottomMargin(margin)
85  			.setPrintBackgroundOnOddRows(true)
86  			.setOddRowBackgroundStyle(oddRowStyle)
87  			.setColumnsPerPage(new Integer(1))
88  			.setColumnSpace(new Integer(5));
89  
90  		AbstractColumn columnState = ColumnBuilder.getNew().setColumnProperty("state", String.class.getName())
91  			.setTitle("State").setWidth(new Integer(85))
92  			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
93  
94  		AbstractColumn columnaItem = ColumnBuilder.getNew().setColumnProperty("item", String.class.getName())
95  			.setTitle("item").setWidth(new Integer(85))
96  			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
97  
98  		AbstractColumn columnCode = ColumnBuilder.getNew().setColumnProperty("id", Long.class.getName())
99  			.setTitle("ID").setWidth(new Integer(40))
100 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
101 
102 		AbstractColumn columnaCantidad = ColumnBuilder.getNew().setColumnProperty("quantity", Long.class.getName())
103 			.setTitle("Quantity").setWidth(new Integer(80))
104 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
105 
106 		AbstractColumn columnAmount = ColumnBuilder.getNew().setColumnProperty("amount", Float.class.getName())
107 			.setTitle("Amount").setWidth(new Integer(90)).setPattern("$ 0.00")
108 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
109 
110 		AbstractColumn columnaCustomExpression = ColumnBuilder.getNew()
111 			.setCustomExpression(getCustomExpression())
112 			.setCustomExpressionForCalculation(getCustomExpression2())
113 			.setTitle("Duration").setWidth(new Integer(90))
114 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
115 
116 		drb.addColumn(columnState);
117 		drb.addColumn(columnaItem);
118 		drb.addColumn(columnCode);
119 		drb.addColumn(columnaCantidad);
120 		drb.addColumn(columnAmount);
121 		drb.addColumn(columnaCustomExpression);
122 		
123 		DJGroup group = new GroupBuilder()
124 			.setCriteriaColumn((PropertyColumn) columnState)
125 			.addFooterVariable(columnaCustomExpression, DJCalculation.SUM, null, getValueFormatter())
126 			.build();
127 		
128 		drb.addGroup(group);
129 
130 		drb.setUseFullPageWidth(true);
131 
132 		drb.addField("productLine", String.class.getName());
133 		drb.addField("branch", String.class.getName());
134 
135 		drb.addGlobalFooterVariable(columnaCustomExpression, DJCalculation.SUM,amountStyle, getValueFormatter());
136 		drb.setGrandTotalLegend("");
137 		
138 		DynamicReport dr = drb.build();
139 		return dr;
140 	}
141 	
142 	public static String getAsMinutes(Long value) {
143 		Long amount = (Long) value;
144 		int sec = amount.intValue() % 60;
145 		int mins = amount.intValue() / 60;
146 		return mins + "' " + sec + "\"";
147 	}
148 	
149 	private DJValueFormatter getValueFormatter() {
150 		return new DJValueFormatter(){
151 
152 			public Object evaluate(Object value, Map fields, Map variables, Map parameters) {
153 				return "Total time: " + getAsMinutes((Long) value);
154 			}
155 
156 			public String getClassName() {
157 				return String.class.getName();
158 			}};
159 	}
160 
161 	private CustomExpression getCustomExpression() {
162 		return new CustomExpression() {
163 
164 			public Object evaluate(Map fields, Map variables, Map parameters) {
165 				Long amount = (Long) fields.get("quantity");
166 				return getAsMinutes((Long) amount);
167 			}
168 
169 			public String getClassName() {
170 				return String.class.getName();
171 			}
172 
173 		};
174 	}
175 
176 	private CustomExpression getCustomExpression2() {
177 		return new CustomExpression() {
178 			
179 			public Object evaluate(Map fields, Map variables, Map parameters) {
180 				return fields.get("quantity");
181 			}
182 			
183 			public String getClassName() {
184 				return Long.class.getName();
185 			}
186 			
187 		};
188 	}
189 
190 	public static void main(String[] args) throws Exception {
191 		VariableValueFormatterReportTest test = new VariableValueFormatterReportTest();
192 
193 		test.testReport();
194 		JasperViewer.viewReport(test.jp);
195 	}
196 	
197 //	public void testReport(){
198 //		log.warn("Skipping this test");
199 //	}
200 
201 }