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.crosstab;
31  
32  
33  import java.awt.Color;
34  import java.util.Date;
35  
36  import net.sf.jasperreports.view.JasperDesignViewer;
37  import net.sf.jasperreports.view.JasperViewer;
38  import ar.com.fdvs.dj.core.DJConstants;
39  import ar.com.fdvs.dj.domain.DJCalculation;
40  import ar.com.fdvs.dj.domain.DJCrosstab;
41  import ar.com.fdvs.dj.domain.DynamicReport;
42  import ar.com.fdvs.dj.domain.Style;
43  import ar.com.fdvs.dj.domain.builders.CrosstabBuilder;
44  import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
45  import ar.com.fdvs.dj.domain.builders.StyleBuilder;
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.HorizontalAlign;
49  import ar.com.fdvs.dj.domain.constants.Transparency;
50  import ar.com.fdvs.dj.domain.constants.VerticalAlign;
51  import ar.com.fdvs.dj.test.BaseDjReportTest;
52  import ar.com.fdvs.dj.test.TestRepositoryProducts;
53  import ar.com.fdvs.dj.util.SortUtils;
54  
55  public class CrosstabReportTest4 extends BaseDjReportTest {
56  
57  	private Style totalHeaderStyle;
58  	private Style colAndRowHeaderStyle;
59  	private Style mainHeaderStyle;
60  	private Style totalStyle;
61  	private Style measureStyle;
62  	private Style titleStyle;
63  
64  	public DynamicReport buildReport() throws Exception {
65  		initStyles(); //init some styles to be used
66  
67  		/**
68  		 * Create an empty report (no columns)!
69  		 */
70  		FastReportBuilder drb = new FastReportBuilder();
71  			drb
72  			.setTitle("November " + getYear() +" sales report")
73  			.setSubtitle("This report was generated at " + new Date())
74  //			.setPageSizeAndOrientation(Page.Page_A4_Landscape())
75  			.setPrintColumnNames(false)
76  			.setUseFullPageWidth(true)
77  			.setWhenNoData("No data for this report", null)
78  			.setDefaultStyles(titleStyle, null, null, null);
79  
80  		DJCrosstab djcross = new CrosstabBuilder()
81  			.setHeight(200)
82  			.setWidth(500)
83  			.setHeaderStyle(mainHeaderStyle)
84  			.setDatasource("sr",DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_COLLECTION)
85  			.setUseFullWidth(true)
86  			.setColorScheme(DJConstants.COLOR_SCHEMA_LIGHT_GREEN)
87  			.setAutomaticTitle(true)
88  			.setCellBorder(Border.THIN())
89  			.addRow("Product Line", "productLine", String.class.getName(),false)
90  //			.addRow("Item", "item", String.class.getName(),true)
91  //			.addRow("Id", "id", Long.class.getName(),true)
92  			.addColumn("State","state",String.class.getName(),false)
93  			.addColumn("Branch","branch",String.class.getName(),true)
94  			.addColumn("Item", "item", String.class.getName(),true)
95  			.addMeasure("id",Long.class.getName(), DJCalculation.SUM , "Id", measureStyle)
96  			.addMeasure("amount",Float.class.getName(), DJCalculation.SUM , "Amount",measureStyle)
97  			.setRowStyles(colAndRowHeaderStyle, totalStyle, totalHeaderStyle)
98  			.setColumnStyles(colAndRowHeaderStyle, totalStyle, totalHeaderStyle)
99  			.setCellDimension(34, 60)
100 			.setColumnHeaderHeight(30)
101 			.setRowHeaderWidth(80)
102 			.build();
103 
104 		drb.addHeaderCrosstab(djcross); //add the crosstab in the header band of the report
105 
106 		DynamicReport dr = drb.build();
107 
108 		//put a collection in the parameters map to be used by the crosstab
109 		params.put("sr", SortUtils.sortCollection(TestRepositoryProducts.getDummyCollection(),djcross));
110 
111 		return dr;
112 	}
113 
114 
115 	/**
116 	 *
117 	 */
118 	private void initStyles() {
119 		titleStyle =  new StyleBuilder(false)
120 			.setFont(Font.ARIAL_BIG_BOLD)
121 			.setHorizontalAlign(HorizontalAlign.LEFT)
122 			.setVerticalAlign(VerticalAlign.MIDDLE)
123 			.setTransparency(Transparency.OPAQUE)
124 			.setBorderBottom(Border.PEN_2_POINT())
125 			.build();
126 
127 		totalHeaderStyle = new StyleBuilder(false)
128 			.setHorizontalAlign(HorizontalAlign.CENTER)
129 			.setVerticalAlign(VerticalAlign.MIDDLE)
130 			.setFont(Font.ARIAL_MEDIUM_BOLD)
131 			.setTextColor(Color.BLUE)
132 			.build();
133 		colAndRowHeaderStyle = new StyleBuilder(false)
134 			.setHorizontalAlign(HorizontalAlign.LEFT)
135 			.setVerticalAlign(VerticalAlign.TOP)
136 			.setFont(Font.ARIAL_MEDIUM_BOLD)
137 			.build();
138 		mainHeaderStyle = new StyleBuilder(false)
139 			.setHorizontalAlign(HorizontalAlign.CENTER)
140 			.setVerticalAlign(VerticalAlign.MIDDLE)
141 			.setFont(Font.ARIAL_BIG_BOLD)
142 			.setTextColor(Color.BLACK)
143 			.build();
144 		totalStyle = new StyleBuilder(false).setPattern("#,###.##")
145 			.setHorizontalAlign(HorizontalAlign.RIGHT)
146 			.setFont(Font.ARIAL_MEDIUM_BOLD)
147 			.build();
148 		measureStyle = new StyleBuilder(false).setPattern("#,###.##")
149 			.setHorizontalAlign(HorizontalAlign.RIGHT)
150 			.setFont(Font.ARIAL_MEDIUM)
151 			.build();
152 	}
153 
154 
155 	public static void main(String[] args) throws Exception {
156 		CrosstabReportTest4 test = new CrosstabReportTest4();
157 		test.testReport();
158 		JasperViewer.viewReport(test.jp);	//finally display the report report
159 		JasperDesignViewer.viewReportDesign(test.jr);
160 	}
161 
162 }