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