1 package org.andromda.cartridges.ejb.metafacades;
2
3 import org.andromda.cartridges.ejb.EJBProfile;
4 import org.andromda.core.common.ExceptionUtils;
5 import org.andromda.metafacades.uml.AttributeFacade;
6 import org.andromda.metafacades.uml.ClassifierFacade;
7 import org.andromda.metafacades.uml.ModelElementFacade;
8 import org.andromda.metafacades.uml.OperationFacade;
9 import org.andromda.metafacades.uml.UMLProfile;
10 import org.apache.commons.collections.CollectionUtils;
11 import org.apache.commons.collections.Predicate;
12 import org.apache.commons.lang.StringUtils;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import java.util.List;
18
19 /***
20 * Contains utilities for use with EJB metafacades.
21 *
22 * @author Chad Brandon
23 */
24 class EJBMetafacadeUtils
25 {
26
27 /***
28 * Gets all create methods for the given <code>classifier</code>.
29 *
30 * @param follow if true, all super type create methods are also retrieved
31 * @return Collection of create methods found.
32 */
33 static Collection getCreateMethods(ClassifierFacade classifier, boolean follow)
34 {
35 ExceptionUtils.checkNull("classifer", classifier);
36 Collection retval = new ArrayList();
37 ClassifierFacade entity = classifier;
38 do
39 {
40 Collection ops = entity.getOperations();
41 for (final Iterator i = ops.iterator(); i.hasNext();)
42 {
43 OperationFacade op = (OperationFacade)i.next();
44 if (op.hasStereotype(EJBProfile.STEREOTYPE_CREATE_METHOD))
45 {
46 retval.add(op);
47 }
48 }
49 if (follow)
50 {
51 entity = (ClassifierFacade)entity.getGeneralization();
52 }
53 }
54 while (follow && entity != null);
55 return retval;
56 }
57
58 /***
59 * Gets the interface name for the passed in <code>classifier</code>. Returns 'LocalHome' if the mode element has
60 * the entity stereotype, returns 'Home' otherwise.
61 *
62 * @return the interface name.
63 */
64 static String getHomeInterfaceName(ClassifierFacade classifier)
65 {
66 ExceptionUtils.checkNull("classifer", classifier);
67 String homeInterfaceName;
68 if (classifier.hasStereotype(UMLProfile.STEREOTYPE_ENTITY))
69 {
70 homeInterfaceName = classifier.getName() + "LocalHome";
71 }
72 else
73 {
74 homeInterfaceName = classifier.getName() + "Home";
75 }
76 return homeInterfaceName;
77 }
78
79 /***
80 * Gets the view type for the passed in <code>classifier</code>. Returns 'local' if the model element has the entity
81 * stereotype, others checks there ejb tagged value and if there is no value defined, returns 'remote'.
82 *
83 * @return String the view type name.
84 */
85 static String getViewType(ClassifierFacade classifier)
86 {
87 ExceptionUtils.checkNull("classifer", classifier);
88 String viewType = "local";
89 if (classifier.hasStereotype(EJBProfile.STEREOTYPE_SERVICE))
90 {
91 String viewTypeValue = (String)classifier.findTaggedValue(EJBProfile.TAGGEDVALUE_EJB_VIEWTYPE);
92
93 if (StringUtils.isEmpty(viewTypeValue))
94 {
95 viewType = (String)CollectionUtils.find(classifier.getAllGeneralizations(), new Predicate()
96 {
97 public boolean evaluate(Object object)
98 {
99 return ((ModelElementFacade)object).findTaggedValue(EJBProfile.TAGGEDVALUE_EJB_VIEWTYPE) !=
100 null;
101 }
102 });
103 }
104 if (StringUtils.isNotEmpty(viewTypeValue))
105 {
106 viewType = viewTypeValue;
107 }
108 else
109 {
110 viewType = "remote";
111 }
112 }
113 return viewType.toLowerCase();
114 }
115
116 /***
117 * Gets all the inherited instance attributes, excluding the instance attributes directory from this
118 * <code>classifier</code>.
119 *
120 * @param classifer the ClassifierFacade from which to retrieve the inherited attributes.
121 * @return a list of ordered attributes.
122 */
123 static List getInheritedInstanceAttributes(ClassifierFacade classifier)
124 {
125 ExceptionUtils.checkNull("classifer", classifier);
126 ClassifierFacade current = (ClassifierFacade)classifier.getGeneralization();
127 if (current == null)
128 {
129 return new ArrayList();
130 }
131 List retval = getInheritedInstanceAttributes(current);
132
133 if (current.getInstanceAttributes() != null)
134 {
135 retval.addAll(current.getInstanceAttributes());
136 }
137 return retval;
138 }
139
140 /***
141 * Gets all instance attributes including those instance attributes belonging to the <code>classifier</code> and any
142 * inherited ones.
143 *
144 * @param classifier the ClassifierFacade from which to retrieve the instance attributes.
145 * @return the list of all instance attributes.
146 */
147 static List getAllInstanceAttributes(ClassifierFacade classifier)
148 {
149 ExceptionUtils.checkNull("classifer", classifier);
150 List retval = getInheritedInstanceAttributes(classifier);
151 retval.addAll(classifier.getInstanceAttributes());
152 return retval;
153 }
154
155 /***
156 * Gets all environment entries for the specified <code>classifier</code>. If <code>follow</code> is true, then a
157 * search up the inheritance hierachy will be performed and all super type environment entries will also be
158 * retrieved.
159 *
160 * @param classifier the classifier from which to retrieve the env-entries
161 * @param follow true/false on whether or not to 'follow' the inheritance hierarchy when retrieving the
162 * env-entries.
163 * @return the collection of enviroment entries
164 */
165 static Collection getEnvironmentEntries(ClassifierFacade classifier, boolean follow)
166 {
167 ExceptionUtils.checkNull("classifer", classifier);
168
169 Collection attributes = classifier.getStaticAttributes();
170
171 if (follow)
172 {
173 for (classifier = (ClassifierFacade)classifier.getGeneralization();
174 classifier != null; classifier = (ClassifierFacade)classifier.getGeneralization())
175 {
176 attributes.addAll(classifier.getStaticAttributes());
177 }
178 }
179
180 CollectionUtils.filter(attributes, new Predicate()
181 {
182 public boolean evaluate(Object object)
183 {
184 return ((AttributeFacade)object).hasStereotype(EJBProfile.STEREOTYPE_ENV_ENTRY);
185 }
186 });
187
188 return attributes;
189 }
190
191 /***
192 * Gets all constants for the specified <code>classifier</code>. If <code>follow</code> is true, then a search up
193 * the inheritance hierachy will be performed and all super type constants will also be retrieved.
194 *
195 * @param classifier the classifier from which to retrieve the constants
196 * @param follow true/false on whether or not to 'follow' the inheritance hierarchy when retrieving the
197 * constants.
198 * @return the collection of enviroment entries
199 */
200 static Collection getConstants(ClassifierFacade classifier, boolean follow)
201 {
202 ExceptionUtils.checkNull("classifer", classifier);
203
204 Collection attributes = classifier.getStaticAttributes();
205
206 if (follow)
207 {
208 for (classifier = (ClassifierFacade)classifier.getGeneralization();
209 classifier != null; classifier = (ClassifierFacade)classifier.getGeneralization())
210 {
211 attributes.addAll(classifier.getStaticAttributes());
212 }
213 }
214
215 CollectionUtils.filter(attributes, new Predicate()
216 {
217 public boolean evaluate(Object object)
218 {
219 return !((AttributeFacade)object).hasStereotype(EJBProfile.STEREOTYPE_ENV_ENTRY);
220 }
221 });
222
223 return attributes;
224 }
225
226 /***
227 * Returns true/false based on whether or not synthetic or auto generated create methods should be allowed.
228 *
229 * @param classifier the entity or session EJB.
230 * @return true/false
231 */
232 static boolean allowSyntheticCreateMethod(ClassifierFacade classifier)
233 {
234 ExceptionUtils.checkNull("classifer", classifier);
235 return !classifier.isAbstract() && classifier.findTaggedValue(
236 EJBProfile.TAGGEDVALUE_EJB_NO_SYNTHETIC_CREATE_METHOD) == null;
237 }
238
239 }