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.constants.VerticalAlign;
44  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
45  import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;
46  
47  /**
48   * This test checks that percentage columns works as expected even when no groups are defined
49   * @author mamana
50   *
51   */
52  public class PercentageColumnReportTest2 extends BaseDjReportTest {
53  
54  	public DynamicReport buildReport() throws Exception {
55  
56  		Style detailStyle = new Style();
57  		Style headerStyle = new Style();
58  		headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD); headerStyle.setBorder(Border.PEN_2_POINT());
59  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER); headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
60  
61  		Style titleStyle = new Style();
62  		titleStyle.setFont(new Font(18,Font._FONT_VERDANA,true));
63  		Style amountStyle = new Style();
64  		amountStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
65  		Style oddRowStyle = new Style();
66  		oddRowStyle.setBorder(Border.NO_BORDER());
67  		Color veryLightGrey = new Color(230,230,230);
68  		oddRowStyle.setBackgroundColor(veryLightGrey);oddRowStyle.setTransparency(Transparency.OPAQUE);
69  
70  		DynamicReportBuilder drb = new DynamicReportBuilder();
71  		Integer margin = new Integer(20);
72  		drb
73  			.setTitle("November " + getYear() +" sales report")					//defines the title of the report
74  			.setSubtitle("The items in this report correspond "
75  					+"to the main products: DVDs, Books, Foods and Magazines")
76  					.setTitleStyle(titleStyle).setTitleHeight(new Integer(30))
77  			.setSubtitleHeight(new Integer(20))
78  			.setDetailHeight(new Integer(16))
79  			.setLeftMargin(margin)
80  			.setRightMargin(margin)
81  			.setTopMargin(margin)
82  			.setBottomMargin(margin)
83  			.setPrintBackgroundOnOddRows(true)
84  			.setOddRowBackgroundStyle(oddRowStyle)
85  			.setColumnsPerPage(new Integer(1))
86  			.setColumnSpace(new Integer(5));
87  
88  		AbstractColumn columnState = ColumnBuilder.getNew().setColumnProperty("state", String.class.getName())
89  			.setTitle("State").setWidth(new Integer(85))
90  			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
91  
92  		AbstractColumn columnBranch = ColumnBuilder.getNew().setColumnProperty("branch", String.class.getName())
93  			.setTitle("Branch").setWidth(new Integer(85))
94  			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
95  
96  		AbstractColumn columnaItem = ColumnBuilder.getNew().setColumnProperty("item", String.class.getName())
97  			.setTitle("item").setWidth(new Integer(85))
98  			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
99  
100 		AbstractColumn columnCode = ColumnBuilder.getNew().setColumnProperty("id", Long.class.getName())
101 			.setTitle("ID").setWidth(new Integer(40))
102 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
103 
104 		AbstractColumn columnaCantidad = ColumnBuilder.getNew().setColumnProperty("quantity", Long.class.getName())
105 			.setTitle("Quantity").setWidth(new Integer(80))
106 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
107 
108 		AbstractColumn columnAmount = ColumnBuilder.getNew().setColumnProperty("amount", Float.class.getName())
109 			.setTitle("Amount").setWidth(new Integer(90)).setPattern("$ 0.00")
110 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
111 		
112 		AbstractColumn columnPercentageAmount = ColumnBuilder.getNew().setPercentageColumn((PropertyColumn) columnAmount)
113 		.setTitle("Amount [%]").setWidth(new Integer(90))
114 		.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
115 
116 		drb.addColumn(columnState);
117 		drb.addColumn(columnBranch);
118 		drb.addColumn(columnaItem);
119 		drb.addColumn(columnaCantidad);
120 		drb.addColumn(columnAmount);		
121 		drb.addColumn(columnPercentageAmount);
122 	
123 		drb.setUseFullPageWidth(true);
124 
125 		drb.addField("productLine", String.class.getName());
126 
127 		DynamicReport dr = drb.build();
128 		return dr;
129 	}
130 	
131 	public static void main(String[] args) throws Exception {
132 		PercentageColumnReportTest2 test = new PercentageColumnReportTest2();
133 
134 		test.testReport();
135 		
136 		JasperViewer.viewReport(test.jp);
137 	}
138 	
139 	protected void exportReport() throws Exception {
140 		ReportExporter.exportReport(jp, System.getProperty("user.dir")+ "/target/reports/" + this.getClass().getName() + ".pdf");
141 		ReportExporter.exportReportXls(jp, System.getProperty("user.dir")+ "/target/reports/" + this.getClass().getName() + ".xls");
142 		exportToJRXML();
143 	}	
144 
145 }