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