1 package org.tigris.scarab.screens; 2 3 48 49 import java.util.Date ; 50 import java.util.List ; 51 import java.util.Iterator ; 52 import java.text.DateFormat ; 53 import java.text.SimpleDateFormat ; 54 55 import org.apache.commons.lang.StringUtils; 56 57 import org.apache.turbine.RunData; 58 import org.apache.turbine.TemplateContext; 59 60 import org.tigris.scarab.om.ScarabUser; 61 import org.tigris.scarab.reports.ReportBridge; 62 import org.tigris.scarab.reports.ReportDate; 63 import org.tigris.scarab.reports.ReportGroup; 64 import org.tigris.scarab.reports.ReportOptionAttribute; 65 import org.tigris.scarab.reports.ReportHeading; 66 import org.tigris.scarab.reports.ReportTableModel; 67 import org.tigris.scarab.reports.ReportUserAttribute; 68 import org.tigris.scarab.tools.Format; 69 import org.tigris.scarab.tools.ScarabRequestTool; 70 import org.tigris.scarab.tools.ScarabLocalizationTool; 71 import org.tigris.scarab.util.Log; 72 73 81 public class ReportExport extends DataExport 82 { 83 86 private static final String NO_HEADER = "======="; 87 88 93 public void doBuildTemplate(RunData data, TemplateContext context) 94 throws Exception 95 { 96 super.doBuildTemplate(data, context); 97 98 ScarabLocalizationTool l10n = getLocalizationTool(context); 99 ScarabUser user = (ScarabUser)data.getUser(); 100 TSVPrinter printer = new TSVPrinter(getWriter(data)); 101 102 ScarabRequestTool scarabR = getScarabRequestTool(context); 103 ReportBridge report = scarabR.getReport(); 104 105 try 110 { 111 String message = (String )scarabR.getInfoMessage(); 112 if (message != null && message.length() > 0) 113 { 114 printer.print(message); 115 } 116 } 117 catch (Exception e) 118 { 119 printer.print(l10n.get("ErrorOccurredCheckingMessage")); 120 } 121 122 if (!report.isReadyForCalculation()) 123 { 124 printer.print(l10n.get("IncompleteReportDefinition")); 125 return; 126 } 127 128 if (report.getReportDefinition().reportQueryIsExpensive()) 129 { 130 printer.print(l10n.format("ReportIsTooExpensive", String.valueOf( 131 report.getReportDefinition().maximumHeadings()))); 132 return; 133 } 134 136 ReportTableModel model = report.getModel(user); 137 138 List rowHeadings = model.getRowHeadings(); 139 List columnHeadings = model.getColumnHeadings(); 140 int multiplier = 1; 141 for (int level = 0; level < columnHeadings.size(); level++) 142 { 143 ReportHeading columnHeadingLevel = 144 (ReportHeading) columnHeadings.get(level); 145 int maxBlank = rowHeadings.size() - 1; 146 if (maxBlank > 0) 147 { 148 for (int i = 1; i <= maxBlank; i++) 149 { 150 printer.print(NO_HEADER); 152 } 153 } 154 155 boolean singleAttribute = columnHeadingLevel.singleAttribute(); 156 printer.print(singleAttribute 161 ? model.displayAttribute(columnHeadingLevel.get(0)) 162 : NO_HEADER); 163 int colspan = model.getColspan(level); 164 for (int i = 1; i <= multiplier; i++) 165 { 166 for (int nextIndex = 1; nextIndex <= columnHeadingLevel.size(); 167 nextIndex++) 168 { 169 Object heading = columnHeadingLevel.get(nextIndex - 1); 170 printer.print(reportLabel(model, heading, singleAttribute, 171 l10n)); 172 } 173 if (colspan > 1) 174 { 175 for (int j = 2; j <= colspan; j++) 176 { 177 printer.print(NO_HEADER); 178 } 179 } 180 } 181 182 printer.println(); 184 multiplier *= columnHeadingLevel.size(); 185 } 186 187 for (Iterator i = rowHeadings.iterator(); i.hasNext(); ) 188 { 189 i.next(); 190 printer.print(NO_HEADER); 191 } 192 int nbrColumns = model.getColumnCount(); 193 for (int i = 0; i < nbrColumns; i++) 194 { 195 printer.print(NO_HEADER); 196 } 197 printer.println(); 198 199 int nbrRows = model.getRowCount(); 200 201 if (rowHeadings.size() > 1 202 || ((ReportHeading) rowHeadings.get(0)).singleAttribute()) 203 { 204 for (int level = 0; level < rowHeadings.size(); level++) 205 { 206 ReportHeading curHeading = 207 (ReportHeading) rowHeadings.get(level); 208 boolean singleAttribute = curHeading.singleAttribute(); 209 printer.print(singleAttribute 212 ? model.displayAttribute(curHeading.get(0)) 213 : NO_HEADER); 214 215 } 216 for (int i = 0; i < nbrRows; i++) 217 { 218 printer.print(NO_HEADER); 219 } 220 printer.println(); 221 } 222 223 DateFormat dateFormat = new SimpleDateFormat (Format.DATE_TIME_FMT); 224 for (int rowIndex = 0; rowIndex < nbrRows; rowIndex++) 225 { 226 for (int level = 0; level < rowHeadings.size(); level++) 227 { 228 ReportHeading curHeading = 229 (ReportHeading) rowHeadings.get(level); 230 boolean singleAttribute = curHeading.singleAttribute(); 231 int rowspan = model.getRowspan(level); 232 if ((rowIndex % rowspan) == 0) 233 { 234 int index = (rowIndex / rowspan) % curHeading.size(); 235 printer.print(reportLabel(model, curHeading.get(index), 236 singleAttribute, l10n)); 237 } 238 else 239 { 240 printer.print(NO_HEADER); 241 } 242 } 243 244 for (int columnIndex = 0; columnIndex < nbrColumns; columnIndex++) 245 { 246 Object cell = model.getValueAt(rowIndex, columnIndex); 247 String cellData = (ReportTableModel.isDate(cell) 248 ? dateFormat.format((Date ) cell) 249 : cell.toString()); 250 if (StringUtils.isEmpty(cellData)) 251 { 252 cellData = NO_CONTENT; 253 } 254 printer.print(cellData); 255 } 256 257 printer.println(); 258 } 259 } 260 261 265 private String reportLabel(ReportTableModel model, Object cellLabel, 266 boolean singleAttribute, 267 ScarabLocalizationTool l10n) 268 { 269 if (model.isReportDate(cellLabel)) 270 { 271 return Format.getDate(Format.DATE_TIME_FMT, 272 ((ReportDate) cellLabel).dateValue()); 273 } 274 else if (model.isOption(cellLabel)) 275 { 276 ReportOptionAttribute roa = (ReportOptionAttribute) cellLabel; 277 return (singleAttribute 278 ? model.displayOption(roa) 279 : model.displayAttribute(roa) + ": " 280 + model.displayOption(roa)); 281 } 282 else if (model.isOptionGroup(cellLabel)) 283 { 284 return ((ReportGroup) cellLabel).getName(); 285 } 286 else if (model.isAttributeAndUser(cellLabel)) 287 { 288 ReportUserAttribute rua = (ReportUserAttribute) cellLabel; 289 return (singleAttribute 290 ? model.displayUser(rua) 291 : model.displayAttribute(rua) + ": " 292 + model.displayUser(rua)); 293 } 294 else if (model.isUser(cellLabel)) 295 { 296 return l10n.get("Author") + ": " 297 + ((ScarabUser) cellLabel).getUserName(); 298 } 299 else 300 { 301 Log.get().debug("Unhandled cell label type: " 302 + cellLabel.getClass().getName()); 303 return cellLabel.toString(); 304 } 305 } 306 } 307 | Popular Tags |