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.CustomExpression;
37 import ar.com.fdvs.dj.domain.DynamicReport;
38 import ar.com.fdvs.dj.domain.Style;
39 import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
40 import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
41 import ar.com.fdvs.dj.domain.constants.Border;
42 import ar.com.fdvs.dj.domain.constants.Font;
43 import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
44 import ar.com.fdvs.dj.domain.constants.Transparency;
45 import ar.com.fdvs.dj.domain.constants.VerticalAlign;
46 import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
47
48 public class CustomExpressionReportTest extends BaseDjReportTest {
49
50 public DynamicReport buildReport() throws Exception {
51
52 Style detailStyle = new Style();
53 Style headerStyle = new Style();
54 headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD); headerStyle.setBorder(Border.PEN_2_POINT);
55 headerStyle.setHorizontalAlign(HorizontalAlign.CENTER); headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
56
57 Style titleStyle = new Style();
58 titleStyle.setFont(new Font(18,Font._FONT_VERDANA,true));
59 Style amountStyle = new Style();
60 amountStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
61 Style oddRowStyle = new Style();
62 oddRowStyle.setBorder(Border.NO_BORDER);
63 Color veryLightGrey = new Color(230,230,230);
64 oddRowStyle.setBackgroundColor(veryLightGrey);oddRowStyle.setTransparency(Transparency.OPAQUE);
65
66 DynamicReportBuilder drb = new DynamicReportBuilder();
67 Integer margin = new Integer(20);
68 drb
69 .setTitle("November 2006 sales report")
70 .setSubtitle("The items in this report correspond "
71 +"to the main products: DVDs, Books, Foods and Magazines")
72 .setTitleStyle(titleStyle).setTitleHeight(new Integer(30))
73 .setSubtitleHeight(new Integer(20))
74 .setDetailHeight(new Integer(15))
75 .setLeftMargin(margin)
76 .setRightMargin(margin)
77 .setTopMargin(margin)
78 .setBottomMargin(margin)
79 .setPrintBackgroundOnOddRows(true)
80 .setOddRowBackgroundStyle(oddRowStyle)
81 .setColumnsPerPage(new Integer(1))
82 .setColumnSpace(new Integer(5));
83
84 AbstractColumn columnState = ColumnBuilder.getNew().setColumnProperty("state", String.class.getName())
85 .setTitle("State").setWidth(new Integer(85))
86 .setStyle(detailStyle).setHeaderStyle(headerStyle).build();
87
88
89
90
91
92
93
94
95
96 AbstractColumn columnaItem = ColumnBuilder.getNew().setColumnProperty("item", String.class.getName())
97 .setTitle("item").setWidth(new Integer(85))
98 .setStyle(detailStyle).setHeaderStyle(headerStyle).build();
99
100 AbstractColumn columnCode = ColumnBuilder.getNew().setColumnProperty("id", Long.class.getName())
101 .setTitle("ID").setWidth(new Integer(40))
102 .setStyle(amountStyle).setHeaderStyle(headerStyle).build();
103
104 AbstractColumn columnaCantidad = ColumnBuilder.getNew().setColumnProperty("quantity", Long.class.getName())
105 .setTitle("Quantity").setWidth(new Integer(80))
106 .setStyle(amountStyle).setHeaderStyle(headerStyle).build();
107
108 AbstractColumn columnAmount = ColumnBuilder.getNew().setColumnProperty("amount", Float.class.getName())
109 .setTitle("Amount").setWidth(new Integer(90)).setPattern("$ 0.00")
110 .setStyle(amountStyle).setHeaderStyle(headerStyle).build();
111
112 AbstractColumn columnaCustomExpression = ColumnBuilder.getNew()
113 .setCustomExpression(getCustomExpression())
114
115 .setTitle("CustomExp").setWidth(new Integer(90))
116 .setStyle(detailStyle).setHeaderStyle(headerStyle).build();
117
118 drb.addColumn(columnState);
119
120
121 drb.addColumn(columnaItem);
122 drb.addColumn(columnCode);
123 drb.addColumn(columnaCantidad);
124 drb.addColumn(columnAmount);
125 drb.addColumn(columnaCustomExpression);
126
127 drb.setUseFullPageWidth(true);
128
129 drb.addField("productLine", String.class.getName());
130 drb.addField("branch", String.class.getName());
131
132 DynamicReport dr = drb.build();
133 return dr;
134 }
135
136 private CustomExpression getCustomExpression() {
137 return new CustomExpression() {
138
139 public Object evaluate(Map fields, Map variables, Map parameters) {
140 String state = (String) fields.get("state");
141 String branch = (String) fields.get("branch");
142 String productLine = (String) fields.get("productLine");
143 Integer count = (Integer) variables.get("REPORT_COUNT");
144 return count + ": " +state.toUpperCase() + " / " + branch.toUpperCase() + " / " + productLine;
145 }
146
147 public String getClassName() {
148 return String.class.getName();
149 }
150
151 };
152 }
153
154 public static void main(String[] args) throws Exception {
155 CustomExpressionReportTest test = new CustomExpressionReportTest();
156
157 test.testReport();
158 JasperViewer.viewReport(test.jp);
159 }
160
161 }