1 package org.tigris.scarab.om; 2 3 48 49 50 51 import java.util.List ; 52 import java.util.ArrayList ; 53 import java.util.LinkedList ; 54 import java.util.Iterator ; 55 import org.apache.torque.TorqueException; 56 import org.apache.torque.om.SimpleKey; 57 import org.apache.torque.om.Persistent; 58 import org.apache.torque.util.Criteria; 59 60 import org.tigris.scarab.services.security.ScarabSecurity; 61 import org.tigris.scarab.services.cache.ScarabCache; 62 import org.tigris.scarab.tools.localization.L10NKeySet; 63 import org.tigris.scarab.om.Module; 64 import org.tigris.scarab.om.ModuleManager; 65 import org.tigris.scarab.util.ScarabException; 66 import org.tigris.scarab.util.Log; 67 import org.tigris.scarab.workflow.WorkflowFactory; 68 69 74 public class AttributeGroup 75 extends org.tigris.scarab.om.BaseAttributeGroup 76 implements Persistent 77 { 78 public static final String GET_ATTRIBUTES = 80 "getAttributes"; 81 private static final String GET_R_ATTRIBUTE_ATTRGROUP = 82 "getRAttributeAttributeGroup"; 83 84 public boolean isGlobal() 85 throws TorqueException 86 { 87 return (getModule() == null); 88 } 89 90 96 public ScarabModule getScarabModule() 97 { 98 throw new UnsupportedOperationException ( 99 "Should use getModule"); } 101 102 107 public void setScarabModule(ScarabModule module) 108 { 109 throw new UnsupportedOperationException ( 110 "Should use setModule(Module). Note module cannot be new."); } 112 113 116 public void setModule(Module me) 117 throws TorqueException 118 { 119 Integer id = me.getModuleId(); 120 if ( id == null) 121 { 122 throw new TorqueException("Modules must be saved prior to " + 123 "being associated with other objects."); } 125 setModuleId(id); 126 } 127 128 133 public Module getModule() 134 throws TorqueException 135 { 136 Module module = null; 137 Integer id = getModuleId(); 138 if ( id != null ) 139 { 140 module = ModuleManager.getInstance(id); 141 } 142 143 return module; 144 } 145 146 private static org.apache.torque.manager.MethodResultCache 147 getMethodResult() 148 { 149 return AttributeGroupManager.getMethodResult(); 150 } 151 152 public boolean hasAnyOptionAttributes() 153 throws Exception 154 { 155 boolean result = false; 156 for (Iterator i = getAttributes().iterator(); i.hasNext() && !result;) 157 { 158 result = ((Attribute)i.next()).isOptionAttribute(); 159 } 160 return result; 161 } 162 163 166 public List getAttributes() 167 throws Exception 168 { 169 List result = null; 170 Object obj = getMethodResult().get(this, GET_ATTRIBUTES); 171 if (obj == null) 172 { 173 Log.get().debug("getAttributes() not cached, getting from db"); 174 Criteria crit = new Criteria() 175 .add(RAttributeAttributeGroupPeer.GROUP_ID, 176 getAttributeGroupId()) 177 .addJoin(RAttributeAttributeGroupPeer.ATTRIBUTE_ID, 178 AttributePeer.ATTRIBUTE_ID) 179 .add(AttributePeer.ATTRIBUTE_TYPE_ID, 180 AttributeTypePeer.USER_TYPE_KEY, Criteria.NOT_EQUAL) 181 .add(AttributePeer.DELETED, false) 182 .addAscendingOrderByColumn(RAttributeAttributeGroupPeer 183 .PREFERRED_ORDER); 184 List raags = RAttributeAttributeGroupPeer.doSelect(crit); 185 result = new LinkedList (); 186 Iterator i = raags.iterator(); 187 while (i.hasNext()) 188 { 189 Integer id = ((RAttributeAttributeGroup)i.next()) 190 .getAttributeId(); 191 result.add(AttributeManager.getInstance(SimpleKey.keyFor(id))); 192 if (Log.get().isDebugEnabled()) 193 { 194 Log.get().debug("attribute id=" + id + 195 "'s relationship to group was in db"); 196 } 197 } 198 getMethodResult().put(result, this, GET_ATTRIBUTES); 199 } 200 else 201 { 202 result = (List )obj; 203 if (Log.get().isDebugEnabled()) 204 { 205 Log.get().debug("getAttributes() returning cached value"); 206 for (Iterator i = result.iterator(); i.hasNext();) 207 { 208 Log.get().debug("attribute id=" + 209 ((Attribute)i.next()).getAttributeId() + 210 "'s relationship to group was in cache"); 211 } 212 } 213 } 214 return result; 215 } 216 217 223 public boolean hasAttribute(Attribute attribute) 224 throws Exception 225 { 226 List raags = getAttributes(); 227 return (raags != null && raags.contains(attribute)); 228 } 229 230 233 public int getHighestOrderedAttribute () 234 throws Exception 235 { 236 int highest = 0; 237 Criteria crit = new Criteria() 238 .add(RAttributeAttributeGroupPeer.GROUP_ID, 239 getAttributeGroupId()) 240 .addAscendingOrderByColumn(RAttributeAttributeGroupPeer 241 .PREFERRED_ORDER); 242 List raags = RAttributeAttributeGroupPeer.doSelect(crit); 243 if (raags.size() > 0) 244 { 245 RAttributeAttributeGroup raag = (RAttributeAttributeGroup) 246 raags.get(raags.size()-1); 247 highest = raag.getOrder(); 248 } 249 return highest + 1; 250 } 251 252 256 public RAttributeAttributeGroup getRAttributeAttributeGroup 257 (Attribute attribute) 258 throws Exception 259 { 260 RAttributeAttributeGroup result = null; 261 Object obj = ScarabCache.get(this, GET_R_ATTRIBUTE_ATTRGROUP, 262 attribute); 263 if (obj == null) 264 { 265 Criteria crit = new Criteria() 266 .add(RAttributeAttributeGroupPeer.GROUP_ID, 267 getAttributeGroupId()) 268 .add(RAttributeAttributeGroupPeer.ATTRIBUTE_ID, 269 attribute.getAttributeId()); 270 271 result = (RAttributeAttributeGroup)RAttributeAttributeGroupPeer 272 .doSelect(crit).get(0); 273 ScarabCache.put(result, this, GET_R_ATTRIBUTE_ATTRGROUP, 274 attribute); 275 } 276 else 277 { 278 result = (RAttributeAttributeGroup)obj; 279 } 280 return result; 281 } 282 283 287 public List getRModuleAttributes() 288 throws Exception 289 { 290 List attrs = getAttributes(); 291 Iterator i = attrs.iterator(); 292 List rmas = new ArrayList (attrs.size()); 293 while (i.hasNext()) 294 { 295 Attribute attr = (Attribute)i.next(); 296 RModuleAttribute rma = getModule().getRModuleAttribute(attr, getIssueType()); 297 if (rma != null) 298 { 299 rmas.add(rma); 300 } 301 } 302 return rmas; 303 } 304 305 306 311 public List getRIssueTypeAttributes() 312 throws Exception 313 { 314 List attrs = getAttributes(); 315 Iterator i = attrs.iterator(); 316 List rias = new ArrayList (attrs.size()); 317 while (i.hasNext()) 318 { 319 Attribute attr = (Attribute)i.next(); 320 RIssueTypeAttribute ria = 321 getIssueType().getRIssueTypeAttribute(attr); 322 323 if (ria != null) 324 { 325 rias.add(ria); 326 } 327 } 328 329 return rias; 330 } 331 332 public void delete() 333 throws Exception 334 { 335 int dupeSequence = 0; 336 Integer issueTypeId = getIssueTypeId(); 337 IssueType issueType = IssueTypeManager 338 .getInstance(SimpleKey.keyFor(issueTypeId), false); 339 List attrGroups = null; 340 341 if (isGlobal()) 342 { 343 attrGroups = issueType.getAttributeGroups(null, false); 344 dupeSequence = issueType.getDedupeSequence(); 345 Criteria crit = new Criteria() 347 .addJoin(RIssueTypeAttributePeer.ATTRIBUTE_ID, 348 RAttributeAttributeGroupPeer.ATTRIBUTE_ID) 349 .add(RAttributeAttributeGroupPeer.GROUP_ID, 350 getAttributeGroupId()) 351 .add(RIssueTypeAttributePeer.ISSUE_TYPE_ID, issueTypeId); 352 List results = RIssueTypeAttributePeer.doSelect(crit); 353 for (Iterator i = results.iterator(); i.hasNext();) 354 { 355 ((RIssueTypeAttribute)i.next()).delete(); 356 } 357 } 358 else 359 { 360 if (issueType.getLocked()) 361 { 362 363 throw new ScarabException(L10NKeySet.ExceptionGroupDeleteForbidden, 364 this.getName(), 365 issueType.getName()); 366 } 367 else 368 { 369 Module module = getModule(); 370 attrGroups = getIssueType().getAttributeGroups(module, false); 371 dupeSequence = module.getDedupeSequence(issueType); 372 Criteria crit = new Criteria() 374 .addJoin(RModuleAttributePeer.ATTRIBUTE_ID, 375 RAttributeAttributeGroupPeer.ATTRIBUTE_ID) 376 .add(RAttributeAttributeGroupPeer.GROUP_ID, 377 getAttributeGroupId()) 378 .add(RModuleAttributePeer.MODULE_ID, 379 getModuleId()); 380 Criteria.Criterion critIssueType = crit.getNewCriterion( 381 RModuleAttributePeer.ISSUE_TYPE_ID, 382 getIssueTypeId(), Criteria.EQUAL); 383 critIssueType.or(crit.getNewCriterion( 384 RModuleAttributePeer.ISSUE_TYPE_ID, 385 issueType.getTemplateId(), Criteria.EQUAL)); 386 crit.and(critIssueType); 387 List results = RModuleAttributePeer.doSelect(crit); 388 for (int i=0; i<results.size(); i++) 389 { 390 RModuleAttribute rma = (RModuleAttribute)results.get(i); 391 rma.delete(); 392 } 393 } 394 } 395 396 Criteria crit2 = new Criteria() 398 .add(RAttributeAttributeGroupPeer.GROUP_ID, getAttributeGroupId()); 399 RAttributeAttributeGroupPeer.doDelete(crit2); 400 401 402 int order = getOrder(); 404 crit2 = new Criteria() 405 .add(AttributeGroupPeer.ATTRIBUTE_GROUP_ID, getAttributeGroupId()); 406 AttributeGroupPeer.doDelete(crit2); 407 IssueTypeManager.getManager().refreshedObject(this); 408 409 for (int i=0; i<attrGroups.size(); i++) 411 { 412 AttributeGroup tempGroup = (AttributeGroup)attrGroups.get(i); 413 int tempOrder = tempGroup.getOrder(); 414 if (tempGroup.getOrder() > order) 415 { 416 if (tempOrder == dupeSequence + 1 && tempOrder == 3) 417 { 418 tempGroup.setOrder(tempOrder - 2); 419 } 420 else 421 { 422 tempGroup.setOrder(tempOrder - 1); 423 } 424 tempGroup.save(); 425 } 426 } 427 } 428 429 public void addAttribute(Attribute attribute) 430 throws Exception 431 { 432 IssueType issueType = getIssueType(); 433 Module module = getModule(); 434 435 RAttributeAttributeGroup raag = 437 addRAttributeAttributeGroup(attribute); 438 raag.save(); 439 if (Log.get().isDebugEnabled()) 440 { 441 Log.get().debug("Calling getAttributes() from addAttribute in order to add attribute id=" 442 + attribute.getAttributeId() + " to the List"); 443 } 444 List attributes = getAttributes(); 446 if (!attributes.contains(attribute)) 447 { 448 attributes.add(attribute); 449 } 450 451 List allOptions = attribute.getAttributeOptions(false); 452 ArrayList options = new ArrayList (); 456 for (int i=0; i<allOptions.size(); i++) 457 { 458 AttributeOption option = (AttributeOption)allOptions.get(i); 459 if (!options.contains(option)) 460 { 461 options.add(option); 462 } 463 } 464 465 if (isGlobal()) 466 { 467 issueType.addRIssueTypeAttribute(attribute); 470 471 for (int j=0;j < options.size();j++) 473 { 474 AttributeOption option = (AttributeOption)options.get(j); 475 List roos = option.getROptionOptionsRelatedByOption2Id(); 476 ROptionOption roo = (ROptionOption)roos.get(0); 477 RIssueTypeOption rio = new RIssueTypeOption(); 478 rio.setIssueTypeId(issueType.getIssueTypeId()); 479 rio.setOptionId(option.getOptionId()); 480 rio.setOrder(roo.getPreferredOrder()); 481 rio.setWeight(roo.getWeight()); 482 try 485 { 486 rio.save(); 487 } 488 catch (Exception e) 489 { 490 Log.get().error("Exception saving rio", e); 491 throw e; } 493 } 494 } 495 else 496 { 497 module.addRModuleAttribute(issueType, attribute); 499 500 for (int j=0;j < options.size();j++) 502 { 503 AttributeOption option = (AttributeOption)options.get(j); 504 List roos = option.getROptionOptionsRelatedByOption2Id(); 505 ROptionOption roo = (ROptionOption)roos.get(0); 506 RModuleOption rmo = new RModuleOption(); 507 rmo.setModuleId(getModuleId()); 508 rmo.setIssueTypeId(issueType.getIssueTypeId()); 509 rmo.setOptionId(option.getOptionId()); 510 rmo.setDisplayValue(option.getName()); 511 rmo.setOrder(roo.getPreferredOrder()); 512 rmo.setWeight(roo.getWeight()); 513 rmo.save(); 514 515 IssueType templateType = IssueTypeManager 517 .getInstance(issueType.getTemplateId(), false); 518 RModuleOption rmo2 = module. 519 addRModuleOption(templateType, option); 520 rmo2.save(); 521 } 522 } 523 getMethodResult().remove(this, AttributeGroup.GET_ATTRIBUTES); 524 } 525 526 public void deleteAttribute(Attribute attribute, ScarabUser user, 527 Module module) 528 throws Exception 529 { 530 String permission = null; 531 if (isGlobal()) 532 { 533 permission = (ScarabSecurity.DOMAIN__EDIT); 534 } 535 else 536 { 537 permission = (ScarabSecurity.MODULE__CONFIGURE); 538 } 539 if (user.hasPermission(permission, module)) 540 { 541 IssueType issueType = getIssueType(); 542 IssueType template = IssueTypeManager.getInstance 543 (issueType.getTemplateId()); 544 boolean success = true; 545 546 RIssueTypeAttribute ria = issueType.getRIssueTypeAttribute(attribute); 547 if (isGlobal()) 548 { 549 List rias = issueType.getRIssueTypeAttributes 552 (false, AttributePeer.NON_USER); 553 if (ria != null) 554 { 555 ria.delete(); 556 } 557 if (rias != null) 558 { 559 rias.remove(ria); 560 } 561 } 562 else 563 { 564 if (ria != null && ria.getLocked()) 566 { 567 success = false; 568 throw new TorqueException(attribute.getName() + "is locked"); } 570 else 571 { 572 List rmas = module.getRModuleAttributes(issueType, false, 574 AttributePeer.NON_USER); 575 RModuleAttribute rma = module 576 .getRModuleAttribute(attribute, issueType); 577 rma.delete(); 578 WorkflowFactory.getInstance().deleteWorkflowsForAttribute 579 (attribute, module, issueType); 580 rmas.remove(rma); 581 582 RModuleAttribute rma2 = module 584 .getRModuleAttribute(attribute, template); 585 rma2.delete(); 586 rmas.remove(rma2); 587 } 588 } 589 590 if (success) 591 { 592 RAttributeAttributeGroup raag = 594 getRAttributeAttributeGroup(attribute); 595 raag.delete(); 596 597 if (attribute.isOptionAttribute()) 598 { 599 if (isGlobal()) 600 { 601 List rios = issueType.getRIssueTypeOptions(attribute); 603 if (rios != null) 604 { 605 for (int i = 0; i<rios.size();i++) 606 { 607 ((RIssueTypeOption) rios.get(i)) 608 .delete(user, module); 609 } 610 } 611 } 612 else 613 { 614 List rmos = module.getRModuleOptions(attribute, issueType); 616 if (rmos != null) 617 { 618 rmos.addAll(module.getRModuleOptions(attribute, template)); 619 for (int j = 0; j<rmos.size();j++) 620 { 621 RModuleOption rmo = (RModuleOption)rmos.get(j); 622 if (rmo.getModuleId().equals(module.getModuleId())) 626 { 627 rmo.delete(); 628 } 629 else 630 { 631 break; 632 } 633 } 634 } 635 } 636 } 637 } 638 } 639 else 640 { 641 throw new ScarabException(L10NKeySet.YouDoNotHavePermissionToAction); 642 } 643 getMethodResult().remove(this, AttributeGroup.GET_ATTRIBUTES); 644 } 645 646 private RAttributeAttributeGroup addRAttributeAttributeGroup(Attribute attribute) 647 throws Exception 648 { 649 RAttributeAttributeGroup raag = new RAttributeAttributeGroup(); 650 raag.setGroupId(getAttributeGroupId()); 651 raag.setAttributeId(attribute.getAttributeId()); 652 if (Log.get().isDebugEnabled()) 653 { 654 Log.get().debug("Calling getAttributes() from addRAttributeAttributeGroup " 655 + "in order to determine order for attribute id=" + 656 attribute.getAttributeId()); 657 } 658 raag.setOrder(getAttributes().size() +1); 659 return raag; 660 } 661 662 public AttributeGroup copyGroup() 663 throws Exception 664 { 665 AttributeGroup newGroup = new AttributeGroup(); 666 newGroup.setName(getName()); 667 newGroup.setDescription(getDescription()); 668 newGroup.setDedupe(getDedupe()); 669 newGroup.setActive(getActive()); 670 newGroup.setOrder(getOrder()); 671 return newGroup; 672 } 673 } 674 | Popular Tags |