KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package xdoclet.modules.ejb.entity;
6
7 import java.util.StringTokenizer JavaDoc;
8
9 import xjavadoc.XClass;
10 import xjavadoc.XTag;
11
12 import xdoclet.XDocletException;
13 import xdoclet.XDocletMessages;
14 import xdoclet.modules.ejb.AbstractEjbCodeGeneratorSubTask;
15 import xdoclet.modules.ejb.XDocletModulesEjbMessages;
16 import xdoclet.tagshandler.PackageTagsHandler;
17
18 import xdoclet.util.Translator;
19
20 /**
21  * Creates "data objects" for Entity EJBs. This task is currently being deprecated in favour of <a
22  * HREF="ValueObjectSubTask.html">Value Object</a> which is more powerful in terms of relationships (1-1, 1-n and n-m).
23  *
24  * @author Ara Abrahamian (ara_e@email.com)
25  * @created Oct 15, 2001
26  * @ant.element display-name="Data Object" name="dataobject" parent="xdoclet.modules.ejb.EjbDocletTask"
27  * @xdoclet.merge-file file="dataobject-custom.xdt" relates-to="{0}Data.java" description="A text file containing
28  * custom template and/or java code to include in the data object class."
29  * @version $Revision: 1.12 $
30  */

31 public class DataObjectSubTask extends AbstractEjbCodeGeneratorSubTask
32 {
33     public final static String JavaDoc DEFAULT_DATAOBJECT_CLASS_PATTERN = "{0}Data";
34
35     protected final static String JavaDoc DEFAULT_TEMPLATE_FILE = "resources/dataobject.xdt";
36
37     /**
38      * A configuration parameter for specifying the data object class name pattern. By default the value is used for
39      * deciding the entity data object class name. {0} in the value mean current class's symbolic name which for an
40      * EJBean is the EJB name.
41      *
42      * @see #getDataObjectClassPattern()
43      */

44     protected String JavaDoc dataObjectClassPattern;
45
46
47     /**
48      * Describe what the DataObjectSubTask constructor does
49      */

50     public DataObjectSubTask()
51     {
52         setTemplateURL(getClass().getResource(DEFAULT_TEMPLATE_FILE));
53         setDestinationFile(getDataObjectClassPattern() + ".java");
54         addOfType("javax.ejb.EntityBean");
55     }
56
57     /**
58      * Returns the configuration parameter for specifying the data object class name pattern. By default the value is
59      * used for deciding the entity data object class name. The placeholder "{0}" in the value means the current class's
60      * symbolic name which for an EJBean is the EJB name. If nothing explicitly specified by user then "{0}Data" is used
61      * by default.
62      *
63      * @return The DataObjectClassPattern value
64      * @see #dataObjectClassPattern
65      */

66     public String JavaDoc getDataObjectClassPattern()
67     {
68         if (dataObjectClassPattern != null) {
69             return dataObjectClassPattern;
70         }
71         else {
72             return DEFAULT_DATAOBJECT_CLASS_PATTERN;
73         }
74     }
75
76     /**
77      * The pattern by which the data object classes are named. The placeholder "{0}" designates the EJB name.
78      *
79      * @param new_pattern The new Pattern value
80      * @ant.not-required No, default is "{0}Data"
81      */

82     public void setPattern(String JavaDoc new_pattern)
83     {
84         dataObjectClassPattern = new_pattern;
85     }
86
87     /**
88      * Called to validate configuration parameters.
89      *
90      * @exception XDocletException
91      */

92     public void validateOptions() throws XDocletException
93     {
94         super.validateOptions();
95
96         if (getDataObjectClassPattern() == null || getDataObjectClassPattern().trim().equals("")) {
97             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"pattern"}));
98         }
99
100         if (getDataObjectClassPattern().indexOf("{0}") == -1) {
101             throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.PATTERN_HAS_NO_PLACEHOLDER));
102         }
103     }
104
105     /**
106      * Gets the GeneratedFileName attribute of the DataObjectSubTask object
107      *
108      * @param clazz Describe what the parameter does
109      * @return The GeneratedFileName value
110      * @exception XDocletException
111      */

112     protected String JavaDoc getGeneratedFileName(XClass clazz) throws XDocletException
113     {
114         return PackageTagsHandler.packageNameAsPathFor(DataObjectTagsHandler.getDataObjectClassFor(getCurrentClass())) + ".java";
115     }
116
117     /**
118      * Describe what the method does
119      *
120      * @param clazz Describe what the parameter does
121      * @return Describe the return value
122      * @exception XDocletException
123      */

124     protected boolean matchesGenerationRules(XClass clazz) throws XDocletException
125     {
126         if (super.matchesGenerationRules(clazz) == false) {
127             return false;
128         }
129
130         if (DataObjectTagsHandler.isGenerationNeeded(getCurrentClass())) {
131             return true;
132         }
133         else {
134             return false;
135         }
136     }
137
138     /**
139      * Describe what the method does
140      *
141      * @param clazz Describe what the parameter does
142      * @exception XDocletException
143      */

144     protected void generateForClass(XClass clazz) throws XDocletException
145     {
146         if (DataObjectTagsHandler.hasCustomBulkData(getCurrentClass())) {
147             // Don't make a new DataObject class; use the custom one instead
148

149             XTag bdo = getCurrentClass().getDoc().getTag("ejb:bulk-data");
150             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(bdo.getValue(), " ");
151
152             DataObjectTagsHandler.setCurrentDataObjectClassname(st.nextToken().trim());
153             DataObjectTagsHandler.putDataObjectClassnames(getCurrentClass().getQualifiedName(), DataObjectTagsHandler.getCurrentDataObjectClassname());
154         }
155         else {
156             DataObjectTagsHandler.setCurrentDataObjectClassname(DataObjectTagsHandler.generateDataObjectClass(clazz));
157             DataObjectTagsHandler.putDataObjectClassnames(getCurrentClass().getQualifiedName(), DataObjectTagsHandler.getCurrentDataObjectClassname());
158         }
159
160         super.generateForClass(clazz);
161     }
162
163     /**
164      * Describe what the method does
165      *
166      * @exception XDocletException
167      */

168     protected void engineStarted() throws XDocletException
169     {
170         System.out.println(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.GENERATING_DATAOBJECT_FOR,
171             new String JavaDoc[]{getCurrentClass().getQualifiedName()}));
172     }
173 }
174
Popular Tags