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