KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > jboss > ejb > JBossRelationTagsHandler


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

5 package xdoclet.modules.jboss.ejb;
6
7 import java.util.*;
8 import xjavadoc.*;
9
10 import xdoclet.XDocletException;
11 import xdoclet.modules.ejb.dd.RelationTagsHandler;
12
13 /**
14  * @author <a HREF="mailto:david@nustiu.net">David Budworth</a>
15  * @created Feb 4, 2002
16  * @xdoclet.taghandler namespace="JBEjbRel"
17  * @version $Revision: 1.12 $
18  */

19 public class JBossRelationTagsHandler extends RelationTagsHandler
20 {
21     private String JavaDoc currentFKRelField = null;
22     private String JavaDoc currentFKCol = null;
23     private String JavaDoc currentJdbcType = null;
24     private String JavaDoc currentSqlType = null;
25
26     public String JavaDoc relationTableAttribute(Properties attributes) throws XDocletException
27     {
28         String JavaDoc attribute = null;
29
30         String JavaDoc paramName = attributes.getProperty("paramName");
31
32         XMethod leftMethod = currentRelation.getLeftMethod();
33         XMethod rightMethod = currentRelation.getRightMethod();
34
35         attribute = leftMethod == null ? null :
36             leftMethod.getDoc().getTagAttributeValue("jboss.relation-table", paramName, false);
37
38         attribute = attribute != null ? attribute : rightMethod == null ? null :
39             rightMethod.getDoc().getTagAttributeValue("jboss.relation-table", paramName, false);
40
41         return attribute;
42     }
43
44     public void ifHasRelationTableAttribute(String JavaDoc template, Properties attributes) throws XDocletException
45     {
46         if (relationTableAttribute(attributes) != null) {
47             generate(template);
48         }
49     }
50
51     /**
52      * Describe what the method does
53      *
54      * @param template Describe what the parameter does
55      * @exception XDocletException Describe the exception
56      */

57     public void ifNotLeftHasFK(String JavaDoc template) throws XDocletException
58     {
59         if (!hasFK(true)) {
60             generate(template);
61         }
62     }
63
64     /**
65      * Describe what the method does
66      *
67      * @param template Describe what the parameter does
68      * @exception XDocletException Describe the exception
69      */

70     public void ifNotRightHasFK(String JavaDoc template) throws XDocletException
71     {
72         if (!hasFK(false)) {
73             generate(template);
74         }
75     }
76
77     /**
78      * Describe what the method does
79      *
80      * @param template Describe what the parameter does
81      * @exception XDocletException Describe the exception
82      */

83     public void ifLeftHasFK(String JavaDoc template) throws XDocletException
84     {
85         if (hasFK(true)) {
86             generate(template);
87         }
88     }
89
90     /**
91      * Describe what the method does
92      *
93      * @param template Describe what the parameter does
94      * @exception XDocletException Describe the exception
95      */

96     public void ifRightHasFK(String JavaDoc template) throws XDocletException
97     {
98         if (hasFK(false)) {
99             generate(template);
100         }
101     }
102
103     public void ifLeftHasReadAhead(String JavaDoc template) throws XDocletException
104     {
105         if (readAheadStrategy(true) != null) {
106             generate(template);
107         }
108     }
109
110     public void ifRightHasReadAhead(String JavaDoc template) throws XDocletException
111     {
112         if (readAheadStrategy(false) != null) {
113             generate(template);
114         }
115     }
116
117     public void ifLeftHasReadAheadPageSize(String JavaDoc template) throws XDocletException
118     {
119         if (readAheadPageSize(true) != null) {
120             generate(template);
121         }
122     }
123
124     public void ifRightHasReadAheadPageSize(String JavaDoc template) throws XDocletException
125     {
126         if (readAheadPageSize(false) != null) {
127             generate(template);
128         }
129     }
130
131     public void ifLeftHasReadAheadEagerLoadGroup(String JavaDoc template) throws XDocletException
132     {
133         if (readAheadEagerLoadGroup(true) != null) {
134             generate(template);
135         }
136     }
137
138     public void ifRightHasReadAheadEagerLoadGroup(String JavaDoc template) throws XDocletException
139     {
140         if (readAheadEagerLoadGroup(false) != null) {
141             generate(template);
142         }
143     }
144
145     /**
146      * Describe what the method does
147      *
148      * @param template Describe what the parameter does
149      * @exception XDocletException Describe the exception
150      */

151     public void forAllLeftForeignKeys(String JavaDoc template) throws XDocletException
152     {
153         forAllForeignKeys(template, true);
154     }
155
156     /**
157      * Describe what the method does
158      *
159      * @param template Describe what the parameter does
160      * @exception XDocletException Describe the exception
161      */

162     public void forAllRightForeignKeys(String JavaDoc template) throws XDocletException
163     {
164         forAllForeignKeys(template, false);
165     }
166
167     /**
168      * Describe what the method does
169      *
170      * @return Describe the return value
171      */

172     public String JavaDoc relatedPKField()
173     {
174         return currentFKRelField;
175     }
176
177     /**
178      * Describe what the method does
179      *
180      * @return Describe the return value
181      */

182     public String JavaDoc fkColumn()
183     {
184         return currentFKCol;
185     }
186
187     /**
188      * Describe what the method does
189      *
190      * @param template Describe what the parameter does
191      * @exception XDocletException Describe the exception
192      */

193     public void ifLeftHasFKConstraint(String JavaDoc template) throws XDocletException
194     {
195         if (hasFKConstraint(true)) {
196             generate(template);
197         }
198     }
199
200     /**
201      * Describe what the method does
202      *
203      * @param template Describe what the parameter does
204      * @exception XDocletException Describe the exception
205      */

206     public void ifRightHasFKConstraint(String JavaDoc template) throws XDocletException
207     {
208         if (hasFKConstraint(false)) {
209             generate(template);
210         }
211     }
212
213     /**
214      * Describe what the method does
215      *
216      * @return Describe the return value
217      * @exception XDocletException Describe the exception
218      */

219     public String JavaDoc leftFKConstraint() throws XDocletException
220     {
221         return fkConstraint(true);
222     }
223
224     /**
225      * Describe what the method does
226      *
227      * @return Describe the return value
228      * @exception XDocletException Describe the exception
229      */

230     public String JavaDoc rightFKConstraint() throws XDocletException
231     {
232         return fkConstraint(false);
233     }
234
235     public String JavaDoc leftReadAheadStrategy() throws XDocletException
236     {
237         return readAheadStrategy(true);
238     }
239
240     public String JavaDoc rightReadAheadStrategy() throws XDocletException
241     {
242         return readAheadStrategy(false);
243     }
244
245     public String JavaDoc leftReadAheadPageSize() throws XDocletException
246     {
247         return readAheadPageSize(true);
248     }
249
250     public String JavaDoc rightReadAheadPageSize() throws XDocletException
251     {
252         return readAheadPageSize(false);
253     }
254
255     public String JavaDoc leftReadAheadEagerLoadGroup() throws XDocletException
256     {
257         return readAheadEagerLoadGroup(true);
258     }
259
260     public String JavaDoc rightReadAheadEagerLoadGroup() throws XDocletException
261     {
262         return readAheadEagerLoadGroup(false);
263     }
264
265     public void ifIsRelationTableMapping(String JavaDoc template) throws XDocletException
266     {
267         if (isRelationTableMapping())
268             generate(template);
269     }
270
271     public void ifNotIsRelationTableMapping(String JavaDoc template) throws XDocletException
272     {
273         if (!isRelationTableMapping())
274             generate(template);
275     }
276
277     public void ifIsRightBatchCascadeDelete(String JavaDoc template) throws XDocletException
278     {
279         if (hasBatchCascadeDelete(false))
280             generate(template);
281     }
282
283     public void ifIsLeftBatchCascadeDelete(String JavaDoc template) throws XDocletException
284     {
285         if (hasBatchCascadeDelete(true))
286             generate(template);
287     }
288
289
290     public void ifIsForeignKeyMapping(String JavaDoc template) throws XDocletException
291     {
292         if (isForeignKeyMapping())
293             generate(template);
294     }
295
296     public void ifNotIsForeignKeyMapping(String JavaDoc template) throws XDocletException
297     {
298         if (!isForeignKeyMapping())
299             generate(template);
300     }
301
302     public void ifHasJdbcType(String JavaDoc template, Properties attributes) throws XDocletException
303     {
304         if (currentJdbcType != null) {
305             generate(template);
306         }
307     }
308
309     public String JavaDoc jdbcType()
310     {
311         return currentJdbcType;
312     }
313
314
315     public void ifHasSqlType(String JavaDoc template, Properties attributes) throws XDocletException
316     {
317         if (currentSqlType != null) {
318             generate(template);
319         }
320     }
321
322     public String JavaDoc sqlType()
323     {
324         return currentSqlType;
325     }
326
327     /**
328      * Checks if the current relation should use a relation table mapping. This can occurs if the current relation is
329      * many-to-many or the programmer has explicitly set jboss.relation-mapping style="relation-table"
330      *
331      * @return true if relation-table-mapping should be used
332      * @exception XDocletException
333      */

334     private boolean isRelationTableMapping() throws XDocletException
335     {
336         if (currentRelation.isMany2Many())
337             return true;
338         if (currentRelation.getLeftMethod() != null)
339             if ("relation-table".equals(currentRelation.getLeftMethod().getDoc().getTagAttributeValue("jboss.relation-mapping", "style")))
340                 return true;
341         if (currentRelation.getRightMethod() != null)
342             if ("relation-table".equals(currentRelation.getRightMethod().getDoc().getTagAttributeValue("jboss.relation-mapping", "style")))
343                 return true;
344
345         return false;
346     }
347
348     /**
349      * Checks if the current relation should use a foreign key mapping. This occurs if the programmer has explicitly set
350      * jboss.relation-mapping style="foreign-key"
351      *
352      * @return true if foreign-key-mapping should be used
353      * @exception XDocletException
354      */

355     private boolean isForeignKeyMapping() throws XDocletException
356     {
357         if (currentRelation.getLeftMethod() != null)
358             if ("foreign-key".equals(currentRelation.getLeftMethod().getDoc().getTagAttributeValue("jboss.relation-mapping", "style")))
359                 return true;
360         if (currentRelation.getRightMethod() != null)
361             if ("foreign-key".equals(currentRelation.getRightMethod().getDoc().getTagAttributeValue("jboss.relation-mapping", "style")))
362                 return true;
363
364         return false;
365     }
366
367     private String JavaDoc fkConstraint(boolean left)
368     {
369         XMethod methodA = null;
370         XMethod methodB = null;
371
372         if (left) {
373             methodA = currentRelation.getLeftMethod();
374             methodB = currentRelation.getRightMethod();
375         }
376         else {
377             methodA = currentRelation.getRightMethod();
378             methodB = currentRelation.getLeftMethod();
379         }
380
381         if (methodA != null)
382             return methodA.getDoc().getTagAttributeValue("jboss.relation", "fk-constraint");
383         else
384             return methodB.getDoc().getTagAttributeValue("jboss.target-relation", "fk-constraint");
385     }
386
387     private String JavaDoc readAheadStrategy(boolean left)
388     {
389         XMethod methodA = null;
390         XMethod methodB = null;
391
392         if (left) {
393             methodA = currentRelation.getLeftMethod();
394             methodB = currentRelation.getRightMethod();
395         }
396         else {
397             methodA = currentRelation.getRightMethod();
398             methodB = currentRelation.getLeftMethod();
399         }
400
401         if (methodA != null)
402             return methodA.getDoc().getTagAttributeValue("jboss.relation-read-ahead", "strategy");
403         else
404             return methodB.getDoc().getTagAttributeValue("jboss.target-relation-read-ahead", "strategy");
405     }
406
407     private String JavaDoc readAheadPageSize(boolean left)
408     {
409         XMethod methodA = null;
410         XMethod methodB = null;
411
412         if (left) {
413             methodA = currentRelation.getLeftMethod();
414             methodB = currentRelation.getRightMethod();
415         }
416         else {
417             methodA = currentRelation.getRightMethod();
418             methodB = currentRelation.getLeftMethod();
419         }
420
421         if (methodA != null)
422             return methodA.getDoc().getTagAttributeValue("jboss.relation-read-ahead", "page-size");
423         else
424             return methodB.getDoc().getTagAttributeValue("jboss.target-relation-read-ahead", "page-size");
425     }
426
427     private String JavaDoc readAheadEagerLoadGroup(boolean left)
428     {
429         XMethod methodA = null;
430         XMethod methodB = null;
431
432         if (left) {
433             methodA = currentRelation.getLeftMethod();
434             methodB = currentRelation.getRightMethod();
435         }
436         else {
437             methodA = currentRelation.getRightMethod();
438             methodB = currentRelation.getLeftMethod();
439         }
440
441         if (methodA != null)
442             return methodA.getDoc().getTagAttributeValue("jboss.relation-read-ahead", "eager-load-group");
443         else
444             return methodB.getDoc().getTagAttributeValue("jboss.target-relation-read-ahead", "eager-load-group");
445     }
446
447     /**
448      * Checks if the current relation has a foreign key constraint.
449      *
450      * @param left if TRUE the left side of the relation will be checked, otherwise the right side will be.
451      * @return TRUE if the current relation has a foreign key constraint.
452      */

453     private boolean hasFKConstraint(boolean left)
454     {
455         XMethod methodA = null;
456         XMethod methodB = null;
457
458         if (left) {
459             methodA = currentRelation.getLeftMethod();
460             methodB = currentRelation.getRightMethod();
461         }
462         else {
463             methodA = currentRelation.getRightMethod();
464             methodB = currentRelation.getLeftMethod();
465         }
466
467         if (methodA != null)
468             return methodA.getDoc().getTagAttributeValue("jboss.relation", "fk-constraint") != null;
469         else
470             return methodB.getDoc().getTagAttributeValue("jboss.target-relation", "fk-constraint") != null;
471     }
472
473
474     /**
475      * Checks if the current relation has a foreign key declaration tag (jboss.relation/jboss.target-relation).
476      *
477      * @param left if TRUE the left side of the relation will be checked, otherwise the right side will be.
478      * @return TRUE if the relation has a foreign key declaration tag.
479      */

480     private boolean hasFK(boolean left)
481     {
482         XMethod methodA = null;
483         XMethod methodB = null;
484
485         if (left) {
486             methodA = currentRelation.getLeftMethod();
487             methodB = currentRelation.getRightMethod();
488         }
489         else {
490             methodA = currentRelation.getRightMethod();
491             methodB = currentRelation.getLeftMethod();
492         }
493
494         if (methodA != null)
495             return methodA.getDoc().hasTag("jboss.relation");
496         else
497             return methodB.getDoc().hasTag("jboss.target-relation");
498     }
499
500     /**
501      * Checks if the current relation has a batch-cascade-delete tag
502      *
503      * @param left if TRUE the left side of the relation will be checked, otherwise the right side will be.
504      * @return TRUE if the relation has a batch-cascade-delete tag
505      */

506     private boolean hasBatchCascadeDelete(boolean left)
507     {
508         XMethod methodA = null;
509         XMethod methodB = null;
510
511         if (left) {
512             methodA = currentRelation.getLeftMethod();
513             methodB = currentRelation.getRightMethod();
514         }
515         else {
516             methodA = currentRelation.getRightMethod();
517             methodB = currentRelation.getLeftMethod();
518         }
519
520         if (methodA != null)
521             return methodA.getDoc().getTagAttributeValue("jboss.relation", "batch-cascade-delete") != null;
522         else
523             return methodB.getDoc().getTagAttributeValue("jboss.target-relation", "batch-cascade-delete") != null;
524     }
525
526     /**
527      * Describe what the method does
528      *
529      * @param template Describe what the parameter does
530      * @param left Describe what the parameter does
531      * @exception XDocletException Describe the exception
532      */

533     private void forAllForeignKeys(String JavaDoc template, boolean left) throws XDocletException
534     {
535         Collection fktags = null;
536         XMethod methodA = null;
537         XMethod methodB = null;
538
539         if (left) {
540             methodA = currentRelation.getLeftMethod();
541             methodB = currentRelation.getRightMethod();
542         }
543         else {
544             methodA = currentRelation.getRightMethod();
545             methodB = currentRelation.getLeftMethod();
546         }
547
548         if (methodA != null)
549             fktags = methodA.getDoc().getTags("jboss.relation");
550         else
551             fktags = methodB.getDoc().getTags("jboss.target-relation");
552
553         // maybe we should throw here?
554
if (fktags == null) {
555             return;
556         }
557
558         for (Iterator i = fktags.iterator(); i.hasNext(); ) {
559             XTag fktag = (XTag) i.next();
560
561             currentFKRelField = fktag.getAttributeValue("related-pk-field");
562             currentFKCol = fktag.getAttributeValue("fk-column");
563             currentJdbcType = fktag.getAttributeValue("jdbc-type");
564             currentSqlType = fktag.getAttributeValue("sql-type");
565             generate(template);
566         }
567     }
568
569 }
570
Popular Tags