1 package xdoclet.modules.ojb; 2 3 17 18 import java.util.*; 19 20 import xjavadoc.*; 21 import xdoclet.XDocletException; 22 import xdoclet.XDocletTagSupport; 23 import xdoclet.modules.ojb.constraints.*; 24 import xdoclet.modules.ojb.model.*; 25 import xdoclet.util.Translator; 26 import xdoclet.util.TypeConversionUtil; 27 28 34 public class OjbTagsHandler extends XDocletTagSupport 35 { 36 private static final String CONFIG_PARAM_CHECKS = "checks"; 37 private static final String CONFIG_PARAM_VERBOSE = "verbose"; 38 private static final String CONFIG_PARAM_DATABASENAME = "databaseName"; 39 40 private static final String ATTRIBUTE_CLASS = "class"; 41 private static final String ATTRIBUTE_CONSTANT = "constant"; 42 private static final String ATTRIBUTE_DEFAULT = "default"; 43 private static final String ATTRIBUTE_DEFAULT_RIGHT = "default-right"; 44 private static final String ATTRIBUTE_LEVEL = "level"; 45 private static final String ATTRIBUTE_NAME = "name"; 46 private static final String ATTRIBUTE_TYPE = "type"; 47 private static final String ATTRIBUTE_UNIQUE = "unique"; 48 private static final String ATTRIBUTE_VALUE = "value"; 49 50 private static final String LEVEL_CLASS = "class"; 51 private static final String LEVEL_COLLECTION = "collection"; 52 private static final String LEVEL_COLUMN = "column"; 53 private static final String LEVEL_FIELD = "field"; 54 private static final String LEVEL_FOREIGNKEY = "foreignkey"; 55 private static final String LEVEL_INDEX = "index"; 56 private static final String LEVEL_INDEX_DESC = "index-desc"; 57 private static final String LEVEL_OBJECT_CACHE = "object-cache"; 58 private static final String LEVEL_PROCEDURE = "procedure"; 59 private static final String LEVEL_PROCEDURE_ARGUMENT = "procedure-argument"; 60 private static final String LEVEL_REFERENCE = "reference"; 61 private static final String LEVEL_TABLE = "table"; 62 63 64 private ModelDef _model = new ModelDef(); 65 66 private TorqueModelDef _torqueModel = null; 67 68 private ClassDescriptorDef _curClassDef = null; 69 70 private FieldDescriptorDef _curFieldDef = null; 71 72 private ReferenceDescriptorDef _curReferenceDef = null; 73 74 private CollectionDescriptorDef _curCollectionDef = null; 75 76 private ClassDescriptorDef _curExtent = null; 77 78 private ObjectCacheDef _curObjectCacheDef = null; 79 80 private IndexDescriptorDef _curIndexDescriptorDef = null; 81 82 private ProcedureDef _curProcedureDef = null; 83 84 private ProcedureArgumentDef _curProcedureArgumentDef = null; 85 86 private String _curIndexColumn = null; 87 88 private TableDef _curTableDef = null; 89 90 private ColumnDef _curColumnDef = null; 91 92 private ForeignkeyDef _curForeignkeyDef = null; 93 94 private IndexDef _curIndexDef = null; 95 96 private String _curPairLeft = null; 97 98 private String _curPairRight = null; 99 100 104 106 143 public void processClass(String template, Properties attributes) throws XDocletException 144 { 145 if (!_model.hasClass(getCurrentClass().getQualifiedName())) 146 { 147 LogHelper.debug(true, OjbTagsHandler.class, "processClass", "Type "+getCurrentClass().getQualifiedName()); 149 } 150 151 ClassDescriptorDef classDef = ensureClassDef(getCurrentClass()); 152 String attrName; 153 154 for (Enumeration attrNames = attributes.propertyNames(); attrNames.hasMoreElements(); ) 155 { 156 attrName = (String )attrNames.nextElement(); 157 classDef.setProperty(attrName, attributes.getProperty(attrName)); 158 } 159 _curClassDef = classDef; 160 generate(template); 161 _curClassDef = null; 162 } 163 164 172 public void forAllClassDefinitions(String template, Properties attributes) throws XDocletException 173 { 174 for (Iterator it = _model.getClasses(); it.hasNext(); ) 175 { 176 _curClassDef = (ClassDescriptorDef)it.next(); 177 generate(template); 178 } 179 _curClassDef = null; 180 181 LogHelper.debug(true, OjbTagsHandler.class, "forAllClassDefinitions", "Processed "+_model.getNumClasses()+" types"); 182 } 183 184 192 public void originalClass(String template, Properties attributes) throws XDocletException 193 { 194 pushCurrentClass(_curClassDef.getOriginalClass()); 195 generate(template); 196 popCurrentClass(); 197 } 198 199 207 public String prepare() throws XDocletException 208 { 209 String checkLevel = (String )getDocletContext().getConfigParam(CONFIG_PARAM_CHECKS); 210 ArrayList queue = new ArrayList(); 211 ClassDescriptorDef classDef, baseDef; 212 XClass original; 213 boolean isFinished; 214 215 for (Iterator it = _model.getClasses(); it.hasNext();) 217 { 218 classDef = (ClassDescriptorDef)it.next(); 219 original = classDef.getOriginalClass(); 220 isFinished = false; 221 queue.clear(); 222 while (!isFinished) 223 { 224 if (original == null) 225 { 226 isFinished = true; 227 for (Iterator baseIt = queue.iterator(); baseIt.hasNext();) 228 { 229 original = (XClass)baseIt.next(); 230 baseDef = _model.getClass(original.getQualifiedName()); 231 baseIt.remove(); 232 if (baseDef != null) 233 { 234 classDef.addDirectBaseType(baseDef); 235 } 236 else 237 { 238 isFinished = false; 239 break; 240 } 241 } 242 } 243 if (!isFinished) 244 { 245 if (original.getInterfaces() != null) 246 { 247 for (Iterator baseIt = original.getInterfaces().iterator(); baseIt.hasNext();) 248 { 249 queue.add(baseIt.next()); 250 } 251 } 252 if (original.getSuperclass() != null) 253 { 254 queue.add(original.getSuperclass()); 255 } 256 original = null; 257 } 258 } 259 } 260 try 261 { 262 _model.process(); 263 _model.checkConstraints(checkLevel); 264 } 265 catch (ConstraintException ex) 266 { 267 throw new XDocletException(ex.getMessage()); 268 } 269 return ""; 270 } 271 272 274 283 public void forAllSubClasses(String template, Properties attributes) throws XDocletException 284 { 285 ArrayList subTypes = new ArrayList(); 286 XClass type = getCurrentClass(); 287 288 addDirectSubTypes(type, subTypes); 289 290 int pos = 0; 291 ClassDescriptorDef classDef; 292 293 while (pos < subTypes.size()) 294 { 295 type = (XClass)subTypes.get(pos); 296 classDef = _model.getClass(type.getQualifiedName()); 297 if ((classDef != null) && classDef.hasProperty(PropertyHelper.OJB_PROPERTY_OJB_PERSISTENT)) 298 { 299 pos++; 300 } 301 else 302 { 303 subTypes.remove(pos); 304 addDirectSubTypes(type, subTypes); 305 } 306 } 307 for (Iterator it = subTypes.iterator(); it.hasNext(); ) 308 { 309 pushCurrentClass((XClass)it.next()); 310 generate(template); 311 popCurrentClass(); 312 } 313 } 314 315 325 public String addExtent(Properties attributes) throws XDocletException 326 { 327 String name = attributes.getProperty(ATTRIBUTE_NAME); 328 329 if (!_model.hasClass(name)) 330 { 331 throw new XDocletException(Translator.getString(XDocletModulesOjbMessages.class, 332 XDocletModulesOjbMessages.COULD_NOT_FIND_TYPE, 333 new String []{name})); 334 } 335 _curClassDef.addExtentClass(_model.getClass(name)); 336 return ""; 337 } 338 339 347 public void forAllExtents(String template, Properties attributes) throws XDocletException 348 { 349 for (Iterator it = _curClassDef.getExtentClasses(); it.hasNext(); ) 350 { 351 _curExtent = (ClassDescriptorDef)it.next(); 352 generate(template); 353 } 354 _curExtent = null; 355 } 356 357 365 public String extent(Properties attributes) throws XDocletException 366 { 367 return _curExtent.getName(); 368 } 369 370 372 384 public String processIndexDescriptor(Properties attributes) throws XDocletException 385 { 386 String name = attributes.getProperty(ATTRIBUTE_NAME); 387 IndexDescriptorDef indexDef = _curClassDef.getIndexDescriptor(name); 388 String attrName; 389 390 if (indexDef == null) 391 { 392 indexDef = new IndexDescriptorDef(name); 393 _curClassDef.addIndexDescriptor(indexDef); 394 } 395 396 if ((indexDef.getName() == null) || (indexDef.getName().length() == 0)) 397 { 398 throw new XDocletException(Translator.getString(XDocletModulesOjbMessages.class, 399 XDocletModulesOjbMessages.INDEX_NAME_MISSING, 400 new String []{_curClassDef.getName()})); 401 } 402 attributes.remove(ATTRIBUTE_NAME); 403 for (Enumeration attrNames = attributes.propertyNames(); attrNames.hasMoreElements(); ) 404 { 405 attrName = (String )attrNames.nextElement(); 406 indexDef.setProperty(attrName, attributes.getProperty(attrName)); 407 } 408 return ""; 409 } 410 411 419 public void forAllIndexDescriptorDefinitions(String template, Properties attributes) throws XDocletException 420 { 421 for (Iterator it = _curClassDef.getIndexDescriptors(); it.hasNext(); ) 422 { 423 _curIndexDescriptorDef = (IndexDescriptorDef)it.next(); 424 generate(template); 425 } 426 _curIndexDescriptorDef = null; 427 } 428 429 437 public void forAllIndexDescriptorColumns(String template, Properties attributes) throws XDocletException 438 { 439 String fields = _curIndexDescriptorDef.getProperty(PropertyHelper.OJB_PROPERTY_FIELDS); 440 FieldDescriptorDef fieldDef; 441 String name; 442 443 for (CommaListIterator it = new CommaListIterator(fields); it.hasNext();) 444 { 445 name = it.getNext(); 446 fieldDef = _curClassDef.getField(name); 447 if (fieldDef == null) 448 { 449 throw new XDocletException(Translator.getString(XDocletModulesOjbMessages.class, 450 XDocletModulesOjbMessages.INDEX_FIELD_MISSING, 451 new String []{name, _curIndexDescriptorDef.getName(), _curClassDef.getName()})); 452 } 453 _curIndexColumn = fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN); 454 generate(template); 455 } 456 _curIndexColumn = null; 457 } 458 459 467 public String indexColumn(Properties attributes) throws XDocletException 468 { 469 return _curIndexColumn; 470 } 471 472 473 475 486 public String processObjectCache(Properties attributes) throws XDocletException 487 { 488 ObjectCacheDef objCacheDef = _curClassDef.setObjectCache(attributes.getProperty(ATTRIBUTE_CLASS)); 489 String attrName; 490 491 attributes.remove(ATTRIBUTE_CLASS); 492 for (Enumeration attrNames = attributes.propertyNames(); attrNames.hasMoreElements(); ) 493 { 494 attrName = (String )attrNames.nextElement(); 495 objCacheDef.setProperty(attrName, attributes.getProperty(attrName)); 496 } 497 return ""; 498 } 499 500 508 public void forObjectCache(String template, Properties attributes) throws XDocletException 509 { 510 _curObjectCacheDef = _curClassDef.getObjectCache(); 511 if (_curObjectCacheDef != null) 512 { 513 generate(template); 514 _curObjectCacheDef = null; 515 } 516 } 517 518 520 540 public String processProcedure(Properties attributes) throws XDocletException 541 { 542 String type = attributes.getProperty(ATTRIBUTE_TYPE); 543 ProcedureDef procDef = _curClassDef.getProcedure(type); 544 String attrName; 545 546 if (procDef == null) 547 { 548 procDef = new ProcedureDef(type); 549 _curClassDef.addProcedure(procDef); 550 } 551 552 for (Enumeration attrNames = attributes.propertyNames(); attrNames.hasMoreElements(); ) 553 { 554 attrName = (String )attrNames.nextElement(); 555 procDef.setProperty(attrName, attributes.getProperty(attrName)); 556 } 557 return ""; 558 } 559 560 568 public void forAllProcedures(String template, Properties attributes) throws XDocletException 569 { 570 for (Iterator it = _curClassDef.getProcedures(); it.hasNext(); ) 571 { 572 _curProcedureDef = (ProcedureDef)it.next(); 573 generate(template); 574 } 575 _curProcedureDef = null; 576 } 577 578 595 public String processProcedureArgument(Properties attributes) throws XDocletException 596 { 597 String id = attributes.getProperty(ATTRIBUTE_NAME); 598 ProcedureArgumentDef argDef = _curClassDef.getProcedureArgument(id); 599 String attrName; 600 601 if (argDef == null) 602 { 603 argDef = new ProcedureArgumentDef(id); 604 _curClassDef.addProcedureArgument(argDef); 605 } 606 607 attributes.remove(ATTRIBUTE_NAME); 608 for (Enumeration attrNames = attributes.propertyNames(); attrNames.hasMoreElements(); ) 609 { 610 attrName = (String )attrNames.nextElement(); 611 argDef.setProperty(attrName, attributes.getProperty(attrName)); 612 } 613 return ""; 614 } 615 616 624 public void forAllProcedureArguments(String template, Properties attributes) throws XDocletException 625 { 626 String argNameList = _curProcedureDef.getProperty(PropertyHelper.OJB_PROPERTY_ARGUMENTS); 627 628 for (CommaListIterator it = new CommaListIterator(argNameList); it.hasNext();) 629 { 630 _curProcedureArgumentDef = _curClassDef.getProcedureArgument(it.getNext()); 631 generate(template); 632 } 633 _curProcedureArgumentDef = null; 634 } 635 636 638 677 public void processAnonymousField(Properties attributes) throws XDocletException 678 { 679 if (!attributes.containsKey(ATTRIBUTE_NAME)) 680 { 681 throw new XDocletException(Translator.getString(XDocletModulesOjbMessages.class, 682 XDocletModulesOjbMessages.PARAMETER_IS_REQUIRED, 683 new String []{ATTRIBUTE_NAME})); 684 } 685 686 String name = attributes.getProperty(ATTRIBUTE_NAME); 687 FieldDescriptorDef fieldDef = _curClassDef.getField(name); 688 String attrName; 689 690 if (fieldDef == null) 691 { 692 fieldDef = new FieldDescriptorDef(name); 693 _curClassDef.addField(fieldDef); 694 } 695 fieldDef.setAnonymous(); 696 LogHelper.debug(false, OjbTagsHandler.class, "processAnonymousField", " Processing anonymous field "+fieldDef.getName()); 697 698 attributes.remove(ATTRIBUTE_NAME); 699 for (Enumeration attrNames = attributes.propertyNames(); attrNames.hasMoreElements(); ) 700 { 701 attrName = (String )attrNames.nextElement(); 702 fieldDef.setProperty(attrName, attributes.getProperty(attrName)); 703 } 704 fieldDef.setProperty(PropertyHelper.OJB_PROPERTY_ACCESS, "anonymous"); 705 } 706 707 748 public void processField(String template, Properties attributes) throws XDocletException 749 { 750 String name = OjbMemberTagsHandler.getMemberName(); 751 String defaultType = getDefaultJdbcTypeForCurrentMember(); 752 String defaultConversion = getDefaultJdbcConversionForCurrentMember(); 753 FieldDescriptorDef fieldDef = _curClassDef.getField(name); 754 String attrName; 755 756 if (fieldDef == null) 757 { 758 fieldDef = new FieldDescriptorDef(name); 759 _curClassDef.addField(fieldDef); 760 } 761 LogHelper.debug(false, OjbTagsHandler.class, "processField", " Processing field "+fieldDef.getName()); 762 763 for (Enumeration attrNames = attributes.propertyNames(); attrNames.hasMoreElements(); ) 764 { 765 attrName = (String )attrNames.nextElement(); 766 fieldDef.setProperty(attrName, attributes.getProperty(attrName)); 767 } 768 fieldDef.setProperty(PropertyHelper.OJB_PROPERTY_JAVA_TYPE, 770 OjbMemberTagsHandler.getMemberType().getQualifiedName()); 771 fieldDef.setProperty(PropertyHelper.OJB_PROPERTY_DEFAULT_JDBC_TYPE, defaultType); 772 if (defaultConversion != null) 773 { 774 fieldDef.setProperty(PropertyHelper.OJB_PROPERTY_DEFAULT_CONVERSION, defaultConversion); 775 } 776 777 _curFieldDef = fieldDef; 778 generate(template); 779 _curFieldDef = null; 780 } 781 782 791 public void forAllFieldDefinitions(String template, Properties attributes) throws XDocletException 792 { 793 for (Iterator it = _curClassDef.getFields(); it.hasNext(); ) 794 { 795 _curFieldDef = (FieldDescriptorDef)it.next(); 796 if (!isFeatureIgnored(LEVEL_FIELD) && 797 !_curFieldDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_IGNORE, false)) 798 { 799 generate(template); 800 } 801 } 802 _curFieldDef = null; 803 } 804 805 813 public String fieldConstraint(Properties attributes) throws XDocletException 814 { 815 return _curFieldDef.getSizeConstraint(); 816 } 817 818 820 847 public void processAnonymousReference(Properties attributes) throws XDocletException 848 { 849 ReferenceDescriptorDef refDef = _curClassDef.getReference("super"); 850 String attrName; 851 852 if (refDef == null) 853 { 854 refDef = new ReferenceDescriptorDef("super"); 855 _curClassDef.addReference(refDef); 856 } 857 refDef.setAnonymous(); 858 LogHelper.debug(false, OjbTagsHandler.class, "processAnonymousReference", " Processing anonymous reference"); 859 860 for (Enumeration attrNames = attributes.propertyNames(); attrNames.hasMoreElements(); ) 861 { 862 attrName = (String )attrNames.nextElement(); 863 refDef.setProperty(attrName, attributes.getProperty(attrName)); 864 } 865 } 866 867 897 public void processReference(String template, Properties attributes) throws XDocletException 898 { 899 String name = OjbMemberTagsHandler.getMemberName(); 900 XClass type = OjbMemberTagsHandler.getMemberType(); 901 int dim = OjbMemberTagsHandler.getMemberDimension(); 902 ReferenceDescriptorDef refDef = _curClassDef.getReference(name); 903 String attrName; 904 905 if (refDef == null) 906 { 907 refDef = new ReferenceDescriptorDef(name); 908 _curClassDef.addReference(refDef); 909 } 910 LogHelper.debug(false, OjbTagsHandler.class, "processReference", " Processing reference "+refDef.getName()); 911 912 for (Enumeration attrNames = attributes.propertyNames(); attrNames.hasMoreElements(); ) 913 { 914 attrName = (String )attrNames.nextElement(); 915 refDef.setProperty(attrName, attributes.getProperty(attrName)); 916 } 917 if (type == null) 919 { 920 throw new XDocletException(Translator.getString(XDocletModulesOjbMessages.class, 921 XDocletModulesOjbMessages.COULD_NOT_DETERMINE_TYPE_OF_MEMBER, 922 new String []{name})); 923 } 924 if (dim > 0) 925 { 926 throw new XDocletException(Translator.getString(XDocletModulesOjbMessages.class, 927 XDocletModulesOjbMessages.MEMBER_CANNOT_BE_A_REFERENCE, 928 new String []{name, _curClassDef.getName()})); 929 } 930 931 refDef.setProperty(PropertyHelper.OJB_PROPERTY_VARIABLE_TYPE, type.getQualifiedName()); 932 933 String typeName = searchForPersistentSubType(type); 935 936 if (typeName != null) 937 { 938 refDef.setProperty(PropertyHelper.OJB_PROPERTY_DEFAULT_CLASS_REF, typeName); 939 } 940 941 _curReferenceDef = refDef; 942 generate(template); 943 _curReferenceDef = null; 944 } 945 946 954 public void forAllReferenceDefinitions(String template, Properties attributes) throws XDocletException 955 { 956 for (Iterator it = _curClassDef.getReferences(); it.hasNext(); ) 957 { 958 _curReferenceDef = (ReferenceDescriptorDef)it.next(); 959 if (_curReferenceDef.isAnonymous() && (_curReferenceDef.getOwner() != _curClassDef)) 961 { 962 continue; 963 } 964 if (!isFeatureIgnored(LEVEL_REFERENCE) && 965 !_curReferenceDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_IGNORE, false)) 966 { 967 generate(template); 968 } 969 } 970 _curReferenceDef = null; 971 } 972 973 975 1019 public void processCollection(String template, Properties attributes) throws XDocletException 1020 { 1021 String name = OjbMemberTagsHandler.getMemberName(); 1022 CollectionDescriptorDef collDef = _curClassDef.getCollection(name); 1023 String attrName; 1024 1025 if (collDef == null) 1026 { 1027 collDef = new CollectionDescriptorDef(name); 1028 _curClassDef.addCollection(collDef); 1029 } 1030 LogHelper.debug(false, OjbTagsHandler.class, "processCollection", " Processing collection "+collDef.getName()); 1031 1032 for (Enumeration attrNames = attributes.propertyNames(); attrNames.hasMoreElements(); ) 1033 { 1034 attrName = (String )attrNames.nextElement(); 1035 collDef.setProperty(attrName, attributes.getProperty(attrName)); 1036 } 1037 if (OjbMemberTagsHandler.getMemberDimension() > 0) 1038 { 1039 collDef.setProperty(PropertyHelper.OJB_PROPERTY_ARRAY_ELEMENT_CLASS_REF, 1041 OjbMemberTagsHandler.getMemberType().getQualifiedName()); 1042 } 1043 else 1044 { 1045 collDef.setProperty(PropertyHelper.OJB_PROPERTY_VARIABLE_TYPE, 1046 OjbMemberTagsHandler.getMemberType().getQualifiedName()); 1047 } 1048 1049 _curCollectionDef = collDef; 1050 generate(template); 1051 _curCollectionDef = null; 1052 } 1053 1054 1062 public void forAllCollectionDefinitions(String template, Properties attributes) throws XDocletException 1063 { 1064 for (Iterator it = _curClassDef.getCollections(); it.hasNext(); ) 1065 { 1066 _curCollectionDef = (CollectionDescriptorDef)it.next(); 1067 if (!isFeatureIgnored(LEVEL_COLLECTION) && 1068 !_curCollectionDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_IGNORE, false)) 1069 { 1070 generate(template); 1071 } 1072 } 1073 _curCollectionDef = null; 1074 } 1075 1076 1084 public String processNested(Properties attributes) throws XDocletException 1085 { 1086 String name = OjbMemberTagsHandler.getMemberName(); 1087 XClass type = OjbMemberTagsHandler.getMemberType(); 1088 int dim = OjbMemberTagsHandler.getMemberDimension(); 1089 NestedDef nestedDef = _curClassDef.getNested(name); 1090 1091 if (type == null) 1092 { 1093 throw new XDocletException(Translator.getString(XDocletModulesOjbMessages.class, 1094 XDocletModulesOjbMessages.COULD_NOT_DETERMINE_TYPE_OF_MEMBER, 1095 new String []{name})); 1096 } 1097 if (dim > 0) 1098 { 1099 throw new XDocletException(Translator.getString(XDocletModulesOjbMessages.class, 1100 XDocletModulesOjbMessages.MEMBER_CANNOT_BE_NESTED, 1101 new String []{name, _curClassDef.getName()})); 1102 } 1103 1104 ClassDescriptorDef nestedTypeDef = _model.getClass(type.getQualifiedName()); 1105 1106 if (nestedTypeDef == null) 1107 { 1108 throw new XDocletException(Translator.getString(XDocletModulesOjbMessages.class, 1109 XDocletModulesOjbMessages.COULD_NOT_DETERMINE_TYPE_OF_MEMBER, 1110 new String []{name})); 1111 } 1112 if (nestedDef == null) 1113 { 1114 nestedDef = new NestedDef(name, nestedTypeDef); 1115 _curClassDef.addNested(nestedDef); 1116 } 1117 LogHelper.debug(false, OjbTagsHandler.class, "processNested", " Processing nested object "+nestedDef.getName()+" of type "+nestedTypeDef.getName()); 1118 1119 String attrName; 1120 1121 for (Enumeration attrNames = attributes.propertyNames(); attrNames.hasMoreElements(); ) 1122 { 1123 attrName = (String )attrNames.nextElement(); 1124 nestedDef.setProperty(attrName, attributes.getProperty(attrName)); 1125 } 1126 return ""; 1127 } 1128 1129 1131 1192 public String processModification(Properties attributes) throws XDocletException 1193 { 1194 String name = attributes.getProperty(ATTRIBUTE_NAME); 1195 Properties mods = _curClassDef.getModification(name); 1196 String key; 1197 String value; 1198 1199 if (mods == null) 1200 { 1201 mods = new Properties(); 1202 _curClassDef.addModification(name, mods); 1203 } 1204 1205 attributes.remove(ATTRIBUTE_NAME); 1206 for (Enumeration en = attributes.keys(); en.hasMoreElements();) 1207 { 1208 key = (String )en.nextElement(); 1209 value = attributes.getProperty(key); 1210 mods.setProperty(key, value); 1211 } 1212 return ""; 1213 } 1214 1215 1276 public String processNestedModification(Properties attributes) throws XDocletException 1277 { 1278 String prefix = OjbMemberTagsHandler.getMemberName() + "::"; 1279 String name = prefix + attributes.getProperty(ATTRIBUTE_NAME); 1280 Properties mods = _curClassDef.getModification(name); 1281 String key; 1282 String value; 1283 1284 if (mods == null) 1285 { 1286 mods = new Properties(); 1287 _curClassDef.addModification(name, mods); 1288 } 1289 1290 attributes.remove(ATTRIBUTE_NAME); 1291 for (Enumeration en = attributes.keys(); en.hasMoreElements();) 1292 { 1293 key = (String )en.nextElement(); 1294 value = attributes.getProperty(key); 1295 mods.setProperty(key, value); 1296 } 1297 return ""; 1298 } 1299 1300 1302 1310 public String createTorqueSchema(Properties attributes) throws XDocletException 1311 { 1312 String dbName = (String )getDocletContext().getConfigParam(CONFIG_PARAM_DATABASENAME); 1313 1314 _torqueModel = new TorqueModelDef(dbName, _model); 1315 return ""; 1316 } 1317 1318 1326 public void forAllTables(String template, Properties attributes) throws XDocletException 1327 { 1328 for (Iterator it = _torqueModel.getTables(); it.hasNext(); ) 1329 { 1330 _curTableDef = (TableDef)it.next(); 1331 generate(template); 1332 } 1333 _curTableDef = null; 1334 } 1335 1336 1344 public void forAllColumns(String template, Properties attributes) throws XDocletException 1345 { 1346 for (Iterator it = _curTableDef.getColumns(); it.hasNext(); ) 1347 { 1348 _curColumnDef = (ColumnDef)it.next(); 1349 generate(template); 1350 } 1351 _curColumnDef = null; 1352 } 1353 1354 1362 public void forAllForeignkeys(String template, Properties attributes) throws XDocletException 1363 { 1364 for (Iterator it = _curTableDef.getForeignkeys(); it.hasNext(); ) 1365 { 1366 _curForeignkeyDef = (ForeignkeyDef)it.next(); 1367 generate(template); 1368 } 1369 _curForeignkeyDef = null; 1370 } 1371 1372 1380 public void forAllForeignkeyColumnPairs(String template, Properties attributes) throws XDocletException 1381 { 1382 for (int idx = 0; idx < _curForeignkeyDef.getNumColumnPairs(); idx++) 1383 { 1384 _curPairLeft = _curForeignkeyDef.getLocalColumn(idx); 1385 _curPairRight = _curForeignkeyDef.getRemoteColumn(idx); 1386 generate(template); 1387 } 1388 _curPairLeft = null; 1389 _curPairRight = null; 1390 } 1391 1392 1402 public void forAllIndices(String template, Properties attributes) throws XDocletException 1403 { 1404 boolean processUnique = TypeConversionUtil.stringToBoolean(attributes.getProperty(ATTRIBUTE_UNIQUE), false); 1405 1406 _curIndexDef = _curTableDef.getIndex(null); 1408 if ((_curIndexDef != null) && (processUnique == _curIndexDef.isUnique())) 1409 { 1410 generate(template); 1411 } 1412 for (Iterator it = _curTableDef.getIndices(); it.hasNext(); ) 1413 { 1414 _curIndexDef = (IndexDef)it.next(); 1415 if (!_curIndexDef.isDefault() && (processUnique == _curIndexDef.isUnique())) 1416 { 1417 generate(template); 1418 } 1419 } 1420 _curIndexDef = null; 1421 } 1422 1423 1431 public void forAllIndexColumns(String template, Properties attributes) throws XDocletException 1432 { 1433 for (Iterator it = _curIndexDef.getColumns(); it.hasNext(); ) 1434 { 1435 _curColumnDef = _curTableDef.getColumn((String )it.next()); 1436 generate(template); 1437 } 1438 _curColumnDef = null; 1439 } 1440 1441 1443 1453 public String name(Properties attributes) throws XDocletException 1454 { 1455 return getDefForLevel(attributes.getProperty(ATTRIBUTE_LEVEL)).getName(); 1456 } 1457 1458 1468 public void ifHasName(String template, Properties attributes) throws XDocletException 1469 { 1470 String name = getDefForLevel(attributes.getProperty(ATTRIBUTE_LEVEL)).getName(); 1471 1472 if ((name != null) && (name.length() > 0)) 1473 { 1474 generate(template); 1475 } 1476 } 1477 1478 1490 public void ifHasProperty(String template, Properties attributes) throws XDocletException 1491 { 1492 String value = getPropertyValue(attributes.getProperty(ATTRIBUTE_LEVEL), attributes.getProperty(ATTRIBUTE_NAME)); 1493 1494 if (value != null) 1495 { 1496 generate(template); 1497 } 1498 } 1499 1500 1512 public void ifDoesntHaveProperty(String template, Properties attributes) throws XDocletException 1513 { 1514 String value = getPropertyValue(attributes.getProperty(ATTRIBUTE_LEVEL), attributes.getProperty(ATTRIBUTE_NAME)); 1515 1516 if (value == null) 1517 { 1518 generate(template); 1519 } 1520 } 1521 1522 1535 public String propertyValue(Properties attributes) throws XDocletException 1536 { 1537 String value = getPropertyValue(attributes.getProperty(ATTRIBUTE_LEVEL), attributes.getProperty(ATTRIBUTE_NAME)); 1538 1539 if (value == null) 1540 { 1541 value = attributes.getProperty(ATTRIBUTE_DEFAULT); 1542 } 1543 return value; 1544 } 1545 1546 1560 public void ifPropertyValueEquals(String template, Properties attributes) throws XDocletException 1561 { 1562 String value = getPropertyValue(attributes.getProperty(ATTRIBUTE_LEVEL), attributes.getProperty(ATTRIBUTE_NAME)); 1563 String expected = attributes.getProperty(ATTRIBUTE_VALUE); 1564 1565 if (value == null) 1566 { 1567 value = attributes.getProperty(ATTRIBUTE_DEFAULT); 1568 } 1569 if (expected.equals(value)) 1570 { 1571 generate(template); 1572 } 1573 } 1574 1575 1589 public void ifPropertyValueDoesntEqual(String template, Properties attributes) throws XDocletException 1590 { 1591 String value = getPropertyValue(attributes.getProperty(ATTRIBUTE_LEVEL), attributes.getProperty(ATTRIBUTE_NAME)); 1592 String expected = attributes.getProperty(ATTRIBUTE_VALUE); 1593 1594 if (value == null) 1595 { 1596 value = attributes.getProperty(ATTRIBUTE_DEFAULT); 1597 } 1598 if (!expected.equals(value)) 1599 { 1600 generate(template); 1601 } 1602 } 1603 1604 1616 public void forAllValuePairs(String template, Properties attributes) throws XDocletException 1617 { 1618 String name = attributes.getProperty(ATTRIBUTE_NAME, "attributes"); 1619 String defaultValue = attributes.getProperty(ATTRIBUTE_DEFAULT_RIGHT, ""); 1620 String attributePairs = getPropertyValue(attributes.getProperty(ATTRIBUTE_LEVEL), name); 1621 1622 if ((attributePairs == null) || (attributePairs.length() == 0)) 1623 { 1624 return; 1625 } 1626 1627 String token; 1628 int pos; 1629 1630 for (CommaListIterator it = new CommaListIterator(attributePairs); it.hasNext();) 1631 { 1632 token = it.getNext(); 1633 pos = token.indexOf('='); 1634 if (pos >= 0) 1635 { 1636 _curPairLeft = token.substring(0, pos); 1637 _curPairRight = (pos < token.length() - 1 ? token.substring(pos + 1) : defaultValue); 1638 } 1639 else 1640 { 1641 _curPairLeft = token; 1642 _curPairRight = defaultValue; 1643 } 1644 if (_curPairLeft.length() > 0) 1645 { 1646 generate(template); 1647 } 1648 } 1649 _curPairLeft = null; 1650 _curPairRight = null; 1651 } 1652 1653 1661 public String pairLeft(Properties attributes) throws XDocletException 1662 { 1663 return _curPairLeft; 1664 } 1665 1666 1674 public String pairRight(Properties attributes) throws XDocletException 1675 { 1676 return _curPairRight; 1677 } 1678 1679 1683 1685 1691 private ClassDescriptorDef ensureClassDef(XClass original) 1692 { 1693 String name = original.getQualifiedName(); 1694 ClassDescriptorDef classDef = _model.getClass(name); 1695 1696 if (classDef == null) 1697 { 1698 classDef = new ClassDescriptorDef(original); 1699 _model.addClass(classDef); 1700 } 1701 return classDef; 1702 } 1703 1704 1710 private void addDirectSubTypes(XClass type, ArrayList subTypes) 1711 { 1712 if (type.isInterface()) 1713 { 1714 if (type.getExtendingInterfaces() != null) 1715 { 1716 subTypes.addAll(type.getExtendingInterfaces()); 1717 } 1718 if (type.getImplementingClasses() != null) 1723 { 1724 Collection declaredInterfaces = null; 1725 XClass subType; 1726 1727 for (Iterator it = type.getImplementingClasses().iterator(); it.hasNext(); ) 1728 { 1729 subType = (XClass)it.next(); 1730 if (subType instanceof AbstractClass) 1731 { 1732 declaredInterfaces = ((AbstractClass)subType).getDeclaredInterfaces(); 1733 if ((declaredInterfaces != null) && declaredInterfaces.contains(type)) 1734 { 1735 subTypes.add(subType); 1736 } 1737 } 1738 else 1739 { 1740 subTypes.add(subType); 1742 } 1743 } 1744 } 1745 } 1746 else 1747 { 1748 subTypes.addAll(type.getDirectSubclasses()); 1749 } 1750 } 1751 1752 1753 1755 1763 public static String getDefaultJdbcTypeForCurrentMember() throws XDocletException 1764 { 1765 if (OjbMemberTagsHandler.getMemberDimension() > 0) 1766 { 1767 return JdbcTypeHelper.JDBC_DEFAULT_TYPE_FOR_ARRAY; 1768 } 1769 1770 String type = OjbMemberTagsHandler.getMemberType().getQualifiedName(); 1771 1772 return JdbcTypeHelper.getDefaultJdbcTypeFor(type); 1773 } 1774 1775 1783 private static String getDefaultJdbcConversionForCurrentMember() throws XDocletException 1784 { 1785 if (OjbMemberTagsHandler.getMemberDimension() > 0) 1786 { 1787 return JdbcTypeHelper.JDBC_DEFAULT_CONVERSION; 1788 } 1789 1790 String type = OjbMemberTagsHandler.getMemberType().getQualifiedName(); 1791 1792 return JdbcTypeHelper.getDefaultConversionFor(type); 1793 } 1794 1795 1801 private String searchForPersistentSubType(XClass type) 1802 { 1803 ArrayList queue = new ArrayList(); 1804 XClass subType; 1805 1806 queue.add(type); 1807 while (!queue.isEmpty()) 1808 { 1809 subType = (XClass)queue.get(0); 1810 queue.remove(0); 1811 if (_model.hasClass(subType.getQualifiedName())) 1812 { 1813 return subType.getQualifiedName(); 1814 } 1815 addDirectSubTypes(subType, queue); 1816 } 1817 return null; 1818 } 1819 1820 1821 1827 private DefBase getDefForLevel(String level) 1828 { 1829 if (LEVEL_CLASS.equals(level)) 1830 { 1831 return _curClassDef; 1832 } 1833 else if (LEVEL_FIELD.equals(level)) 1834 { 1835 return _curFieldDef; 1836 } 1837 else if (LEVEL_REFERENCE.equals(level)) 1838 { 1839 return _curReferenceDef; 1840 } 1841 else if (LEVEL_COLLECTION.equals(level)) 1842 { 1843 return _curCollectionDef; 1844 } 1845 else if (LEVEL_OBJECT_CACHE.equals(level)) 1846 { 1847 return _curObjectCacheDef; 1848 } 1849 else if (LEVEL_INDEX_DESC.equals(level)) 1850 { 1851 return _curIndexDescriptorDef; 1852 } 1853 else if (LEVEL_TABLE.equals(level)) 1854 { 1855 return _curTableDef; 1856 } 1857 else if (LEVEL_COLUMN.equals(level)) 1858 { 1859 return _curColumnDef; 1860 } 1861 else if (LEVEL_FOREIGNKEY.equals(level)) 1862 { 1863 return _curForeignkeyDef; 1864 } 1865 else if (LEVEL_INDEX.equals(level)) 1866 { 1867 return _curIndexDef; 1868 } 1869 else if (LEVEL_PROCEDURE.equals(level)) 1870 { 1871 return _curProcedureDef; 1872 } 1873 else if (LEVEL_PROCEDURE_ARGUMENT.equals(level)) 1874 { 1875 return _curProcedureArgumentDef; 1876 } 1877 else 1878 { 1879 return null; 1880 } 1881 } 1882 1883 1889 private boolean isFeatureIgnored(String level) 1890 { 1891 return getDefForLevel(level).getBooleanProperty(PropertyHelper.OJB_PROPERTY_IGNORE, false); 1892 } 1893 1894 1901 private String getPropertyValue(String level, String name) 1902 { 1903 return getDefForLevel(level).getProperty(name); 1904 } 1905 1906} 1907 | Popular Tags |