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