001: package jimm.datavision;
002:
003: import jimm.datavision.field.Field;
004: import jimm.datavision.source.*;
005: import jimm.datavision.source.sql.SubreportQuery;
006: import jimm.util.StringUtils;
007: import jimm.util.XMLWriter;
008: import java.util.*;
009:
010: /**
011: * A subreport is a report whose query is run every time the field
012: * containing it is output.
013: * <p>
014: * When first created, the subreport adds the joins given to it
015: * to its SQL where clause, turning the columns from the current
016: * report into parameters.
017: *
018: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
019: */
020: public class Subreport extends Report implements Identity {
021:
022: protected Report parentReport;
023: protected Long id;
024: protected String cachedValue;
025:
026: public Subreport(Report parent, Long id) {
027: if (id == null) // Generate new value
028: id = parent.generateNewSubreportId();
029: this .id = id;
030:
031: parentReport = parent;
032: parentReport.addSubreport(this );
033: }
034:
035: public Object getId() {
036: return id;
037: }
038:
039: public Report getParentReport() {
040: return parentReport;
041: }
042:
043: public void addJoin(Join join) {
044: ((SubreportQuery) getDataSource().getQuery())
045: .addSubreportJoin(join);
046: }
047:
048: public void addAllJoins(Collection coll) {
049: ((SubreportQuery) getDataSource().getQuery())
050: .addSubreportJoins(coll);
051: }
052:
053: /**
054: * Returns an iterator over all of the columns that need to be included in
055: * the parent report's query so that the values are available to this
056: * subreport when it builds its query.
057: *
058: * @return an iterator over selectables
059: */
060: public Iterator parentColumns() {
061: return ((SubreportQuery) getDataSource().getQuery())
062: .parentColumns();
063: }
064:
065: public void clearCache() {
066: cachedValue = null;
067: }
068:
069: /**
070: * Runs the query and returns a string containing a line of text for each
071: * row returned by the subreport query.
072: *
073: * @return a string with newlines separating each row of data
074: * @see #makeRowStrings
075: */
076: public Object getValue() {
077: if (cachedValue != null)
078: return cachedValue;
079:
080: rset = null;
081: cachedValue = ""; // In case something happens
082: try {
083: rset = getDataSource().execute();
084: if (rset != null)
085: cachedValue = StringUtils.join(makeRowStrings(), "\n");
086: } catch (Exception e) {
087: ErrorHandler.error(e.toString());
088: } finally {
089: if (rset != null)
090: rset.close();
091: }
092:
093: return cachedValue;
094: }
095:
096: /**
097: * Returns an array of strings, each containing the values returned by the
098: * subreport query separated by spaces.
099: */
100: protected Collection makeRowStrings() {
101: ArrayList rowStrings = new ArrayList();
102: Section detail = getFirstSectionByArea(SectionArea.DETAIL);
103: while (rset.next()) {
104: ArrayList values = new ArrayList();
105: for (Iterator iter = detail.fields(); iter.hasNext();) {
106: String str = ((Field) iter.next()).toString();
107: values.add(str == null ? "" : str);
108: }
109: rowStrings.add(StringUtils.join(values, " "));
110: }
111: return rowStrings;
112: }
113:
114: public void writeXML(XMLWriter out) {
115: out.startElement("subreport");
116: out.attr("id", id);
117: getDataSource().getQuery().writeXML(out);
118: ListWriter.writeList(out, formulas.values(), "formulas");
119: ListWriter.writeList(out, usercols.values(), "usercols");
120: ListWriter.writeList(out, details.sections(), "details");
121: out.endElement();
122: }
123:
124: }
|