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.text.FieldPosition;
34  import java.text.Format;
35  import java.text.ParsePosition;
36  import java.util.Locale;
37  import java.util.Map;
38  import java.util.Random;
39  
40  import net.sf.jasperreports.view.JasperViewer;
41  import ar.com.fdvs.dj.domain.AutoText;
42  import ar.com.fdvs.dj.domain.CustomExpression;
43  import ar.com.fdvs.dj.domain.DJCalculation;
44  import ar.com.fdvs.dj.domain.DJChart;
45  import ar.com.fdvs.dj.domain.DynamicReport;
46  import ar.com.fdvs.dj.domain.ImageBanner;
47  import ar.com.fdvs.dj.domain.Style;
48  import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
49  import ar.com.fdvs.dj.domain.builders.DJChartBuilder;
50  import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
51  import ar.com.fdvs.dj.domain.builders.GroupBuilder;
52  import ar.com.fdvs.dj.domain.constants.Border;
53  import ar.com.fdvs.dj.domain.constants.Font;
54  import ar.com.fdvs.dj.domain.constants.GroupLayout;
55  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
56  import ar.com.fdvs.dj.domain.constants.Transparency;
57  import ar.com.fdvs.dj.domain.constants.VerticalAlign;
58  import ar.com.fdvs.dj.domain.entities.DJGroup;
59  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
60  import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;
61  
62  public class ExpressionToGroupByReportTest extends BaseDjReportTest {
63  
64  	public DynamicReport buildReport() throws Exception {
65  
66  		Style detailStyle = new Style();
67  		Style headerStyle = new Style();
68  		headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD); headerStyle.setBorder(Border.PEN_2_POINT());
69  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER); headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
70  
71  		Style titleStyle = new Style();
72  		titleStyle.setFont(new Font(18,Font._FONT_VERDANA,true));
73  		Style importeStyle = new Style();
74  		importeStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
75  		Style oddRowStyle = new Style();
76  		oddRowStyle.setBorder(Border.NO_BORDER()); oddRowStyle.setBackgroundColor(Color.LIGHT_GRAY);oddRowStyle.setTransparency(Transparency.OPAQUE);
77  
78  		DynamicReportBuilder drb = new DynamicReportBuilder();
79  		Integer margin = new Integer(20);
80  			drb.setTitleStyle(titleStyle)
81  			.setTitle("November " + getYear() +" sales report")					//defines the title of the report
82  			.setSubtitle("The items in this report correspond "
83  					+"to the main products: DVDs, Books, Foods and Magazines")
84  			.setDetailHeight(new Integer(15))
85  			.setLeftMargin(margin)
86  			.setRightMargin(margin)
87  			.setTopMargin(margin)
88  			.setBottomMargin(margin)
89  			.setPrintBackgroundOnOddRows(true)
90  			.setPrintColumnNames(false)
91  			.setOddRowBackgroundStyle(oddRowStyle)
92  			.addFirstPageImageBanner(System.getProperty("user.dir") +"/target/test-classes/images/logo_fdv_solutions_60.jpg", new Integer(197), new Integer(60), ImageBanner.ALIGN_LEFT)
93  			.addFirstPageImageBanner(System.getProperty("user.dir") +"/target/test-classes/images/dynamicJasper_60.jpg", new Integer(300), new Integer(60), ImageBanner.ALIGN_RIGHT)
94  			.addImageBanner(System.getProperty("user.dir") +"/target/test-classes/images/logo_fdv_solutions_60.jpg", new Integer(100), new Integer(30), ImageBanner.ALIGN_LEFT)
95  			.addImageBanner(System.getProperty("user.dir") +"/target/test-classes/images/dynamicJasper_60.jpg", new Integer(150), new Integer(30), ImageBanner.ALIGN_RIGHT);
96  
97  		AbstractColumn columnState = ColumnBuilder.getNew().setColumnProperty("state", String.class.getName())
98  			.setTitle("State").setWidth(new Integer(85))
99  			.setCustomExpressionToGroupBy(new CustomExpression(){
100 				
101 				Random rd = new Random();
102 
103 				public Object evaluate(Map fields, Map variables, Map parameters) {					
104 					return rd.nextFloat()+"";
105 				}
106 
107 				public String getClassName() {
108 					return String.class.getName();
109 				}
110 				
111 			})
112 			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
113 
114 		AbstractColumn columnBranch = ColumnBuilder.getNew().setColumnProperty("branch", String.class.getName())
115 			.setTitle("Branch").setWidth(new Integer(85))
116 			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
117 
118 		AbstractColumn columnaProductLine = ColumnBuilder.getNew().setColumnProperty("productLine", String.class.getName())
119 			.setTitle("Product Line").setWidth(new Integer(85))
120 			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
121 
122 		AbstractColumn columnaItem = ColumnBuilder.getNew().setColumnProperty("item", String.class.getName())
123 			.setTitle("Item").setWidth(new Integer(85))
124 			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
125 
126 		AbstractColumn columnCode = ColumnBuilder.getNew().setColumnProperty("id", Long.class.getName())
127 			.setTitle("ID").setWidth(new Integer(40))
128 			.setStyle(importeStyle).setHeaderStyle(headerStyle).build();
129 
130 		AbstractColumn columnaQuantity = ColumnBuilder.getNew().setColumnProperty("quantity", Long.class.getName())
131 			.setTitle("Quantity").setWidth(new Integer(80))
132 			.setStyle(importeStyle).setHeaderStyle(headerStyle).build();
133 
134 		AbstractColumn columnAmount = ColumnBuilder.getNew().setColumnProperty("amount", Float.class.getName())
135 			.setTitle("Amount").setWidth(new Integer(90)).setPattern("$ 0.00")
136 			.setStyle(importeStyle).setHeaderStyle(headerStyle).build();
137 
138 		AbstractColumn columnaCode = ColumnBuilder.getNew().setColumnProperty("code.code", String.class.getName())
139 		.setTitle("Code").setWidth(new Integer(85))
140 		.setStyle(detailStyle).setHeaderStyle(headerStyle).build();		
141 		
142 		Format textFormatter = new Format(){
143 
144 			public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
145 				if (obj == null || Boolean.FALSE.equals(obj))
146 					toAppendTo.append("No");
147 				else
148 					toAppendTo.append("Yes");
149 				
150 				return toAppendTo;
151 			}
152 
153 			public Object parseObject(String source, ParsePosition pos) {
154 				return null;
155 			}};
156 		AbstractColumn columnavailable = ColumnBuilder.getNew().setColumnProperty("isAvailable", Boolean.class.getName())
157 		.setTitle("In stock").setWidth(new Integer(40)).setTextFormatter(textFormatter)
158 		.setStyle(importeStyle).setHeaderStyle(headerStyle).build();
159 
160 
161 		GroupBuilder gb1 = new GroupBuilder();
162 		DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState)		//define the criteria column to group by (columnState)
163 			.addFooterVariable(columnAmount,DJCalculation.SUM)		//tell the group place a variable in the footer
164 																					//of the column "columnAmount" with the SUM of all
165 																					//values of the columnAmount in this group.
166 
167 			.addFooterVariable(columnaQuantity,DJCalculation.SUM)	//idem for the columnaQuantity column
168 			.setGroupLayout(GroupLayout.DEFAULT_WITH_HEADER)				//tells the group how to be shown, there are many			
169 																					//posibilities, see the GroupLayout for more.
170 			.build();
171 
172 		GroupBuilder gb2 = new GroupBuilder();										//Create another group (using another column as criteria)
173 		DJGroup g2 = gb2.setCriteriaColumn((PropertyColumn) columnBranch)		//and we add the same operations for the columnAmount and
174 			.addFooterVariable(columnAmount,DJCalculation.SUM)		//columnaQuantity columns
175 			.addFooterVariable(columnaQuantity,DJCalculation.SUM)
176 			.build();
177 
178 		drb.addColumn(columnState);
179 		drb.addColumn(columnBranch);
180 		drb.addColumn(columnaProductLine);
181 		drb.addColumn(columnaItem);
182 		drb.addColumn(columnCode);
183 		drb.addColumn(columnaQuantity);
184 		drb.addColumn(columnAmount);
185 		drb.addColumn(columnavailable);
186 		drb.addColumn(columnaCode);
187 
188 		drb.addGroup(g1);	//add group g1
189 //		drb.addGroup(g2);	//add group g2
190 
191 		drb.setUseFullPageWidth(true);
192 
193 		//Autotext
194 		drb.addAutoText(AutoText.AUTOTEXT_CREATED_ON, AutoText.POSITION_HEADER, AutoText.ALIGNMENT_LEFT,AutoText.PATTERN_DATE_DATE_TIME);
195 		drb.addAutoText(AutoText.AUTOTEXT_PAGE_X_OF_Y, AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_LEFT);
196 
197 		//i18N
198 		drb.setReportLocale(Locale.ENGLISH);
199 
200 		//Charts
201 		DJChartBuilder cb = new DJChartBuilder();
202 		DJChart chart =  cb.setType(DJChart.BAR_CHART)
203 						.setOperation(DJChart.CALCULATION_SUM)
204 						.setColumnsGroup(g1).setHeight(150)
205 						.addColumn(columnAmount)
206 						.build();
207 
208 		drb.addChart(chart); //add chart
209 
210 		DynamicReport dr = drb.build();
211 		return dr;
212 	}
213 
214 	public static void main(String[] args) throws Exception {
215 		ExpressionToGroupByReportTest test = new ExpressionToGroupByReportTest();
216 		test.testReport();
217 		JasperViewer.viewReport(test.jp);
218 //		JasperDesignViewer.viewReportDesign(test.jr);
219 //		JRXmlWriter.writeReport(test.jr, "e:\\temp\\reporte.jrxml", "UTF-8");
220 	}
221 
222 }