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.DJCalculation;
36  import ar.com.fdvs.dj.domain.DynamicReport;
37  import ar.com.fdvs.dj.domain.ImageBanner;
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.builders.GroupBuilder;
42  import ar.com.fdvs.dj.domain.constants.Border;
43  import ar.com.fdvs.dj.domain.constants.Font;
44  import ar.com.fdvs.dj.domain.constants.GroupLayout;
45  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
46  import ar.com.fdvs.dj.domain.constants.ImageScaleMode;
47  import ar.com.fdvs.dj.domain.constants.Transparency;
48  import ar.com.fdvs.dj.domain.constants.VerticalAlign;
49  import ar.com.fdvs.dj.domain.entities.DJGroup;
50  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
51  import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;
52  
53  public class ImageBannerReportTest extends BaseDjReportTest {
54  
55  	public DynamicReport buildReport() throws Exception {
56  
57  		Style detailStyle = new Style();
58  		Style headerStyle = new Style();
59  		headerStyle.setFont(Font.VERDANA_MEDIUM_BOLD);
60  		headerStyle.setBorderBottom(Border.PEN_2_POINT());
61  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
62  		headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
63  		headerStyle.setBackgroundColor(Color.DARK_GRAY);
64  		headerStyle.setTextColor(Color.WHITE);
65  		headerStyle.setTransparency(Transparency.OPAQUE);
66  
67  		Style titleStyle = new Style();
68  		titleStyle.setFont(new Font(18,Font._FONT_VERDANA,true));
69  		Style importeStyle = new Style();
70  		importeStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
71  		Style oddRowStyle = new Style();
72  		oddRowStyle.setBorder(Border.NO_BORDER()); oddRowStyle.setBackgroundColor(Color.LIGHT_GRAY);oddRowStyle.setTransparency(Transparency.OPAQUE);
73  
74  		DynamicReportBuilder drb = new DynamicReportBuilder();
75  		Integer margin = new Integer(20);
76  			drb.setTitleStyle(titleStyle)
77  			.setTitle("November " + getYear() +" sales report")					//defines the title of the report
78  			.setSubtitle("The items in this report correspond "
79  					+"to the main products: DVDs, Books, Foods and Magazines")
80  			.setDetailHeight(new Integer(15))
81  			.setLeftMargin(margin)
82  			.setRightMargin(margin)
83  			.setTopMargin(margin)
84  			.setBottomMargin(margin)
85  			.setPrintBackgroundOnOddRows(true)
86  			.setOddRowBackgroundStyle(oddRowStyle)
87  			.addFirstPageImageBanner(System.getProperty("user.dir") +"/target/test-classes/images/logo_fdv_solutions_60.jpg", new Integer(197), new Integer(60), ImageBanner.ALIGN_LEFT)
88  			.addFirstPageImageBanner(System.getProperty("user.dir") +"/target/test-classes/images/dynamicJasper_60.jpg", new Integer(300), new Integer(60), ImageBanner.ALIGN_RIGHT)
89  			.addImageBanner(System.getProperty("user.dir") +"/target/test-classes/images/logo_fdv_solutions_60.jpg", new Integer(100), new Integer(25), ImageBanner.ALIGN_LEFT, ImageScaleMode.FILL)
90  			.addImageBanner(System.getProperty("user.dir") +"/target/test-classes/images/dynamicJasper_60.jpg", new Integer(150), new Integer(25), ImageBanner.ALIGN_RIGHT, ImageScaleMode.FILL);
91  
92  		AbstractColumn columnState = ColumnBuilder.getNew().setColumnProperty("state", String.class.getName())
93  			.setTitle("State").setWidth(new Integer(85))
94  			.setStyle(titleStyle).setHeaderStyle(headerStyle).build();
95  
96  		AbstractColumn columnBranch = ColumnBuilder.getNew().setColumnProperty("branch", String.class.getName())
97  			.setTitle("Branch").setWidth(new Integer(85))
98  			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
99  
100 		AbstractColumn columnaProductLine = ColumnBuilder.getNew().setColumnProperty("productLine", String.class.getName())
101 			.setTitle("Product Line").setWidth(new Integer(85))
102 			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
103 
104 		AbstractColumn columnaItem = ColumnBuilder.getNew().setColumnProperty("item", String.class.getName())
105 			.setTitle("Item").setWidth(new Integer(85))
106 			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
107 
108 		AbstractColumn columnCode = ColumnBuilder.getNew().setColumnProperty("id", Long.class.getName())
109 			.setTitle("ID").setWidth(new Integer(40))
110 			.setStyle(importeStyle).setHeaderStyle(headerStyle).build();
111 
112 		AbstractColumn columnaQuantity = ColumnBuilder.getNew().setColumnProperty("quantity", Long.class.getName())
113 			.setTitle("Quantity").setWidth(new Integer(80))
114 			.setStyle(importeStyle).setHeaderStyle(headerStyle).build();
115 
116 		AbstractColumn columnAmount = ColumnBuilder.getNew().setColumnProperty("amount", Float.class.getName())
117 			.setTitle("Amount").setWidth(new Integer(90)).setPattern("$ 0.00")
118 			.setStyle(importeStyle).setHeaderStyle(headerStyle).build();
119 
120 
121 		GroupBuilder gb1 = new GroupBuilder();
122 		DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState)		//define the criteria column to group by (columnState)
123 			.addFooterVariable(columnAmount,DJCalculation.SUM)		//tell the group place a variable in the footer
124 																					//of the column "columnAmount" with the SUM of all
125 																					//values of the columnAmount in this group.
126 
127 			.addFooterVariable(columnaQuantity,DJCalculation.SUM)	//idem for the columnaQuantity column
128 			.setGroupLayout(GroupLayout.VALUE_IN_HEADER)				//tells the group how to be shown, there are many
129 																					//posibilities, see the GroupLayout for more.
130 			.build();
131 
132 		GroupBuilder gb2 = new GroupBuilder();										//Create another group (using another column as criteria)
133 		DJGroup g2 = gb2.setCriteriaColumn((PropertyColumn) columnBranch)		//and we add the same operations for the columnAmount and
134 			.addFooterVariable(columnAmount,DJCalculation.SUM)		//columnaQuantity columns
135 			.addFooterVariable(columnaQuantity,DJCalculation.SUM)
136 			.build();
137 
138 		drb.addColumn(columnState);
139 		drb.addColumn(columnBranch);
140 		drb.addColumn(columnaProductLine);
141 		drb.addColumn(columnaItem);
142 		drb.addColumn(columnCode);
143 		drb.addColumn(columnaQuantity);
144 		drb.addColumn(columnAmount);
145 
146 		drb.addGroup(g1);	//add group g1
147 //		drb.addGroup(g2);	//add group g2
148 
149 		drb.setUseFullPageWidth(true);
150 
151 		DynamicReport dr = drb.build();
152 		return dr;
153 	}
154 
155 	public static void main(String[] args) throws Exception {
156 		ImageBannerReportTest test = new ImageBannerReportTest();
157 		test.testReport();
158 		JasperViewer.viewReport(test.jp);
159 	}
160 
161 }