KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package xdoclet.modules.ejb.entity;
6
7 //import xdoclet.util.serialveruid.*;
8

9 import java.text.MessageFormat JavaDoc;
10 import java.util.Properties JavaDoc;
11 import java.util.StringTokenizer JavaDoc;
12
13 import xjavadoc.XClass;
14 import xjavadoc.XMethod;
15 import xdoclet.DocletContext;
16 import xdoclet.DocletTask;
17 import xdoclet.XDocletException;
18 import xdoclet.modules.ejb.EjbTagsHandler;
19 import xdoclet.modules.ejb.home.HomeTagsHandler;
20 import xdoclet.tagshandler.MethodTagsHandler;
21 import xdoclet.tagshandler.PropertyTagsHandler;
22 import xdoclet.tagshandler.TypeTagsHandler;
23
24 import xdoclet.util.TypeConversionUtil;
25
26 /**
27  * Tags used in generating PK classes for entity EJBs.
28  *
29  * @author Ara Abrahamian (ara_e@email.com)
30  * @created 13. juni 2002
31  * @xdoclet.taghandler namespace="EjbPk"
32  * @version $Revision: 1.18 $
33  */

34 public class PkTagsHandler extends EjbTagsHandler
35 {
36     /**
37      * Get the primary key field specified for a given class. This is given by the <code>primkey-field</code> parameter
38      * on the <code>ejb.bean</code> tag, although this may be inherited from a superclass.
39      *
40      * @param clazz The class to look into
41      * @return The value of the ejb.bean primkey-field parameter
42      * @exception XDocletException
43      */

44     public static String JavaDoc getPrimkeyFieldFor(XClass clazz) throws XDocletException
45     {
46         // the primkey-field may be specified in a super class, so check them as well
47
//
48
String JavaDoc pkField = clazz.getDoc().getTagAttributeValue("ejb:bean", "primkey-field", true);
49
50         /*
51          * TODO what should be done if only one cmp-field is marked with ejb.pk-field
52          * if (pkField == null) {
53          * / search for a single ejb.pk-field tag
54          * String fields = PersistentTagsHandler.fieldList(clazz, "ejb:pk-field", null, 3, null, true);
55          * if (fields.length() > 0 && fields.indexOf(",") < 0) {
56          * pkField = fields;
57          * }
58          * }
59          */

60         return pkField;
61     }
62
63     /**
64      * Test if a given method is the getter or setter for the primary key field specified for the class.
65      *
66      * @param clazz The class to look into
67      * @param method The method to check for primkey-field
68      * @return true if the method is a getter or setter for the primkey-field
69      * @exception XDocletException
70      * @see #getPrimkeyFieldFor(XClass)
71      */

72     public static boolean isMethodPrimkeyField(XClass clazz, XMethod method)
73          throws XDocletException
74     {
75         String JavaDoc pkField = getPrimkeyFieldFor(clazz);
76         String JavaDoc propertyName = MethodTagsHandler.getPropertyNameFor(method);
77
78         return propertyName.equals(pkField);
79     }
80
81     /**
82      * Returns the getter method of the primary key field specified for a given class.
83      *
84      * @param clazz The class to look into
85      * @return The name of the ejb.bean primkey-field parameter's getter method, or null if there
86      * isn't one
87      * @exception XDocletException
88      * @see #getPrimkeyFieldFor(XClass)
89      */

90     public static String JavaDoc getPrimkeyGetterFor(XClass clazz) throws XDocletException
91     {
92         if (!classHasPrimkeyField(clazz)) {
93             return null;
94         }
95
96         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
97
98         buffer.append("get");
99         buffer.append(Character.toUpperCase(getPrimkeyFieldFor(clazz).charAt(0)));
100         buffer.append(getPrimkeyFieldFor(clazz).substring(1));
101
102         String JavaDoc getterName = buffer.toString();
103
104         if (MethodTagsHandler.hasMethod(clazz, getterName, null, false)) {
105             return getterName;
106         }
107         else {
108             return null;
109         }
110     }
111
112     /**
113      * Returns the setter method of the primary key field specified for a given class.
114      *
115      * @param clazz The class to look into
116      * @return The name of the ejb.bean primkey-field parameter's setter method, or null if there
117      * isn't one
118      * @exception XDocletException
119      * @see #getPrimkeyFieldFor(XClass)
120      */

121     public static String JavaDoc getPrimkeySetterFor(XClass clazz) throws XDocletException
122     {
123         if (!classHasPrimkeyField(clazz)) {
124             return null;
125         }
126
127         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
128
129         buffer.append("set");
130         buffer.append(Character.toUpperCase(getPrimkeyFieldFor(clazz).charAt(0)));
131         buffer.append(getPrimkeyFieldFor(clazz).substring(1));
132
133         String JavaDoc setterName = buffer.toString();
134
135         if (MethodTagsHandler.hasMethod(clazz, setterName, null, false)) {
136             return setterName;
137         }
138         else {
139             return null;
140         }
141     }
142
143     /**
144      * Get the primary key class specified for a given class. If a primary key field has been specified, using the
145      * <code>primkey-field</code> parameter on the <code>ejb.bean</code> tag, this will be the return type of that
146      * field's getter method. Otherwise, it will be determined by the various parameters of the <code>ejb.pk</code> tag
147      * and the subtask's settings for default pattern, packageSubstitution, etc.
148      *
149      * @param clazz The class to look into
150      * @return Fully qualified name of the primary key's type
151      * @exception XDocletException
152      */

153     public static String JavaDoc getPkClassFor(XClass clazz) throws XDocletException
154     {
155         if (classHasPrimkeyField(clazz)) {
156             String JavaDoc fieldName = getPrimkeyFieldFor(clazz);
157             String JavaDoc getter = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
158
159             // the getter might be in a superclass, so check those as well
160
//
161
XMethod method = PropertyTagsHandler.getXMethodForMethodName(getter, true);
162
163             if (method == null) {
164                 throw new XDocletException("Could not find method " + getter + " that is supposed to return the PrimKeyField.");
165             }
166
167             return method.getReturnType().getType().getQualifiedName();
168         }
169
170         String JavaDoc fileName = clazz.getContainingPackage().getName();
171
172         String JavaDoc pkClass = clazz.getDoc().getTagAttributeValue("ejb:pk", "class", false);
173
174         if (pkClass != null) {
175             return pkClass;
176         }
177
178         String JavaDoc namePattern = clazz.getDoc().getTagAttributeValue("ejb:pk", "pattern", false);
179
180         if (namePattern == null) {
181             namePattern = getEntityPkClassPattern();
182         }
183
184         String JavaDoc package_pattern = clazz.getDoc().getTagAttributeValue("ejb:pk", "package", false);
185
186         String JavaDoc pkclass_name = null;
187
188         if (namePattern.indexOf("{0}") != -1) {
189             pkclass_name = MessageFormat.format(namePattern, new Object JavaDoc[]{getShortEjbNameFor(clazz)});
190         }
191         else {
192             pkclass_name = namePattern;
193         }
194
195         // Fix package name
196
fileName = choosePackage(fileName, package_pattern, DocletTask.getSubTaskName(EntityPkSubTask.class));
197         if (fileName.length() > 0) {
198             fileName += ".";
199         }
200
201         fileName += pkclass_name;
202
203         return fileName;
204     }
205
206
207     /**
208      * Gets the PkClassForEjbJarXmlFor attribute of the PkTagsHandler class
209      *
210      * @param clazz The class to look into
211      * @return The PkClassForEjbJarXmlFor value
212      * @exception XDocletException
213      */

214     public static String JavaDoc getPkClassForEjbJarXmlFor(XClass clazz) throws XDocletException
215     {
216         XMethod createMethod = HomeTagsHandler.findFirstCreateMethodFor(clazz);
217         String JavaDoc createMethodReturnType = createMethod != null ? createMethod.getReturnType().getType().getQualifiedName() : null;
218
219         String JavaDoc generate_str = clazz.getDoc().getTagAttributeValue("ejb:pk", "generate", false);
220         boolean generate = TypeConversionUtil.stringToBoolean(generate_str, true);
221
222         if (isPkClassGenerateable(createMethodReturnType) == false) {
223             return createMethodReturnType;
224         }
225
226         if (generate == true) {
227             return getPkClassFor(clazz);
228         }
229         else {
230             return createMethodReturnType;
231         }
232     }
233
234     /**
235      * Returns true if the specified class has a primkey-field defined on the ejb.bean tag (opposed to having a separate
236      * PK class).
237      *
238      * @param clazz The class to look into.
239      * @return true if the class has a defined primkey-field
240      * @exception XDocletException
241      * @see #getPrimkeyFieldFor(XClass)
242      */

243     public static boolean classHasPrimkeyField(XClass clazz) throws XDocletException
244     {
245         String JavaDoc pkField = getPrimkeyFieldFor(clazz);
246
247         return (pkField != null);
248     }
249
250     /**
251      * Returns the pattern which is used to calculate the Primary Key class' name from the EJB name. The default is to
252      * append "PK", unless overridden on the subtask.
253      *
254      * @return pattern
255      */

256     protected static String JavaDoc getEntityPkClassPattern()
257     {
258         EntityPkSubTask entitypk_subtask = ((EntityPkSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(EntityPkSubTask.class)));
259
260         if (entitypk_subtask != null) {
261             return entitypk_subtask.getEntityPkClassPattern();
262         }
263         else {
264             return EntityPkSubTask.DEFAULT_ENTITY_PK_CLASS_PATTERN;
265         }
266     }
267
268     /**
269      * Gets the PkClassGenerateable attribute of the PkTagsHandler class
270      *
271      * @param create_method_return_type Describe what the parameter does
272      * @return The PkClassGenerateable value
273      */

274     private static boolean isPkClassGenerateable(String JavaDoc create_method_return_type)
275     {
276         if (create_method_return_type == null) {
277             return true;
278         }
279
280         if (create_method_return_type.equals("java.lang.Object") || TypeTagsHandler.isPrimitiveType(create_method_return_type)) {
281             return false;
282         }
283         else {
284             return true;
285         }
286     }
287
288     /**
289      * Process the tag body if the current class has a defined primkey-field.
290      *
291      * @param template The body of the block tag
292      * @param attributes The attributes of the template tag
293      * @exception XDocletException
294      * @doc.tag type="block"
295      */

296     public void ifHasPrimkeyField(String JavaDoc template, Properties JavaDoc attributes)
297          throws XDocletException
298     {
299         if (classHasPrimkeyField(getCurrentClass())) {
300             generate(template);
301         }
302     }
303
304     /**
305      * Process the tag body if the current method is a getter or setter for the primkey-field.
306      *
307      * @param template The body of the block tag
308      * @param attributes The attributes of the template tag
309      * @exception XDocletException
310      * @doc.tag type="block"
311      */

312     public void ifIsPrimkeyField(String JavaDoc template, Properties JavaDoc attributes)
313          throws XDocletException
314     {
315         if (isMethodPrimkeyField(getCurrentClass(), getCurrentMethod())) {
316             generate(template);
317         }
318     }
319
320     /**
321      * Process the tag body if the current method is not a getter or setter for the primkey-field.
322      *
323      * @param template The body of the block tag
324      * @param attributes The attributes of the template tag
325      * @exception XDocletException
326      * @doc.tag type="block"
327      */

328     public void ifIsNotPrimkeyField(String JavaDoc template, Properties JavaDoc attributes)
329          throws XDocletException
330     {
331         if (!isMethodPrimkeyField(getCurrentClass(), getCurrentMethod())) {
332             generate(template);
333         }
334     }
335
336     /**
337      * Process the tag body if the current class doesn't have a defined primkey-field.
338      *
339      * @param template The body of the block tag
340      * @param attributes The attributes of the template tag
341      * @exception XDocletException
342      * @doc.tag type="block"
343      */

344     public void ifDoesntHavePrimkeyField(String JavaDoc template, Properties JavaDoc attributes)
345          throws XDocletException
346     {
347         if (!classHasPrimkeyField(getCurrentClass())) {
348             generate(template);
349         }
350     }
351
352     /**
353      * Returns the primkey-field defined for the current class.
354      *
355      * @param attributes The attributes of the template tag
356      * @return The value of the ejb.bean primkey-field parameter
357      * @exception XDocletException
358      * @see #getPrimkeyFieldFor(XClass)
359      * @doc.tag type="content"
360      */

361     public String JavaDoc primkeyField(Properties JavaDoc attributes)
362          throws XDocletException
363     {
364         return getPrimkeyFieldFor(getCurrentClass());
365     }
366
367     /**
368      * Returns the getter name for the primkey-field.
369      *
370      * @param attributes The attributes of the template tag
371      * @return The primkey-field getter
372      * @exception XDocletException
373      * @see #getPrimkeyGetterFor(XClass)
374      * @doc.tag type="content"
375      */

376     public String JavaDoc primkeyGetter(Properties JavaDoc attributes)
377          throws XDocletException
378     {
379         return getPrimkeyGetterFor(getCurrentClass());
380     }
381
382     /**
383      * Returns the setter name for the primkey-field.
384      *
385      * @param attributes The attributes of the template tag
386      * @return The primkey-field setter
387      * @exception XDocletException
388      * @see #getPrimkeySetterFor(XClass)
389      * @doc.tag type="content"
390      */

391     public String JavaDoc primkeySetter(Properties JavaDoc attributes)
392          throws XDocletException
393     {
394         return getPrimkeySetterFor(getCurrentClass());
395     }
396
397     /**
398      * Process the tag body if the current class has defined a setter for the primkey-field.
399      *
400      * @param template The body of the block tag
401      * @param attributes The attributes of the template tag
402      * @exception XDocletException
403      * @see #getPrimkeySetterFor(XClass)
404      * @doc.tag type="block"
405      */

406     public void ifHasPrimkeySetter(String JavaDoc template, Properties JavaDoc attributes)
407          throws XDocletException
408     {
409         if (getPrimkeySetterFor(getCurrentClass()) != null) {
410             generate(template);
411         }
412     }
413
414     /**
415      * Returns the name of generated PK class for the current class.
416      *
417      * @return The name of generated PK class.
418      * @exception XDocletException
419      * @doc.tag type="content"
420      */

421     public String JavaDoc pkClass() throws XDocletException
422     {
423         return getPkClassFor(getCurrentClass());
424     }
425
426     /**
427      * Returns the name of PK class for the current class.
428      *
429      * @return The name of generated PK class.
430      * @exception XDocletException
431      * @doc.tag type="content"
432      */

433     public String JavaDoc pkClassForEjbJarXml() throws XDocletException
434     {
435         return getPkClassForEjbJarXmlFor(getCurrentClass());
436     }
437
438     /**
439      * Returns a string containing comma-separated list of primary key fields with their types.
440      *
441      * @return A string containing comma-separated list of primary key fields with their types.
442      * @exception XDocletException
443      * @see xdoclet.modules.ejb.entity.PersistentTagsHandler#fieldList(XClass, String, String,
444      * int, String, boolean)
445      * @doc.tag type="content"
446      */

447     public String JavaDoc pkfieldList() throws XDocletException
448     {
449         return PersistentTagsHandler.fieldList(getCurrentClass(), "ejb:pk-field", null, 0, null, true);
450     }
451
452     /**
453      * Returns a string containing comma-separated list of primary key fields getting from an object specified as
454      * parameter.
455      *
456      * @param attributes The attributes of the template tag
457      * @return A string containing comma-separated list of primary key fields without their types.
458      * @exception XDocletException
459      * @see xdoclet.modules.ejb.entity.PersistentTagsHandler#fieldList(XClass, String, String,
460      * int, String, boolean)
461      * @doc.tag type="content"
462      * @doc.param name="name" optional="false" description="The name of the variable to get the fields
463      * from."
464      */

465     public String JavaDoc pkfieldListFrom(Properties JavaDoc attributes) throws XDocletException
466     {
467         String JavaDoc name = attributes.getProperty("name");
468         String JavaDoc commaSep = PersistentTagsHandler.fieldList(getCurrentClass(), "ejb:pk-field", null, 2, null, true);
469
470         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(commaSep, ",");
471         String JavaDoc ret = "";
472
473         while (st.hasMoreTokens()) {
474             String JavaDoc attr = st.nextToken();
475
476             ret += name + "." + attr;
477             if (st.hasMoreTokens()) {
478                 ret += ",";
479             }
480         }
481         return ret;
482     }
483
484     /**
485      * Returns the name of the class pk class extends.
486      *
487      * @return superclass name
488      * @exception XDocletException
489      * @doc.tag type="content"
490      */

491     public String JavaDoc extendsFrom() throws XDocletException
492     {
493         return extendsFromFor(getCurrentClass(), "ejb:pk", null, "extends", "java.lang.Object");
494     }
495
496     /**
497      * Evaluates the body if the current method is a primary key field.
498      *
499      * @param template The body of the block tag
500      * @exception XDocletException
501      * @doc.tag type="block"
502      */

503     public void ifIsPkField(String JavaDoc template) throws XDocletException
504     {
505         if (PersistentTagsHandler.isPkField(getCurrentMethod())) {
506             generate(template);
507         }
508     }
509
510     /**
511      * Gets the DependentClassFor attribute of the PkTagsHandler object
512      *
513      * @param clazz Describe what the parameter does
514      * @param type Describe what the parameter does
515      * @return The DependentClassFor value
516      * @exception XDocletException
517      */

518     protected String JavaDoc getDependentClassFor(XClass clazz, String JavaDoc type) throws XDocletException
519     {
520         return getPkClassFor(clazz);
521     }
522 }
523
Popular Tags