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 19 public class Subreport extends Report implements Identity { 20 21 protected Report parentReport; 22 protected Long id; 23 protected String cachedValue; 24 25 public Subreport(Report parent, Long id) { 26 if (id == null) id = parent.generateNewSubreportId(); 28 this.id = id; 29 30 parentReport = parent; 31 parentReport.addSubreport(this); 32 } 33 34 public Object 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 53 public Iterator parentColumns() { 54 return ((SubreportQuery)getDataSource().getQuery()).parentColumns(); 55 } 56 57 public void clearCache() { 58 cachedValue = null; 59 } 60 61 68 public Object getValue() { 69 if (cachedValue != null) 70 return cachedValue; 71 72 rset = null; 73 cachedValue = ""; try { 75 rset = getDataSource().execute(); 76 if (rset != null) 77 cachedValue = StringUtils.join(makeRowStrings(), "\n"); 78 } 79 catch (Exception e) { 80 ErrorHandler.error(e.toString()); 81 } 82 finally { 83 if (rset != null) 84 rset.close(); 85 } 86 87 return cachedValue; 88 } 89 90 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 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 |