1 package org.tigris.scarab.util.xmlissues; 2 3 48 49 import java.io.File ; 50 import java.text.ParseException ; 51 import java.util.ArrayList ; 52 import java.util.HashMap ; 53 import java.util.HashSet ; 54 import java.util.Iterator ; 55 import java.util.List ; 56 import java.util.Locale ; 57 import java.util.Map ; 58 import java.util.Set ; 59 60 import org.apache.commons.collections.map.LinkedMap; 61 import org.apache.commons.logging.Log; 62 import org.apache.commons.logging.LogFactory; 63 import org.apache.fulcrum.localization.Localization; 64 import org.tigris.scarab.om.Activity; 65 import org.tigris.scarab.om.ActivityManager; 66 import org.tigris.scarab.om.ActivitySet; 67 import org.tigris.scarab.om.ActivitySetManager; 68 import org.tigris.scarab.om.ActivitySetType; 69 import org.tigris.scarab.om.ActivitySetTypeManager; 70 import org.tigris.scarab.om.ActivitySetTypePeer; 71 import org.tigris.scarab.om.Attachment; 72 import org.tigris.scarab.om.AttachmentManager; 73 import org.tigris.scarab.om.AttachmentType; 74 import org.tigris.scarab.om.Attribute; 75 import org.tigris.scarab.om.AttributeOption; 76 import org.tigris.scarab.om.AttributeValue; 77 import org.tigris.scarab.om.Depend; 78 import org.tigris.scarab.om.DependManager; 79 import org.tigris.scarab.om.Issue; 80 import org.tigris.scarab.om.IssueManager; 81 import org.tigris.scarab.om.IssueType; 82 import org.tigris.scarab.om.Module; 83 import org.tigris.scarab.om.ModuleManager; 84 import org.tigris.scarab.om.RModuleOption; 85 import org.tigris.scarab.om.RModuleOptionManager; 86 import org.tigris.scarab.om.ScarabUser; 87 import org.tigris.scarab.om.ScarabUserManager; 88 import org.tigris.scarab.util.ScarabConstants; 89 90 118 public class ScarabIssues implements java.io.Serializable 119 { 120 private static final Log LOG = LogFactory.getLog(ScarabIssues.class); 121 122 private XmlModule module = null; 123 124 127 private List issues = null; 128 129 private String importType = null; 130 131 private int importTypeCode = -1; 132 133 private List allDependencies = new ArrayList (); 134 135 138 private Map issueXMLMap = new HashMap (); 139 140 144 private Map activitySetIdMap = new HashMap (); 145 146 150 private Map attachmentIdMap = new HashMap (); 151 152 156 private List dependActivitySetId = new ArrayList (); 157 158 private static final int CREATE_SAME_DB = 1; 159 private static final int CREATE_DIFFERENT_DB = 2; 160 private static final int UPDATE_SAME_DB = 3; 161 162 private static Attribute nullAttribute = null; 163 164 168 private boolean inValidationMode = true; 169 170 174 ImportErrors importErrors; 175 176 179 private Set importUsers = new HashSet (); 180 181 192 private boolean allowFileAttachments = false; 193 194 public ScarabIssues() 195 { 196 issues = new ArrayList (); 197 if (nullAttribute == null) 198 { 199 try 200 { 201 nullAttribute = Attribute.getInstance(0); 202 } 203 catch (Exception e) 204 { 205 LOG.warn("Could not assign nullAttribute", e); 206 } 207 } 208 } 209 210 221 public void allowFileAttachments(boolean flag) 222 { 223 this.allowFileAttachments = flag; 224 } 225 226 public void inValidationMode(boolean flag) 227 { 228 inValidationMode = flag; 229 } 230 231 public void setImportType(String value) 232 { 233 this.importType = value; 234 if (importType.equals("create-same-db")) 235 { 236 importTypeCode = CREATE_SAME_DB; 237 } 238 else if (importType.equals("create-different-db")) 239 { 240 importTypeCode = CREATE_DIFFERENT_DB; 241 } 242 else if (importType.equals("update-same-db")) 243 { 244 importTypeCode = UPDATE_SAME_DB; 245 } 246 } 247 248 public String getImportType() 249 { 250 return this.importType; 251 } 252 253 public int getImportTypeCode() 254 { 255 return this.importTypeCode; 256 } 257 258 261 public Map getIDs() 262 { 263 return this.issueXMLMap; 264 } 265 266 public XmlModule getModule() 267 { 268 return this.module; 269 } 270 271 public void setModule(XmlModule module) 272 { 273 LOG.debug("Module.setModule(): " + module.getName()); 274 this.module = module; 275 } 276 277 void doValidateUsers() 278 throws Exception 279 { 280 if (importUsers != null && !importUsers.isEmpty()) 281 { 282 for (Iterator itr = importUsers.iterator(); itr.hasNext();) 283 { 284 String userStr = (String )itr.next(); 285 try 286 { 287 ScarabUser user = ScarabUserManager.getInstance(userStr, 288 getModule().getDomain()); 289 if (user == null) 290 { 291 throw new Exception (); } 293 } 294 catch (Exception e) 295 { 296 String error = Localization.format( 297 ScarabConstants.DEFAULT_BUNDLE_NAME, 298 getLocale(), 299 "CouldNotLocateUsername", userStr); 300 importErrors.add(error); 301 } 302 } 303 } 304 } 305 306 void doValidateDependencies() 307 throws Exception 308 { 309 if (allDependencies != null && !allDependencies.isEmpty()) 310 { 311 for (Iterator itr = allDependencies.iterator(); itr.hasNext();) 312 { 313 XmlActivity activity = (XmlActivity)itr.next(); 314 Dependency dependency = activity.getDependency(); 315 String child = (String )issueXMLMap.get(dependency.getChild()); 316 String parent = (String )issueXMLMap.get(dependency.getParent()); 317 if (parent == null || child == null) 318 { 319 LOG.debug("Could not find issues for parent '" + parent + 320 "' and child '" + child + '\''); 321 continue; 322 } 323 try 324 { 325 Issue parentIssueOM = IssueManager.getIssueById(parent); 326 if (parentIssueOM == null) 327 { 328 throw new Exception (); } 330 } 331 catch (Exception e) 332 { 333 String error = Localization.format( 334 ScarabConstants.DEFAULT_BUNDLE_NAME, 335 getLocale(), 336 "CouldNotLocateParentDepend", parent); 337 importErrors.add(error); 338 } 339 try 340 { 341 Issue childIssueOM = IssueManager.getIssueById(child); 342 if (childIssueOM == null) 343 { 344 throw new Exception (); } 346 } 347 catch (Exception e) 348 { 349 String error = Localization.format( 350 ScarabConstants.DEFAULT_BUNDLE_NAME, 351 getLocale(), 352 "CouldNotLocateChildDepend", child); 353 importErrors.add(error); 354 } 355 } 356 } 357 allDependencies.clear(); 358 } 359 360 void doHandleDependencies() 361 throws Exception 362 { 363 LOG.debug("Number of dependencies found: " + allDependencies.size()); 364 for (Iterator itr = allDependencies.iterator(); itr.hasNext();) 365 { 366 Object [] data = (Object [])itr.next(); 367 ActivitySet activitySetOM = (ActivitySet) data[0]; 368 XmlActivity activity = (XmlActivity) data[1]; 369 370 371 Dependency dependency = activity.getDependency(); 372 String child = (String )issueXMLMap.get(dependency.getChild()); 373 String parent = (String )issueXMLMap.get(dependency.getParent()); 374 if (parent == null || child == null) 375 { 376 LOG.debug("Could not find issues: parent: " + parent + " child: " + child); 377 continue; 378 } 379 380 if (getImportTypeCode() == UPDATE_SAME_DB) 381 { 382 LOG.error("update-same-db import type not yet implemented"); 383 } 384 else 385 { 386 try 387 { 388 String type = dependency.getType(); 389 Depend newDependOM = DependManager.getInstance(); 390 Issue parentIssueOM = IssueManager.getIssueById(parent); 391 Issue childIssueOM = IssueManager.getIssueById(child); 392 newDependOM.setDefaultModule(parentIssueOM.getModule()); 393 newDependOM.setObservedId(parentIssueOM.getIssueId()); 394 newDependOM.setObserverId(childIssueOM.getIssueId()); 395 newDependOM.setDependType(type); 396 LOG.debug("Dep: " + dependency.getId() + " Type: " + type + " Parent: " + parent + " Child: " + child); 397 LOG.debug("XML Activity id: " + activity.getId()); 398 if (activity.isAddDependency()) 399 { 400 parentIssueOM 401 .doAddDependency(activitySetOM, newDependOM, childIssueOM, null); 402 LOG.debug("Added Dep Type: " + type + " Parent: " + parent + " Child: " + child); 403 LOG.debug("----------------------------------------------------"); 404 } 405 else if (activity.isDeleteDependency()) 406 { 407 parentIssueOM 408 .doDeleteDependency(activitySetOM, newDependOM, null); 409 LOG.debug("Deleted Dep Type: " + type + " Parent: " + parent + " Child: " + child); 410 LOG.debug("----------------------------------------------------"); 411 } 412 else if (activity.isUpdateDependency()) 413 { 414 Depend oldDependOM = parentIssueOM.getDependency(childIssueOM); 415 if (oldDependOM == null) 416 { 417 throw new Exception ("Whoops! Could not find the original dependency!"); } 419 newDependOM.setDeleted(false); 421 parentIssueOM 422 .doChangeDependencyType(activitySetOM, oldDependOM, newDependOM, null); 423 LOG.debug("Updated Dep Type: " + type + " Parent: " + parent + " Child: " + child); 424 LOG.debug("Old Type: " + oldDependOM.getDependType().getName() + " New type: " + newDependOM.getDependType().getName()); 425 LOG.debug("----------------------------------------------------"); 426 } 427 } 428 catch (Exception e) 429 { 430 e.printStackTrace(); 431 throw e; } 433 } 434 } 435 } 436 437 public List getIssues() 438 { 439 return issues; 440 } 441 442 public void addIssue(XmlIssue issue) 443 throws Exception 444 { 445 LOG.debug("Module.addIssue(): " + issue.getId()); 446 try 447 { 448 if (inValidationMode) 449 { 450 importErrors.setParseContext(module.getCode() + issue.getId()); 451 doIssueValidateEvent(getModule(), issue); 452 } 453 else 454 { 455 doIssueEvent(getModule(), issue); 456 } 457 } 458 catch (Exception e) 459 { 460 e.printStackTrace(); 461 throw e; } 463 finally 464 { 465 importErrors.setParseContext(null); 466 } 467 } 468 469 477 private void doIssueValidateEvent(XmlModule module, XmlIssue issue) 478 throws Exception 479 { 480 Module moduleOM = null; 482 try 483 { 484 moduleOM = ModuleManager.getInstance(module.getDomain(), 485 module.getName(), 486 module.getCode()); 487 if (moduleOM == null) 488 { 489 throw new Exception (); } 491 492 } 496 catch (Exception e) 497 { 498 Object [] args = { module.getName(), module.getCode(), 499 module.getDomain() }; 500 String error = Localization.format( 501 ScarabConstants.DEFAULT_BUNDLE_NAME, 502 getLocale(), 503 "CouldNotFindModule", args); 504 importErrors.add(error); 505 } 506 507 IssueType issueTypeOM = null; 509 try 510 { 511 issueTypeOM = IssueType.getInstance(issue.getArtifactType()); 512 if (issueTypeOM == null) 513 { 514 throw new Exception (); } 516 } 517 catch (Exception e) 518 { 519 String error = Localization.format( 520 ScarabConstants.DEFAULT_BUNDLE_NAME, 521 getLocale(), 522 "CouldNotFindIssueType", issue.getArtifactType()); 523 importErrors.add(error); 524 } 525 if (!moduleOM.getRModuleIssueType(issueTypeOM).getActive()) 526 { 527 String error = Localization.format( 528 ScarabConstants.DEFAULT_BUNDLE_NAME, 529 getLocale(), 530 "IssueTypeInactive", issue.getArtifactType()); 531 importErrors.add(error); 532 } 533 List moduleAttributeList = null; 534 if (moduleOM != null) 535 { 536 moduleAttributeList = moduleOM.getAttributes(issueTypeOM); 537 } 538 539 List activitySets = issue.getActivitySets(); 540 for (Iterator itr = activitySets.iterator(); itr.hasNext();) 541 { 542 XmlActivitySet activitySet = (XmlActivitySet) itr.next(); 543 if (activitySet.getCreatedBy() != null) 544 { 545 importUsers.add(activitySet.getCreatedBy()); 546 } 547 if (activitySet.getAttachment() != null) 548 { 549 String attachCreatedBy = activitySet.getAttachment().getCreatedBy(); 550 if (attachCreatedBy != null) 551 { 552 importUsers.add(attachCreatedBy); 553 } 554 } 555 556 try 558 { 559 ActivitySetType ttOM = 560 ActivitySetTypeManager.getInstance(activitySet.getType()); 561 if (ttOM == null) 562 { 563 throw new Exception (); } 565 } 566 catch (Exception e) 567 { 568 String error = Localization.format( 569 ScarabConstants.DEFAULT_BUNDLE_NAME, 570 getLocale(), 571 "CouldNotFindActivitySetType", activitySet.getType()); 572 importErrors.add(error); 573 } 574 575 validateDate(activitySet.getCreatedDate(), true); 577 578 List activities = activitySet.getActivities(); 579 for (Iterator itrb = activities.iterator(); itrb.hasNext();) 580 { 581 validateActivity(moduleOM, issueTypeOM, moduleAttributeList, 582 activitySet, (XmlActivity) itrb.next()); 583 } 584 } 585 } 586 587 600 private void validateActivity(Module moduleOM, IssueType issueTypeOM, 601 List moduleAttributeList, 602 XmlActivitySet activitySet, 603 XmlActivity activity) 604 { 605 validateDate(activity.getEndDate(), false); 606 if (activity.getOldUser() != null) 607 { 608 importUsers.add(activity.getOldUser()); 609 } 610 if (activity.getNewUser() != null) 611 { 612 importUsers.add(activity.getNewUser()); 613 } 614 XmlAttachment activityAttachment = activity.getAttachment(); 615 if (activityAttachment != null) 616 { 617 if (allowFileAttachments && 618 activityAttachment.getReconcilePath() && 619 !new File (activityAttachment.getFilename()).exists()) 620 { 621 String error = Localization.format 622 (ScarabConstants.DEFAULT_BUNDLE_NAME, getLocale(), 623 "CouldNotFindFileAttachment", 624 activityAttachment.getFilename()); 625 importErrors.add(error); 626 } 627 628 validateDate(activityAttachment.getCreatedDate(), true); 629 validateDate(activityAttachment.getModifiedDate(), false); 630 631 String attachCreatedBy = activityAttachment.getCreatedBy(); 632 if (attachCreatedBy != null) 633 { 634 importUsers.add(attachCreatedBy); 635 } 636 } 637 638 Attribute attributeOM = null; 640 String activityAttribute = activity.getAttribute(); 641 try 642 { 643 attributeOM = Attribute.getInstance(activityAttribute); 644 if (attributeOM == null) 645 { 646 throw new Exception (); } 648 } 649 catch (Exception e) 650 { 651 String error = Localization.format 652 (ScarabConstants.DEFAULT_BUNDLE_NAME, getLocale(), 653 "CouldNotFindGlobalAttribute", activityAttribute); 654 importErrors.add(error); 655 } 656 657 if (attributeOM != null) 658 { 659 if (attributeOM.equals(nullAttribute)) 660 { 661 if (isDependencyActivity(activity)) 664 { 665 if (!isDuplicateDependency(activitySet)) 666 { 667 allDependencies.add(activity); 668 LOG.debug("-------------Stored Dependency # " + 669 allDependencies.size() + "-------------"); 670 } 671 672 return; 675 } 676 } 677 else 678 { 679 if (moduleAttributeList != null && 681 !moduleAttributeList.contains(attributeOM)) 682 { 683 String error = Localization.format 684 (ScarabConstants.DEFAULT_BUNDLE_NAME, getLocale(), 685 "CouldNotFindRModuleAttribute", activityAttribute); 686 importErrors.add(error); 687 } 688 else if (activity.getNewOption() != null) 689 { 690 AttributeOption attributeOptionOM = null; 692 try 693 { 694 attributeOptionOM = AttributeOption 695 .getInstance(attributeOM, activity.getNewOption(), 696 moduleOM, issueTypeOM); 697 if (attributeOptionOM == null) 698 { 699 throw new Exception (); } 701 } 702 catch (Exception e) 703 { 704 Object [] args = { activity.getNewOption(), 705 attributeOM.getName() }; 706 String error = Localization.format 707 (ScarabConstants.DEFAULT_BUNDLE_NAME, getLocale(), 708 "CouldNotFindAttributeOption", args); 709 importErrors.add(error); 710 } 711 try 713 { 714 RModuleOption rmo = RModuleOptionManager 715 .getInstance(moduleOM, issueTypeOM, 716 attributeOptionOM); 717 if (rmo == null) 718 { 719 throw new Exception (); } 721 } 722 catch (Exception e) 723 { 724 Object [] args = { activity.getNewOption(), 725 attributeOM.getName() }; 726 String error = Localization.format 727 (ScarabConstants.DEFAULT_BUNDLE_NAME, 728 getLocale(), 729 "CouldNotFindModuleAttributeOption", 730 args); 731 importErrors.add(error); 732 } 733 } 734 else if (activity.getOldOption() != null) 735 { 736 AttributeOption attributeOptionOM = null; 737 try 738 { 739 attributeOptionOM = AttributeOption 740 .getInstance(attributeOM, activity.getOldOption()); 741 if (attributeOptionOM == null) 742 { 743 throw new Exception (); } 745 } 746 catch (Exception e) 747 { 748 String error = Localization.format 749 (ScarabConstants.DEFAULT_BUNDLE_NAME, getLocale(), 750 "CouldNotFindAttributeOption", 751 activity.getOldOption()); 752 importErrors.add(error); 753 } 754 try 756 { 757 RModuleOption rmo = RModuleOptionManager 758 .getInstance(moduleOM, issueTypeOM, 759 attributeOptionOM); 760 if (rmo == null) 761 { 762 throw new Exception (); } 764 } 765 catch (Exception e) 766 { 767 Object [] args = { activity.getOldOption(), 768 attributeOM.getName() }; 769 String error = Localization.format 770 (ScarabConstants.DEFAULT_BUNDLE_NAME, getLocale(), 771 "CouldNotFindModuleAttributeOption", args); 772 importErrors.add(error); 773 } 774 } 775 } 776 } 777 } 778 779 786 private void validateDate(BaseDate xmlDate, boolean required) 787 { 788 try 789 { 790 if ((xmlDate != null && xmlDate.getDate() == null) && required) 792 { 793 throw new ParseException (null, -1); } 796 } 797 catch (ParseException e) 798 { 799 String errorMsg = 800 (e.getErrorOffset() != -1 ? ": " + e.getMessage() : ""); 801 String [] args = { xmlDate.getTimestamp(), xmlDate.getFormat(), 802 errorMsg }; 803 String error = Localization.format 804 (ScarabConstants.DEFAULT_BUNDLE_NAME, getLocale(), 805 "InvalidDate", args); 806 importErrors.add(error); 807 } 808 } 809 810 private Issue createNewIssue(XmlModule module, XmlIssue issue) 811 throws Exception 812 { 813 Module moduleOM = ModuleManager.getInstance(module.getDomain(), 815 module.getName(), 816 module.getCode()); 817 IssueType issueTypeOM = IssueType.getInstance(issue.getArtifactType()); 819 issueTypeOM.setName(issue.getArtifactType()); 820 Issue issueOM = Issue.getNewInstance(moduleOM, issueTypeOM); 822 issueOM.save(); 824 825 String issueID = "Null (" + Integer.toString(issueXMLMap.size()) + ")"; 833 if(issue.getId() != null) 834 { 835 issueID = module.getCode() + issue.getId(); 836 } 837 issueXMLMap.put(issueID, issueOM.getUniqueId()); 838 839 LOG.debug("Created new Issue: " + issueOM.getUniqueId()); 840 return issueOM; 841 } 842 843 private void doIssueEvent(XmlModule module, XmlIssue issue) 844 throws Exception 845 { 846 Issue issueOM = null; 849 if (getImportTypeCode() == CREATE_SAME_DB || getImportTypeCode() == CREATE_DIFFERENT_DB) 850 { 851 issueOM = createNewIssue(module, issue); 852 } 853 else 854 { 855 issueOM = IssueManager.getIssueById(module.getCode() + issue.getId()); 856 if (issueOM == null) 857 { 858 issueOM = createNewIssue(module, issue); 859 } 860 else 861 { 862 LOG.debug("Found Issue in db: " + issueOM.getUniqueId()); 863 } 864 } 865 866 868 List activitySets = issue.getActivitySets(); 870 LOG.debug("-----------------------------------"); 871 LOG.debug("Number of ActivitySets in Issue: " + activitySets.size()); 872 for (Iterator itr = activitySets.iterator(); itr.hasNext();) 873 { 874 XmlActivitySet activitySet = (XmlActivitySet) itr.next(); 875 LOG.debug("Processing ActivitySet: " + activitySet.getId()); 876 877 XmlAttachment activitySetAttachment = activitySet.getAttachment(); 880 Attachment activitySetAttachmentOM = null; 881 if (activitySetAttachment != null) 882 { 883 if (getImportTypeCode() == UPDATE_SAME_DB) 884 { 885 try 886 { 887 activitySetAttachmentOM = AttachmentManager 888 .getInstance(activitySetAttachment.getId()); 889 LOG.debug("Found existing ActivitySet Attachment"); 890 } 891 catch (Exception e) 892 { 893 activitySetAttachmentOM = createAttachment(issueOM, activitySetAttachment); 894 } 895 } 896 else 897 { 898 activitySetAttachmentOM = createAttachment(issueOM, activitySetAttachment); 899 LOG.debug("Created ActivitySet Attachment object"); 900 } 901 } 902 else 903 { 904 LOG.debug("OK- No Attachment in this ActivitySet"); 905 } 906 907 boolean alreadyCreated = false; 910 ActivitySet activitySetOM = null; 911 if (getImportTypeCode() == UPDATE_SAME_DB) 912 { 913 try 914 { 915 activitySetOM = ActivitySetManager.getInstance(activitySet.getId()); 916 LOG.debug("Found ActivitySet: " + activitySet.getId() + 917 " in db: " + activitySetOM.getActivitySetId()); 918 } 919 catch (Exception e) 920 { 921 activitySetOM = ActivitySetManager.getInstance(); 922 } 923 } 924 else 925 { 926 if (activitySetIdMap.containsKey(activitySet.getId())) 928 { 929 activitySetOM = ActivitySetManager.getInstance( 930 (String )activitySetIdMap.get(activitySet.getId())); 931 alreadyCreated = true; 932 LOG.debug("Found ActivitySet: " + activitySet.getId() + 933 " in map: " + activitySetOM.getActivitySetId()); 934 } 935 else { 937 activitySetOM = ActivitySetManager.getInstance(); 938 LOG.debug("Created new ActivitySet"); 939 } 940 } 941 942 ScarabUser activitySetCreatedByOM = ScarabUserManager.getInstance(activitySet.getCreatedBy(), 943 module.getDomain()); 944 if (!alreadyCreated) 945 { 946 ActivitySetType ttOM = ActivitySetTypeManager.getInstance(activitySet.getType()); 949 activitySetOM.setActivitySetType(ttOM); 950 activitySetOM.setCreatedBy(activitySetCreatedByOM.getUserId()); 951 activitySetOM.setCreatedDate(activitySet.getCreatedDate().getDate()); 952 if (activitySetAttachmentOM != null) 953 { 954 activitySetAttachmentOM.save(); 955 activitySetOM.setAttachment(activitySetAttachmentOM); 956 } 957 activitySetOM.save(); 958 activitySetIdMap.put(activitySet.getId(), 959 activitySetOM.getPrimaryKey().toString()); 960 } 961 962 ActivitySet creationSet = issueOM.getActivitySet(); 965 if (ActivitySetTypePeer.CREATE_ISSUE__PK 966 .equals(activitySetOM.getTypeId()) 967 || 968 (ActivitySetTypePeer.MOVE_ISSUE__PK 969 .equals(activitySetOM.getTypeId()) && 970 (creationSet == null || activitySetOM.getCreatedDate() 971 .before(creationSet.getCreatedDate()))) ) 972 { 973 issueOM.setActivitySet(activitySetOM); 974 } 975 976 984 if (activitySet.isChangeUserAttribute()) 985 { 986 List activities = activitySet.getActivities(); 987 XmlActivity activityA = (XmlActivity)activities.get(0); 988 XmlActivity activityB = (XmlActivity)activities.get(1); 989 990 ScarabUser assigneeOM = ScarabUserManager 991 .getInstance(activityA.getOldUser(), module.getDomain()); 992 ScarabUser assignerOM = ScarabUserManager 993 .getInstance(activityB.getNewUser(), module.getDomain()); 994 995 Attribute oldAttributeOM = Attribute.getInstance(activityA.getAttribute()); 996 997 AttributeValue oldAttValOM = issueOM.getUserAttributeValue(assigneeOM, oldAttributeOM); 998 if (oldAttValOM == null) 999 { 1000 LOG.error("User '" + assigneeOM.getName() + "' was not previously '" + oldAttributeOM.getName() + "' to the issue!"); 1001 } 1002 1003 Attribute newAttributeOM = Attribute.getInstance(activityB.getAttribute()); 1005 1006 issueOM.changeUserAttributeValue(activitySetOM, 1007 assigneeOM, 1008 assignerOM, 1009 oldAttValOM, 1010 newAttributeOM, null); 1011 LOG.debug("-------------Updated User AttributeValue------------"); 1012 continue; 1013 } 1014 1015 1017 List activities = activitySet.getActivities(); 1019 LOG.debug("Number of Activities in ActivitySet: " + activities.size()); 1020 1021 LinkedMap avMap = issueOM.getModuleAttributeValuesMap(); 1022 LOG.debug("Total Module Attribute Values: " + avMap.size()); 1023 for (Iterator itrb = activities.iterator(); itrb.hasNext();) 1024 { 1025 XmlActivity activity = (XmlActivity) itrb.next(); 1026 LOG.debug("Looking at activity id: " + activity.getId()); 1027 1028 Attribute attributeOM = Attribute.getInstance(activity.getAttribute()); 1030 1031 XmlAttachment activityAttachment = activity.getAttachment(); 1033 Attachment activityAttachmentOM = null; 1034 if (activityAttachment != null) 1035 { 1036 String previousXmlId = activityAttachment.getId(); 1042 String previousId = (String )attachmentIdMap 1043 .get(previousXmlId); 1044 if (previousId == null) 1045 { 1046 activityAttachmentOM = createAttachment( 1047 issueOM, activityAttachment); 1048 activityAttachmentOM.save(); 1049 attachmentIdMap.put(previousXmlId, 1050 activityAttachmentOM.getPrimaryKey().toString()); 1051 1052 if (allowFileAttachments && activityAttachment.getReconcilePath()) 1058 { 1059 activityAttachmentOM 1060 .copyFileFromTo(activityAttachment.getFilename(), 1061 activityAttachmentOM.getFullPath()); 1062 } 1063 LOG.debug("Created Activity Attachment object"); 1064 } 1065 else 1066 { 1067 activityAttachmentOM = AttachmentManager 1068 .getInstance(previousId); 1069 LOG.debug("Found existing Activity Attachment"); 1070 } 1071 } 1072 else 1073 { 1074 LOG.debug("OK- No Attachment in this Activity"); 1075 } 1076 1077 if (attributeOM.equals(nullAttribute)) 1080 { 1081 if (isDependencyActivity(activity)) 1083 { 1084 if (!isDuplicateDependency(activitySet)) 1085 { 1086 Object [] obj = {activitySetOM, activity, activityAttachmentOM}; 1087 allDependencies.add(obj); 1088 dependActivitySetId.add(activitySet.getId()); 1089 LOG.debug("-------------Stored Dependency # " + allDependencies.size() + "-------------"); 1090 continue; 1091 } 1092 } 1093 else 1094 { 1095 ActivityManager.createTextActivity(issueOM, nullAttribute, activitySetOM, 1097 activity.getDescription(), activityAttachmentOM, 1098 activity.getOldValue(), activity.getNewValue()); 1099 1100 LOG.debug("-------------Saved Null Attribute-------------"); 1101 continue; 1102 } 1103 } 1104 1105 createActivity(activity, module, 1107 issueOM, attributeOM, activitySetOM); 1108 1109 AttributeValue avalOM = null; 1111 for (Iterator moduleAttributeValueItr = avMap.mapIterator(); 1112 moduleAttributeValueItr.hasNext() && avalOM == null;) 1113 { 1114 AttributeValue testAvalOM = (AttributeValue) 1115 avMap.get(moduleAttributeValueItr.next()); 1116 Attribute avalAttributeOM = testAvalOM.getAttribute(); 1117 1118 LOG.debug("Checking Attribute match: " + avalAttributeOM.getName() + 1119 " against: " + attributeOM.getName()); 1120 if (avalAttributeOM.equals(attributeOM)) 1121 { 1122 avalOM = testAvalOM; 1123 } 1124 } 1125 1126 if (avalOM != null) 1127 { 1128 Attribute avalAttributeOM = avalOM.getAttribute(); 1129 LOG.debug("Attributes match!"); 1130 AttributeValue avalOM2 = null; 1131 if (!activity.isNewActivity()) 1132 { 1133 LOG.debug("Activity is not new."); 1134 avalOM2 = AttributeValue.getNewInstance( 1135 avalAttributeOM.getAttributeId(), 1136 avalOM.getIssue()); 1137 avalOM2.setProperties(avalOM); 1138 } 1139 1140 if (avalAttributeOM.isOptionAttribute()) 1141 { 1142 LOG.debug("We have an Option Attribute: " + 1143 avalAttributeOM.getName()); 1144 AttributeOption newAttributeOptionOM = AttributeOption 1145 .getInstance(attributeOM, activity.getNewOption(), 1146 issueOM.getModule(), 1147 issueOM.getIssueType()); 1148 if (activity.isNewActivity()) 1149 { 1150 if (newAttributeOptionOM != null) 1151 { 1152 avalOM.setOptionId(newAttributeOptionOM.getOptionId()); 1153 avalOM.startActivitySet(activitySetOM); 1154 avalOM.setAttribute(attributeOM); 1155 avalOM.setActivityDescription( 1156 activity.getDescription()); 1157 avalOM.save(); 1158 LOG.debug("-------------Saved Attribute Value-------------"); 1159 } 1160 else 1161 { 1162 LOG.warn("NewAttributeOptionOM is null for " + 1163 activity.getNewOption()); 1164 } 1165 } 1166 else 1167 { 1168 avalOM2.setOptionId(newAttributeOptionOM.getOptionId()); 1169 HashMap map = new HashMap (); 1170 map.put(avalOM.getAttributeId(), avalOM2); 1171 issueOM.setAttributeValues(activitySetOM, map, null, activitySetCreatedByOM); 1172 LOG.debug("-------------Saved Option Attribute Change-------------"); 1173 } 1174 } 1175 else if (avalAttributeOM.isUserAttribute()) 1176 { 1177 LOG.debug("We have a User Attribute: " 1178 + avalAttributeOM.getName()); 1179 if (activity.isNewActivity()) 1180 { 1181 ScarabUser assigneeOM = ScarabUserManager 1186 .getInstance(activity.getNewUser(), 1187 module.getDomain()); 1188 assigneeOM = (assigneeOM != null) 1189 ? assigneeOM: activitySetCreatedByOM; 1190 issueOM.assignUser(activitySetOM, 1191 activity.getDescription(), 1192 assigneeOM, null, avalAttributeOM, null); 1193 LOG.debug("-------------Saved User Assign-------------"); 1194 } 1195 else if (activity.isRemoveUserActivity()) 1196 { 1197 ScarabUser oldUserOM = ScarabUserManager 1199 .getInstance(activity.getOldUser(), module.getDomain()); 1200 avalOM = null; 1205 for (Iterator i = issueOM.getAttributeValues( 1206 avalAttributeOM).iterator(); 1207 i.hasNext() && avalOM == null;) 1208 { 1209 AttributeValue av = (AttributeValue)i.next(); 1210 if (oldUserOM.getUserId().equals(av.getUserId())) 1211 { 1212 avalOM = av; 1213 } 1214 } 1215 1216 if (avalOM == null) 1217 { 1218 if (LOG.isDebugEnabled()) 1219 { 1220 LOG.debug("Could not find previous AttributeValue assigning " + 1221 (oldUserOM == null ? "NULL" : 1222 oldUserOM.getUserName()) + 1223 " to attribute " + 1224 avalAttributeOM.getName()); 1225 } 1226 } 1227 else 1228 { 1229 issueOM.deleteUser(activitySetOM, oldUserOM, 1232 activitySetCreatedByOM, avalOM, null); 1233 LOG.debug("-------------Saved User Remove-------------"); 1234 } 1235 } 1236 } 1237 else if (avalAttributeOM.isTextAttribute()) 1238 { 1239 LOG.debug("We have a Text Attribute: " + avalAttributeOM.getName()); 1240 1241 avalOM.startActivitySet(activitySetOM); 1242 avalOM.setAttribute(attributeOM); 1243 avalOM.setActivityDescription(activity.getDescription()); 1244 1245 if (activity.isNewActivity()) 1246 { 1247 avalOM.setValue(activity.getNewValue()); 1248 } 1249 else if (!activity.getNewValue() 1250 .equals(avalOM.getValue())) 1251 { 1252 avalOM2.setValue(activity.getNewValue()); 1253 avalOM.setProperties(avalOM2); 1254 } 1255 1256 avalOM.save(); 1257 LOG.debug("-------------Saved Attribute Value-------------"); 1258 } 1259 } 1260 issueOM.save(); 1261 LOG.debug("-------------Saved Issue-------------"); 1262 } 1263 } 1264 } 1265 1266 1269 private boolean isDependencyActivity(XmlActivity activity) 1270 { 1271 return (activity.getDependency() != null); 1272 } 1273 1274 private boolean isDuplicateDependency(XmlActivitySet activitySet) 1275 { 1276 return (dependActivitySetId.contains(activitySet.getId())); 1277 } 1278 1279 private Activity createActivity(XmlActivity activity, XmlModule module, 1280 Issue issueOM, 1281 Attribute attributeOM, 1282 ActivitySet activitySetOM) 1283 throws Exception 1284 { 1285 Activity activityOM = null; 1286 if (getImportTypeCode() == UPDATE_SAME_DB) 1287 { 1288 try 1289 { 1290 activityOM = ActivityManager.getInstance(activity.getId()); 1291 } 1292 catch (Exception e) 1293 { 1294 activityOM = ActivityManager.getInstance(); 1295 } 1296 } 1297 else 1298 { 1299 activityOM = ActivityManager.getInstance(); 1300 } 1301 1302 activityOM.setIssue(issueOM); 1303 activityOM.setAttribute(attributeOM); 1304 activityOM.setActivitySet(activitySetOM); 1305 if (activity.getEndDate() != null) 1306 { 1307 activityOM.setEndDate(activity.getEndDate().getDate()); 1308 } 1309 1310 Attachment newAttachmentOM = null; 1312 if (activity.getAttachment() != null) 1313 { 1314 newAttachmentOM = createAttachment(issueOM, activity.getAttachment()); 1315 newAttachmentOM.save(); 1316 activityOM.setAttachment(newAttachmentOM); 1317 } 1318 1319 LOG.debug("Created New Activity"); 1320 return activityOM; 1321 } 1322 1323 private Attachment createAttachment(Issue issueOM, XmlAttachment attachment) 1324 throws Exception 1325 { 1326 Attachment attachmentOM = AttachmentManager.getInstance(); 1327 attachmentOM.setIssue(issueOM); 1328 AttachmentType type = AttachmentType.getInstance(attachment.getType()); 1329 if (allowFileAttachments || !Attachment.FILE__PK.equals(type.getAttachmentTypeId())) 1330 { 1331 attachmentOM.setName(attachment.getName()); 1332 attachmentOM.setAttachmentType(type); 1333 attachmentOM.setMimeType(attachment.getMimetype()); 1334 attachmentOM.setFileName(attachment.getFilename()); 1335 attachmentOM.setData(attachment.getData()); 1336 } 1337 else 1338 { 1339 attachmentOM.setName("comment"); 1343 attachmentOM.setTypeId(Attachment.COMMENT__PK); 1344 attachmentOM.setMimeType("text/plain"); 1345 String text = "File, " + attachment.getFilename() + 1346 ", was not imported. The old description follows:\n\n" + attachment.getName(); 1347 String data = attachment.getData(); if (data != null) 1349 { 1350 text += "\n\n" + data; 1351 } 1352 attachmentOM.setData(text); 1353 } 1354 1355 attachmentOM.setCreatedDate(attachment.getCreatedDate().getDate()); 1356 ModifiedDate modifiedDate = attachment.getModifiedDate(); 1357 if (modifiedDate != null) 1358 { 1359 attachmentOM.setModifiedDate(modifiedDate.getDate()); 1360 } 1361 ScarabUser creUser = ScarabUserManager 1362 .getInstance(attachment.getCreatedBy(), issueOM.getModule().getScarabInstanceId()); 1363 if (creUser != null) 1364 { 1365 attachmentOM.setCreatedBy(creUser.getUserId()); 1366 } 1367 1368 ScarabUser modUserOM = null; 1369 String modifiedBy = attachment.getModifiedBy(); 1370 if (modifiedBy != null) 1371 { 1372 modUserOM = ScarabUserManager 1373 .getInstance(attachment.getModifiedBy(), 1374 issueOM.getModule().getScarabInstanceId()); 1375 if (modUserOM != null) 1376 { 1377 attachmentOM.setModifiedBy(modUserOM.getUserId()); 1378 } 1379 } 1380 1381 attachmentOM.setDeleted(attachment.getDeleted()); 1382 return attachmentOM; 1383 } 1384 1385 private Locale getLocale() 1386 { 1387 return ScarabConstants.DEFAULT_LOCALE; 1388 } 1389} 1390 | Popular Tags |