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  
34  import net.sf.jasperreports.view.JasperViewer;
35  import ar.com.fdvs.dj.domain.DynamicReport;
36  import ar.com.fdvs.dj.domain.Style;
37  import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
38  import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
39  import ar.com.fdvs.dj.domain.constants.Border;
40  import ar.com.fdvs.dj.domain.constants.Font;
41  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
42  import ar.com.fdvs.dj.domain.constants.Transparency;
43  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
44  
45  public class TemplateFileReportTest extends BaseDjReportTest {
46  
47  	public DynamicReport buildReport() throws Exception {
48  
49  		Style detailStyle = new Style();
50  		Style headerStyle = new Style();
51  
52  		headerStyle.setBackgroundColor(new Color(230,230,230));
53  		headerStyle.setBorderBottom(Border.THIN());
54  		headerStyle.setBorderColor(Color.black);
55  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
56  		headerStyle.setTransparency(Transparency.OPAQUE);
57  
58  		Style titleStyle = new Style();
59  		titleStyle.setHorizontalAlign(HorizontalAlign.CENTER);
60  		titleStyle.setFont(Font.ARIAL_BIG_BOLD);
61  		Style subtitleStyle = new Style();
62  		Style amountStyle = new Style(); amountStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
63  
64  		/**
65  		 * Creates the DynamicReportBuilder and sets the basic options for
66  		 * the report
67  		 */
68  		DynamicReportBuilder drb = new DynamicReportBuilder();
69  		drb.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  			.setDetailHeight(15)						//defines the height for each record of the report
73  			.setMargins(30, 20, 30, 15)							//define the margin space for each side (top, bottom, left and right)
74  			.setDefaultStyles(titleStyle, subtitleStyle, headerStyle, detailStyle)
75  			.setColumnsPerPage(1);						//defines columns per page (like in the telephone guide)
76  
77  		/**
78  		 * Note that we didn't call the build() method yet
79  		 */
80  
81  		/**
82  		 * Column definitions. We use a new ColumnBuilder instance for each
83  		 * column, the ColumnBuilder.getNew() method returns a new instance
84  		 * of the builder
85  		 */
86  		AbstractColumn columnState = ColumnBuilder.getNew()		//creates a new instance of a ColumnBuilder
87  			.setColumnProperty("state", String.class.getName())			//defines the field of the data source that this column will show, also its type
88  			.setTitle("State")											//the title for the column
89  			.setWidth(85)									//the width of the column
90  			.build();													//builds and return a new AbstractColumn
91  
92  		//Create more columns
93  		AbstractColumn columnBranch = ColumnBuilder.getNew()
94  			.setColumnProperty("branch", String.class.getName())
95  			.setTitle("Branch").setWidth(85)
96  			.build();
97  
98  		AbstractColumn columnaProductLine = ColumnBuilder.getNew()
99  			.setColumnProperty("productLine", String.class.getName())
100 			.setTitle("Product Line").setWidth(85)
101 			.build();
102 
103 		AbstractColumn columnaItem = ColumnBuilder.getNew()
104 			.setColumnProperty("item", String.class.getName())
105 			.setTitle("Item").setWidth(85)
106 			.build();
107 
108 		AbstractColumn columnCode = ColumnBuilder.getNew()
109 			.setColumnProperty("id", Long.class.getName())
110 			.setTitle("ID").setWidth(40)
111 			.build();
112 
113 		AbstractColumn columnaCantidad = ColumnBuilder.getNew()
114 			.setColumnProperty("quantity", Long.class.getName())
115 			.setTitle("Quantity").setWidth(80)
116 			.build();
117 
118 		AbstractColumn columnAmount = ColumnBuilder.getNew()
119 			.setColumnProperty("amount", Float.class.getName())
120 			.setTitle("Amount").setWidth(90)
121 			.setPattern("$ 0.00")		//defines a pattern to apply to the values swhown (uses TextFormat)
122 			.setStyle(amountStyle)		//special style for this column (align right)
123 			.build();
124 
125 		/**
126 		 * We add the columns to the report (through the builder) in the order
127 		 * we want them to appear
128 		 */
129 		drb.addColumn(columnState);
130 		drb.addColumn(columnBranch);
131 		drb.addColumn(columnaProductLine);
132 		drb.addColumn(columnaItem);
133 		drb.addColumn(columnCode);
134 		drb.addColumn(columnaCantidad);
135 		drb.addColumn(columnAmount);
136 
137 		/**
138 		 * add some more options to the report (through the builder)
139 		 */
140 		drb.setUseFullPageWidth(true);	//we tell the report to use the full width of the page. this rezises
141 										//the columns width proportionally to meet the page width.
142 
143 		//This look for the resource in the classpath
144 		drb.setTemplateFile("templates/TemplateReportTest.jrxml");
145 
146 		//Portrait (looks the resource as a file in the filesystem)
147 //		drb.setTemplateFile(System.getProperty("user.dir") + "/target/test-classes/templates/TemplateReportTest.jrxml");
148 
149 		//Landscape  (looks the resource as a file in the filesystem)
150 //		drb.setTemplateFile(System.getProperty("user.dir") + "/target/test-classes/templates/TemplateReportTestPortLandscape.jrxml");
151 
152 		DynamicReport dr = drb.build();	//Finally build the report!
153 
154 		params.put("leftHeader", "DynamicJasper is the Best!!!");
155 		params.put("rightHeader", "This is the right header");
156 		
157 		return dr;
158 	}
159 
160     public static void main(String[] args) throws Exception {
161 		TemplateFileReportTest test = new TemplateFileReportTest();
162 		test.testReport();
163 		JasperViewer.viewReport(test.jp);	//finally display the report report
164 	}
165 
166 }