1 package org.tigris.scarab.reports; 2 3 48 49 import java.util.List ; 50 import java.util.ArrayList ; 51 import java.util.Iterator ; 52 import java.util.Collections ; 53 54 import java.io.StringWriter ; 55 import org.apache.commons.betwixt.io.BeanWriter; 56 import org.tigris.scarab.util.Log; 57 import org.apache.torque.om.NumberKey; 58 59 import org.tigris.scarab.om.ScarabUserManager; 60 import org.tigris.scarab.om.RModuleAttribute; 61 import org.tigris.scarab.om.RModuleAttributeManager; 62 import org.tigris.scarab.om.RModuleOption; 63 import org.tigris.scarab.om.RModuleOptionManager; 64 import org.tigris.scarab.om.AttributeOptionManager; 65 import org.tigris.scarab.om.AttributeManager; 66 import org.tigris.scarab.util.ScarabConstants; 67 68 79 public class ReportDefinition 80 implements java.io.Serializable 81 { 83 87 private static final int MAX_CRITERIA = ScarabConstants.REPORT_MAX_CRITERIA; 88 89 private String name; 90 91 private String description; 92 93 private String format; 94 95 private List moduleIssueTypes; 96 97 private List reportAxisList; 98 99 private ReportDate defaultDate; 100 101 105 public String getName() 106 { 107 return name; 108 } 109 110 114 public void setName(String newName) 115 { 116 this.name = newName; 117 } 118 119 123 public String getDescription() 124 { 125 return description; 126 } 127 128 132 public void setDescription(String newDescription) 133 { 134 this.description = newDescription; 135 } 136 137 141 public String getFormat() 142 { 143 return format; 144 } 145 146 150 public void setFormat(String format) 151 { 152 this.format = format; 153 } 154 155 159 public List getModuleIssueTypes() 160 { 161 return moduleIssueTypes; 162 } 163 164 168 public void setModuleIssueTypes(List newModuleIssueTypes) 169 { 170 this.moduleIssueTypes = newModuleIssueTypes; 171 } 172 173 177 public void addModuleIssueType(ModuleIssueType newModuleIssueType) 178 { 179 Log.get().debug("Added a mit, " + newModuleIssueType + 180 ", to reportDefn"); 181 if (moduleIssueTypes == null) 182 { 183 moduleIssueTypes = new ArrayList (); 184 } 185 if (!moduleIssueTypes.contains(newModuleIssueType)) 186 { 187 moduleIssueTypes.add(newModuleIssueType); 188 } 189 } 190 191 195 public List getReportAxisList() 196 { 197 return reportAxisList; 198 } 199 200 204 public void setReportAxisList(List newReportAxisList) 205 { 206 this.reportAxisList = newReportAxisList; 207 } 208 209 213 public void addReportAxis(ReportAxis newReportAxis) 214 { 215 if (reportAxisList == null) 216 { 217 reportAxisList = new ArrayList (); 218 } 219 reportAxisList.add(newReportAxis); 220 } 221 222 226 public ReportDate getDefaultDate() 227 { 228 return defaultDate; 229 } 230 231 235 public void setDefaultDate(ReportDate newDefaultDate) 236 { 237 this.defaultDate = newDefaultDate; 238 } 239 240 241 242 244 266 267 274 public ReportAxis getAxis(int axisIndex) 275 { 276 List axisList = getReportAxisList(); 277 while (axisList == null || axisList.size() < axisIndex + 1) 278 { 279 addReportAxis(new ReportAxis()); 280 axisList = getReportAxisList(); 281 } 282 283 return (ReportAxis)axisList.get(axisIndex); 284 } 285 286 public List retrieveAllReportOptionAttributes() 287 { 288 List result = null; 289 List axes = getReportAxisList(); 290 if (axes != null && !axes.isEmpty()) 291 { 292 for (Iterator axi = axes.iterator(); axi.hasNext();) 293 { 294 List headings = ((ReportAxis)axi.next()).getReportHeadings(); 295 if (headings != null && !headings.isEmpty()) 296 { 297 for (Iterator hi = headings.iterator(); hi.hasNext();) 298 { 299 List options = ((ReportHeading)hi.next()) 300 .consolidateReportOptionAttributes(); 301 if (options != null && !options.isEmpty()) 302 { 303 for (Iterator i = options.iterator(); i.hasNext();) 304 { 305 if (result == null) 306 { 307 result = new ArrayList (); 308 } 309 result.add(i.next()); 310 } 311 } 312 } 313 } 314 } 315 } 316 return result == null ? Collections.EMPTY_LIST : result; 317 } 318 319 public List retrieveAllReportUserAttributes() 320 { 321 List result = null; 322 List axes = getReportAxisList(); 323 if (axes != null && !axes.isEmpty()) 324 { 325 for (Iterator axi = axes.iterator(); axi.hasNext();) 326 { 327 List headings = ((ReportAxis)axi.next()).getReportHeadings(); 328 if (headings != null && !headings.isEmpty()) 329 { 330 for (Iterator hi = headings.iterator(); hi.hasNext();) 331 { 332 List users = ((ReportHeading)hi.next()) 333 .consolidateReportUserAttributes(); 334 if (users != null && !users.isEmpty()) 335 { 336 for (Iterator i = users.iterator(); i.hasNext();) 337 { 338 if (result == null) 339 { 340 result = new ArrayList (); 341 } 342 result.add(i.next()); 343 } 344 } 345 } 346 } 347 } 348 } 349 return result == null ? Collections.EMPTY_LIST : result; 350 } 351 352 public String toXmlString() 353 { 354 String s; 355 try 356 { 357 StringWriter sw = new StringWriter (1024); 358 BeanWriter bw = new BeanWriter(sw); 359 366 bw.enablePrettyPrint(); 367 bw.writeXmlDeclaration("<?xml version='1.0' encoding='UTF-8' ?>"); 368 bw.write(this); 369 bw.flush(); 370 s = sw.toString(); 371 bw.close(); 372 } 373 catch (Exception e) 374 { 375 s = "ERROR! on " + super.toString(); 376 Log.get().error("", e); 377 } 378 379 return s; 380 } 381 382 public String displayAttribute(Object obj) 383 { 385 Integer attId = null; 386 if (obj instanceof ReportOptionAttribute) 387 { 388 try 389 { 390 attId = new Integer (AttributeOptionManager.getInstance( 391 new NumberKey(((ReportOptionAttribute)obj).getOptionId() 392 .toString())).getAttributeId().toString()); 393 } 394 catch (Exception e) 395 { 396 Log.get().error("Error on Attribute Id=" + attId, e); 397 return "Error on Attribute Id=" + attId; 398 } 399 } 400 else if (obj instanceof ReportUserAttribute) 401 { 402 attId = ((ReportUserAttribute)obj).getAttributeId(); 403 } 404 else 405 { 406 return ""; 407 } 408 409 410 String result = null; 411 List mits = getModuleIssueTypes(); 412 if (mits != null && mits.size() == 1) 413 { 414 ModuleIssueType mit = (ModuleIssueType)mits.get(0); 415 try 416 { 417 RModuleAttribute rma = RModuleAttributeManager.getInstance( 418 mit.getModuleId(), attId, mit.getIssueTypeId()); 419 result = rma.getDisplayValue(); 420 } 421 catch (Exception e) 422 { 423 result = "Error on Attribute Id=" + attId; 424 Log.get().error(result, e); 425 } 426 } 427 else 428 { 429 try 430 { 431 result = AttributeManager.getInstance( 432 new NumberKey(attId.toString())).getName(); 433 } 434 catch (Exception e) 435 { 436 result = "Error on Attribute Id=" + attId; 437 Log.get().error(result, e); 438 } 439 } 440 return result; 441 } 442 443 public String displayOption(ReportOptionAttribute roa) 444 { 446 Integer optionId = roa.getOptionId(); 447 String result = null; 448 List mits = getModuleIssueTypes(); 449 if (mits != null && mits.size() == 1) 450 { 451 ModuleIssueType mit = (ModuleIssueType)mits.get(0); 452 try 453 { 454 RModuleOption rmo = RModuleOptionManager.getInstance( 455 mit.getModuleId(), mit.getIssueTypeId(), optionId); 456 result = rmo.getDisplayValue(); 457 } 458 catch (Exception e) 459 { 460 result = "Error on Option Id=" + optionId; 461 Log.get().error(result, e); 462 } 463 } 464 else 465 { 466 try 467 { 468 result = AttributeOptionManager.getInstance( 469 new NumberKey(optionId.toString())).getName(); 470 } 471 catch (Exception e) 472 { 473 result = "Error on Option Id=" + optionId; 474 Log.get().error(result, e); 475 } 476 } 477 return result; 478 } 479 480 public String displayUser(ReportUserAttribute rua) 481 { 483 String result = null; 484 try 485 { 486 result = ScarabUserManager.getInstance( 487 new NumberKey(rua.getUserId().toString())).getName(); 488 } 489 catch (Exception e) 490 { 491 result = "Error on Option Id=" + rua.getUserId(); 492 Log.get().error(result, e); 493 } 494 return result; 495 } 496 497 public String displayHeading(ReportHeading heading) 498 { 499 String summary = null; 500 List options = heading.getReportOptionAttributes(); 501 List groups = heading.getReportGroups(); 502 List users = heading.getReportUserAttributes(); 503 if (options != null && !options.isEmpty()) 504 { 505 String attribute = null; 506 StringBuffer sb = new StringBuffer (20*options.size()); 507 for (Iterator i = options.iterator(); i.hasNext();) 508 { 509 ReportOptionAttribute roa = (ReportOptionAttribute)i.next(); 510 String newAttribute = displayAttribute(roa); 511 if (!newAttribute.equals(attribute)) 512 { 513 if (attribute != null) 514 { 515 sb.append("; "); 516 } 517 attribute = newAttribute; 518 sb.append(attribute).append(": "); 519 } 520 sb.append(displayOption(roa)).append(", "); 521 } 522 sb.setLength(sb.length() - 2); 523 summary = sb.toString(); 524 } 525 else if (groups != null && !groups.isEmpty()) 526 { 527 StringBuffer sb = 528 new StringBuffer (10*groups.size()); 529 for (Iterator i = groups.iterator(); i.hasNext();) 530 { 531 sb.append(((ReportGroup)i.next()).getName()) 532 .append('/'); 533 } 534 sb.setLength(sb.length() - 1); 535 summary = sb.toString(); 536 } 537 else if (users != null && !users.isEmpty()) 538 { 539 String attribute = null; 540 StringBuffer sb = new StringBuffer (20*users.size()); 541 for (Iterator i = users.iterator(); i.hasNext();) 542 { 543 ReportUserAttribute rua = (ReportUserAttribute)i.next(); 544 String newAttribute = displayAttribute(rua); 545 if (!newAttribute.equals(attribute)) 546 { 547 if (attribute != null) 548 { 549 sb.append("; "); 550 } 551 attribute = newAttribute; 552 sb.append(attribute).append(": "); 553 } 554 sb.append(displayUser(rua)).append(", "); 555 } 556 sb.setLength(sb.length() - 2); 557 summary = sb.toString(); 558 } 559 else if (heading.getReportDates() != null 561 && !heading.getReportDates().isEmpty()) 562 { 564 summary = "Dates"; 565 } 566 return summary; 567 } 568 569 573 public int maximumHeadings() 574 { 575 return MAX_CRITERIA; 576 } 577 578 583 public boolean allowMoreHeadings(ReportAxis axis) 584 { 585 return availableNumberOfHeadings(axis) > 0; 586 } 587 588 593 public boolean reportQueryIsExpensive() 594 { 595 return totalNumberOfNonDateHeadings() > MAX_CRITERIA; 596 } 597 598 602 public int totalAvailableNumberOfHeadings() 603 { 604 return maximumHeadings() - totalNumberOfNonDateHeadings(); 605 } 606 607 613 public int availableNumberOfHeadings(ReportAxis axis) 614 { 615 int result = maximumHeadings() - totalNumberOfNonDateHeadings() - 1; 617 List axes = getReportAxisList(); 618 if (axes != null) 619 { 620 ReportAxis tmpAxis; 621 for (Iterator i = axes.iterator(); i.hasNext();) 622 { 623 tmpAxis = (ReportAxis)i.next(); 624 if (tmpAxis != null && !tmpAxis.equals(axis)) 625 { 626 List headings = tmpAxis.getReportHeadings(); 627 if (headings != null && headings.size() > 0 && 628 (((ReportHeading)headings.get(0)).size() > 0)) 629 { 630 result++; 631 } 632 break; 633 } 634 } 635 } 636 return result; 637 } 638 639 private int totalNumberOfNonDateHeadings() 640 { 641 int count = 0; 642 List axes = getReportAxisList(); 643 if (axes != null) 644 { 645 ReportAxis axis; 646 for (Iterator i = axes.iterator(); i.hasNext();) 647 { 648 axis = (ReportAxis)i.next(); 649 if (axis != null) 650 { 651 count += numberOfNonDateHeadings(axis); 652 } 653 } 654 } 655 return count; 656 } 657 658 private int numberOfNonDateHeadings(ReportAxis axis) 659 { 660 int count = 0; 661 List headings = axis.getReportHeadings(); 662 if (headings != null) 663 { 664 int size = headings.size(); 665 if (size > 0) 666 { 667 if ( size != 1 || 668 !(((ReportHeading)headings.get(0)) 669 .get(0) instanceof ReportDate)) 670 { 671 count += size; 672 } 673 } 674 } 675 return count; 676 } 677 } 678 679 | Popular Tags |