KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package xdoclet.modules.ejb.entity;
6
7 import org.apache.commons.logging.Log;
8
9 import xjavadoc.XClass;
10
11 import xdoclet.XDocletException;
12 import xdoclet.XDocletMessages;
13 import xdoclet.modules.ejb.AbstractEjbCodeGeneratorSubTask;
14 import xdoclet.modules.ejb.XDocletModulesEjbMessages;
15 import xdoclet.tagshandler.PackageTagsHandler;
16
17 import xdoclet.util.LogUtil;
18 import xdoclet.util.Translator;
19
20 /**
21  * Generates primary key classes for entity EJBs.
22  *
23  * @author Ara Abrahamian (ara_e@email.com)
24  * @created Oct 15, 2001
25  * @ant.element display-name="PK Class" name="entitypk" parent="xdoclet.modules.ejb.EjbDocletTask"
26  * @version $Revision: 1.11 $
27  * @xdoclet.merge-file file="entitypk-custom.xdt" relates-to="{0}PK.java" description="A text file containing custom
28  * template and/or java code to include in the primary key class."
29  */

30 public class EntityPkSubTask extends AbstractEjbCodeGeneratorSubTask
31 {
32     public final static String JavaDoc DEFAULT_ENTITY_PK_CLASS_PATTERN = "{0}PK";
33
34     protected final static String JavaDoc DEFAULT_TEMPLATE_FILE = "resources/entitypk.xdt";
35
36     /**
37      * A configuration parameter for specifying the entity bean primary class name pattern. By default the value is used
38      * for deciding the entity bean primary class name. The placeholder "{0}" in the value means the current class's
39      * symbolic name which for an EJBean is the EJB name.
40      *
41      * @see #getEntityPkClassPattern()
42      */

43     protected String JavaDoc entityPkClassPattern;
44
45     /**
46      * Describe what the EntityPkSubTask constructor does
47      */

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

64     public String JavaDoc getEntityPkClassPattern()
65     {
66         if (entityPkClassPattern != null) {
67             return entityPkClassPattern;
68         }
69         else {
70             return DEFAULT_ENTITY_PK_CLASS_PATTERN;
71         }
72     }
73
74     /**
75      * The pattern by which the primary key classes are named. The placeholder "{0}" designates the EJB name.
76      *
77      * @param new_pattern The new Pattern value
78      * @ant.not-required No, defaults to {0}PK
79      */

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

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

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

122     protected boolean matchesGenerationRules(XClass clazz) throws XDocletException
123     {
124         if (super.matchesGenerationRules(clazz) == false) {
125             return false;
126         }
127
128         Log log = LogUtil.getLog(EntityPkSubTask.class, "matchesGenerationRules");
129
130         if (log.isDebugEnabled()) {
131             log.debug("clazz=" + clazz);
132         }
133
134         if ("false".equalsIgnoreCase(clazz.getDoc().getTagAttributeValue("ejb:pk", "generate", false))) {
135             log.debug("Skip primary key for " + clazz.getQualifiedName() + " because of false generate flag");
136             return false;
137         }
138
139         if (PkTagsHandler.classHasPrimkeyField(clazz)) {
140             log.debug("Skip primary key for " + clazz.getQualifiedName() + " because it has a PK Field.");
141             return false;
142         }
143
144         return true;
145     }
146
147     /**
148      * Describe what the method does
149      *
150      * @exception XDocletException
151      */

152     protected void engineStarted() throws XDocletException
153     {
154         System.out.println(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.GENERATING_PK_FOR,
155             new String JavaDoc[]{getCurrentClass().getQualifiedName()}));
156     }
157
158 }
159
Popular Tags