1 package org.tigris.scarab.actions.admin; 2 3 48 49 import java.util.ArrayList ; 50 import java.util.Iterator ; 51 import java.util.List ; 52 import java.util.Date ; 53 54 import org.apache.turbine.RunData; 55 import org.apache.turbine.TemplateContext; 56 import org.apache.torque.TorqueException; 57 import org.apache.torque.om.NumberKey; 58 import org.apache.turbine.tool.IntakeTool; 59 import org.apache.fulcrum.intake.model.Group; 60 import org.apache.fulcrum.intake.model.Field; 61 62 import org.tigris.scarab.actions.base.RequireLoginFirstAction; 63 import org.tigris.scarab.om.Attribute; 64 import org.tigris.scarab.om.AttributeGroup; 65 import org.tigris.scarab.om.AttributeManager; 66 import org.tigris.scarab.om.AttributeType; 67 import org.tigris.scarab.om.AttributeTypeManager; 68 import org.tigris.scarab.om.ROptionOption; 69 import org.tigris.scarab.om.ParentChildAttributeOption; 70 import org.tigris.scarab.om.ScarabUser; 71 import org.tigris.scarab.om.AttributeOption; 72 import org.tigris.scarab.om.AttributeOptionPeer; 73 import org.tigris.scarab.om.IssueType; 74 import org.tigris.scarab.om.Module; 75 import org.tigris.scarab.om.Transition; 76 import org.tigris.scarab.om.TransitionPeer; 77 import org.tigris.scarab.util.ScarabConstants; 78 import org.tigris.scarab.util.Log; 79 import org.tigris.scarab.tools.ScarabRequestTool; 80 import org.tigris.scarab.tools.ScarabLocalizationTool; 81 import org.tigris.scarab.services.cache.ScarabCache; 82 83 89 public class GlobalAttributeEdit extends RequireLoginFirstAction 90 { 91 95 public boolean doSaveattributedata(RunData data, TemplateContext context) 96 throws Exception 97 { 98 IntakeTool intake = getIntakeTool(context); 99 ScarabRequestTool scarabR = getScarabRequestTool(context); 100 ScarabLocalizationTool l10n = getLocalizationTool(context); 101 boolean success = true; 102 boolean confirmDelete = false; 103 104 if (intake.isAllValid()) 105 { 106 Attribute attr = scarabR.getAttribute(); 107 108 Group attrGroup = null; 109 boolean isDupe = false; 110 Field attributeName = null; 111 Field description = null; 112 if (attr.getAttributeId() == null) 113 { 114 attrGroup = intake.get("Attribute", IntakeTool.DEFAULT_KEY); 116 attr.setCreatedBy(((ScarabUser)data.getUser()).getUserId()); 117 attr.setCreatedDate(new Date ()); 118 } 119 else 120 { 121 attrGroup = intake.get("Attribute", attr.getQueryKey()); 122 } 123 attributeName = attrGroup.get("Name"); 124 description = attrGroup.get("Description"); 125 isDupe = Attribute.checkForDuplicate(attributeName.toString().trim(), attr); 126 127 if (attributeName.toString().trim().length() == 0) 129 { 130 attributeName.setMessage("intake_AttributeNameNotAllowedEmpty"); 131 scarabR.setAlertMessage(l10n.get(ERROR_MESSAGE)); 132 success = false; 133 } 134 if (description.toString().trim().length() == 0) 135 { 136 description.setMessage("intake_AttributeDescriptionNotAllowedEmpty"); 137 scarabR.setAlertMessage(l10n.get(ERROR_MESSAGE)); 138 success = false; 139 } 140 else if (isDupe) 142 { 143 scarabR.setAlertMessage( 144 l10n.get("CannotCreateDuplicateAttribute")); 145 success = false; 146 } 147 else 148 { 149 if (!attr.getDeleted() && 152 attrGroup.get("Deleted").toString().equals("true") && 153 (attr.hasModuleMappings() || 154 attr.hasGlobalIssueTypeMappings())) 155 { 156 context.put("deleting", "deleting"); 157 confirmDelete=true; 158 success = false; 159 } 160 for (int i = 0; i < attrGroup.getFieldNames().length; i++) 161 { 162 String fieldName = attrGroup.getFieldNames()[i]; 163 if (!fieldName.equals("Deleted") || !confirmDelete) 164 { 165 attrGroup.get(fieldName).setProperty(attr); 166 } 167 } 168 attr.save(); 169 mapAttribute(data,context); 170 if (success) 171 { 172 scarabR.setConfirmMessage(l10n.get(DEFAULT_MSG)); 173 AttributeManager.clear(); 174 } 175 } 176 } 177 else 178 { 179 success = false; 180 scarabR.setAlertMessage(l10n.get(ERROR_MESSAGE)); 181 } 182 return success; 183 } 184 185 188 public void doDeleteattribute(RunData data, TemplateContext context) 189 throws Exception 190 { 191 ScarabRequestTool scarabR = getScarabRequestTool(context); 192 Attribute attr = scarabR.getAttribute(); 193 if (attr.isSystemDefined()) 194 { 195 scarabR.setAlertMessage(getLocalizationTool(context).get 196 ("CannotDeleteSystemSpecifiedAttribute")); 197 return; 198 } 199 if (attr.getAttributeId() != null) 200 { 201 attr.deleteModuleMappings(); 202 attr.deleteIssueTypeMappings(); 203 attr.setDeleted(true); 204 attr.save(); 205 scarabR.setConfirmMessage(getLocalizationTool(context).get(DEFAULT_MSG)); 206 setTarget(data, getCancelTemplate(data)); 207 } 208 } 209 210 214 public synchronized boolean 215 doSaveoptions(RunData data, TemplateContext context) 216 throws Exception 217 { 218 IntakeTool intake = (IntakeTool)context 219 .get(ScarabConstants.INTAKE_TOOL); 220 ScarabRequestTool scarabR = (ScarabRequestTool)context 221 .get(ScarabConstants.SCARAB_REQUEST_TOOL); 222 ScarabLocalizationTool l10n = getLocalizationTool(context); 223 224 if (intake.isAllValid()) 225 { 226 Attribute attribute = scarabR.getAttribute(); 228 if (log().isDebugEnabled()) 229 { 230 log().debug("doSaveoptions for attribute id=" + 231 attribute.getAttributeId()); 232 } 233 Group attGroup = intake.get("Attribute", attribute.getQueryKey()); 234 String attributeTypeId = attGroup.get("TypeId").toString(); 235 AttributeType attributeType = AttributeTypeManager 236 .getInstance(new NumberKey(attributeTypeId), false); 237 238 if (attributeType.getAttributeClass().getName() 239 .equals("select-one")) 240 { 241 if (log().isDebugEnabled()) 242 { 243 log().debug("attribute id=" + attribute.getAttributeId() + 244 " is an option attribute"); 245 } 246 boolean somethingSaved = false; 247 List pcaoList = attribute.getParentChildAttributeOptions(); 250 if (areThereDupeSequences(pcaoList, intake, 252 "ParentChildAttributeOption","PreferredOrder", 0)) 253 { 254 scarabR.setAlertMessage(l10n.format("DuplicateSequenceNumbersFound", 255 l10n.get("AttributeOptions").toLowerCase())); 256 return false; 257 } 258 for (int i=pcaoList.size()-1; i>=0; i--) 259 { 260 ParentChildAttributeOption pcao = 261 (ParentChildAttributeOption)pcaoList.get(i); 262 263 if (pcao.getChildOption().isSystemDefined()) 264 { 265 if (Log.get().isDebugEnabled()) 266 { 267 Log.get().debug("PCAO(" + pcao + 268 ") is used by a system defined issue type"); 269 } 270 } 271 else 272 { 273 Group pcaoGroup = intake.get("ParentChildAttributeOption", 274 pcao.getQueryKey()); 275 276 Integer currentParentId = null; 279 try 280 { 281 currentParentId = pcao.getParentId(); 283 pcaoGroup.setProperties(pcao); 285 286 if (pcao.getDeleted()) 288 { 289 AttributeOption option = AttributeOptionPeer 290 .retrieveByPK(pcao.getOptionId()); 291 if (log().isDebugEnabled()) 292 { 293 log().debug("deleting mappings for option id=" + 294 option.getOptionId()); 295 } 296 option.deleteModuleMappings(); 297 option.deleteIssueTypeMappings(); 298 } 299 300 List ancestors = null; 301 try 302 { 303 ancestors= pcao.getAncestors(); 304 } 305 catch (Exception e) 306 { 307 scarabR.setAlertMessage( 308 l10n.get("RecursiveParentChildRelationship")); 309 intake.remove(pcaoGroup); 310 return false; 311 } 312 if (ancestors.contains(pcao.getOptionId())) 313 { 314 scarabR.setAlertMessage( 315 l10n.get("RecursiveParentChildRelationship")); 316 intake.remove(pcaoGroup); 317 return false; 318 } 319 320 pcao.save(); 322 323 if (!pcao.getParentId().equals(currentParentId)) 326 { 327 if (log().isDebugEnabled()) 328 { 329 log().debug("removing parent relationship for option id=" + 330 pcao.getOptionId() + ", old parent id=" 331 + currentParentId); 332 } 333 ROptionOption.doRemove(currentParentId, 334 pcao.getOptionId()); 335 } 336 337 intake.remove(pcaoGroup); 340 if (log().isDebugEnabled()) 341 { 342 log().debug("Saved pcao for attribute id=" + 343 pcao.getAttributeId() + " and option id=" 344 + pcao.getOptionId()); 345 } 346 somethingSaved = true; 347 } 348 catch (Exception se) 349 { 350 intake.remove(pcaoGroup); 352 scarabR.setAlertMessage(l10n.getMessage(se)); 353 log().error("", se); 354 return false; 355 } 356 } 357 } 358 if (somethingSaved) 359 { 360 scarabR.setConfirmMessage(l10n.get(DEFAULT_MSG)); 361 AttributeManager.clear(); 362 } 363 364 ParentChildAttributeOption newPCAO = 366 ParentChildAttributeOption.getInstance(); 367 Group newPCAOGroup = intake.get("ParentChildAttributeOption", 368 newPCAO.getQueryKey()); 369 if (newPCAOGroup != null) 370 { 371 log().debug("checking for a new pcao"); 372 try 373 { 374 newPCAOGroup.setProperties(newPCAO); 376 if (newPCAO.getName() != null && newPCAO.getName().length() > 0) 378 { 379 newPCAO.setAttributeId(attribute.getAttributeId()); 381 try 382 { 383 newPCAO.save(); 384 if (log().isDebugEnabled()) 385 { 386 log().debug("Saved NEW pcao for attribute id=" 387 + newPCAO.getAttributeId() + " and option id=" 388 + newPCAO.getOptionId()); 389 } 390 pcaoList.add(newPCAO); 391 IssueType issueType = null; 392 AttributeOption option = null; 393 394 String cancelTemplate = getCancelTemplate(data); 397 if (cancelTemplate != null 398 && (cancelTemplate.equals("admin,AttributeOptionSelect.vm") 399 || cancelTemplate.equals("admin,GlobalAttributeOptionSelect.vm"))) 400 { 401 issueType = scarabR.getIssueType(); 402 option = scarabR.getAttributeOption(newPCAO.getOptionId()); 403 if (log().isDebugEnabled()) 404 { 405 log().debug("cancelTemplate=" + 406 cancelTemplate + 407 " issuetype id=" + 408 issueType.getIssueTypeId() + 409 " and option id=" + 410 option.getOptionId()); 411 } 412 } 413 if (cancelTemplate.equals("admin,AttributeOptionSelect.vm")) 415 { 416 scarabR.getCurrentModule() 417 .addAttributeOption(issueType, option); 418 data.getParameters().setString( 419 ScarabConstants.CANCEL_TEMPLATE, 420 "admin,ModuleAttributeEdit.vm"); 421 if (log().isDebugEnabled()) 422 { 423 log().debug("Adding mapping to module id" 424 + scarabR.getCurrentModule() 425 .getModuleId()); 426 } 427 } 428 else if (cancelTemplate.equals("admin,GlobalAttributeOptionSelect.vm")) 430 { 431 issueType.addRIssueTypeOption(option); 432 data.getParameters().setString( 433 ScarabConstants.CANCEL_TEMPLATE, 434 "admin,IssueTypeAttributeEdit.vm"); 435 log().debug("Adding mapping to issuetype"); 436 } 437 scarabR.setConfirmMessage( 438 l10n.get("AttributeOptionAdded") + 439 l10n.get(DEFAULT_MSG)); 440 } 441 catch (Exception e) 442 { 443 log().error("Error adding attribute option:", e); 444 scarabR.setAlertMessage("Error adding attribute option:" + l10n.getMessage(e)); 445 } 446 } 447 } 448 catch (Exception se) 449 { 450 intake.remove(newPCAOGroup); 451 scarabR.setAlertMessage(se.getMessage()); 452 log().error("", se); 453 return false; 454 } 455 456 intake.remove(newPCAOGroup); 458 attribute.buildOptionsMap(); 459 ScarabCache.clear(); 460 } 461 } 462 } 463 return true; 464 } 465 466 469 public void doDone(RunData data, TemplateContext context) 470 throws Exception 471 { 472 log().debug("called doDone"); 473 boolean success = doSaveattributedata(data, context); 474 if (getScarabRequestTool(context).getAttribute().isOptionAttribute()) 475 { 476 success = doSaveoptions(data, context); 477 updatetransitiondata(data, context); 478 } 479 if (success) 480 { 481 if(!"deleting".equals(context.get("deleting"))) 484 { 485 log().debug("calling doCancel"); 486 doCancel(data, context); 487 } 488 } 489 else 491 { 492 getScarabRequestTool(context).setConfirmMessage(null); 493 } 494 } 495 498 499 private void mapAttribute(RunData data, TemplateContext context) 500 throws Exception 501 { 502 ScarabRequestTool scarabR = getScarabRequestTool(context); 503 ScarabLocalizationTool l10n = getLocalizationTool(context); 504 String lastTemplate = getCancelTemplate(data); 505 Attribute attribute = scarabR.getAttribute(); 506 507 if (log().isDebugEnabled()) 508 { 509 log().debug("called mapAttribute; lastTemplate=" + lastTemplate + 510 " and attribute id=" + attribute.getAttributeId()); 511 } 512 if (lastTemplate != null && attribute.getAttributeId() != null) 513 { 514 if (lastTemplate.equals("admin,AttributeGroupEdit.vm") || 516 lastTemplate.equals("admin,GlobalAttributeGroupEdit.vm")) 517 { 518 String groupId = data.getParameters().getString("groupId"); 520 if (groupId != null) 521 { 522 if (log().isDebugEnabled()) 523 { 524 log().debug("Adding attribute to group id=" + groupId); 525 } 526 AttributeGroup attributeGroup = scarabR.getAttributeGroup(groupId); 527 if(!attributeGroup.hasAttribute(attribute)) 528 { 529 scarabR.getAttributeGroup(groupId).addAttribute(attribute); 530 scarabR.setConfirmMessage(l10n.get("AttributeAdded")); 531 } 532 } 533 } 534 else if (lastTemplate.equals("admin,ArtifactTypeEdit.vm")) 535 { 536 Module currentModule = scarabR.getCurrentModule(); 537 IssueType issueType = scarabR.getIssueType(); 538 if (log().isDebugEnabled()) 539 { 540 log().debug("Adding attribute to module id=" + 541 currentModule.getModuleId()); 542 } 543 if (!attribute.hasMapping(currentModule, issueType)) 545 { 546 currentModule.addRModuleAttribute(issueType,attribute); 547 scarabR.setConfirmMessage(l10n.get("AttributeAdded")); 548 } 549 550 } 551 else if (lastTemplate.equals("admin,GlobalArtifactTypeEdit.vm")) 552 { 553 IssueType issueType = scarabR.getIssueType(); 554 if (log().isDebugEnabled()) 555 { 556 log().debug("Assuming user attribute and adding to " 557 + "issuetype id " 558 + issueType.getIssueTypeId()); 559 } 560 if (!attribute.hasGlobalMapping(issueType)) 562 { 563 issueType.addRIssueTypeAttribute(attribute); 564 scarabR.setConfirmMessage(l10n.get("AttributeAdded")); 565 } 566 } 567 ScarabCache.clear(); 568 } 569 570 } 571 572 577 public void doCancel(RunData data, TemplateContext context) 578 throws Exception 579 { 580 String lastTemplate = getCancelTemplate(data); 581 if (lastTemplate != null) 582 { 583 setTarget(data, lastTemplate); 584 } 585 else 586 { 587 super.doCancel(data, context); 588 } 589 } 590 591 599 public boolean doSavetransitiondata(RunData data, TemplateContext context) 600 throws Exception 601 { 602 boolean bRdo = false; 603 ScarabRequestTool scarabR = getScarabRequestTool(context); 604 Attribute attr = scarabR.getAttribute(); 605 Integer attributeId = attr.getAttributeId(); 606 if (!data.getParameters().getString("trans_new_RoleId").equals("choose") && 607 !data.getParameters().getString("trans_new_FromId").equals("choose") && 608 !data.getParameters().getString("trans_new_ToId").equals("choose")) 609 { 610 Integer roleId = data.getParameters().getInteger("trans_new_RoleId"); 611 Integer fromId = data.getParameters().getInteger("trans_new_FromId"); 612 Integer toId = data.getParameters().getInteger("trans_new_ToId"); 613 614 if (roleId.intValue() == -1) 615 roleId = null; 616 if (fromId.intValue() == -1) 617 fromId = null; 618 if (toId.intValue() == -1) 619 toId = null; 620 try 621 { 622 Transition transition = new Transition(); 623 transition.setRoleId(roleId); 624 transition.setFromOptionId(fromId); 625 transition.setToOptionId(toId); 626 transition.setAttributeId(attributeId); 627 attr.addTransition(transition); 628 attr.save(); 629 transition.save(); 630 bRdo = true; 631 } 632 catch (TorqueException te) 633 { 634 this.log().error("doSavetransitiondata(): " + te); 635 } 636 } 637 updatetransitiondata(data, context); 638 639 return bRdo; 640 } 641 642 650 public boolean doDeletetransitiondata(RunData data, TemplateContext context) 651 throws Exception 652 { 653 boolean bRdo = false; 654 boolean bChanges = false; 655 ScarabRequestTool scarabR = getScarabRequestTool(context); 656 ScarabLocalizationTool l10n = getLocalizationTool(context); 657 Attribute attr = scarabR.getAttribute(); 658 List transitions = attr.getTransitions(); 659 Iterator iter = transitions.iterator(); 660 661 List toDelete = new ArrayList (); 664 try 665 { 666 while (iter.hasNext()) 667 { 668 Transition trans = (Transition) iter.next(); 669 boolean bDelete = data.getParameters().getBoolean( 670 "trans_delete_" + trans.getTransitionId()); 671 if (bDelete) 672 { 673 bChanges = true; 674 toDelete.add(trans); 675 AttributeManager.clear(); 676 } 677 } 678 iter = toDelete.iterator(); 679 while (iter.hasNext()) 680 { 681 Transition trans = (Transition) iter.next(); 682 transitions.remove(trans); 683 TransitionPeer.doDelete(trans); 684 } 685 bRdo = true; 686 } 687 catch (TorqueException te) 688 { 689 this.log().error("doDeleteTransition(): " + te); 690 } 691 scarabR.setConfirmMessage(l10n.get(DEFAULT_MSG)); 692 return bRdo; 693 } 694 695 701 private void updatetransitiondata(RunData data, TemplateContext context) throws Exception 702 { 703 ScarabRequestTool scarabR = getScarabRequestTool(context); 704 ScarabLocalizationTool l10n = getLocalizationTool(context); 705 Attribute attr = scarabR.getAttribute(); 706 List transitions = attr.getTransitions(); 707 for (Iterator iter = transitions.iterator(); iter.hasNext(); ) 708 { 709 Transition trans = (Transition)iter.next(); 711 boolean value = data.getParameters().getBoolean("trans_disabled_value_" + trans.getTransitionId()); 712 boolean newValue = data.getParameters().getBoolean("trans_disabled_new_" + trans.getTransitionId()); 713 if (value != newValue) 714 { 715 trans.setDisabledIfBlocked(newValue); 716 trans.save(); 717 } 718 } 719 } 720 } 721 | Popular Tags |