1 package org.andromda.cartridges.ejb.metafacades;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.Iterator;
7 import java.util.List;
8
9 import org.andromda.cartridges.ejb.EJBGlobals;
10 import org.andromda.cartridges.ejb.EJBProfile;
11 import org.andromda.core.common.ExceptionRecorder;
12 import org.andromda.metafacades.uml.AttributeFacade;
13 import org.andromda.metafacades.uml.ClassifierFacade;
14 import org.andromda.metafacades.uml.DependencyFacade;
15 import org.andromda.metafacades.uml.MetafacadeUtils;
16 import org.andromda.metafacades.uml.OperationFacade;
17 import org.andromda.metafacades.uml.TypeMappings;
18 import org.andromda.metafacades.uml.UMLMetafacadeProperties;
19 import org.apache.commons.collections.CollectionUtils;
20 import org.apache.commons.collections.Predicate;
21 import org.apache.commons.lang.StringUtils;
22
23
24 /**
25 * <p>
26 * Represents an entity EJB. </p> Metaclass facade implementation.
27 */
28 public class EJBEntityFacadeLogicImpl
29 extends EJBEntityFacadeLogic
30 {
31
32 public EJBEntityFacadeLogicImpl(
33 java.lang.Object metaObject,
34 String context)
35 {
36 super(metaObject, context);
37 }
38
39 public Collection handleGetIdentifiers()
40 {
41 Collection identifiers = new ArrayList();
42 Iterator iter = this.getSourceDependencies().iterator();
43 while (iter.hasNext())
44 {
45 DependencyFacade dep = (DependencyFacade)iter.next();
46 if (dep.hasStereotype(EJBProfile.STEREOTYPE_IDENTIFIER))
47 {
48 identifiers = ((ClassifierFacade)dep.getTargetElement()).getInstanceAttributes();
49 MetafacadeUtils.filterByStereotype(
50 identifiers,
51 EJBProfile.STEREOTYPE_IDENTIFIER);
52 return identifiers;
53 }
54 }
55
56
57 if (super.getIdentifiers() != null && !super.getIdentifiers().isEmpty())
58 {
59 AttributeFacade attr = (AttributeFacade)super.getIdentifiers().iterator().next();
60 identifiers.add(attr);
61 return identifiers;
62 }
63
64
65 EJBEntityFacade decorator = (EJBEntityFacade)this.getGeneralization();
66 return decorator.getIdentifiers();
67 }
68
69 /**
70 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getAllEntityRelations()
71 */
72 protected java.util.Collection handleGetAllEntityRelations()
73 {
74
75
76 if (this.isAbstract())
77 {
78 return Collections.EMPTY_LIST;
79 }
80 Collection result = new ArrayList();
81 result.addAll(getEntityRelations());
82 ClassifierFacade classifier = (ClassifierFacade)this.getGeneralization();
83 while (classifier != null && classifier instanceof EJBEntityFacade && classifier.isAbstract())
84 {
85 EJBEntityFacade entity = (EJBEntityFacade)classifier;
86 result.addAll(entity.getEntityRelations());
87 classifier = (ClassifierFacade)classifier.getGeneralization();
88 }
89 return result;
90 }
91
92 /**
93 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getViewType()
94 */
95 protected String handleGetViewType()
96 {
97 return EJBMetafacadeUtils.getViewType(this);
98 }
99
100 /**
101 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getEntityRelations()
102 */
103 protected java.util.Collection handleGetEntityRelations()
104 {
105 Collection result = new ArrayList();
106 for (final Iterator iterator = this.getAssociationEnds().iterator(); iterator.hasNext();)
107 {
108 EJBAssociationEndFacade associationEnd = (EJBAssociationEndFacade)iterator.next();
109 ClassifierFacade target = associationEnd.getOtherEnd().getType();
110 if (target instanceof EJBEntityFacade && associationEnd.getOtherEnd().isNavigable())
111 {
112
113 Object value =
114 associationEnd.getOtherEnd().getAssociation().findTaggedValue(EJBProfile.TAGGEDVALUE_GENERATE_CMR);
115 String generateCmr = value == null ? null : value.toString();
116 if (target.isAbstract() && !"false".equalsIgnoreCase(generateCmr))
117 {
118 throw new IllegalStateException("Relation '" + associationEnd.getAssociation().getName() +
119 "' has the abstract target '" + target.getName() +
120 "'. Abstract targets are not allowed in EJB.");
121 }
122 result.add(associationEnd);
123 }
124 }
125
126 return result;
127 }
128
129 /**
130 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getAllInstanceAttributes()
131 */
132 protected List handleGetAllInstanceAttributes()
133 {
134 return EJBMetafacadeUtils.getAllInstanceAttributes(this);
135 }
136
137 /**
138 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getInheritedInstanceAttributes()
139 */
140 protected List handleGetInheritedInstanceAttributes()
141 {
142 return EJBMetafacadeUtils.getInheritedInstanceAttributes(this);
143 }
144
145 /**
146 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getCreateMethods(boolean)
147 */
148 protected Collection handleGetCreateMethods(boolean follow)
149 {
150 return EJBMetafacadeUtils.getCreateMethods(
151 this,
152 follow);
153 }
154
155 /**
156 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getSelectMethods(boolean)
157 */
158 protected Collection handleGetSelectMethods(boolean follow)
159 {
160 Collection retval = new ArrayList();
161 EJBEntityFacade entity = null;
162 do
163 {
164 Collection ops = this.getOperations();
165 for (final Iterator i = ops.iterator(); i.hasNext();)
166 {
167 OperationFacade op = (OperationFacade)i.next();
168 if (op.hasStereotype(EJBProfile.STEREOTYPE_SELECT_METHOD))
169 {
170 retval.add(op);
171 }
172 }
173 if (follow)
174 {
175 entity = (EJBEntityFacade)this.getGeneralization();
176 }
177 else
178 {
179 break;
180 }
181 }
182 while (entity != null);
183 return retval;
184 }
185
186 /**
187 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getHomeInterfaceName()
188 */
189 protected String handleGetHomeInterfaceName()
190 {
191 return EJBMetafacadeUtils.getHomeInterfaceName(this);
192 }
193
194 /**
195 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getEnvironmentEntries(boolean)
196 */
197 protected Collection handleGetEnvironmentEntries(boolean follow)
198 {
199 return EJBMetafacadeUtils.getEnvironmentEntries(
200 this,
201 follow);
202 }
203
204 /**
205 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getConstants(boolean)
206 */
207 protected Collection handleGetConstants(boolean follow)
208 {
209 return EJBMetafacadeUtils.getConstants(
210 this,
211 follow);
212 }
213
214 /**
215 * @see org.andromda.cartridges.ejb.metafacades.EJBEntity#getJndiName()
216 */
217 protected java.lang.String handleGetJndiName()
218 {
219 StringBuffer jndiName = new StringBuffer();
220 String jndiNamePrefix = StringUtils.trimToEmpty(this.getJndiNamePrefix());
221 if (StringUtils.isNotEmpty(jndiNamePrefix))
222 {
223 jndiName.append(jndiNamePrefix);
224 jndiName.append("/");
225 }
226 jndiName.append("ejb/");
227 jndiName.append(this.getFullyQualifiedName());
228 return jndiName.toString();
229 }
230
231 /**
232 * Gets the <code>jndiNamePrefix</code> for this EJB.
233 *
234 * @return the EJB Jndi name prefix.
235 */
236 protected String getJndiNamePrefix()
237 {
238 String prefix = null;
239 if (this.isConfiguredProperty(EJBGlobals.JNDI_NAME_PREFIX))
240 {
241 prefix = (String)this.getConfiguredProperty(EJBGlobals.JNDI_NAME_PREFIX);
242 }
243 return prefix;
244 }
245
246 /**
247 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#allowSyntheticCreateMethod()
248 */
249 protected boolean handleIsSyntheticCreateMethodAllowed()
250 {
251 return EJBMetafacadeUtils.allowSyntheticCreateMethod(this);
252 }
253
254 /**
255 * @see org.andromda.metafacades.uml.EntityFacade#getBusinessOperations()
256 */
257 public Collection getBusinessOperations()
258 {
259 Collection operations = super.getBusinessOperations();
260 CollectionUtils.filter(
261 operations,
262 new Predicate()
263 {
264 public boolean evaluate(Object object)
265 {
266 boolean businessOperation = false;
267 if (EJBOperationFacade.class.isAssignableFrom(object.getClass()))
268 {
269 businessOperation = ((EJBOperationFacade)object).isBusinessOperation();
270 }
271 return businessOperation;
272 }
273 });
274 return operations;
275 }
276
277 /**
278 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getValueDependencies()
279 */
280 protected Collection handleGetValueDependencies()
281 {
282 Collection dependencies = super.getSourceDependencies();
283 CollectionUtils.filter(
284 dependencies,
285 new Predicate()
286 {
287 public boolean evaluate(Object object)
288 {
289 boolean isValueRef = false;
290 if (object instanceof DependencyFacade)
291 {
292 DependencyFacade dep = (DependencyFacade)object;
293 isValueRef =
294 dep.getStereotypeNames().contains(EJBProfile.STEREOTYPE_VALUE_REF) &&
295 dep.getTargetElement().hasExactStereotype(EJBProfile.STEREOTYPE_VALUE_OBJECT);
296 }
297 return isValueRef;
298 }
299 });
300 return dependencies;
301 }
302
303 /**
304 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#containsIdentifier(java.lang.String)
305 */
306 protected boolean handleIsIdentifierPresent(String identifier)
307 {
308 Collection collIdentifier = this.getIdentifiers(true);
309 Iterator it = collIdentifier.iterator();
310 while (it.hasNext())
311 {
312 AttributeFacade attr = (AttributeFacade)it.next();
313 if (attr.getName().equalsIgnoreCase(identifier))
314 {
315 return true;
316 }
317 }
318 return false;
319 }
320
321 /**
322 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#containsAttribute(java.lang.String)
323 */
324 protected boolean handleIsAttributePresent(String strAttr)
325 {
326 Collection collAttrib = this.getAttributes(true);
327 Iterator it = collAttrib.iterator();
328 while (it.hasNext())
329 {
330 AttributeFacade attr = (AttributeFacade)it.next();
331 if (attr.getName().equalsIgnoreCase(strAttr))
332 {
333 return true;
334 }
335 }
336 return false;
337 }
338
339 /**
340 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#containsOperation(java.lang.String)
341 */
342 protected boolean handleIsOperationPresent(String op)
343 {
344 Collection collOps = this.getOperations();
345 Iterator it = collOps.iterator();
346 while (it.hasNext())
347 {
348 OperationFacade operation = (OperationFacade)it.next();
349 if (operation.getName().equalsIgnoreCase(op))
350 {
351 return true;
352 }
353 }
354 return false;
355 }
356
357 /**
358 * Gets a Mappings instance from a property registered under the given <code>propertyName</code>.
359 *
360 * @param propertyName the property name to register under.
361 * @return the Mappings instance.
362 */
363 private TypeMappings getMappingsProperty(final String propertyName)
364 {
365 Object property = this.getConfiguredProperty(propertyName);
366 TypeMappings mappings = null;
367 String uri = null;
368 if (property instanceof String)
369 {
370 uri = (String)property;
371 try
372 {
373 mappings = TypeMappings.getInstance(uri);
374 this.setProperty(
375 propertyName,
376 mappings);
377 }
378 catch (Throwable th)
379 {
380 String errMsg = "Error getting '" + propertyName + "' --> '" + uri + "'";
381 logger.error(errMsg);
382
383 ExceptionRecorder.instance().record(
384 errMsg,
385 th);
386 }
387 }
388 else
389 {
390 mappings = (TypeMappings)property;
391 }
392 return mappings;
393 }
394
395 /**
396 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getSqlType()
397 */
398 protected String handleGetSqlType()
399 {
400 String mpSql = this.getMappingsProperty(UMLMetafacadeProperties.SQL_MAPPINGS_URI).getMappings().getName();
401 if (mpSql.startsWith("Oracle"))
402 {
403 mpSql = "ORACLE";
404 }
405 return mpSql;
406 }
407
408 /**
409 * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getTransactionType()
410 */
411 protected java.lang.String handleGetTransactionType()
412 {
413 String transactionType = (String)this.findTaggedValue(EJBProfile.TAGGEDVALUE_EJB_TRANSACTION_TYPE);
414 if (StringUtils.isBlank(transactionType))
415 {
416 transactionType = String.valueOf(this.getConfiguredProperty(EJBGlobals.TRANSACTION_TYPE));
417 }
418 return transactionType;
419 }
420 }