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.groups;
31  
32  import java.awt.Color;
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  import net.sf.jasperreports.engine.JRParameter;
37  import net.sf.jasperreports.view.JasperViewer;
38  import ar.com.fdvs.dj.domain.AutoText;
39  import ar.com.fdvs.dj.domain.DJCalculation;
40  import ar.com.fdvs.dj.domain.DynamicReport;
41  import ar.com.fdvs.dj.domain.ImageBanner;
42  import ar.com.fdvs.dj.domain.Style;
43  import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
44  import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
45  import ar.com.fdvs.dj.domain.builders.GroupBuilder;
46  import ar.com.fdvs.dj.domain.constants.Border;
47  import ar.com.fdvs.dj.domain.constants.Font;
48  import ar.com.fdvs.dj.domain.constants.GroupLayout;
49  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
50  import ar.com.fdvs.dj.domain.constants.Transparency;
51  import ar.com.fdvs.dj.domain.constants.VerticalAlign;
52  import ar.com.fdvs.dj.domain.entities.DJGroup;
53  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
54  import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;
55  import ar.com.fdvs.dj.test.BaseDjReportTest;
56  
57  public class SystemVariableReportTest extends BaseDjReportTest {
58  
59  	public DynamicReport buildReport() throws Exception {
60  
61  		Style detailStyle = new Style();
62  
63  		Style headerStyle = new Style();
64  		headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD);
65  		headerStyle.setBorderBottom(Border.PEN_1_POINT());
66  		headerStyle.setBackgroundColor(Color.gray);
67  		headerStyle.setTextColor(Color.white);
68  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
69  		headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
70  		headerStyle.setTransparency(Transparency.OPAQUE);
71  
72  		Style headerVariables = new Style();
73  		headerVariables.setFont(Font.ARIAL_MEDIUM_BOLD);
74  //		headerVariables.setBorderBottom(Border.THIN());
75  		headerVariables.setHorizontalAlign(HorizontalAlign.RIGHT);
76  		headerVariables.setVerticalAlign(VerticalAlign.MIDDLE);
77  
78  		Style titleStyle = new Style();
79  		titleStyle.setFont(new Font(18, Font._FONT_VERDANA, true));
80  		Style importeStyle = new Style();
81  		importeStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
82  		Style oddRowStyle = new Style();
83  		oddRowStyle.setBorder(Border.NO_BORDER());
84  		oddRowStyle.setBackgroundColor(Color.LIGHT_GRAY);
85  		oddRowStyle.setTransparency(Transparency.OPAQUE);
86  
87  		DynamicReportBuilder drb = new DynamicReportBuilder();
88  		Integer margin = new Integer(20);
89  		drb
90  			.setTitleStyle(titleStyle)
91  			.setTitle("November 2006 sales report")					//defines the title of the report
92  			.setSubtitle("The items in this report correspond "
93  					+"to the main products: DVDs, Books, Foods and Magazines")
94  			.setDetailHeight(new Integer(15)).setLeftMargin(margin)
95  			.setRightMargin(margin).setTopMargin(margin).setBottomMargin(margin)
96  			.setPrintBackgroundOnOddRows(false)
97  			.setGrandTotalLegend("Grand Total")
98  			.setGrandTotalLegendStyle(headerVariables)
99  			.setDefaultStyles(titleStyle, null, headerStyle, detailStyle)
100 			.setPrintColumnNames(true)
101 			.addImageBanner(System.getProperty("user.dir") +"/target/test-classes/images/logo_fdv_solutions_60.jpg", new Integer(100), new Integer(30), ImageBanner.ALIGN_RIGHT)
102 			.setOddRowBackgroundStyle(oddRowStyle);
103 			
104 
105 
106 		AbstractColumn columnState = ColumnBuilder.getNew()
107 				.setColumnProperty("state", String.class.getName())
108 				.setTitle("State").setWidth(new Integer(85))
109 				//.setStyle(titleStyle).setHeaderStyle(titleStyle)
110 				.build();
111 
112 		AbstractColumn columnBranch = ColumnBuilder.getNew()
113 				.setColumnProperty("branch", String.class.getName())
114 				.setTitle("Branch").setWidth(new Integer(85))
115 				.setStyle(detailStyle).setHeaderStyle(headerStyle)
116 				.build();
117 
118 		AbstractColumn columnaProductLine = ColumnBuilder.getNew()
119 				.setColumnProperty("productLine", String.class.getName())
120 				.setTitle("Product Line").setWidth(new Integer(85))
121 				.setStyle(detailStyle).setHeaderStyle(headerStyle)
122 				.build();
123 
124 		AbstractColumn columnaQuantity = ColumnBuilder.getNew()
125 				.setColumnProperty("quantity", Long.class.getName())
126 				.setTitle("Quantity").setWidth(new Integer(80))
127 				.setStyle(importeStyle).setHeaderStyle(headerStyle)
128 				.build();
129 
130 		AbstractColumn columnAmount = ColumnBuilder.getNew()
131 				.setColumnProperty("amount", Float.class.getName())
132 				.setTitle("Amount").setWidth(new Integer(90)).setPattern("$ 0.00")
133 				.setStyle(importeStyle).setHeaderStyle(headerStyle)
134 				.build();
135 
136 		GroupBuilder gb1 = new GroupBuilder();
137 
138 //		 define the criteria column to group by (columnState)
139 		DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState)
140 				.addHeaderVariable(columnAmount,DJCalculation.SUM,headerVariables) 
141 				.addHeaderVariable(columnaQuantity,DJCalculation.SYSTEM,headerVariables) 
142 				.setGroupLayout(GroupLayout.VALUE_IN_HEADER) 
143 				
144 				.build();
145 
146 
147 		drb.addColumn(columnState);
148 		drb.addColumn(columnBranch);
149 		drb.addColumn(columnaProductLine);
150 		drb.addColumn(columnaQuantity);
151 		drb.addColumn(columnAmount);
152 
153 		drb.addGroup(g1); // add group g1
154 
155 		drb.setUseFullPageWidth(true);
156 		drb.addAutoText(AutoText.AUTOTEXT_PAGE_X_SLASH_Y, AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_RIGHT);
157 		
158 		/**
159 		 * The scriptlet that will know how to get precalculated values
160 		 */
161 		MyDjScriptlet myDjScriptlet = new MyDjScriptlet();
162 		myDjScriptlet.setPrecalculatedValues(getPrecalculatedValues());
163 		
164 		/**
165 		 * The scriptlet is introduced as a parameter
166 		 */
167 		params.put(JRParameter.REPORT_SCRIPTLET, myDjScriptlet);
168 
169 		DynamicReport dr = drb.build();
170 		return dr;
171 	}
172 
173 	/**
174 	 * Silly method to illustrate that that the scriptlet is initialized with precalculated values 
175 	 * @return
176 	 */
177 	private Map getPrecalculatedValues() {
178 		Map map = new HashMap();
179 		map.put("Arizona", new Long(111));
180 		map.put("Florida", new Long(999));
181 		map.put("New York", new Long(555));
182 		map.put("Washington", new Long(888));
183 		return map;
184 	}
185 
186 	public static void main(String[] args) throws Exception {
187 		SystemVariableReportTest test = new SystemVariableReportTest();
188 		test.testReport();
189 		JasperViewer.viewReport(test.jp);
190 	}
191 
192 }