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.Style;
38  import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
39  import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
40  import ar.com.fdvs.dj.domain.builders.GroupBuilder;
41  import ar.com.fdvs.dj.domain.constants.Border;
42  import ar.com.fdvs.dj.domain.constants.Font;
43  import ar.com.fdvs.dj.domain.constants.GroupLayout;
44  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
45  import ar.com.fdvs.dj.domain.constants.Transparency;
46  import ar.com.fdvs.dj.domain.constants.VerticalAlign;
47  import ar.com.fdvs.dj.domain.entities.DJGroup;
48  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
49  import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;
50  
51  public class PercentageColumnReportTest extends BaseDjReportTest {
52  
53  	public DynamicReport buildReport() throws Exception {
54  
55  		Style detailStyle = new Style();
56  		Style headerStyle = new Style();
57  		headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD); headerStyle.setBorder(Border.PEN_2_POINT());
58  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER); headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
59  
60  		Style titleStyle = new Style();
61  		titleStyle.setFont(new Font(18,Font._FONT_VERDANA,true));
62  		Style amountStyle = new Style();
63  		amountStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
64  		Style oddRowStyle = new Style();
65  		oddRowStyle.setBorder(Border.NO_BORDER());
66  		Color veryLightGrey = new Color(230,230,230);
67  		oddRowStyle.setBackgroundColor(veryLightGrey);oddRowStyle.setTransparency(Transparency.OPAQUE);
68  
69  		DynamicReportBuilder drb = new DynamicReportBuilder();
70  		Integer margin = new Integer(20);
71  		drb
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  					.setTitleStyle(titleStyle).setTitleHeight(new Integer(30))
76  			.setSubtitleHeight(new Integer(20))
77  			.setDetailHeight(new Integer(16))
78  			.setLeftMargin(margin)
79  			.setRightMargin(margin)
80  			.setTopMargin(margin)
81  			.setBottomMargin(margin)
82  			.setPrintBackgroundOnOddRows(true)
83  			.setOddRowBackgroundStyle(oddRowStyle)
84  			.setColumnsPerPage(new Integer(1))
85  			.setColumnSpace(new Integer(5));
86  
87  		AbstractColumn columnState = ColumnBuilder.getNew().setColumnProperty("state", String.class.getName())
88  			.setTitle("State").setWidth(new Integer(85))
89  			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
90  
91  		AbstractColumn columnBranch = ColumnBuilder.getNew().setColumnProperty("branch", String.class.getName())
92  			.setTitle("Branch").setWidth(new Integer(85))
93  			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
94  
95  //		AbstractColumn columnaProductLine = ColumnBuilder.getNew().setColumnProperty("productLine", String.class.getName())
96  //			.setTitle("Product Line").setWidth(new Integer(85))
97  //			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
98  
99  		AbstractColumn columnaItem = ColumnBuilder.getNew().setColumnProperty("item", String.class.getName())
100 			.setTitle("item").setWidth(new Integer(85))
101 			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
102 
103 		AbstractColumn columnCode = ColumnBuilder.getNew().setColumnProperty("id", Long.class.getName())
104 			.setTitle("ID").setWidth(new Integer(40))
105 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
106 
107 		AbstractColumn columnaCantidad = ColumnBuilder.getNew().setColumnProperty("quantity", Long.class.getName())
108 			.setTitle("Quantity").setWidth(new Integer(80))
109 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
110 
111 		AbstractColumn columnAmount = ColumnBuilder.getNew().setColumnProperty("amount", Float.class.getName())
112 			.setTitle("Amount").setWidth(new Integer(90)).setPattern("$ 0.00")
113 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
114 		
115 		AbstractColumn columnPercentageAmount = ColumnBuilder.getNew().setPercentageColumn((PropertyColumn) columnAmount)
116 		.setTitle("Amount [%]").setWidth(new Integer(90))
117 		.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
118 
119 		drb.addColumn(columnState);
120 		drb.addColumn(columnBranch);
121 //		drb.addColumn(columnaProductLine);
122 		drb.addColumn(columnaItem);
123 //		drb.addColumn(columnCode);
124 		drb.addColumn(columnaCantidad);
125 		drb.addColumn(columnAmount);		
126 		drb.addColumn(columnPercentageAmount);
127 		
128 		GroupBuilder gb1 = new GroupBuilder();		
129 //	 define the criteria column to group by (columnState)
130 	DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState)
131 			.addFooterVariable(columnAmount,DJCalculation.SUM)
132 			.setGroupLayout(GroupLayout.DEFAULT) // tells the group how to be shown, there are manyposibilities, see the GroupLayout for more.
133 			.setFooterVariablesHeight(new Integer(20))
134 			.setFooterHeight(new Integer(50),true)
135 			.setHeaderVariablesHeight(new Integer(35))
136 			.build();
137 	
138 	GroupBuilder gb2 = new GroupBuilder();		
139 	DJGroup g2 = gb2.setCriteriaColumn((PropertyColumn) columnBranch)
140 	.addFooterVariable(columnAmount,DJCalculation.SUM)
141 	.addFooterVariable(columnPercentageAmount,DJCalculation.SUM)
142 	.setGroupLayout(GroupLayout.DEFAULT) // tells the group how to be shown, there are manyposibilities, see the GroupLayout for more.
143 	.setFooterVariablesHeight(new Integer(20))
144 	.setFooterHeight(new Integer(50),true)
145 	.setHeaderVariablesHeight(new Integer(35))
146 	.build();
147 		
148 	drb.addGroup(g1);
149 	drb.addGroup(g2);
150 	
151 	
152 		drb.setUseFullPageWidth(true);
153 
154 		drb.addField("productLine", String.class.getName());
155 //		drb.addField("branch", String.class.getName());
156 
157 		DynamicReport dr = drb.build();
158 		return dr;
159 	}
160 	
161 	public static void main(String[] args) throws Exception {
162 		PercentageColumnReportTest test = new PercentageColumnReportTest();
163 
164 		test.testReport();
165 		
166 		JasperViewer.viewReport(test.jp);
167 	}
168 	
169 	protected void exportReport() throws Exception {
170 		ReportExporter.exportReport(jp, System.getProperty("user.dir")+ "/target/reports/" + this.getClass().getName() + ".pdf");
171 		ReportExporter.exportReportXls(jp, System.getProperty("user.dir")+ "/target/reports/" + this.getClass().getName() + ".xls");
172 		exportToJRXML();
173 	}	
174 
175 }