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  
33  import java.util.Date;
34  
35  import net.sf.jasperreports.view.JasperDesignViewer;
36  import net.sf.jasperreports.view.JasperViewer;
37  import ar.com.fdvs.dj.domain.DJCalculation;
38  import ar.com.fdvs.dj.domain.DynamicReport;
39  import ar.com.fdvs.dj.domain.Style;
40  import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
41  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
42  import ar.com.fdvs.dj.domain.customexpression.DJSimpleExpression;
43  import ar.com.fdvs.dj.domain.entities.DJVariable;
44  
45  public class ReportWithVariablesTest extends BaseDjReportTest {
46  
47  	public DynamicReport buildReport() throws Exception {
48  		Style style = new Style("currency");
49  		style.setHorizontalAlign(HorizontalAlign.RIGHT);
50  		style.setPattern("$ #,###.00");
51  
52  		/**
53  		 * Creates the DynamicReportBuilder and sets the basic options for
54  		 * the report
55  		 */
56  		FastReportBuilder drb = new FastReportBuilder();
57  		drb.addColumn("State", "state", String.class.getName(),30)
58  			.addColumn("Branch", "branch", String.class.getName(),30)
59  			.addColumn("Product Line", "productLine", String.class.getName(),50)
60  			.addColumn("Item", "item", String.class.getName(),50)
61  			.addColumn("Item Code", "id", Long.class.getName(),30,true)
62  			.addColumn("Quantity", "quantity", Long.class.getName(),60,true)
63  			.addColumn("Amount", "amount", Float.class.getName(),70,true)
64  			.addColumn("Balance",new DJSimpleExpression(DJSimpleExpression.TYPE_VARIABLE, "acum", Float.class.getName()),80,false,null,style)
65  			.addVariable("acum", DJCalculation.SUM, new DJSimpleExpression(DJSimpleExpression.TYPE_FIELD, "amount", Float.class.getName()))
66  			.addGroups(2)
67  			.setTitle("November " + getYear() + " sales report")
68  			.setSubtitle("This report was generated at " + new Date())
69  			.setPrintBackgroundOnOddRows(true)			
70  			.setUseFullPageWidth(true);
71  
72  
73  //		AbstractColumn columnWithVar = ColumnBuilder.getNew().setTitle("Acumn")
74  //			.setCustomExpression(new DJSimpleExpression(DJSimpleExpression.TYPE_VARIABLE, "acum", Float.class.getName())).setWidth(80).setStyle(style).build();
75  //		
76  //		drb.addColumn(columnWithVar);
77  		
78  		DynamicReport dr = drb.build();
79  		
80  //		DJVariable var = createVariable();
81  //		
82  //		dr.getVariables().add(var);
83  		
84  		
85  
86  		return dr;
87  	}
88  
89  	private DJVariable createVariable() {
90  		DJSimpleExpression expression = new DJSimpleExpression(DJSimpleExpression.TYPE_FIELD, "amount", Float.class.getName());
91  		DJVariable var = new DJVariable("acum",Float.class.getName(),DJCalculation.SUM,expression);
92  		return var;
93  	}
94  
95  	public static void main(String[] args) throws Exception {
96  		ReportWithVariablesTest test = new ReportWithVariablesTest();
97  		test.testReport();
98  		test.exportToJRXML();
99  		JasperViewer.viewReport(test.jp);	//finally display the report report
100 		JasperDesignViewer.viewReportDesign(test.jr);
101 	}
102 
103 }