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