KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ejb > entity > PersistentTagsHandler


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

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 /**
20  * @author Ara Abrahamian (ara_e@email.com)
21  * @created Oct 16, 2001
22  * @xdoclet.taghandler namespace="EjbPersistent"
23  * @version $Revision: 1.23 $
24  */

25 public class PersistentTagsHandler extends CmpTagsHandler
26 {
27
28     /**
29      * Returns true if method is a persistent field, false otherwise. Persistent fields are getter methods marked with a
30      * ejb:persistent-field tag.
31      *
32      * @param method Description of Parameter
33      * @return The PersistentField value
34      */

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     /**
42      * Gets the ValueObjectField attribute of the PersistentTagsHandler class
43      *
44      * @param clazz Describe what the parameter does
45      * @param method Describe what the parameter does
46      * @param valueObject Describe what the parameter does
47      * @return The ValueObjectField value
48      * @exception XDocletException
49      */

50     public static boolean isValueObjectField(XClass clazz, XMethod method, String JavaDoc 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 JavaDoc exclude = tag.getAttributeValue("exclude");
70                     String JavaDoc aggreg = tag.getAttributeValue("aggregate");
71                     String JavaDoc 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 JavaDoc 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     /**
106      * Returns true if method is a primary key field, false otherwise. PK fields are getter methods marked with a
107      * ejb:pk-field tag.
108      *
109      * @param method Description of Parameter
110      * @return The PkField value
111      */

112     public static boolean isPkField(XMethod method)
113     {
114         return method.getDoc().hasTag("ejb:pk-field");
115     }
116
117     /**
118      * Returns true if clazz has ejb:pk-field defined.
119      *
120      * @param clazz Description of Parameter
121      * @return The PkFieldInHeader value
122      */

123     public static boolean isPkFieldInHeader(XClass clazz)
124     {
125         return clazz.getDoc().hasTag("ejb:pk-field");
126     }
127
128
129     /**
130      * Returns comma-separated list of fields, excluding fields that have tags of exclTag list, including fields that
131      * have tags of inclTag list. If name_value_out is true, then the list is in fieldname="value" format.
132      *
133      * @param inclTag Fields that have at least of the tags of this comma-separated list are used.
134      * @param exclTag Fields that don't have all of the tags of this comma-separated list are used.
135      * @param type Type of return (0: comma separated list - 1: field=value comma separated list - 2:
136      * getter comma sperated list, 3: comma separated name list)
137      * @param clazz Description of Parameter
138      * @param valueObject
139      * @param superclasses
140      * @return Description of the Returned Value
141      * @exception XDocletException
142      * @todo This method only takes the field of the current class. I think it's ok. Any
143      * objection ?
144      */

145     public static String JavaDoc fieldList(XClass clazz, String JavaDoc inclTag, String JavaDoc exclTag, int type, String JavaDoc 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 JavaDoc st = new StringBuffer JavaDoc();
153         String JavaDoc methodType = null;
154         String JavaDoc 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                         // Store that we found this field so we don't add it twice
170
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                             //String value = MethodTagsHandler.getPropertyNameFor(methods[j]);
186

187                             if (foundFields.size() > 1) {
188                                 st.append(" + " + "\" \"" + " + ");
189                             }
190
191                             //st.append("\"").append(name).append("=\" + ").append(value);
192
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             //Add super class info
212
clazz = clazz.getSuperclass();
213         } while (clazz != null && superclasses);
214
215         return st.toString();
216     }
217
218     /**
219      * Evaluates the body if the class has at least one primary key field.
220      *
221      * @param template The body of the block tag
222      * @exception XDocletException
223      * @doc.tag type="block"
224      */

225     public void ifHasAtLeastOnePkField(String JavaDoc 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             // Add super class info
242
clazz = clazz.getSuperclass();
243         } while (clazz != null);
244     }
245
246
247     /**
248      * Evaluates the body if the class has at least one persistent field.
249      *
250      * @param template The body of the block tag
251      * @exception XDocletException
252      * @doc.tag type="block"
253      */

254     public void ifHasAtLeastOnePersistentField(String JavaDoc 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             // Add super class info
271
clazz = clazz.getSuperclass();
272         } while (clazz != null);
273     }
274
275
276     /**
277      * Evaluates the body for each persistent field. If only-pk="true" then use only primary keys, if not-pk="true" then
278      * use only persistent fields that are not primary keys. By default use all regardless of being primary key field or
279      * not.
280      *
281      * @param template The body of the block tag
282      * @param attributes The attributes of the template tag
283      * @exception XDocletException
284      * @see #forAllPersistentMatchedFields(java.lang.String,java.lang.String,java.lang.String,boolean,java.lang.String)
285      * @doc.tag type="block"
286      * @doc.param name="superclasses" optional="true" values="true,false" description="Include
287      * persistent fields of superclasses. True by default."
288      * @doc.param name="valueobject" optional="true" description="If present, only loop over fields
289      * with a matching \@ejb.value-object tag."
290      * @doc.param name="only-pk" optional="true" values="true,false" description="If true, only loop
291      * over the persistent fields that are in the primary key. False by default."
292      * @doc.param name="not-pk" optional="true" values="true,false" description="If true, only loop
293      * over the persistent fields that are not in the primary key. False by default."
294      */

295     public void forAllPersistentFields(String JavaDoc template, Properties attributes) throws XDocletException
296     {
297         String JavaDoc superclasses_str = attributes.getProperty("superclasses");
298         boolean superclasses = TypeConversionUtil.stringToBoolean(superclasses_str, true);
299
300         String JavaDoc 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     /**
314      * Returns a string containing comma-separated list of persistent fields without their types in fieldname="value"
315      * format.
316      *
317      * @param attributes
318      * @return A string containing comma-separated list of persistent fields with their types like
319      * an ordinary method parameter definition.
320      * @exception XDocletException
321      * @see #fieldList(XClass, String, String, int, String, boolean)
322      * @doc.tag type="content"
323      */

324     public String JavaDoc persistentfieldNameValueList(Properties attributes) throws XDocletException
325     {
326         String JavaDoc valueObject = attributes.getProperty("valueobject");
327         String JavaDoc 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     /**
335      * Returns a string containing comma-separated list of persistent fields with their types.
336      *
337      * @param attributes
338      * @return A string containing comma-separated list of persistent fields without their types.
339      * @exception XDocletException
340      * @see #fieldList(XClass, String, String, int, String, boolean)
341      * @doc.tag type="content"
342      */

343     public String JavaDoc persistentfieldList(Properties attributes) throws XDocletException
344     {
345         String JavaDoc superclasses_str = attributes.getProperty("superclasses");
346         boolean superclasses = TypeConversionUtil.stringToBoolean(superclasses_str, true);
347
348         String JavaDoc valueObject = attributes.getProperty("valueobject");
349
350         return fieldList(getCurrentClass(), null, null, 0, valueObject, superclasses);
351     }
352
353
354     /**
355      * Returns a string containing comma-separated list of persistent fields without their types in fieldname="value"
356      * format.
357      *
358      * @return A string containing comma-separated list of persistent fields with their types like
359      * an ordinary method parameter definition.
360      * @exception XDocletException
361      * @see #fieldList(XClass, String, String, int, String, boolean)
362      * @doc.tag type="content"
363      */

364     public String JavaDoc persistentfieldNameValueList() throws XDocletException
365     {
366         return fieldList(getCurrentClass(), null, null, 1, null, true);
367     }
368
369
370     /**
371      * Returns an array containing ejb:pk-field tags defined in class level.
372      *
373      * @return The PkFieldsInHeader value
374      * @exception XDocletException
375      */

376     protected String JavaDoc[] 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 JavaDoc[]) lPKs.toArray(new String JavaDoc[0]);
388     }
389
390
391     /**
392      * Generate only for all Persisted Fields matching a specific XTag or Persisted fields that do not match a specific
393      * Tag
394      *
395      * @param template The body of the block tag
396      * @param include_tags only fields having these tags
397      * @param exclude_tags only fields not having these tags
398      * @param superclasses traverse superclasses too
399      * @param valueObject
400      * @exception XDocletException
401      * @see #forAllPersistentFields(java.lang.String,java.util.Properties)
402      */

403     protected void forAllPersistentMatchedFields(String JavaDoc template, String JavaDoc include_tags, String JavaDoc exclude_tags, boolean superclasses, String JavaDoc 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                         // Store that we found this field so we don't add it twice
438
foundFields.put(getCurrentMethod().getName(), getCurrentMethod().getName());
439
440                         generate(template);
441                     }
442                 }
443             }
444
445             // Add super class info
446
XClass cur = getCurrentClass();
447             XClass sup = cur.getSuperclass();
448             String JavaDoc 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             //superclasses is true for *CMP/BMP/Session
459
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