1 5 package xdoclet.modules.ejb.entity; 6 7 import java.util.*; 8 9 import org.apache.commons.logging.Log; 10 11 import xjavadoc.*; 12 import xdoclet.XDocletException; 13 14 import xdoclet.tagshandler.MethodTagsHandler; 15 16 import xdoclet.util.LogUtil; 17 import xdoclet.util.TypeConversionUtil; 18 19 25 public class PersistentTagsHandler extends CmpTagsHandler 26 { 27 28 35 public static boolean isPersistentField(XMethod method) 36 { 37 return method.getDoc().hasTag("ejb:persistent-field") || method.getDoc().hasTag("ejb:persistence"); 38 } 39 40 41 50 public static boolean isValueObjectField(XClass clazz, XMethod method, String valueObject) throws XDocletException 51 { 52 Log log = LogUtil.getLog(PersistentTagsHandler.class, "valueobject"); 53 boolean ret = method.getDoc().hasTag("ejb:value-object"); 54 55 log.debug("hasTag " + ret); 56 if (ret) { 57 Collection tags = method.getDoc().getTags("ejb:value-object"); 58 59 if (tags.size() == 0 && !"*".equals(valueObject)) { 60 ret = false; 61 } 62 else { 63 ret = false; 64 65 boolean excluded = true; 66 67 for (Iterator i = tags.iterator(); i.hasNext(); ) { 68 XTag tag = (XTag) i.next(); 69 String exclude = tag.getAttributeValue("exclude"); 70 String aggreg = tag.getAttributeValue("aggregate"); 71 String comp = tag.getAttributeValue("compose"); 72 73 if ("true".equals(exclude) || aggreg != null || comp != null) { 74 excluded = true; 75 ret = false; 76 break; 77 } 78 79 String value = tag.getAttributeValue("match"); 80 81 log.debug(method.getName() + " " + value + "==" + valueObject); 82 if (valueObject.equals(value) || "*".equals(value) || "*".equals(valueObject)) { 83 ret = true; 84 break; 85 } 86 } 87 if ("*".equals(valueObject) && !excluded) { 88 ret = true; 89 } 90 } 91 } 92 else if ("*".equals(valueObject)) { 93 log.debug("All Fields VO " + method.getName() + " " + isPersistentField(method)); 94 if (isPersistentField(method)) { 95 ret = true; 96 } 97 else { 98 ret = false; 99 } 100 } 101 return ret; 102 } 103 104 105 112 public static boolean isPkField(XMethod method) 113 { 114 return method.getDoc().hasTag("ejb:pk-field"); 115 } 116 117 123 public static boolean isPkFieldInHeader(XClass clazz) 124 { 125 return clazz.getDoc().hasTag("ejb:pk-field"); 126 } 127 128 129 145 public static String fieldList(XClass clazz, String inclTag, String exclTag, int type, String valueObject, boolean superclasses) throws XDocletException 146 { 147 Log log = LogUtil.getLog(PersistentTagsHandler.class, "fieldList"); 148 149 log.debug("fieldList(" + clazz.getName() + ",incl tag=" + inclTag + ",excl tag=" + exclTag + ",type=" + type); 150 151 Map foundFields = new HashMap(); 152 StringBuffer st = new StringBuffer (); 153 String methodType = null; 154 String name = null; 155 156 do { 157 log.debug(" --> " + clazz); 158 159 Collection methods = clazz.getMethods(); 160 161 for (Iterator j = methods.iterator(); j.hasNext(); ) { 162 XMethod method = (XMethod) j.next(); 163 164 if ((isPersistentField(method) && MethodTagsHandler.isGetter(method.getName()) && !foundFields.containsKey(method.getName()) && (valueObject == null || isValueObjectField(clazz, method, valueObject))) 165 || (MethodTagsHandler.isGetter(method.getName()) && !foundFields.containsKey(method.getName()) && valueObject != null && isValueObjectField(clazz, method, valueObject))) { 166 if ((inclTag == null && exclTag == null) 167 || ((inclTag != null) && (method.getDoc().hasTag(inclTag))) 168 || ((exclTag != null) && (!method.getDoc().hasTag(exclTag)))) { 169 foundFields.put(method.getName(), method); 171 172 name = MethodTagsHandler.getPropertyNameFor(method); 173 174 methodType = MethodTagsHandler.getMethodTypeFor(method); 175 176 switch (type) { 177 case 0: 178 if (foundFields.size() > 1) { 179 st.append(','); 180 } 181 182 st.append(methodType).append(' ').append(name); 183 break; 184 case 1: 185 187 if (foundFields.size() > 1) { 188 st.append(" + " + "\" \"" + " + "); 189 } 190 191 st.append("\"").append(name).append("=\" + ").append(method.getName()).append("()"); 193 break; 194 case 2: 195 if (foundFields.size() > 1) { 196 st.append(','); 197 } 198 st.append(method.getName()).append("()"); 199 break; 200 case 3: 201 if (foundFields.size() > 1) { 202 st.append(','); 203 } 204 st.append(name); 205 break; 206 } 207 } 208 } 209 } 210 211 clazz = clazz.getSuperclass(); 213 } while (clazz != null && superclasses); 214 215 return st.toString(); 216 } 217 218 225 public void ifHasAtLeastOnePkField(String template) throws XDocletException 226 { 227 XClass clazz = getCurrentClass(); 228 229 do { 230 Collection methods = clazz.getMethods(); 231 232 for (Iterator j = methods.iterator(); j.hasNext(); ) { 233 XMethod method = (XMethod) j.next(); 234 235 if (isPkField(method) && MethodTagsHandler.isGetter(method.getName())) { 236 generate(template); 237 return; 238 } 239 } 240 241 clazz = clazz.getSuperclass(); 243 } while (clazz != null); 244 } 245 246 247 254 public void ifHasAtLeastOnePersistentField(String template) throws XDocletException 255 { 256 XClass clazz = getCurrentClass(); 257 258 do { 259 Collection methods = clazz.getMethods(); 260 261 for (Iterator j = methods.iterator(); j.hasNext(); ) { 262 XMethod method = (XMethod) j.next(); 263 264 if (isPersistentField(method) && MethodTagsHandler.isGetter(method.getName())) { 265 generate(template); 266 return; 267 } 268 } 269 270 clazz = clazz.getSuperclass(); 272 } while (clazz != null); 273 } 274 275 276 295 public void forAllPersistentFields(String template, Properties attributes) throws XDocletException 296 { 297 String superclasses_str = attributes.getProperty("superclasses"); 298 boolean superclasses = TypeConversionUtil.stringToBoolean(superclasses_str, true); 299 300 String valueObject = attributes.getProperty("valueobject"); 301 302 if (TypeConversionUtil.stringToBoolean(attributes.getProperty("only-pk"), false)) { 303 forAllPersistentMatchedFields(template, "ejb.pk-field", null, superclasses, valueObject); 304 } 305 else if (TypeConversionUtil.stringToBoolean(attributes.getProperty("not-pk"), false)) { 306 forAllPersistentMatchedFields(template, null, "ejb.pk-field", superclasses, valueObject); 307 } 308 else { 309 forAllPersistentMatchedFields(template, null, null, superclasses, valueObject); 310 } 311 } 312 313 324 public String persistentfieldNameValueList(Properties attributes) throws XDocletException 325 { 326 String valueObject = attributes.getProperty("valueobject"); 327 String superclasses_str = attributes.getProperty("superclasses"); 328 boolean superclasses = TypeConversionUtil.stringToBoolean(superclasses_str, true); 329 330 return fieldList(getCurrentClass(), null, null, 1, valueObject, superclasses); 331 } 332 333 334 343 public String persistentfieldList(Properties attributes) throws XDocletException 344 { 345 String superclasses_str = attributes.getProperty("superclasses"); 346 boolean superclasses = TypeConversionUtil.stringToBoolean(superclasses_str, true); 347 348 String valueObject = attributes.getProperty("valueobject"); 349 350 return fieldList(getCurrentClass(), null, null, 0, valueObject, superclasses); 351 } 352 353 354 364 public String persistentfieldNameValueList() throws XDocletException 365 { 366 return fieldList(getCurrentClass(), null, null, 1, null, true); 367 } 368 369 370 376 protected String [] getPkFieldsInHeader() throws XDocletException 377 { 378 ArrayList lPKs = new ArrayList(); 379 Collection lTags = getCurrentClass().getDoc().getTags("ejb:pk-field"); 380 381 for (Iterator i = lTags.iterator(); i.hasNext(); ) { 382 XTag tag = (XTag) i.next(); 383 384 lPKs.add(tag.getValue()); 385 } 386 387 return (String []) lPKs.toArray(new String [0]); 388 } 389 390 391 403 protected void forAllPersistentMatchedFields(String template, String include_tags, String exclude_tags, boolean superclasses, String valueObject) throws XDocletException 404 { 405 Log log = LogUtil.getLog(PersistentTagsHandler.class, "forAllPersistentFields"); 406 Map foundFields = new HashMap(); 407 XClass cur_class = getCurrentClass(); 408 XMethod cur_method = getCurrentMethod(); 409 410 if (log.isDebugEnabled()) { 411 log.debug("Called."); 412 } 413 414 do { 415 pushCurrentClass(cur_class); 416 417 if (log.isDebugEnabled()) { 418 log.debug("getCurrentClass()=" + getCurrentClass()); 419 } 420 421 Collection methods = getCurrentClass().getMethods(); 422 423 for (Iterator j = methods.iterator(); j.hasNext(); ) { 424 XMethod method = (XMethod) j.next(); 425 426 setCurrentMethod(method); 427 428 if ((isPersistentField(getCurrentMethod()) && MethodTagsHandler.isGetter(getCurrentMethod().getName()) && !foundFields.containsKey(getCurrentMethod().getName()) && (valueObject == null || isValueObjectField(getCurrentClass(), getCurrentMethod(), valueObject))) 429 || (MethodTagsHandler.isGetter(getCurrentMethod().getName()) && !foundFields.containsKey(getCurrentMethod().getName()) && valueObject != null && isValueObjectField(getCurrentClass(), getCurrentMethod(), valueObject))) { 430 if ((include_tags == null && exclude_tags == null) 431 || ((include_tags != null) && (getCurrentMethod().getDoc().hasTag(include_tags))) 432 || ((exclude_tags != null) && (!getCurrentMethod().getDoc().hasTag(exclude_tags)))) { 433 if (log.isDebugEnabled()) { 434 log.debug("METHOD(I=" + include_tags + " - E=" + exclude_tags + "=" + getCurrentMethod().getName()); 435 } 436 437 foundFields.put(getCurrentMethod().getName(), getCurrentMethod().getName()); 439 440 generate(template); 441 } 442 } 443 } 444 445 XClass cur = getCurrentClass(); 447 XClass sup = cur.getSuperclass(); 448 String qname = sup.getQualifiedName(); 449 boolean top = qname.equals("java.lang.Object"); 450 451 if (top) { 452 popCurrentClass(); 453 break; 454 } 455 456 popCurrentClass(); 457 458 if (superclasses == true) { 460 cur_class = cur_class.getSuperclass(); 461 } 462 else { 463 if (shouldTraverseSuperclassForDependentClass(cur_class.getSuperclass(), getDependentClassTagName())) { 464 cur_class = cur_class.getSuperclass(); 465 } 466 else { 467 break; 468 } 469 } 470 } while (true); 471 472 setCurrentMethod(cur_method); 473 474 if (log.isDebugEnabled()) { 475 log.debug("Finished."); 476 } 477 } 478 479 } 480 | Popular Tags |