KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > libelis > jdo > LidoSubTask


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

5 package xdoclet.modules.libelis.jdo;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14 import xjavadoc.XClass;
15 import xjavadoc.XDoc;
16 import xjavadoc.XTag;
17 import xdoclet.XDocletException;
18 import xdoclet.modules.jdo.VendorExtension;
19 import xdoclet.modules.jdo.VendorExtensionsSubTask;
20 import xdoclet.modules.jdo.XDocletModulesJdoMessages;
21 import xdoclet.util.Translator;
22
23 /**
24  * @author Ludovic Claude (ludovicc@users.sourceforge.net)
25  * @created 10 October 2002
26  * @version $Revision: 1.3 $
27  * @ant.element display-name="Libelis LIDO support" name="lido" parent="xdoclet.modules.jdo.JdoDocletTask"
28  */

29 public class LidoSubTask extends VendorExtensionsSubTask
30 {
31
32     private final static Log LOG = LogFactory.getLog(LidoSubTask.class);
33
34     private String JavaDoc version = LidoVersionTypes.VERSION_1_3;
35
36     /**
37      * Gets the Version attribute of the LidoSubTask object
38      *
39      * @return The Version value
40      */

41     public String JavaDoc getVersion()
42     {
43         return version;
44     }
45
46     /**
47      * @return
48      * @see xdoclet.modules.jdo.VendorExtensionsSubTask#getVendorName()
49      */

50     public String JavaDoc getVendorName()
51     {
52         return "libelis";
53     }
54
55     public String JavaDoc getVendorDescription()
56     {
57         return "LIDO by Libelis";
58     }
59
60     /**
61      * The version of Lido. Supported versions are 1.3.
62      *
63      * @param version The new Version value
64      * @ant.not-required No, default is "1.3".
65      */

66     public void setVersion(LidoVersionTypes version)
67     {
68         this.version = version.getValue();
69     }
70
71     protected Collection JavaDoc getClassExtensions() throws XDocletException
72     {
73         Collection JavaDoc extensions = new ArrayList JavaDoc();
74         XDoc doc = getCurrentClass().getDoc();
75
76         if (doc.hasTag(SQL_TABLE_TAG)) {
77             String JavaDoc sqlName = doc.getTagAttributeValue(SQL_TABLE_TAG, TABLE_NAME_ATTR);
78
79             extensions.add(new VendorExtension(getVendorName(), "sql-name", sqlName));
80         }
81         return extensions;
82     }
83
84     protected Collection JavaDoc getFieldExtensions() throws XDocletException
85     {
86         Collection JavaDoc extensions = new ArrayList JavaDoc();
87         XDoc doc = getCurrentField().getDoc();
88
89         if (doc.hasTag(SQL_RELATION_TAG)) {
90             String JavaDoc style = doc.getTagAttributeValue(SQL_RELATION_TAG, STYLE_ATTR);
91
92             if (STYLE_FOREIGN_KEY_VALUE.equals(style)) {
93                 if (doc.getTagAttributeValue("jdo.field", "collection-type") == null) {
94                     Collection JavaDoc tags = doc.getTags(SQL_FIELD_TAG);
95                     String JavaDoc sqlName = getSqlNames(tags.iterator());
96
97                     extensions.add(new VendorExtension(getVendorName(), "sql-name", sqlName));
98                 }
99                 // else ignore
100
}
101             else if (STYLE_RELATION_TABLE_VALUE.equals(style)) {
102                 String JavaDoc tableName = doc.getTagAttributeValue(SQL_RELATION_TAG, TABLE_NAME_ATTR);
103
104                 extensions.add(new VendorExtension(getVendorName(), "sql-name", tableName));
105             }
106             else {
107                 LOG.warn(Translator.getString(XDocletModulesJdoMessages.class, XDocletModulesJdoMessages.NOT_SUPPORTED_TAG_ATTRIBUTE_VALUE,
108                     new String JavaDoc[]{SQL_RELATION_TAG, STYLE_ATTR, style, getVendorDescription()}));
109             }
110
111         }
112         else if (doc.hasTag(SQL_FIELD_TAG)) {
113             // todo: check that the class is a primitive type or standard java-type
114
//not sure I got the tag translations right...
115
String JavaDoc tableName = doc.getTagAttributeValue(SQL_FIELD_TAG, TABLE_NAME_ATTR);
116             String JavaDoc sqlName = doc.getTagAttributeValue(SQL_FIELD_TAG, COLUMN_NAME_ATTR);
117
118             if (tableName != null &&
119                 !tableName.equals(doc.getTagAttributeValue(SQL_TABLE_TAG, TABLE_NAME_ATTR))) {
120                 LOG.warn(Translator.getString(XDocletModulesJdoMessages.class, XDocletModulesJdoMessages.NOT_SUPPORTED_MULTIPLE_TABLE_MAPPING,
121                     new String JavaDoc[]{getVendorDescription()}));
122             }
123             extensions.add(new VendorExtension(getVendorName(), "sql-name", sqlName));
124         }
125         return extensions;
126     }
127
128     protected Collection JavaDoc getCollectionExtensions() throws XDocletException
129     {
130         Collection JavaDoc extensions = new ArrayList JavaDoc();
131         XDoc doc = getCurrentField().getDoc();
132
133         if (doc.hasTag(SQL_RELATION_TAG)) {
134             String JavaDoc style = doc.getTagAttributeValue(SQL_RELATION_TAG, STYLE_ATTR);
135
136             if (STYLE_FOREIGN_KEY_VALUE.equals(style)) {
137                 String JavaDoc relatedField = doc.getTagAttributeValue(SQL_RELATION_TAG, RELATED_FIELD_ATTR);
138
139                 extensions.add(new VendorExtension(getVendorName(), "sql-reverse", "javaField:" + relatedField));
140             }
141             else if (STYLE_RELATION_TABLE_VALUE.equals(style)) {
142                 Collection JavaDoc tags = doc.getTags(SQL_FIELD_TAG);
143                 String JavaDoc sqlName = getSqlNames(tags.iterator());
144
145                 extensions.add(new VendorExtension(getVendorName(), "sql-owner-name", sqlName));
146
147                 String JavaDoc embeddedClassName = doc.getTagAttributeValue("jdo.field", "element-type");
148                 XClass embeddedClass = getXJavaDoc().getXClass(embeddedClassName);
149                 String JavaDoc relatedCollection = doc.getTagAttributeValue(SQL_RELATION_TAG, RELATED_FIELD_ATTR);
150                 XDoc relatedDoc = embeddedClass.getField(relatedCollection).getDoc();
151                 Collection JavaDoc targetTags = relatedDoc.getTags(SQL_FIELD_TAG);
152                 String JavaDoc targetSqlName = getSqlNames(targetTags.iterator());
153
154                 extensions.add(new VendorExtension(getVendorName(), "sql-value-name", targetSqlName));
155             }
156             else {
157                 LOG.warn(Translator.getString(XDocletModulesJdoMessages.class, XDocletModulesJdoMessages.NOT_SUPPORTED_TAG_ATTRIBUTE_VALUE,
158                     new String JavaDoc[]{SQL_RELATION_TAG, STYLE_ATTR, style, getVendorDescription()}));
159             }
160
161         }
162         return extensions;
163     }
164
165     protected Collection JavaDoc getArrayExtensions() throws XDocletException
166     {
167         Collection JavaDoc extensions = new ArrayList JavaDoc();
168
169         LOG.warn(Translator.getString(XDocletModulesJdoMessages.class, XDocletModulesJdoMessages.NOT_SUPPORTED_ARRAY,
170             new String JavaDoc[]{getVendorDescription()}));
171
172         return extensions;
173     }
174
175     protected Collection JavaDoc getMapExtensions() throws XDocletException
176     {
177         Collection JavaDoc extensions = new ArrayList JavaDoc();
178
179         LOG.warn(Translator.getString(XDocletModulesJdoMessages.class, XDocletModulesJdoMessages.NOT_SUPPORTED_MAP,
180             new String JavaDoc[]{getVendorDescription()}));
181
182         return extensions;
183     }
184
185     private String JavaDoc getSqlNames(Iterator JavaDoc i)
186     {
187         StringBuffer JavaDoc sqlNames = new StringBuffer JavaDoc();
188
189         while (i.hasNext()) {
190             XTag tag = (XTag)i.next();
191             String JavaDoc columnName = tag.getAttributeValue(COLUMN_NAME_ATTR);
192             String JavaDoc relatedField = tag.getAttributeValue(RELATED_FIELD_ATTR);
193
194             if (sqlNames.length() > 0)
195                 sqlNames.append(", ");
196             sqlNames.append(relatedField + ":" + columnName);
197         }
198         return sqlNames.toString();
199     }
200
201     /**
202      * @author Ludovic Claude (ludovicc@users.sourceforge.net)
203      * @created 10 October 2002
204      */

205     public static class LidoVersionTypes extends org.apache.tools.ant.types.EnumeratedAttribute
206     {
207         public final static String JavaDoc VERSION_1_3 = "1.3";
208
209         /**
210          * Gets the Values attribute of the LidoVersionTypes object
211          *
212          * @return The Values value
213          */

214         public String JavaDoc[] getValues()
215         {
216             return new String JavaDoc[]{VERSION_1_3};
217         }
218     }
219
220 }
221
Popular Tags