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  
36  import org.apache.commons.beanutils.BeanUtils;
37  
38  import ar.com.fdvs.dj.domain.DJCalculation;
39  import ar.com.fdvs.dj.domain.DynamicReport;
40  import ar.com.fdvs.dj.domain.Style;
41  import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
42  import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
43  import ar.com.fdvs.dj.domain.builders.GroupBuilder;
44  import ar.com.fdvs.dj.domain.builders.StyleBuilder;
45  import ar.com.fdvs.dj.domain.constants.Border;
46  import ar.com.fdvs.dj.domain.constants.Font;
47  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
48  import ar.com.fdvs.dj.domain.constants.Rotation;
49  import ar.com.fdvs.dj.domain.constants.Transparency;
50  import ar.com.fdvs.dj.domain.constants.VerticalAlign;
51  import ar.com.fdvs.dj.domain.entities.DJGroup;
52  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
53  import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;
54  
55  public class StylesReport2Test extends BaseDjReportTest {
56  
57  	public DynamicReport buildReport() throws Exception {
58  
59  //		Style detailStyle = new Style();
60  		Style detailStyle = new StyleBuilder(false).setTransparency(Transparency.OPAQUE).setBackgroundColor(new Color(200,200,230)).build();
61  
62  		Style headerStyle = new Style();
63  		headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD);
64  		headerStyle.getFont().setItalic(true);
65  		headerStyle.setBorderTop(Border.PEN_2_POINT());
66  		headerStyle.setBorderBottom(Border.THIN());
67  		headerStyle.setBackgroundColor(Color.blue);
68  		headerStyle.setTransparency(Transparency.OPAQUE);
69  		headerStyle.setTextColor(Color.white);
70  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
71  		headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
72  		headerStyle.setRotation(Rotation.LEFT);
73  
74  		Style titleStyle = new Style();
75  		titleStyle.setFont(new Font(10,Font._FONT_VERDANA,true));
76  		Style numberStyle = new Style();
77  		numberStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
78  		Style amountStyle = new Style();
79  		amountStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
80  		amountStyle.setBackgroundColor(Color.cyan);
81  		amountStyle.setTransparency(Transparency.OPAQUE);
82  		amountStyle.setFont(Font.ARIAL_MEDIUM_BOLD);
83  		amountStyle.getFont().setUnderline(true);
84  		amountStyle.setPaddingBottom(new Integer(5));
85  		Style oddRowStyle = new Style();
86  		oddRowStyle.setBorder(Border.NO_BORDER());
87  		Color veryLightGrey = new Color(230,230,230);
88  		oddRowStyle.setBackgroundColor(veryLightGrey);oddRowStyle.setTransparency(Transparency.OPAQUE);
89  
90  		Style variableStyle = new Style();
91  		BeanUtils.copyProperties(variableStyle, amountStyle);
92  		variableStyle.setFont(Font.ARIAL_MEDIUM_BOLD);
93  		variableStyle.setBackgroundColor(Color.PINK);
94  
95  		Style variableStyle2 = new Style();
96  		BeanUtils.copyProperties(variableStyle2, amountStyle);
97  		variableStyle2.setFont(Font.ARIAL_MEDIUM_BOLD);
98  		variableStyle2.setBackgroundColor(Color.ORANGE);
99  
100 
101 		DynamicReportBuilder drb = new DynamicReportBuilder();
102 		Integer margin = new Integer(20);
103 		
104 		drb.setTitle("November " + getYear() +" sales report")					//defines the title of the report
105 			.setSubtitle("The items in this report correspond "
106 					+"to the main products: DVDs, Books, Foods and Magazines")
107 			.setTitleStyle(titleStyle).setTitleHeight(new Integer(30))
108 			.setDefaultStyles(null, null, null, detailStyle)
109 			.setSubtitleHeight(new Integer(20))
110 			.setDetailHeight(new Integer(15))
111 //			.setLeftMargin(margin)
112 //			.setRightMargin(margin)
113 //			.setTopMargin(margin)
114 //			.setBottomMargin(margin)
115 			.setPrintBackgroundOnOddRows(true)
116 			.setOddRowBackgroundStyle(oddRowStyle)
117 			.setColumnsPerPage(new Integer(1))
118 			.setColumnSpace(new Integer(5));
119 
120 		AbstractColumn columnState = ColumnBuilder.getNew().setColumnProperty("state", String.class.getName())
121 			.setTitle("State").setWidth(new Integer(85))
122 			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
123 
124 		AbstractColumn columnBranch = ColumnBuilder.getNew().setColumnProperty("branch", String.class.getName())
125 			.setTitle("Branch").setWidth(new Integer(85))
126 			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
127 
128 		AbstractColumn columnaProductLine = ColumnBuilder.getNew().setColumnProperty("productLine", String.class.getName())
129 			.setTitle("Product Line").setWidth(new Integer(85))
130 			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
131 
132 		AbstractColumn columnaItem = ColumnBuilder.getNew().setColumnProperty("item", String.class.getName())
133 			.setTitle("item").setWidth(new Integer(85))
134 			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
135 
136 		AbstractColumn columnCode = ColumnBuilder.getNew().setColumnProperty("id", Long.class.getName())
137 			.setTitle("ID").setWidth(new Integer(40))
138 			.setStyle(numberStyle).setHeaderStyle(headerStyle).build();
139 
140 		AbstractColumn columnaCantidad = ColumnBuilder.getNew().setColumnProperty("quantity", Long.class.getName())
141 			.setTitle("Quantity").setWidth(new Integer(80))
142 			.setStyle(numberStyle).setHeaderStyle(headerStyle).build();
143 
144 		AbstractColumn columnAmount = ColumnBuilder.getNew().setColumnProperty("amount", Float.class.getName())
145 			.setTitle("Amount").setWidth(new Integer(90)).setPattern("$ 0.00")
146 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
147 
148 		drb.addColumn(columnState);
149 		drb.addColumn(columnaItem);
150 		drb.addColumn(columnBranch);
151 		drb.addColumn(columnaProductLine);
152 		drb.addColumn(columnCode);
153 		drb.addColumn(columnaCantidad);
154 		drb.addColumn(columnAmount);
155 
156 		DJGroup group = new GroupBuilder()
157 			.setCriteriaColumn((PropertyColumn) columnState)
158 			.addFooterVariable(columnAmount, DJCalculation.SUM,variableStyle).build();
159 		drb.addGroup(group);
160 
161 		DJGroup group2 = new GroupBuilder()
162 		.setCriteriaColumn((PropertyColumn) columnaItem)
163 		.addFooterVariable(columnAmount, DJCalculation.SUM).build();
164 		drb.addGroup(group2);
165 
166 		group2.setDefaulFooterVariableStyle(variableStyle2);
167 
168 		drb.setUseFullPageWidth(true);
169 
170 		DynamicReport dr = drb.build();
171 //		saveXML(dr,"dynamicReport");
172 		return dr;
173 	}
174 
175 //	private static void saveXML(Object object, String filename) throws Exception {
176 //
177 //		OutputStream out = new FileOutputStream(System.getProperty("user.dir")+ "/target/" + filename +".xml");
178 //		XMLEncoder enc = new XMLEncoder(out);
179 //		enc.writeObject(object);
180 //		enc.close();
181 //		out.close();
182 //
183 //	}
184 
185 	public static void main(String[] args) throws Exception {
186 		StylesReport2Test test = new StylesReport2Test();
187 		test.testReport();
188 		JasperViewer.viewReport(test.jp);
189 	}
190 
191 }