KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package xdoclet.modules.ejb.entity;
6
7 import java.text.MessageFormat JavaDoc;
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 /**
24  * @author Ara Abrahamian (ara_e@email.com)
25  * @created Oct 16, 2001
26  * @xdoclet.taghandler namespace="EjbCmp"
27  * @version $Revision: 1.17 $
28  */

29 public class CmpTagsHandler extends EntityTagsHandler
30 {
31     /**
32      * Gets the EntityCmpClassFor attribute of the CmpTagsHandler class
33      *
34      * @param clazz Describe what the parameter does
35      * @return The EntityCmpClassFor value
36      */

37     public static String JavaDoc getEntityCmpClassFor(XClass clazz)
38     {
39         String JavaDoc fileName = clazz.getContainingPackage().getName();
40         String JavaDoc entityName = MessageFormat.format(getEntityCmpClassPattern(), new Object JavaDoc[]{EjbTagsHandler.getShortEjbNameFor(clazz)});
41
42         // Fix package name
43
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     /**
54      * Returns true if clazz is an CMP entity bean, false otherwise. Entity type is determined by looking at the
55      * ejb:bean's type parameter.
56      *
57      * @param clazz Description of Parameter
58      * @return The EntityCmp value
59      * @exception XDocletException
60      * @todo refactor this method up in superclass with isEntityBmp
61      */

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         // assume CMP if not specified
71
if (beanTag == null) {
72             return true;
73         }
74
75         String JavaDoc value = beanTag.getAttributeValue("type");
76
77         // assume CMP if not specified
78
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     /**
91      * Returns true if ejbspec config parameter is "2.0" and ejb:bean's cmp-version either not defined or is "2.x",
92      * false otherwise.
93      *
94      * @param clazz
95      * @return Description of the Returned Value
96      * @exception XDocletException
97      */

98     public static boolean isUsingCmp2Impl(XClass clazz) throws XDocletException
99     {
100         String JavaDoc 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 JavaDoc 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 JavaDoc[]{clazz.getQualifiedName()}));
144             }
145         }
146
147         return EntityCmpSubTask.CmpSpecVersion.CMP_2_0.equals(cmp);
148     }
149
150     /**
151      * Returns the cmp-version 2.x compatible cmp / cmr field name for the passed method.
152      *
153      * @param method the method
154      * @return the field name
155      * @exception XDocletException
156      */

157     public static String JavaDoc getFieldNameFor(XMethod method) throws XDocletException
158     {
159         // remove get/set
160
String JavaDoc m = method.getName().substring(3);
161
162         // replace first character to lowercase
163
char firstU = m.charAt(0);
164         char firstL = Character.toLowerCase(firstU);
165
166         return firstL + m.substring(1);
167     }
168
169     /**
170      * Gets the EntityCmpClassPattern attribute of the CmpTagsHandler class
171      *
172      * @return The EntityCmpClassPattern value
173      */

174     protected static String JavaDoc 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     /**
187      * Returns the name of generated CMP class.
188      *
189      * @return The name of generated CMP class.
190      * @exception XDocletException
191      * @doc.tag type="content"
192      */

193     public String JavaDoc entityCmpClass() throws XDocletException
194     {
195         return getEntityCmpClassFor(getCurrentClass());
196     }
197
198     /**
199      * Evaluate the body block if using EJB 2.0 and CMP version 2.x.
200      *
201      * @param template The body of the block tag
202      * @exception XDocletException
203      * @see #isUsingCmp2Impl(xjavadoc.XClass)
204      * @see #ifNotUsingCmp2(java.lang.String)
205      * @doc.tag type="block"
206      */

207     public void ifUsingCmp2(String JavaDoc template) throws XDocletException
208     {
209         if (isUsingCmp2Impl(getCurrentClass())) {
210             generate(template);
211         }
212     }
213
214     /**
215      * Evaluate the body block if not using EJB 2.0 or using EJB 2.0 but CMP version 1.x.
216      *
217      * @param template The body of the block tag
218      * @exception XDocletException
219      * @see #isUsingCmp2Impl(xjavadoc.XClass)
220      * @see #ifUsingCmp2(java.lang.String)
221      * @doc.tag type="block"
222      */

223     public void ifNotUsingCmp2(String JavaDoc template) throws XDocletException
224     {
225         if (!isUsingCmp2Impl(getCurrentClass())) {
226             generate(template);
227         }
228     }
229
230     /**
231      * Evaluates the body block if current class is an CMP entity bean.
232      *
233      * @param template The body of the block tag
234      * @exception XDocletException
235      * @doc.tag type="block"
236      */

237     public void ifEntityIsCmp(String JavaDoc template) throws XDocletException
238     {
239         if (isEntityCmp(getCurrentClass())) {
240             generate(template);
241         }
242     }
243
244     /**
245      * Evaluates the body block for each EJBean derived from EntityBean which is CMP.
246      *
247      * @param template The body of the block tag
248      * @exception XDocletException
249      * @see #isEntityCmp(xjavadoc.XClass)
250      * @doc.tag type="block"
251      */

252     public void forAllCmpEntityBeans(String JavaDoc 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     /**
276      * Evaluates the body block for each persistent field of current class (if entity CMP). Looks at super classes as
277      * well. Searches for the getter methods which has ejb:persistent-field defined.
278      *
279      * @param template The body of the block tag
280      * @exception XDocletException
281      * @see #isEntityCmp(xjavadoc.XClass)
282      * @see xdoclet.modules.ejb.entity.PersistentTagsHandler#isPersistentField(xjavadoc.XMethod)
283      * @see xdoclet.tagshandler.MethodTagsHandler#isGetter(java.lang.String)
284      * @doc.tag type="block"
285      */

286     public void forAllCmpFields(String JavaDoc 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                 // Add super class info
314
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     /**
327      * Returns the dbms column. Looks for ejb.persistence column-name, then for legacy app-server specific tags, then
328      * propertyName as a fall-back
329      *
330      * @return
331      * @exception XDocletException
332      * @todo add more tags/params here
333      */

334     public String JavaDoc dbmsColumn() throws XDocletException
335     {
336         String JavaDoc tags = "ejb.persistence,jboss.column-name,jboss.column-name,weblogic.dbms-column";
337         String JavaDoc attribs = "column-name,name,,";
338         Properties props = new Properties();
339
340         props.setProperty("tagName", tags);
341         props.setProperty("paramName", attribs);
342
343         String JavaDoc result = getTagValue(props, FOR_METHOD);
344
345         if (result == null) {
346             result = MethodTagsHandler.getPropertyNameFor(getCurrentMethod());
347         }
348         return result;
349     }
350
351     /**
352      * Returns the table name for the current class.
353      *
354      * @return
355      * @exception XDocletException
356      * @todo add more tags/params here
357      */

358     public String JavaDoc dbmsTable() throws XDocletException
359     {
360         String JavaDoc tags = "ejb.persistence,jboss.table-name,jboss.table-name,weblogic.table-name";
361         String JavaDoc attribs = "table-name,name,,";
362         Properties props = new Properties();
363
364         props.setProperty("tagName", tags);
365         props.setProperty("paramName", attribs);
366
367         String JavaDoc 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 JavaDoc 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     /**
388      * Returns the cmp-version 2.x compatible cmp / cmr field name for the current method.
389      *
390      * @return the field name
391      * @exception XDocletException
392      * @doc.tag type="content"
393      */

394     public String JavaDoc fieldName() throws XDocletException
395     {
396         return getFieldNameFor(getCurrentMethod());
397     }
398 }
399
Popular Tags