KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > Subreport


1 package jimm.datavision;
2 import jimm.datavision.field.Field;
3 import jimm.datavision.source.*;
4 import jimm.datavision.source.sql.SubreportQuery;
5 import jimm.util.StringUtils;
6 import jimm.util.XMLWriter;
7 import java.util.*;
8
9 /**
10  * A subreport is a report whose query is run every time the field
11  * containing it is output.
12  * <p>
13  * When first created, the subreport adds the joins given to it
14  * to its SQL where clause, turning the columns from the current
15  * report into parameters.
16  *
17  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
18  */

19 public class Subreport extends Report implements Identity {
20
21 protected Report parentReport;
22 protected Long JavaDoc id;
23 protected String JavaDoc cachedValue;
24
25 public Subreport(Report parent, Long JavaDoc id) {
26     if (id == null) // Generate new value
27
id = parent.generateNewSubreportId();
28     this.id = id;
29
30     parentReport = parent;
31     parentReport.addSubreport(this);
32 }
33
34 public Object JavaDoc getId() { return id; }
35
36 public Report getParentReport() { return parentReport; }
37
38 public void addJoin(Join join) {
39     ((SubreportQuery)getDataSource().getQuery()).addSubreportJoin(join);
40 }
41
42 public void addAllJoins(Collection coll) {
43     ((SubreportQuery)getDataSource().getQuery()).addSubreportJoins(coll);
44 }
45
46 /**
47  * Returns an iterator over all of the columns that need to be included in
48  * the parent report's query so that the values are available to this
49  * subreport when it builds its query.
50  *
51  * @return an iterator over selectables
52  */

53 public Iterator parentColumns() {
54     return ((SubreportQuery)getDataSource().getQuery()).parentColumns();
55 }
56
57 public void clearCache() {
58     cachedValue = null;
59 }
60
61 /**
62  * Runs the query and returns a string containing a line of text for each
63  * row returned by the subreport query.
64  *
65  * @return a string with newlines separating each row of data
66  * @see #makeRowStrings
67 */

68 public Object JavaDoc getValue() {
69     if (cachedValue != null)
70     return cachedValue;
71
72     rset = null;
73     cachedValue = ""; // In case something happens
74
try {
75     rset = getDataSource().execute();
76     if (rset != null)
77         cachedValue = StringUtils.join(makeRowStrings(), "\n");
78     }
79     catch (Exception JavaDoc e) {
80     ErrorHandler.error(e.toString());
81     }
82     finally {
83     if (rset != null)
84         rset.close();
85     }
86
87     return cachedValue;
88 }
89
90 /**
91  * Returns an array of strings, each containing the values returned by the
92  * subreport query separated by spaces.
93  */

94 protected Collection makeRowStrings() {
95     ArrayList rowStrings = new ArrayList();
96     Section detail = getFirstSectionByArea(SectionArea.DETAIL);
97     while (rset.next()) {
98     ArrayList values = new ArrayList();
99     for (Iterator iter = detail.fields(); iter.hasNext(); ) {
100         String JavaDoc str = ((Field)iter.next()).toString();
101         values.add(str == null ? "" : str);
102     }
103     rowStrings.add(StringUtils.join(values, " "));
104     }
105     return rowStrings;
106 }
107
108 public void writeXML(XMLWriter out) {
109     out.startElement("subreport");
110     out.attr("id", id);
111     getDataSource().getQuery().writeXML(out);
112     ListWriter.writeList(out, formulas.values(), "formulas");
113     ListWriter.writeList(out, usercols.values(), "usercols");
114     ListWriter.writeList(out, details.sections(), "details");
115     out.endElement();
116 }
117
118 }
119
Popular Tags