1 5 package xdoclet.modules.ejb.entity; 6 7 import java.text.MessageFormat ; 8 import java.util.*; 9 10 import xjavadoc.*; 11 import xdoclet.DocletContext; 12 import xdoclet.DocletSupport; 13 14 import xdoclet.DocletTask; 15 import xdoclet.XDocletException; 16 import xdoclet.modules.ejb.EjbDocletTask; 17 import xdoclet.modules.ejb.EjbTagsHandler; 18 import xdoclet.modules.ejb.XDocletModulesEjbMessages; 19 import xdoclet.modules.ejb.entity.EntityCmpSubTask; 20 import xdoclet.tagshandler.MethodTagsHandler; 21 import xdoclet.util.Translator; 22 23 29 public class CmpTagsHandler extends EntityTagsHandler 30 { 31 37 public static String getEntityCmpClassFor(XClass clazz) 38 { 39 String fileName = clazz.getContainingPackage().getName(); 40 String entityName = MessageFormat.format(getEntityCmpClassPattern(), new Object []{EjbTagsHandler.getShortEjbNameFor(clazz)}); 41 42 fileName = choosePackage(fileName, null, DocletTask.getSubTaskName(EntityCmpSubTask.class)); 44 if (fileName.length() > 0) { 45 fileName += "."; 46 } 47 48 fileName += entityName; 49 50 return fileName; 51 } 52 53 62 public static boolean isEntityCmp(XClass clazz) throws XDocletException 63 { 64 if (isEntity(clazz) == false) { 65 return false; 66 } 67 68 XTag beanTag = clazz.getDoc().getTag("ejb:bean"); 69 70 if (beanTag == null) { 72 return true; 73 } 74 75 String value = beanTag.getAttributeValue("type"); 76 77 if (value == null) { 79 return true; 80 } 81 82 if (value.equals("CMP")) { 83 return true; 84 } 85 else { 86 return false; 87 } 88 } 89 90 98 public static boolean isUsingCmp2Impl(XClass clazz) throws XDocletException 99 { 100 String bmp = getTagValue( 101 FOR_CLASS, 102 clazz.getDoc(), 103 "ejb:bean", 104 "type", 105 "CMP,BMP", 106 null, 107 true, 108 false 109 ); 110 111 if (bmp != null && bmp.equalsIgnoreCase("BMP")) { 112 return false; 113 } 114 115 boolean ejbspec2 = EjbTagsHandler.getEjbSpec(). 116 equals(EjbDocletTask.EjbSpecVersion.EJB_2_0) 117 || EjbTagsHandler.getEjbSpec(). 118 equals(EjbDocletTask.EjbSpecVersion.EJB_2_1); 119 120 121 if (ejbspec2 == false) { 122 return false; 123 } 124 125 String cmp = getTagValue( 126 FOR_CLASS, 127 clazz.getDoc(), 128 "ejb:bean", 129 "cmp-version", 130 EntityCmpSubTask.CmpSpecVersion.getVersions(), 131 null, 132 true, 133 false 134 ); 135 136 if (cmp == null) { 137 EntityCmpSubTask entityCmpSubtask = ((EntityCmpSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(EntityCmpSubTask.class))); 138 139 if (entityCmpSubtask != null) { 140 cmp = entityCmpSubtask.getCmpSpec(); 141 } 142 else { 143 throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.CANT_GUESS_CMP_VERSION, new String []{clazz.getQualifiedName()})); 144 } 145 } 146 147 return EntityCmpSubTask.CmpSpecVersion.CMP_2_0.equals(cmp); 148 } 149 150 157 public static String getFieldNameFor(XMethod method) throws XDocletException 158 { 159 String m = method.getName().substring(3); 161 162 char firstU = m.charAt(0); 164 char firstL = Character.toLowerCase(firstU); 165 166 return firstL + m.substring(1); 167 } 168 169 174 protected static String getEntityCmpClassPattern() 175 { 176 EntityCmpSubTask entityCmpSubtask = ((EntityCmpSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(EntityCmpSubTask.class))); 177 178 if (entityCmpSubtask != null) { 179 return entityCmpSubtask.getEntityCmpClassPattern(); 180 } 181 else { 182 return EntityCmpSubTask.DEFAULT_ENTITYCMP_CLASS_PATTERN; 183 } 184 } 185 186 193 public String entityCmpClass() throws XDocletException 194 { 195 return getEntityCmpClassFor(getCurrentClass()); 196 } 197 198 207 public void ifUsingCmp2(String template) throws XDocletException 208 { 209 if (isUsingCmp2Impl(getCurrentClass())) { 210 generate(template); 211 } 212 } 213 214 223 public void ifNotUsingCmp2(String template) throws XDocletException 224 { 225 if (!isUsingCmp2Impl(getCurrentClass())) { 226 generate(template); 227 } 228 } 229 230 237 public void ifEntityIsCmp(String template) throws XDocletException 238 { 239 if (isEntityCmp(getCurrentClass())) { 240 generate(template); 241 } 242 } 243 244 252 public void forAllCmpEntityBeans(String template) throws XDocletException 253 { 254 Collection classes = getXJavaDoc().getSourceClasses(); 255 256 for (Iterator i = classes.iterator(); i.hasNext(); ) { 257 XClass clazz = (XClass) i.next(); 258 259 setCurrentClass(clazz); 260 261 if (DocletSupport.isDocletGenerated(getCurrentClass())) { 262 continue; 263 } 264 265 if (!hasHavingClassTag(getCurrentClass())) { 266 continue; 267 } 268 269 if (isEntityCmp(getCurrentClass())) { 270 generate(template); 271 } 272 } 273 } 274 275 286 public void forAllCmpFields(String template) throws XDocletException 287 { 288 if (isEntityCmp(getCurrentClass())) { 289 XClass oldClass = getCurrentClass(); 290 XClass superclass = null; 291 List already = new ArrayList(); 292 293 do { 294 Collection methods = getCurrentClass().getMethods(); 295 XMethod oldCurrentMethod = getCurrentMethod(); 296 297 for (Iterator j = methods.iterator(); j.hasNext(); ) { 298 XMethod currentMethod = (XMethod) j.next(); 299 300 if (!already.contains(currentMethod.getName())) { 301 setCurrentMethod(currentMethod); 302 303 if (PersistentTagsHandler.isPersistentField(currentMethod) && MethodTagsHandler.isGetter(currentMethod.getName())) { 304 generate(template); 305 } 306 307 already.add(currentMethod.getName()); 308 } 309 } 310 311 setCurrentMethod(oldCurrentMethod); 312 313 superclass = getCurrentClass().getSuperclass(); 315 316 if (superclass != null) { 317 pushCurrentClass(superclass); 318 } 319 320 } while (superclass != null); 321 322 setCurrentClass(oldClass); 323 } 324 } 325 326 334 public String dbmsColumn() throws XDocletException 335 { 336 String tags = "ejb.persistence,jboss.column-name,jboss.column-name,weblogic.dbms-column"; 337 String attribs = "column-name,name,,"; 338 Properties props = new Properties(); 339 340 props.setProperty("tagName", tags); 341 props.setProperty("paramName", attribs); 342 343 String result = getTagValue(props, FOR_METHOD); 344 345 if (result == null) { 346 result = MethodTagsHandler.getPropertyNameFor(getCurrentMethod()); 347 } 348 return result; 349 } 350 351 358 public String dbmsTable() throws XDocletException 359 { 360 String tags = "ejb.persistence,jboss.table-name,jboss.table-name,weblogic.table-name"; 361 String attribs = "table-name,name,,"; 362 Properties props = new Properties(); 363 364 props.setProperty("tagName", tags); 365 props.setProperty("paramName", attribs); 366 367 String result = getTagValue(props, FOR_CLASS); 368 369 if (result == null) { 370 result = getCurrentClass().getName(); 371 } 372 return result; 373 } 374 375 public void ifIsPersistent(String template) throws XDocletException 376 { 377 boolean persistent = getCurrentClass().getMethodTags("ejb.persistence", true).size() != 0; 378 379 persistent = persistent || getCurrentClass().getMethodTags("jboss.table-name", true).size() != 0; 380 persistent = persistent || getCurrentClass().getMethodTags("weblogic.table-name", true).size() != 0; 381 382 if (persistent) { 383 generate(template); 384 } 385 } 386 387 394 public String fieldName() throws XDocletException 395 { 396 return getFieldNameFor(getCurrentMethod()); 397 } 398 } 399 | Popular Tags |