1 5 package xdoclet.modules.ejb.dd; 6 7 import java.util.*; 8 9 import org.apache.commons.logging.Log; 10 11 import xjavadoc.*; 12 13 import xdoclet.XDocletException; 14 import xdoclet.modules.ejb.EjbTagsHandler; 15 import xdoclet.modules.ejb.XDocletModulesEjbMessages; 16 import xdoclet.modules.ejb.entity.CmpTagsHandler; 17 import xdoclet.tagshandler.MethodTagsHandler; 18 19 import xdoclet.util.LogUtil; 20 import xdoclet.util.Translator; 21 import xdoclet.util.TypeConversionUtil; 22 23 30 public class RelationTagsHandler extends EjbTagsHandler 31 { 32 35 protected static Map relationMap = new HashMap(); 36 37 43 protected static RelationHolder currentRelation; 44 45 51 public void ifLeftCascadeDelete(String template) throws XDocletException 52 { 53 if (currentRelation.isLeftCascadeDelete()) { 54 generate(template); 55 } 56 } 57 58 65 public void ifRightCascadeDelete(String template) throws XDocletException 66 { 67 if (currentRelation.isRightCascadeDelete()) { 68 generate(template); 69 } 70 } 71 72 78 public String leftEJBName() throws XDocletException 79 { 80 if (currentRelation.getLeft() != null) { 81 return EjbTagsHandler.getEjbNameFor(currentRelation.getLeft()); 82 } 83 else { 84 String name = getTagValue( 85 FOR_METHOD, 86 currentRelation.getRightMethod().getDoc(), 87 "ejb:relation", 88 "target-ejb", 89 null, 90 null, 91 true, 92 !currentRelation.isBidirectional() 93 ); 94 95 return name; 96 } 97 } 98 99 105 public String rightEJBName() throws XDocletException 106 { 107 if (currentRelation.getRight() != null) { 108 return EjbTagsHandler.getEjbNameFor(currentRelation.getRight()); 109 } 110 else { 111 String name = getTagValue( 112 FOR_METHOD, 113 currentRelation.getLeftMethod().getDoc(), 114 "ejb:relation", 115 "target-ejb", 116 null, 117 null, 118 true, 119 !currentRelation.isBidirectional() 120 ); 121 122 return name; 123 } 124 } 125 126 132 public void ifHasRelationships(String template) throws XDocletException 133 { 134 boolean hasRelationships = hasRelationships(); 135 136 if (hasRelationships) { 137 generate(template); 138 } 139 } 140 141 147 public void ifNotHasRelationships(String template) throws XDocletException 148 { 149 boolean hasRelationships = hasRelationships(); 150 151 if (!hasRelationships) { 152 generate(template); 153 } 154 } 155 156 160 public String relationName() throws XDocletException 161 { 162 return currentRelation.getName(); 163 } 164 165 177 public void forAllRelationships(String template) throws XDocletException 178 { 179 Log log = LogUtil.getLog(RelationTagsHandler.class, "forAllRelationships"); 180 181 Collection classes = getXJavaDoc().getSourceClasses(); 182 183 relationMap.clear(); 184 185 for (Iterator i = classes.iterator(); i.hasNext(); ) { 186 XClass clazz = (XClass) i.next(); 187 188 setCurrentClass(clazz); 189 190 Collection methods = clazz.getMethods(); 191 192 for (Iterator j = methods.iterator(); j.hasNext(); ) { 193 XMethod method = (XMethod) j.next(); 194 195 setCurrentMethod(method); 196 197 XTag relationTag = method.getDoc().getTag("ejb:relation"); 198 199 if (relationTag != null) { 200 String relationName = relationTag.getAttributeValue("name"); 201 202 if (relationName == null) { 203 throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.RELATION_MUST_HAVE_NAME, new String []{getCurrentClass().getName()})); 204 } 205 206 RelationHolder relationHolder = (RelationHolder) relationMap.get(relationName); 207 208 if (relationHolder != null && relationHolder.getLeft() != null && relationHolder.getRight() != null) { 209 String leftSignature = relationHolder.getLeftMethod().getContainingClass().getQualifiedName() + "." + relationHolder.getLeftMethod(); 210 String rightSignature = relationHolder.getRightMethod().getContainingClass().getQualifiedName() + "." + relationHolder.getRightMethod(); 211 String currentSignature = method.getContainingClass().getQualifiedName() + "." + method; 212 213 throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.RELATION_TOO_MANY_NAMES, new String []{ 214 relationName, 215 leftSignature, 216 rightSignature, 217 currentSignature 218 })); 219 } 220 221 if (relationHolder == null) { 222 if (log.isDebugEnabled()) { 223 log.debug("Created new relationship for " + clazz + "." + method); 224 } 225 226 relationHolder = new RelationHolder(); 227 relationHolder.left = clazz; 228 relationHolder.leftMethod = method; 229 relationMap.put(relationName, relationHolder); 230 } 231 else { 232 if (log.isDebugEnabled()) { 233 log.debug("Added " + clazz + " to relationship: " + relationHolder); 234 } 235 relationHolder.right = clazz; 236 relationHolder.rightMethod = method; 237 } 238 } 239 } 240 } 241 242 Iterator relations = relationMap.entrySet().iterator(); 258 259 while (relations.hasNext()) { 260 Map.Entry entry = (Map.Entry) relations.next(); 261 RelationHolder relationHolder = (RelationHolder) entry.getValue(); 262 263 if (relationHolder.isLeftMany() && !relationHolder.isRightMany()) { 264 if (log.isDebugEnabled()) { 266 log.debug("Swapping left -> right in attempt to make 1-n be 1-n (rather than n-1)"); 267 } 268 relationHolder.swap(); 269 } 270 if (!relationHolder.isBidirectional() && 271 relationHolder.isOne2Many() && 272 relationHolder.getLeftMethod() == null) { 273 relationHolder.swap(); 274 } 275 } 276 277 Iterator relationNameIterator = relationMap.keySet().iterator(); 279 280 while (relationNameIterator.hasNext()) { 281 String relationName = (String ) relationNameIterator.next(); 283 284 RelationHolder relationHolder = (RelationHolder) relationMap.get(relationName); 285 286 setCurrentClass(relationHolder.getLeft()); 287 setCurrentMethod(relationHolder.getLeftMethod()); 288 289 currentRelation = relationHolder; 290 291 316 if (log.isDebugEnabled()) { 317 log.debug("Generating template for Relation: " + currentRelation); 318 } 319 generate(template); 320 } 321 } 322 323 329 public void ifIsLeftMany(String template) throws XDocletException 330 { 331 if (currentRelation.isLeftMany()) { 332 generate(template); 333 } 334 } 335 336 342 public void ifIsRightMany(String template) throws XDocletException 343 { 344 if (currentRelation.isRightMany()) { 345 generate(template); 346 } 347 } 348 349 355 public String leftFieldName() throws XDocletException 356 { 357 return CmpTagsHandler.getFieldNameFor(currentRelation.getLeftMethod()); 358 } 359 360 366 public String rightFieldName() throws XDocletException 367 { 368 return CmpTagsHandler.getFieldNameFor(currentRelation.getRightMethod()); 369 } 370 371 377 public String leftFieldType() throws XDocletException 378 { 379 return MethodTagsHandler.getMethodTypeFor(currentRelation.getLeftMethod()); 380 } 381 382 388 public String rightFieldType() throws XDocletException 389 { 390 return MethodTagsHandler.getMethodTypeFor(currentRelation.getRightMethod()); 391 } 392 393 402 public void ifIsNotACollection(String template) throws XDocletException 403 { 404 if (!isSetOrCollection(MethodTagsHandler.getMethodTypeFor(getCurrentMethod()))) { 405 generate(template); 406 } 407 } 408 409 414 public String relationComment() 415 { 416 return "<!-- " + currentRelation.toString() + " -->"; 417 } 418 419 428 public void ifIsOne2One(String template) throws XDocletException 429 { 430 if (currentRelation.isOne2One()) { 431 generate(template); 432 } 433 } 434 435 444 public void ifNotIsOne2One(String template) throws XDocletException 445 { 446 if (!currentRelation.isOne2One()) { 447 generate(template); 448 } 449 } 450 451 460 public void ifIsOne2Many(String template) throws XDocletException 461 { 462 if (currentRelation.isOne2Many()) { 463 generate(template); 464 } 465 } 466 467 476 public void ifNotIsOne2Many(String template) throws XDocletException 477 { 478 if (!currentRelation.isOne2Many()) { 479 generate(template); 480 } 481 } 482 483 492 public void ifIsMany2Many(String template) throws XDocletException 493 { 494 if (currentRelation.isMany2Many()) { 495 generate(template); 496 } 497 } 498 499 508 public void ifNotIsMany2Many(String template) throws XDocletException 509 { 510 if (!currentRelation.isMany2Many()) { 511 generate(template); 512 } 513 } 514 515 521 public void ifIsBidirectional(String template) throws XDocletException 522 { 523 if (currentRelation.isBidirectional()) { 524 generate(template); 525 } 526 } 527 528 534 public void ifIsUnidirectional(String template) throws XDocletException 535 { 536 if (!currentRelation.isBidirectional()) { 537 generate(template); 538 } 539 } 540 541 547 public void ifHasLeftRoleName(String template) throws XDocletException 548 { 549 if (leftRoleName() != null) { 550 generate(template); 551 } 552 } 553 554 560 public void ifHasRightRoleName(String template) throws XDocletException 561 { 562 if (rightRoleName() != null) { 563 generate(template); 564 } 565 } 566 567 573 public void ifLeftNavigable(String template) throws XDocletException 574 { 575 if (currentRelation.isLeftNavigable()) { 576 generate(template); 577 } 578 } 579 580 586 public void ifRightNavigable(String template) throws XDocletException 587 { 588 if (currentRelation.isRightNavigable()) { 589 generate(template); 590 } 591 } 592 593 599 public String leftRoleName() throws XDocletException 600 { 601 return currentRelation.getLeftRoleName(); 602 } 603 604 610 public String rightRoleName() throws XDocletException 611 { 612 return currentRelation.getRightRoleName(); 613 } 614 615 621 public String leftMultiplicity() throws XDocletException 622 { 623 return currentRelation.getLeftMultiplicity(); 624 } 625 626 632 public String rightMultiplicity() throws XDocletException 633 { 634 return currentRelation.getRightMultiplicity(); 635 } 636 637 643 protected boolean isSetOrCollection(String type) 644 { 645 return (type.equals("java.util.Collection") || type.equals("java.util.Set")); 646 } 647 648 protected boolean hasRelationships() 649 { 650 Collection classes = getXJavaDoc().getSourceClasses(); 651 boolean hasRelationships = false; 652 653 for (Iterator i = classes.iterator(); i.hasNext(); ) { 654 XClass clazz = (XClass) i.next(); 655 656 setCurrentClass(clazz); 657 658 Collection methods = clazz.getMethods(); 659 660 for (Iterator j = methods.iterator(); j.hasNext(); ) { 661 XMethod method = (XMethod) j.next(); 662 663 setCurrentMethod(method); 664 665 XTag relation = method.getDoc().getTag("ejb:relation"); 666 667 if (relation != null) { 668 hasRelationships = true; 669 break; 670 } 671 } 672 if (hasRelationships) { 673 break; 674 } 675 } 676 return hasRelationships; 677 } 678 679 685 public class RelationHolder 686 { 687 private final static String ONE = "One"; 688 689 private final static String MANY = "Many"; 690 691 private XClass left; 692 693 private XMethod leftMethod; 694 695 private XClass right; 696 697 private XMethod rightMethod; 698 699 704 public boolean isBidirectional() 705 { 706 return getRightMethod() != null && getLeftMethod() != null; 707 } 708 709 715 public boolean isOne2One() throws XDocletException 716 { 717 return !isLeftMany() && !isRightMany(); 718 } 719 720 726 public boolean isOne2Many() throws XDocletException 727 { 728 return !isLeftMany() && isRightMany(); 729 } 730 731 737 public boolean isMany2Many() throws XDocletException 738 { 739 return isLeftMany() && isRightMany(); 740 } 741 742 748 public String getLeftMultiplicity() throws XDocletException 749 { 750 return isLeftMany() ? MANY : ONE; 751 } 752 753 759 public String getRightMultiplicity() throws XDocletException 760 { 761 return isRightMany() ? MANY : ONE; 762 } 763 764 769 public XClass getLeft() 770 { 771 return left; 772 } 773 774 779 public XMethod getLeftMethod() 780 { 781 return leftMethod; 782 } 783 784 789 public XClass getRight() 790 { 791 return right; 792 } 793 794 799 public XMethod getRightMethod() 800 { 801 return rightMethod; 802 } 803 804 809 public boolean isRightNavigable() 810 { 811 return getRightMethod() != null; 812 } 813 814 819 public boolean isLeftNavigable() 820 { 821 return getLeftMethod() != null; 822 } 823 824 830 public String getLeftRoleName() throws XDocletException 831 { 832 String result = null; 833 834 if (getLeftMethod() != null) { 835 result = getLeftMethod().getDoc().getTagAttributeValue("ejb:relation", "role-name", false); 836 } 837 else { 838 result = getRightMethod().getDoc().getTagAttributeValue("ejb:relation", "target-role-name", false); 839 } 840 return result; 841 } 842 843 849 public String getRightRoleName() throws XDocletException 850 { 851 String result = null; 852 853 if (getRightMethod() != null) { 854 result = getRightMethod().getDoc().getTagAttributeValue("ejb:relation", "role-name", false); 855 } 856 else { 857 result = getLeftMethod().getDoc().getTagAttributeValue("ejb:relation", "target-role-name", false); 858 } 859 return result; 860 } 861 862 868 public boolean isLeftMany() throws XDocletException 869 { 870 boolean result; 871 872 if (getLeftMethod() != null) { 873 result = isSetOrCollection(getLeftMethod().getReturnType().getType().getQualifiedName()); 874 } 875 else { 876 String targetMultiple = getRightMethod().getDoc().getTagAttributeValue("ejb:relation", "target-multiple", false); 877 878 result = TypeConversionUtil.stringToBoolean(targetMultiple, false); 879 } 880 return result; 881 } 882 883 889 public boolean isRightMany() throws XDocletException 890 { 891 boolean result; 892 893 if (getRightMethod() != null) { 894 result = isSetOrCollection(getRightMethod().getReturnType().getType().getQualifiedName()); 895 } 896 else { 897 String targetMultiple = getLeftMethod().getDoc().getTagAttributeValue("ejb:relation", "target-multiple", false); 898 899 result = TypeConversionUtil.stringToBoolean(targetMultiple, false); 900 } 901 return result; 902 } 903 904 910 public boolean isLeftCascadeDelete() throws XDocletException 911 { 912 boolean result; 913 914 if (getLeftMethod() != null) { 915 result = isCascadeDelete(getLeftMethod(), "cascade-delete"); 916 } 917 else { 918 result = isCascadeDelete(getRightMethod(), "target-cascade-delete"); 919 } 920 return result; 921 } 922 923 929 public boolean isRightCascadeDelete() throws XDocletException 930 { 931 boolean result; 932 933 if (getRightMethod() != null) { 934 result = isCascadeDelete(getRightMethod(), "cascade-delete"); 935 } 936 else { 937 result = isCascadeDelete(getLeftMethod(), "target-cascade-delete"); 938 } 939 return result; 940 } 941 942 946 public String getName() throws XDocletException 947 { 948 String result = null; 949 950 if (getRightMethod() != null) { 951 result = getRightMethod().getDoc().getTagAttributeValue("ejb:relation", "name", false); 952 } 953 else { 954 result = getLeftMethod().getDoc().getTagAttributeValue("ejb:relation", "name", false); 955 } 956 return result; 957 } 958 959 962 public void swap() 963 { 964 XClass c = right; 965 XMethod m = rightMethod; 966 967 right = left; 968 rightMethod = leftMethod; 969 left = c; 970 leftMethod = m; 971 } 972 973 978 public int hashCode() 979 { 980 int result = 17; 981 982 if (getLeft() != null) { 983 result = 37 * result + getLeft().hashCode(); 984 } 985 if (getLeftMethod() != null) { 986 result = 37 * result + getLeftMethod().hashCode(); 987 } 988 if (getRight() != null) { 989 result = 37 * result + getRight().hashCode(); 990 } 991 if (getRightMethod() != null) { 992 result = 37 * result + getRightMethod().hashCode(); 993 } 994 995 return result; 996 } 997 998 1003 public String toString() 1004 { 1005 return new StringBuffer ("RelationHolder left=").append(getLeft()).append('.').append(getLeftMethod()).append(" right=").append(getRight()).append('.').append(getRightMethod()).toString(); 1006 } 1007 1008 1014 public boolean equals(Object o) 1015 { 1016 if (o == this) { 1017 return true; 1018 } 1019 1020 if (!(o instanceof RelationHolder)) { 1021 return false; 1022 } 1023 1024 RelationHolder other = (RelationHolder) o; 1025 1026 return ( 1027 (getLeft() == null ? other.getLeft() == null : getLeft().equals(other.getLeft())) && 1028 (getLeftMethod() == null ? other.getLeftMethod() == null : getLeftMethod().equals(other.getLeftMethod())) && 1029 (getRight() == null ? other.getRight() == null : getRight().equals(other.getRight())) && 1030 (getRightMethod() == null ? other.getRightMethod() == null : getRightMethod().equals(other.getRightMethod()))); 1031 } 1032 1033 1041 private boolean isCascadeDelete(XMethod method, String tag) throws XDocletException 1042 { 1043 String cd = null; 1044 1045 cd = getTagValue( 1046 FOR_METHOD, 1047 method.getDoc(), 1048 "ejb:relation", 1049 tag, 1050 "yes,no,true,false", 1051 "no", 1052 false, 1053 false 1054 ); 1055 return TypeConversionUtil.stringToBoolean(cd, false); 1056 } 1057 } 1058} 1059 | Popular Tags |