1 package org.tigris.scarab.reports; 2 3 48 49 import java.util.ArrayList ; 51 import java.util.Date ; 52 import java.util.Iterator ; 53 import java.util.List ; 54 import java.util.Map ; 55 import java.util.HashMap ; 56 import java.io.StringReader ; 57 58 import org.apache.log4j.Logger; 59 60 import org.apache.torque.TorqueException; 62 63 import org.tigris.scarab.tools.localization.L10NKeySet; 64 import org.tigris.scarab.util.word.IssueSearch; 65 import org.tigris.scarab.om.ScarabUserManager; 66 import org.tigris.scarab.om.Module; 67 import org.tigris.scarab.om.IssueType; 68 import org.tigris.scarab.om.ModuleManager; 69 import org.tigris.scarab.om.AttributeValue; 70 import org.tigris.scarab.om.AttributeOptionManager; 71 import org.tigris.scarab.om.AttributeOption; 72 import org.tigris.scarab.om.AttributeManager; 73 import org.tigris.scarab.om.Attribute; 74 import org.tigris.scarab.om.ScarabUser; 75 import org.tigris.scarab.om.Scope; 76 import org.tigris.scarab.om.MITList; 77 import org.tigris.scarab.om.MITListItem; 78 import org.tigris.scarab.util.Log; 79 import org.tigris.scarab.util.ScarabConstants; 80 import org.tigris.scarab.services.security.ScarabSecurity; 81 82 import org.apache.commons.betwixt.io.BeanReader; 83 84 89 public class ReportBridge 90 implements java.io.Serializable , 91 org.apache.fulcrum.intake.Retrievable { 93 94 private ScarabUser generatedBy; 95 private Date generatedDate; 96 97 private org.tigris.scarab.om.Report torqueReport; 98 private ReportDefinition reportDefn; 99 private ReportHeading newHeading; 100 101 public ReportBridge() 102 { 103 torqueReport = new org.tigris.scarab.om.Report(); 104 reportDefn = new ReportDefinition(); 105 } 106 107 public ReportBridge(org.tigris.scarab.om.Report report) 108 throws Exception 109 { 110 torqueReport = report; 111 populate(report.getQueryString()); 112 } 113 114 public ReportDefinition getReportDefinition() 115 { 116 return reportDefn; 117 } 118 119 public ReportHeading getNewHeading() 120 { 121 if (newHeading == null) 122 { 123 newHeading = new ReportHeading(); 124 } 125 return newHeading; 126 } 127 128 public String getQueryKey() 131 { 132 return torqueReport.getQueryKey(); 133 } 134 public void setQueryKey(String key) 135 throws TorqueException 136 { 137 torqueReport.setQueryKey(key); 138 } 139 140 public String getName() 141 { 142 return torqueReport.getName(); 143 } 144 public void setName(String name) 145 { 146 torqueReport.setName(name); 147 reportDefn.setName(name); 148 } 149 public String getDescription() 150 { 151 return torqueReport.getDescription(); 152 } 153 public void setDescription(String name) 154 { 155 torqueReport.setDescription(name); 156 reportDefn.setDescription(name); 157 } 158 159 public String getFormat() 160 { 161 return reportDefn.getFormat(); 162 } 163 164 public void setFormat(String format) 165 { 166 reportDefn.setFormat(format); 167 } 168 169 public boolean getDeleted() 170 { 171 return torqueReport.getDeleted(); 172 } 173 public void setDeleted(boolean b) 174 { 175 torqueReport.setDeleted(b); 176 } 177 public Integer getScopeId() 178 { 179 return torqueReport.getScopeId(); 180 } 181 public void setScopeId(Integer id) 182 throws TorqueException 183 { 184 torqueReport.setScopeId(id); 185 } 186 187 public Integer getUserId() 188 { 189 return torqueReport.getUserId(); 190 } 191 public void setUserId(Integer id) 192 throws TorqueException 193 { 194 torqueReport.setUserId(id); 195 } 196 197 public Integer getReportId() 198 { 199 return torqueReport.getReportId(); 200 } 201 public void setReportId(Integer id) 202 { 203 torqueReport.setReportId(id); 204 } 205 206 public Scope getScope() 207 throws TorqueException 208 { 209 return torqueReport.getScope(); 210 } 211 212 216 public Module getModule() 217 throws TorqueException 218 { 219 Module module = null; 220 if (torqueReport.getModuleId() != null) 221 { 222 module = ModuleManager.getInstance(torqueReport.getModuleId()); 223 } 224 225 return module; 226 } 227 228 232 public void setModule(Module v) 233 throws TorqueException 234 { 235 reportDefn.setModuleIssueTypes(null); 236 if (v == null) 237 { 238 torqueReport.setModuleId((Integer )null); 239 } 240 else 241 { 242 torqueReport.setModuleId(v.getModuleId()); 243 if (torqueReport.getIssueTypeId() != null) 244 { 245 ModuleIssueType mit = new ModuleIssueType(); 246 mit.setModuleId(v.getModuleId()); 247 mit.setIssueTypeId(torqueReport.getIssueTypeId()); 248 reportDefn.addModuleIssueType(mit); 249 } 250 } 251 } 252 253 257 public void setIssueType(IssueType v) 258 throws TorqueException 259 { 260 reportDefn.setModuleIssueTypes(null); 261 if (v == null) 262 { 263 torqueReport.setIssueTypeId(ScarabConstants.INTEGER_0); 265 } 266 else 267 { 268 torqueReport.setIssueTypeId(v.getIssueTypeId()); 269 if (torqueReport.getModuleId() != null) 270 { 271 ModuleIssueType mit = new ModuleIssueType(); 272 mit.setModuleId(torqueReport.getModuleId()); 273 mit.setIssueTypeId(v.getIssueTypeId()); 274 reportDefn.addModuleIssueType(mit); 275 } 276 } 277 } 278 279 282 private boolean hasPermission(String permission, ScarabUser user) 283 { 284 boolean result = false; 285 try 286 { 287 MITList mitlist = getMITList(); 288 result = mitlist.isSingleModule() ? 289 user.hasPermission(permission, mitlist.getModule()) : 290 user.hasPermission(permission, mitlist.getModules()); 291 } 292 catch (Exception e) 293 { 294 result = false; 295 Log.get().error(e); 296 } 297 return result; 298 } 299 300 public boolean isEditable(ScarabUser user) 301 { 302 return torqueReport.isNew() 303 || 304 (Scope.PERSONAL__PK.equals(torqueReport.getScopeId()) 305 && user.getUserId().equals(torqueReport.getUserId())) 306 || 307 (Scope.MODULE__PK.equals(torqueReport.getScopeId()) && 308 hasPermission(ScarabSecurity.MODULE__EDIT, user)); 309 } 310 311 public boolean isDeletable(ScarabUser user) 312 { 313 return (Scope.PERSONAL__PK.equals(torqueReport.getScopeId()) 314 && user.getUserId().equals(torqueReport.getUserId())) 315 || 316 (Scope.MODULE__PK.equals(torqueReport.getScopeId()) && 317 hasPermission(ScarabSecurity.ITEM__DELETE, user)); 318 } 319 320 public boolean isSavable(ScarabUser user) 321 { 322 return isEditable(user) && 323 hasPermission( ScarabSecurity.USER__EDIT_PREFERENCES, user); 324 } 325 326 330 public ScarabUser getGeneratedBy() 331 throws Exception 332 { 333 if (generatedBy == null) 334 { 335 if (torqueReport.getUserId() != null) 336 { 337 generatedBy = 338 ScarabUserManager.getInstance(torqueReport.getUserId()); 339 } 340 } 341 342 return generatedBy; 343 } 344 345 349 public void setGeneratedBy(ScarabUser v) 350 throws Exception 351 { 352 this.generatedBy = v; 353 torqueReport.setUserId(v.getUserId()); 354 } 355 356 357 363 public Date getGeneratedDate() 364 { 365 if (generatedDate == null) 366 { 367 generatedDate = getDefaultDate(); 370 if (generatedDate == null) 371 { 372 generatedDate = new Date (); 373 } 374 } 375 376 return generatedDate; 377 } 378 379 382 public Date getDefaultDate() 383 { 384 ReportDate rdate = reportDefn.getDefaultDate(); 385 return (rdate != null ? new Date (rdate.getTime()) : null); 386 } 387 388 391 public void setDefaultDate(Date date) 392 { 393 if (date == null) 394 { 395 reportDefn.setDefaultDate(null); 396 } 397 else 398 { 399 ReportDate rdate = reportDefn.getDefaultDate(); 400 if (rdate == null) 401 { 402 rdate = new ReportDate(); 403 reportDefn.setDefaultDate(rdate); 404 } 405 rdate.setTime(date.getTime()); 406 Log.get().debug("Default date set to " + date); 407 } 408 } 409 410 411 public MITList getMITList() 412 throws TorqueException 413 { 414 MITList mitList = null; 415 List mits = reportDefn.getModuleIssueTypes(); 416 if (mits != null) 417 { 418 Log.get().debug("mits were not null"); 419 mitList = new MITList(); 420 for (Iterator i = mits.iterator(); i.hasNext();) 421 { 422 ModuleIssueType mit = (ModuleIssueType)i.next(); 423 MITListItem item = new MITListItem(); 424 item.setModuleId(mit.getModuleId()); 425 item.setIssueTypeId(mit.getIssueTypeId()); 426 mitList.addMITListItem(item); 427 } 428 } 429 430 return mitList; 431 } 432 433 public void setMITList(MITList mitList) 434 throws Exception 435 { 436 if (mitList == null) 437 { 438 reportDefn.setModuleIssueTypes(null); 439 setModule(null); 440 setIssueType(null); 441 } 442 else 443 { 444 boolean isOk = true; 445 for (Iterator roai = reportDefn 448 .retrieveAllReportOptionAttributes().iterator(); 449 roai.hasNext() && isOk;) 450 { 451 isOk = mitList.isCommon( AttributeOptionManager.getInstance( 452 ((ReportOptionAttribute)roai.next()) 453 .getOptionId())); 454 } 455 for (Iterator ruai = reportDefn 456 .retrieveAllReportUserAttributes().iterator(); 457 ruai.hasNext() && isOk;) 458 { 459 isOk = mitList.isCommon( AttributeManager.getInstance( 460 ((ReportUserAttribute)ruai.next()) 461 .getAttributeId())); 462 } 463 464 if (!isOk) 465 { 466 throw new IncompatibleMITListException(L10NKeySet.ExceptionIncompatibleMITListChanges); } 468 469 reportDefn.setModuleIssueTypes(null); 470 setModule(null); 471 setIssueType(null); 472 473 for (Iterator i = mitList.getExpandedMITListItems().iterator(); i.hasNext();) 474 { 475 MITListItem item = (MITListItem)i.next(); 476 ModuleIssueType mit = new ModuleIssueType(); 477 mit.setModuleId(item.getModuleId()); 478 mit.setIssueTypeId(item.getIssueTypeId()); 479 reportDefn.addModuleIssueType(mit); 480 } 481 if (mitList.isSingleModule()) 482 { 483 torqueReport.setModule(mitList.getModule()); 484 } 485 if (mitList.isSingleIssueType()) 486 { 487 torqueReport.setIssueType(mitList.getIssueType()); 488 } 489 } 490 } 491 492 public boolean isReadyForCalculation() 493 { 494 List mits = reportDefn.getModuleIssueTypes(); 495 boolean result = mits != null && !mits.isEmpty(); 496 List axes = reportDefn.getReportAxisList(); 497 result &= axes != null && axes.size() > 1; 498 if (result) 499 { 500 for (Iterator i = axes.iterator(); i.hasNext() && result;) 501 { 502 ReportAxis axis = (ReportAxis)i.next(); 503 List headings = axis.getReportHeadings(); 504 result &= headings != null && !headings.isEmpty(); 505 if (result) 506 { 507 for (Iterator j = headings.iterator(); j.hasNext() && result;) 508 { 509 ReportHeading heading = (ReportHeading)j.next(); 510 result &= heading.size() > 0; 511 } 512 } 513 } 514 } 515 516 return result; 517 } 518 519 public boolean removeStaleDefinitions() 520 throws Exception 521 { 522 boolean reportModified = false; 523 MITList mitList = getMITList(); 524 List axes = reportDefn.getReportAxisList(); 525 if (axes != null) 526 { 527 for (Iterator i = axes.iterator(); i.hasNext();) 528 { 529 ReportAxis axis = (ReportAxis)i.next(); 530 List headings = axis.getReportHeadings(); 531 if (headings != null) 532 { 533 for (Iterator j = headings.iterator(); j.hasNext();) 534 { 535 ReportHeading heading = (ReportHeading)j.next(); 536 reportModified |= removeStaleOptions( 537 heading.getReportOptionAttributes(), mitList); 538 reportModified |= removeStaleUserAttributes(heading 539 .getReportUserAttributes(), mitList); 540 List groups = heading.getReportGroups(); 541 if (groups != null && !groups.isEmpty()) 542 { 543 for (Iterator n = groups.iterator(); n.hasNext();) 544 { 545 ReportGroup group = (ReportGroup)n.next(); 546 reportModified |= removeStaleOptions( 547 group.getReportOptionAttributes(), mitList ); 548 reportModified |= removeStaleUserAttributes( 549 group.getReportUserAttributes(), mitList ); 550 } 551 } 552 } 553 } 554 } 555 } 556 return reportModified; 557 } 558 559 private boolean removeStaleOptions(List options, MITList mitList) 560 throws Exception 561 { 562 boolean anyRemoved = false; 563 if (options != null && !options.isEmpty()) 564 { 565 for (Iterator n = options.iterator(); n.hasNext();) 566 { 567 ReportOptionAttribute rao = 568 (ReportOptionAttribute)n.next(); 569 AttributeOption ao = AttributeOptionManager 570 .getInstance(rao.getOptionId()); 571 if (!mitList.isCommon(ao, false)) 572 { 573 n.remove(); 574 anyRemoved = true; 575 } 576 else if (!mitList.isCommon(ao.getAttribute(), false)) 577 { 578 n.remove(); 579 anyRemoved = true; 580 } 581 } 582 } 583 return anyRemoved; 584 } 585 586 private boolean removeStaleUserAttributes(List attributes, MITList mitList) 587 throws Exception 588 { 589 boolean anyRemoved = false; 590 if (attributes != null && !attributes.isEmpty()) 591 { 592 for (Iterator n = attributes.iterator(); n.hasNext();) 593 { 594 ReportUserAttribute rua = 595 (ReportUserAttribute)n.next(); 596 Attribute attr = AttributeManager 597 .getInstance(rua.getAttributeId()); 598 if (!mitList.isCommon(attr, false)) 599 { 600 n.remove(); 601 anyRemoved = true; 602 } 603 } 604 } 605 return anyRemoved; 606 } 607 608 public ReportTableModel getModel(ScarabUser searcher) 609 throws Exception 610 { 611 return new ReportTableModel(this, getGeneratedDate(), searcher); 612 } 613 614 public void save() 615 throws Exception 616 { 617 torqueReport.setQueryString(getQueryString()); 618 torqueReport.save(); 619 } 620 621 626 public boolean isNew() 627 { 628 return torqueReport.isNew(); 629 } 630 631 public void populate(String v) 632 throws Exception 633 { 634 if (v == null) 635 { 636 reportDefn = new ReportDefinition(); 637 } 638 else 639 { 640 BeanReader reader = new BeanReader(); 641 reader.registerBeanClass(ReportDefinition.class); 642 reportDefn = (ReportDefinition) 643 reader.parse(new StringReader (v)); 644 645 Logger log = Log.get(); 646 if (log.isDebugEnabled()) 647 { 648 log.debug("Created a new report using:\n " + v + 649 "; and it resulted in:\n " + 650 reportDefn.toXmlString()); 651 } 652 } 653 } 654 655 String getQueryString() 656 throws Exception 657 { 658 return reportDefn.toXmlString(); 659 } 660 661 662 public void populateSearch(IssueSearch search, ReportHeading heading) 663 throws Exception 664 { 665 List reportOptions = heading.getReportOptionAttributes(); 666 if (reportOptions == null || reportOptions.isEmpty()) 667 { 668 List groups = heading.getReportGroups(); 669 if (groups != null && !groups.isEmpty()) 670 { 671 reportOptions = new ArrayList (); 672 for (Iterator i = groups.iterator(); i.hasNext();) 673 { 674 ReportGroup group = (ReportGroup)i.next(); 675 List tmpOptions = group.getReportOptionAttributes(); 676 if (tmpOptions != null && !tmpOptions.isEmpty()) 677 { 678 for (Iterator j = tmpOptions.iterator(); j.hasNext();) 679 { 680 reportOptions.add(j.next()); 681 } 682 } 683 } 684 } 685 } 686 687 if (reportOptions != null && !reportOptions.isEmpty()) 688 { 689 Map commonAttributeMap = new HashMap (reportOptions.size()); 690 for (Iterator i = reportOptions.iterator(); i.hasNext();) 691 { 692 ReportOptionAttribute roa = (ReportOptionAttribute)i.next(); 693 Integer optionId = roa.getOptionId(); 694 Integer attId = AttributeOptionManager.getInstance(optionId) 695 .getAttributeId(); 696 AttributeValue av = AttributeValue 697 .getNewInstance(attId, search); 698 av.setOptionId(optionId); 699 if (commonAttributeMap.containsKey(attId)) 700 { 701 AttributeValue prevAV = 702 (AttributeValue)commonAttributeMap.get(attId); 703 prevAV.setChainedValue(av); 704 } 705 else 706 { 707 search.addAttributeValue(av); 708 commonAttributeMap.put(attId, av); 709 } 710 } 711 } 712 } 713 } 714 | Popular Tags |