1 package jimm.datavision; 2 import jimm.datavision.field.Field; 3 import jimm.datavision.source.DataSource; 4 import jimm.datavision.source.sql.SQLQuery; 5 import jimm.util.XMLWriter; 6 7 13 public class Group implements Writeable { 14 15 18 public static final int SORT_ASCENDING = 0; 19 22 public static final int SORT_DESCENDING = 1; 23 24 protected Report report; 25 protected SectionArea headers; 26 protected SectionArea footers; 27 protected Selectable selectable; 28 protected int sortOrder; 29 protected Object value; protected boolean newValue; 31 protected boolean firstValue; 32 protected int recordCount; 33 34 43 public static Group create(Report report, Selectable selectable) { 44 Group group = new Group(report, selectable); 45 46 Section header = new Section(report); 48 49 Field f = Field.create(null, report, header, selectable.fieldTypeString(), 51 selectable.getId(), true); 52 f.getFormat().setBold(true); 53 54 header.addField(f); 56 group.headers().add(header); 57 58 group.footers().add(new Section(report)); 60 61 return group; 62 } 63 64 public static String sortOrderIntToString(int order) { 65 return order == SORT_ASCENDING ? "asc" : "desc"; 66 } 67 68 public static int sortOrderStringToInt(String order) { 69 if (order == null || order.length() == 0) 70 return SORT_ASCENDING; 71 return "desc".equals(order.toLowerCase()) 72 ? SORT_DESCENDING : SORT_ASCENDING; 73 } 74 75 81 public Group(Report report, Selectable selectable) { 82 this.report = report; 83 this.selectable = selectable; 84 sortOrder = SORT_ASCENDING; 85 headers = new SectionArea(SectionArea.GROUP_HEADER); 86 footers = new SectionArea(SectionArea.GROUP_FOOTER); 87 } 88 89 94 public Selectable getSelectable() { return selectable; } 95 96 101 public void setSelectable(Selectable newSelectable) { selectable = newSelectable; } 102 103 106 public void reloadSelectable(DataSource dataSource) { 107 setSelectable(selectable.reloadInstance(dataSource)); 108 } 109 110 public String getSelectableName() { 111 return selectable.getDisplayName(); 112 } 113 114 public String getSortString(SQLQuery query) { 115 return selectable.getSortString(query); 116 } 117 118 124 public int getSortOrder() { return sortOrder; } 125 126 132 public void setSortOrder(int newSortOrder) { sortOrder = newSortOrder; } 133 134 141 public Object getValue() { return value; } 142 143 149 public void setValue(Report report) { 150 Object val = selectable.getValue(report); 151 if (value == null) { 152 value = val; 153 firstValue = true; 154 newValue = true; 155 } 156 else if (value.equals(val)) { 157 newValue = false; 158 firstValue = false; 159 } 160 else { 161 value = val; 162 newValue = true; 163 firstValue = false; 164 } 165 } 166 167 public void updateCounter() { 168 if (newValue) 169 recordCount = 1; 170 else 171 ++recordCount; 172 } 173 174 181 public boolean isNewValue() { return newValue; } 182 183 188 public int getRecordCount() { return recordCount; } 189 190 197 public void forceFooterOutput() { newValue = true; } 198 199 205 public boolean isFirstValue() { return firstValue; } 206 207 212 public SectionArea headers() { return headers; } 213 214 219 public SectionArea footers() { return footers; } 220 221 228 public boolean contains(Section s) { 229 return headers.contains(s) || footers.contains(s); 230 } 231 232 236 public void reset() { 237 value = null; 238 newValue = firstValue = true; 239 recordCount = 1; 240 } 241 242 248 public void writeXML(XMLWriter out) { 249 out.startElement("group"); 250 out.attr("groupable-id", selectable.getId()); 251 out.attr("groupable-type", selectable.fieldTypeString()); 252 out.attr("sort-order", sortOrderIntToString(sortOrder)); 253 254 ListWriter.writeList(out, headers.sections(), "headers"); 255 ListWriter.writeList(out, footers.sections(), "footers"); 256 257 out.endElement(); 258 } 259 260 } 261 | Popular Tags |