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 * Use an iterator as data source HOWTO 1) create a Collection of X. Where X
26 * is a complex object with 'fields', X can either be a Set or a Bean. The
27 * container must support Iterator(). Object X must be supported by apache
28 * bean utils 2) somewhere in pagecontext (like session) put the Collection
29 * with key "jasper.input" 3) invoke gets field data from multiple sources,
30 * not just DBform fields context : prefix context__xxx . Uses
31 * pageContext.findAttribute(xxx) session : prefix session__xxxx . Uses
32 * pageContext.getSession().findAttribute(xxx) internal : prefix internal_zzz
33 * attribute of iterator object : no prefix For internal, only
34 * internal__rownum is currently defined For content and session, xxxx may be
35 * a 'simple' type or 'complex' type a 'complex' type is an object supporting
36 * javabean access, instead of the usual '.' notation, use '__' instead.
37 */
38 package org.dbforms.servlets.reports;
39
40 import java.util.Iterator;
41 import java.util.Map;
42
43 import net.sf.jasperreports.engine.JRException;
44
45 import org.apache.commons.beanutils.PropertyUtils;
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48
49
50
51 public final class JRDataSourceIter extends AbstractJRDataSource {
52 private static Log logCat = LogFactory.getLog(JRDataSourceIter.class
53 .getName());
54 Object current = null;
55 private Iterator iter;
56 private int rownum = 0;
57
58 /***
59 * DOCUMENT ME!
60 *
61 * @param iter
62 * @param pageContext
63 */
64 public JRDataSourceIter(Map attributes, Iterator iter) {
65 super(attributes);
66 this.iter = iter;
67 rownum = 0;
68 }
69
70 /***
71 * @see net.sf.jasperreports.engine.JRDataSource#next()
72 */
73 public boolean next() throws JRException {
74 if (!iter.hasNext()) {
75 return false;
76 }
77 current = iter.next();
78 rownum++;
79 return true;
80 }
81
82 /***
83 * DOCUMENT ME!
84 *
85 * @param search
86 *
87 * @return
88 */
89 public final Object getFieldValue(String search) {
90 Object obj = null;
91 try {
92 if (current != null) {
93
94 logCat.debug("calling PropertyUtils.getProperty " + current + " "
95 + search);
96 obj = PropertyUtils.getProperty(current, search);
97 }
98 } catch (Exception e) {
99 logCat.error("getCurrentValue: " + e);
100 }
101
102 return obj;
103 }
104 }