KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > bea > wls > ejb > WeblogicRelationTagsHandler


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

5 package xdoclet.modules.bea.wls.ejb;
6
7 import java.util.*;
8
9 import xjavadoc.*;
10 import xdoclet.XDocletException;
11 import xdoclet.modules.ejb.EjbTagsHandler;
12 import xdoclet.modules.ejb.dd.RelationTagsHandler;
13
14 import xdoclet.util.Translator;
15
16 /**
17  * This tag handler handles tags needed for Weblogic relations
18  *
19  * @author <a HREF="mailto:aslak.nospam@users.sf.net">Aslak Hellesøy</a>
20  * @created 12 mei 2002
21  * @xdoclet.taghandler namespace="WlEjbRel"
22  * @version $Revision: 1.11 $
23  */

24 public class WeblogicRelationTagsHandler extends RelationTagsHandler
25 {
26     private Collection columnMapTags = null;
27     private Iterator columnMapTagIterator = null;
28     private XClass pkClass;
29     private String JavaDoc currentKeyColumn = null;
30     private String JavaDoc currentForeignKeyColumn = null;
31
32     /**
33      * Describe what the method does
34      *
35      * @param template Describe what the parameter does
36      * @param left
37      * @exception XDocletException Describe the exception
38      */

39     public void forAllColumnMaps(String JavaDoc template, boolean left) throws XDocletException
40     {
41         XMethod methodA;
42         XMethod methodB;
43         XClass classA;
44         XClass classB;
45
46         if (left) {
47             methodA = currentRelation.getLeftMethod();
48             methodB = currentRelation.getRightMethod();
49             classA = currentRelation.getLeft();
50             classB = currentRelation.getRight();
51         }
52         else {
53             methodA = currentRelation.getRightMethod();
54             methodB = currentRelation.getLeftMethod();
55             classA = currentRelation.getRight();
56             classB = currentRelation.getLeft();
57         }
58
59         if (methodA != null) {
60             // bidirectional
61
columnMapTags = methodA.getDoc().getTags("weblogic.column-map");
62
63             columnMapTagIterator = columnMapTags.iterator();
64             pkClass = classB;
65             if (pkClass == null) {
66                 pkClass = EjbTagsHandler.getEjb(methodA.getDoc().getTagAttributeValue("ejb.relation", "target-ejb"));
67             }
68             if (columnMapTags.size() == 0) {
69                 //throw new XDocletException( currentRelation.getRightMethod().name() + " should have at least one @weblogic:column-map tag (B)" );
70
}
71         }
72         else {
73             // unidirectional
74
columnMapTags = methodB.getDoc().getTags("weblogic:target-column-map");
75
76             columnMapTagIterator = columnMapTags.iterator();
77             pkClass = classB;
78             if (columnMapTags.size() == 0) {
79                 //throw new XDocletException( currentRelation.getLeftMethod().name() + " should have at least one @weblogic:target-column-map tag (C)" );
80
}
81         }
82
83         while (columnMapTagIterator.hasNext()) {
84             XTag columnMapTag = (XTag) columnMapTagIterator.next();
85
86             currentKeyColumn = columnMapTag.getAttributeValue("key-column");
87             currentForeignKeyColumn = columnMapTag.getAttributeValue("foreign-key-column");
88             generate(template);
89         }
90     }
91
92     public void forAllLeftColumnMaps(String JavaDoc template) throws XDocletException
93     {
94         forAllColumnMaps(template, true);
95     }
96
97     /**
98      * Describe what the method does
99      *
100      * @param template Describe what the parameter does
101      * @exception XDocletException Describe the exception
102      */

103     public void forAllRightColumnMaps(String JavaDoc template) throws XDocletException
104     {
105         forAllColumnMaps(template, false);
106     }
107
108     /**
109      * Makes sure the column-map tags are on the right side and target-column-map tags left
110      */

111     public void ensureColumnMapTagsRight()
112     {
113         boolean rightHasTargetColumnMapTags = false;
114         boolean leftHasColumnMapTags = false;
115
116         if (currentRelation.getLeftMethod() != null) {
117             rightHasTargetColumnMapTags = currentRelation.getLeftMethod().getDoc().getTags("weblogic.target-column-map").size() != 0;
118         }
119         if (currentRelation.getRightMethod() != null) {
120             leftHasColumnMapTags = currentRelation.getRightMethod().getDoc().getTags("weblogic.column-map").size() != 0;
121         }
122         if (rightHasTargetColumnMapTags || leftHasColumnMapTags) {
123             currentRelation.swap();
124         }
125     }
126
127     /**
128      * Describe what the method does
129      *
130      * @return Describe the return value
131      * @exception XDocletException Describe the exception
132      */

133     public String JavaDoc joinTableName() throws XDocletException
134     {
135         // We say mandatory is false (last parameter), so we can provide a more detailed error message.
136
String JavaDoc leftJoinTableName = currentRelation.getLeftMethod().getDoc().getTagAttributeValue("weblogic:relation", "join-table-name", false);
137         String JavaDoc joinTableName = leftJoinTableName;
138         String JavaDoc rightJoinTableName = null;
139
140         // do some sanity checking
141
if (leftJoinTableName == null && currentRelation.getRightMethod() != null) {
142             rightJoinTableName = currentRelation.getRightMethod().getDoc().getTagAttributeValue("weblogic:relation", "join-table-name", false);
143             joinTableName = rightJoinTableName;
144         }
145         if (leftJoinTableName != null && rightJoinTableName != null) {
146             throw new XDocletException(Translator.getString(XDocletModulesBeaWlsEjbMessages.class, XDocletModulesBeaWlsEjbMessages.JOIN_TABLE_NAME_ONLY_ONE_SIDE));
147         }
148         if (leftJoinTableName == null && rightJoinTableName == null) {
149             throw new XDocletException(Translator.getString(XDocletModulesBeaWlsEjbMessages.class, XDocletModulesBeaWlsEjbMessages.JOIN_TABLE_NAME_NEEDED, new String JavaDoc[]{relationName()}));
150         }
151         return joinTableName;
152     }
153
154     /**
155      * Describe what the method does
156      *
157      * @return Describe the return value
158      * @exception XDocletException Describe the exception
159      */

160     public String JavaDoc leftGroupName() throws XDocletException
161     {
162         if (currentRelation.getLeftMethod() != null) {
163             return currentRelation.getLeftMethod().getDoc().getTagAttributeValue("weblogic:relation", "group-name", false);
164         }
165         else {
166             return null;
167         }
168     }
169
170     /**
171      * Describe what the method does
172      *
173      * @param template Describe what the parameter does
174      * @exception XDocletException Describe the exception
175      */

176     public void ifHasLeftGroupName(String JavaDoc template) throws XDocletException
177     {
178         if (leftGroupName() != null) {
179             generate(template);
180         }
181     }
182
183     /**
184      * Describe what the method does
185      *
186      * @return Describe the return value
187      * @exception XDocletException Describe the exception
188      */

189     public String JavaDoc rightGroupName() throws XDocletException
190     {
191         String JavaDoc groupName = null;
192
193         if (currentRelation.getRightMethod() != null) {
194             // bidirectional
195
groupName = currentRelation.getRightMethod().getDoc().getTagAttributeValue("weblogic:relation", "group-name", false);
196         }
197         else {
198             // unidirectional
199
groupName = currentRelation.getLeftMethod().getDoc().getTagAttributeValue("weblogic:relation", "group-name", false);
200         }
201         return groupName;
202     }
203
204     /**
205      * Describe what the method does
206      *
207      * @param template Describe what the parameter does
208      * @exception XDocletException Describe the exception
209      */

210     public void ifHasRightGroupName(String JavaDoc template) throws XDocletException
211     {
212         if (rightGroupName() != null) {
213             generate(template);
214         }
215     }
216
217     /**
218      * Describe what the method does
219      *
220      * @param template Describe what the parameter does
221      * @exception XDocletException Describe the exception
222      */

223     public void ifHasKeyColumn(String JavaDoc template) throws XDocletException
224     {
225         if (keyColumn() != null) {
226             generate(template);
227         }
228     }
229
230     /**
231      * Describe what the method does
232      *
233      * @return Describe the return value
234      * @exception XDocletException
235      */

236     public String JavaDoc keyColumn() throws XDocletException
237     {
238         if (currentKeyColumn == null) {
239             // Not specified. Look on related bean
240
if (columnMapTags.size() > 1) {
241                 throw new XDocletException(Translator.getString(XDocletModulesBeaWlsEjbMessages.class, XDocletModulesBeaWlsEjbMessages.CANT_GUESS_PK_FIELD));
242             }
243
244             Collection methods = pkClass.getMethods();
245
246             for (Iterator i = methods.iterator(); i.hasNext(); ) {
247                 XMethod method = (XMethod) i.next();
248                 boolean isPk = method.getDoc().getTag("ejb.pk-field") != null;
249                 String JavaDoc tmpKeyColumn = method.getDoc().getTagAttributeValue("ejb.persistence", "column-name");
250
251                 // see that we only have one pk field
252
if (isPk) {
253                     if (currentKeyColumn != null) {
254                         throw new XDocletException(Translator.getString(XDocletModulesBeaWlsEjbMessages.class, XDocletModulesBeaWlsEjbMessages.MORE_THAN_ONE_PK_FIELD, new String JavaDoc[]{pkClass.getQualifiedName()}));
255                     }
256                     else {
257                         currentKeyColumn = tmpKeyColumn;
258                     }
259                 }
260             }
261             if (currentKeyColumn == null) {
262                 throw new XDocletException(Translator.getString(XDocletModulesBeaWlsEjbMessages.class, XDocletModulesBeaWlsEjbMessages.NO_PK_FIELD, new String JavaDoc[]{pkClass.getQualifiedName()}));
263             }
264         }
265         return currentKeyColumn;
266     }
267
268     /**
269      * Describe what the method does
270      *
271      * @return Describe the return value
272      */

273     public String JavaDoc foreignKeyColumn()
274     {
275         return currentForeignKeyColumn;
276     }
277 }
278
Popular Tags