1 package org.andromda.cartridges.ejb.metafacades;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6 import org.andromda.cartridges.ejb.EJBProfile;
7 import org.andromda.core.common.ExceptionUtils;
8 import org.andromda.metafacades.uml.AttributeFacade;
9 import org.andromda.metafacades.uml.ClassifierFacade;
10 import org.andromda.metafacades.uml.ModelElementFacade;
11 import org.andromda.metafacades.uml.OperationFacade;
12 import org.andromda.metafacades.uml.UMLProfile;
13 import org.apache.commons.collections.CollectionUtils;
14 import org.apache.commons.collections.Predicate;
15 import org.apache.commons.lang.StringUtils;
16
17
18
19
20
21
22 class EJBMetafacadeUtils
23 {
24
25
26
27
28
29
30 static Collection<OperationFacade> getCreateMethods(ClassifierFacade classifier, boolean follow)
31 {
32 ExceptionUtils.checkNull("classifer", classifier);
33 Collection<OperationFacade> retval = new ArrayList<OperationFacade>();
34 ClassifierFacade entity = classifier;
35 do
36 {
37 for (final OperationFacade op : entity.getOperations())
38 {
39 if (op.hasStereotype(EJBProfile.STEREOTYPE_CREATE_METHOD))
40 {
41 retval.add(op);
42 }
43 }
44 if (follow)
45 {
46 entity = (ClassifierFacade)entity.getGeneralization();
47 }
48 }
49 while (follow && entity != null);
50 return retval;
51 }
52
53
54
55
56
57
58
59 static String getHomeInterfaceName(ClassifierFacade classifier)
60 {
61 ExceptionUtils.checkNull("classifer", classifier);
62 String homeInterfaceName;
63 if (classifier.hasStereotype(UMLProfile.STEREOTYPE_ENTITY))
64 {
65 homeInterfaceName = classifier.getName() + "LocalHome";
66 }
67 else
68 {
69 homeInterfaceName = classifier.getName() + "Home";
70 }
71 return homeInterfaceName;
72 }
73
74
75
76
77
78
79
80 static String getViewType(ClassifierFacade classifier)
81 {
82 ExceptionUtils.checkNull("classifer", classifier);
83 String viewType = "local";
84 if (classifier.hasStereotype(EJBProfile.STEREOTYPE_SERVICE) || classifier.hasStereotype(EJBProfile.STEREOTYPE_SERVICE_ELEMENT))
85 {
86 String viewTypeValue = (String)classifier.findTaggedValue(EJBProfile.TAGGEDVALUE_EJB_VIEWTYPE);
87
88 if (StringUtils.isEmpty(viewTypeValue))
89 {
90 viewType = (String)CollectionUtils.find(classifier.getAllGeneralizations(), new Predicate()
91 {
92 public boolean evaluate(Object object)
93 {
94 return ((ModelElementFacade)object).findTaggedValue(EJBProfile.TAGGEDVALUE_EJB_VIEWTYPE) !=
95 null;
96 }
97 });
98 }
99 if (StringUtils.isNotBlank(viewTypeValue))
100 {
101 viewType = viewTypeValue;
102 }
103 else
104 {
105 viewType = "remote";
106 }
107 }
108 return viewType.toLowerCase();
109 }
110
111
112
113
114
115
116
117 static List getInheritedInstanceAttributes(ClassifierFacade classifier)
118 {
119 ExceptionUtils.checkNull("classifer", classifier);
120 ClassifierFacade current = (ClassifierFacade)classifier.getGeneralization();
121 if (current == null)
122 {
123 return new ArrayList();
124 }
125 List retval = getInheritedInstanceAttributes(current);
126
127 if (current.getInstanceAttributes() != null)
128 {
129 retval.addAll(current.getInstanceAttributes());
130 }
131 return retval;
132 }
133
134
135
136
137
138
139
140
141 static List getAllInstanceAttributes(ClassifierFacade classifier)
142 {
143 ExceptionUtils.checkNull("classifer", classifier);
144 List retval = getInheritedInstanceAttributes(classifier);
145 retval.addAll(classifier.getInstanceAttributes());
146 return retval;
147 }
148
149
150
151
152
153
154
155
156
157
158
159 static Collection getEnvironmentEntries(ClassifierFacade classifier, boolean follow)
160 {
161 ExceptionUtils.checkNull("classifer", classifier);
162
163 Collection attributes = classifier.getStaticAttributes();
164
165 if (follow)
166 {
167 for (classifier = (ClassifierFacade)classifier.getGeneralization();
168 classifier != null; classifier = (ClassifierFacade)classifier.getGeneralization())
169 {
170 attributes.addAll(classifier.getStaticAttributes());
171 }
172 }
173
174 CollectionUtils.filter(attributes, new Predicate()
175 {
176 public boolean evaluate(Object object)
177 {
178 return ((AttributeFacade)object).hasStereotype(EJBProfile.STEREOTYPE_ENV_ENTRY);
179 }
180 });
181
182 return attributes;
183 }
184
185
186
187
188
189
190
191
192
193
194 static Collection getConstants(ClassifierFacade classifier, boolean follow)
195 {
196 ExceptionUtils.checkNull("classifer", classifier);
197
198 Collection attributes = classifier.getStaticAttributes();
199
200 if (follow)
201 {
202 for (classifier = (ClassifierFacade)classifier.getGeneralization();
203 classifier != null; classifier = (ClassifierFacade)classifier.getGeneralization())
204 {
205 attributes.addAll(classifier.getStaticAttributes());
206 }
207 }
208
209 CollectionUtils.filter(attributes, new Predicate()
210 {
211 public boolean evaluate(Object object)
212 {
213 return !((AttributeFacade)object).hasStereotype(EJBProfile.STEREOTYPE_ENV_ENTRY);
214 }
215 });
216
217 return attributes;
218 }
219
220
221
222
223
224
225
226 static boolean allowSyntheticCreateMethod(ClassifierFacade classifier)
227 {
228 ExceptionUtils.checkNull("classifer", classifier);
229 return !classifier.isAbstract() && classifier.findTaggedValue(
230 EJBProfile.TAGGEDVALUE_EJB_NO_SYNTHETIC_CREATE_METHOD) == null;
231 }
232
233 }