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 ar.com.fdvs.dj.domain.DynamicReport;
33  import ar.com.fdvs.dj.domain.Style;
34  import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
35  import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
36  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
37  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
38  
39  public class PlainReportTest extends BaseDjReportTest {
40  
41  	public DynamicReport buildReport() throws Exception {
42  
43  		Style detailStyle = new Style();
44  		Style headerStyle = new Style();
45  
46  		Style titleStyle = new Style();
47  		Style subtitleStyle = new Style();
48  		Style amountStyle = new Style(); amountStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
49  
50  		/**
51  		 * Creates the DynamicReportBuilder and sets the basic options for
52  		 * the report
53  		 */
54  		DynamicReportBuilder drb = new DynamicReportBuilder();
55  		drb.setTitle("November " + getYear() +" sales report")					//defines the title of the report
56  			.setSubtitle("The items in this report correspond "
57  					+"to the main products: DVDs, Books, Foods and Magazines")
58  			.setDetailHeight(15)						//defines the height for each record of the report
59  			.setMargins(30, 20, 30, 15)							//define the margin space for each side (top, bottom, left and right)
60  			.setDefaultStyles(titleStyle, subtitleStyle, headerStyle, detailStyle)
61  			.setColumnsPerPage(1);						//defines columns per page (like in the telephone guide)
62  
63  		/**
64  		 * Note that we still didn�t call the build() method
65  		 */
66  
67  		/**
68  		 * Column definitions. We use a new ColumnBuilder instance for each
69  		 * column, the ColumnBuilder.getNew() method returns a new instance
70  		 * of the builder
71  		 */
72  		AbstractColumn columnState = ColumnBuilder.getNew()		//creates a new instance of a ColumnBuilder
73  			.setColumnProperty("state", String.class.getName())			//defines the field of the data source that this column will show, also its type
74  			.setTitle("State")											//the title for the column
75  			.setWidth(85)									//the width of the column
76  			.build();													//builds and return a new AbstractColumn
77  
78  		//Create more columns
79  		AbstractColumn columnBranch = ColumnBuilder.getNew()
80  			.setColumnProperty("branch", String.class.getName())
81  			.setTitle("Branch").setWidth(85)
82  			.build();
83  
84  		AbstractColumn columnaProductLine = ColumnBuilder.getNew()
85  			.setColumnProperty("productLine", String.class.getName())
86  			.setTitle("Product Line").setWidth(85)
87  			.build();
88  
89  		AbstractColumn columnaItem = ColumnBuilder.getNew()
90  			.setColumnProperty("item", String.class.getName())
91  			.setTitle("Item").setWidth(85)
92  			.build();
93  
94  		AbstractColumn columnCode = ColumnBuilder.getNew()
95  			.setColumnProperty("id", Long.class.getName())
96  			.setTitle("ID").setWidth(40)
97  			.build();
98  
99  		AbstractColumn columnaCantidad = ColumnBuilder.getNew()
100 			.setColumnProperty("quantity", Long.class.getName())
101 			.setTitle("Quantity").setWidth(80)
102 			.build();
103 
104 		AbstractColumn columnAmount = ColumnBuilder.getNew()
105 			.setColumnProperty("amount", Float.class.getName())
106 			.setTitle("Amount").setWidth(90)
107 			.setPattern("$ 0.00")		//defines a pattern to apply to the values swhown (uses TextFormat)
108 			.setStyle(amountStyle)		//special style for this column (align right)
109 			.build();
110 
111 		/**
112 		 * We add the columns to the report (through the builder) in the order
113 		 * we want them to appear
114 		 */
115 		drb.addColumn(columnState);
116 		drb.addColumn(columnBranch);
117 		drb.addColumn(columnaProductLine);
118 		drb.addColumn(columnaItem);
119 		drb.addColumn(columnCode);
120 		drb.addColumn(columnaCantidad);
121 		drb.addColumn(columnAmount);
122 
123 		/**
124 		 * add some more options to the report (through the builder)
125 		 */
126 		drb.setUseFullPageWidth(true);	//we tell the report to use the full width of the page. this rezises
127 										//the columns width proportionally to meat the page width.
128 
129 
130 		DynamicReport dr = drb.build();	//Finally build the report!
131 
132 		return dr;
133 	}
134 
135     public static void main(String[] args) throws Exception {
136 		PlainReportTest test = new PlainReportTest();
137 		test.testReport();
138 	}
139 
140 }