1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package ar.com.fdvs.dj.test;
31
32 import java.awt.Color;
33 import java.util.Map;
34
35 import net.sf.jasperreports.view.JasperViewer;
36 import ar.com.fdvs.dj.domain.BooleanExpression;
37 import ar.com.fdvs.dj.domain.CustomExpression;
38 import ar.com.fdvs.dj.domain.DJCalculation;
39 import ar.com.fdvs.dj.domain.DynamicReport;
40 import ar.com.fdvs.dj.domain.ExpressionHelper;
41 import ar.com.fdvs.dj.domain.Style;
42 import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
43 import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
44 import ar.com.fdvs.dj.domain.builders.GroupBuilder;
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.GroupLayout;
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.domain.entities.DJGroup;
52 import ar.com.fdvs.dj.domain.entities.DJGroupVariable;
53 import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
54 import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;
55
56 public class CustomExpressionReportTest3 extends BaseDjReportTest {
57
58 public DynamicReport buildReport() throws Exception {
59
60 Style detailStyle = new Style();
61 Style headerStyle = new Style();
62 headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD); headerStyle.setBorder(Border.PEN_2_POINT);
63 headerStyle.setHorizontalAlign(HorizontalAlign.CENTER); headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
64
65 Style titleStyle = new Style();
66 titleStyle.setFont(new Font(18,Font._FONT_VERDANA,true));
67 Style amountStyle = new Style();
68 amountStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
69 Style oddRowStyle = new Style();
70 oddRowStyle.setBorder(Border.NO_BORDER);
71 Color veryLightGrey = new Color(230,230,230);
72 oddRowStyle.setBackgroundColor(veryLightGrey);oddRowStyle.setTransparency(Transparency.OPAQUE);
73
74 DynamicReportBuilder drb = new DynamicReportBuilder();
75 Integer margin = new Integer(20);
76 drb
77 .setTitle("November 2006 sales report")
78 .setSubtitle("The items in this report correspond "
79 +"to the main products: DVDs, Books, Foods and Magazines")
80 .setTitleStyle(titleStyle).setTitleHeight(new Integer(30))
81 .setSubtitleHeight(new Integer(20))
82 .setDetailHeight(new Integer(15))
83 .setLeftMargin(margin)
84 .setRightMargin(margin)
85 .setTopMargin(margin)
86 .setBottomMargin(margin)
87 .setPrintBackgroundOnOddRows(true)
88 .setOddRowBackgroundStyle(oddRowStyle)
89 .setColumnsPerPage(new Integer(1))
90 .setColumnSpace(new Integer(5));
91
92 AbstractColumn columnState = ColumnBuilder.getNew().setColumnProperty("state", String.class.getName())
93 .setTitle("State").setWidth(new Integer(85))
94 .setStyle(detailStyle).setHeaderStyle(headerStyle).build();
95
96
97
98
99
100
101
102
103
104 AbstractColumn columnaItem = ColumnBuilder.getNew().setColumnProperty("item", String.class.getName())
105 .setTitle("item").setWidth(new Integer(85))
106 .setStyle(detailStyle).setHeaderStyle(headerStyle).build();
107
108 AbstractColumn columnCode = ColumnBuilder.getNew().setColumnProperty("id", Long.class.getName())
109 .setTitle("ID").setWidth(new Integer(40))
110 .setStyle(amountStyle).setHeaderStyle(headerStyle).build();
111
112 AbstractColumn columnaCantidad = ColumnBuilder.getNew().setColumnProperty("quantity", Long.class.getName())
113 .setTitle("Quantity").setWidth(new Integer(80))
114 .setStyle(amountStyle).setHeaderStyle(headerStyle).build();
115
116 AbstractColumn columnAmount = ColumnBuilder.getNew().setColumnProperty("amount", Float.class.getName())
117 .setTitle("Amount").setWidth(new Integer(90)).setPattern("$ 0.00")
118 .setStyle(amountStyle).setHeaderStyle(headerStyle).build();
119
120 AbstractColumn columnaCustomExpression = ColumnBuilder.getNew()
121 .setCustomExpression(getRatioExpression())
122
123
124 .setTitle("CustomExp").setWidth(new Integer(90)).setPattern("#,##0.0")
125 .setStyle(detailStyle).setHeaderStyle(headerStyle).build();
126
127 drb.addColumn(columnState);
128
129
130 drb.addColumn(columnaItem);
131 drb.addColumn(columnCode);
132 drb.addColumn(columnaCantidad);
133 drb.addColumn(columnAmount);
134 drb.addColumn(columnaCustomExpression);
135
136 drb.addGlobalVariable("quantity_sum", columnaCantidad, DJCalculation.SUM);
137 drb.addGlobalVariable("amount_sum", columnAmount, DJCalculation.SUM);
138 drb.addGlobalFooterVariable(columnaCustomExpression, getGlobalRatioExpression());
139
140 GroupBuilder gb1 = new GroupBuilder("g1");
141 DJGroupVariable var = new DJGroupVariable(columnaCantidad, DJCalculation.SUM);
142 var.setPrintWhenExpression(ExpressionHelper.printWhenGroupHasMoreThanOneRecord("g1"));
143
144
145 DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState)
146 .addFooterVariable(var)
147 .addVariable("quantity_sum_g1", columnaCantidad,DJCalculation.SUM)
148 .addVariable("amount_sum_g1", columnAmount,DJCalculation.SUM)
149 .addFooterVariable(columnaCustomExpression, getGroupRatioExpression())
150 .setGroupLayout(GroupLayout.VALUE_IN_HEADER)
151 .setFooterVariablesHeight(new Integer(20))
152 .setFooterHeight(new Integer(50),true)
153 .setHeaderVariablesHeight(new Integer(35))
154 .build();
155
156 drb.addGroup(g1);
157 drb.setUseFullPageWidth(true);
158
159 drb.addField("productLine", String.class.getName());
160 drb.addField("branch", String.class.getName());
161
162 DynamicReport dr = drb.build();
163 return dr;
164 }
165
166 private CustomExpression getRatioExpression() {
167 return new CustomExpression() {
168
169 public Object evaluate(Map fields, Map variables, Map parameters) {
170 Long quantity = (Long) fields.get("quantity");
171 Float amount = (Float) fields.get("amount");
172 return new Double(amount.doubleValue() / quantity.doubleValue());
173 }
174
175 public String getClassName() {
176 return Double.class.getName();
177 }
178
179 };
180 }
181
182 private CustomExpression getGlobalRatioExpression() {
183 return new CustomExpression() {
184
185 public Object evaluate(Map fields, Map variables, Map parameters) {
186 Long quantity = (Long) variables.get("quantity_sum");
187 Float amount = (Float) variables.get("amount_sum");
188 return new Double(amount.doubleValue() / quantity.doubleValue());
189 }
190
191 public String getClassName() {
192 return Double.class.getName();
193 }
194
195 };
196 }
197
198 private CustomExpression getGroupRatioExpression() {
199 return new CustomExpression() {
200
201 public Object evaluate(Map fields, Map variables, Map parameters) {
202 Long quantity = (Long) variables.get("quantity_sum_g1");
203 Float amount = (Float) variables.get("amount_sum_g1");
204 return new Double(amount.doubleValue() / quantity.doubleValue());
205 }
206
207 public String getClassName() {
208 return Double.class.getName();
209 }
210
211 };
212 }
213
214
215
216
217
218
219
220
221
222
223
224
225 public static void main(String[] args) throws Exception {
226 CustomExpressionReportTest3 test = new CustomExpressionReportTest3();
227
228 test.testReport();
229 JasperViewer.viewReport(test.jp);
230 }
231
232 }