1 package org.andromda.cartridges.ejb3.metafacades;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Iterator;
6 import java.util.List;
7 import org.andromda.cartridges.ejb3.EJB3Globals;
8 import org.andromda.cartridges.ejb3.EJB3Profile;
9 import org.andromda.core.common.ExceptionUtils;
10 import org.andromda.metafacades.uml.AttributeFacade;
11 import org.andromda.metafacades.uml.ClassifierFacade;
12 import org.andromda.metafacades.uml.EntityMetafacadeUtils;
13 import org.andromda.metafacades.uml.ModelElementFacade;
14 import org.andromda.metafacades.uml.OperationFacade;
15 import org.andromda.metafacades.uml.UMLProfile;
16 import org.apache.commons.collections.CollectionUtils;
17 import org.apache.commons.collections.Predicate;
18 import org.apache.commons.lang.StringUtils;
19
20
21
22
23
24
25
26 class EJB3MetafacadeUtils
27 {
28
29
30
31
32
33
34
35 static Collection<OperationFacade> getCreateMethods(
36 ClassifierFacade classifier,
37 boolean follow)
38 {
39 ExceptionUtils.checkNull("classifer", classifier);
40 Collection<OperationFacade> retval = new ArrayList<OperationFacade>();
41 ClassifierFacade entity = classifier;
42 do
43 {
44 for (final OperationFacade op : entity.getOperations())
45 {
46 if (op.hasStereotype(EJB3Profile.STEREOTYPE_CREATE_METHOD))
47 {
48 retval.add(op);
49 }
50 }
51 if (follow)
52 {
53 entity = (ClassifierFacade)entity.getGeneralization();
54 }
55 }
56 while (follow && entity != null);
57 return retval;
58 }
59
60
61
62
63
64
65
66 static String getHomeInterfaceName(ClassifierFacade classifier)
67 {
68 ExceptionUtils.checkNull("classifer", classifier);
69 String homeInterfaceName;
70 if (classifier.hasStereotype(UMLProfile.STEREOTYPE_ENTITY))
71 {
72 homeInterfaceName = classifier.getName() + "LocalHome";
73 }
74 else
75 {
76 homeInterfaceName = classifier.getName() + "Home";
77 }
78 return homeInterfaceName;
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93 static String getViewType(
94 ClassifierFacade classifier,
95 String defaultViewType)
96 {
97 ExceptionUtils.checkNull("classifer", classifier);
98 String viewType = (String)classifier.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_VIEWTYPE);
99 if (StringUtils.isBlank(viewType))
100 {
101 if (classifier.hasStereotype(EJB3Profile.STEREOTYPE_SERVICE))
102 {
103
104 if (StringUtils.isBlank(viewType))
105 {
106 viewType = (String)CollectionUtils.find(
107 classifier.getAllGeneralizations(),
108 new Predicate()
109 {
110 public boolean evaluate(Object object)
111 {
112 return ((ModelElementFacade)object).findTaggedValue(
113 EJB3Profile.TAGGEDVALUE_EJB_VIEWTYPE) != null;
114 }
115 });
116 }
117 if (StringUtils.isBlank(viewType))
118 {
119 viewType = (StringUtils.isNotBlank(defaultViewType) ?
120 defaultViewType : EJB3Globals.VIEW_TYPE_REMOTE);
121 }
122 }
123 }
124 return viewType.toLowerCase();
125 }
126
127
128
129
130
131
132
133
134 static List<AttributeFacade> getInheritedInstanceAttributes(ClassifierFacade classifier)
135 {
136 ExceptionUtils.checkNull("classifer", classifier);
137 ClassifierFacade current = (ClassifierFacade)classifier.getGeneralization();
138 if (current == null)
139 {
140 return new ArrayList<AttributeFacade>();
141 }
142 List<AttributeFacade> retval = getInheritedInstanceAttributes(current);
143
144 if (current.getInstanceAttributes() != null)
145 {
146 retval.addAll(current.getInstanceAttributes());
147 }
148 return retval;
149 }
150
151
152
153
154
155
156
157
158 static List<AttributeFacade> getAllInstanceAttributes(ClassifierFacade classifier)
159 {
160 ExceptionUtils.checkNull("classifer", classifier);
161 List<AttributeFacade> retval = getInheritedInstanceAttributes(classifier);
162 retval.addAll(classifier.getInstanceAttributes());
163 return retval;
164 }
165
166
167
168
169
170
171
172
173
174
175
176 static Collection<AttributeFacade> getEnvironmentEntries(
177 ClassifierFacade classifier,
178 boolean follow)
179 {
180 ExceptionUtils.checkNull("classifer", classifier);
181
182 Collection<AttributeFacade> attributes = classifier.getStaticAttributes();
183
184 if (follow)
185 {
186 for (classifier = (ClassifierFacade)classifier.getGeneralization();
187 classifier != null; classifier = (ClassifierFacade)classifier.getGeneralization())
188 {
189 attributes.addAll(classifier.getStaticAttributes());
190 }
191 }
192
193 CollectionUtils.filter(attributes, new Predicate()
194 {
195 public boolean evaluate(Object object)
196 {
197 return ((AttributeFacade)object).hasStereotype(EJB3Profile.STEREOTYPE_ENV_ENTRY);
198 }
199 });
200
201 return attributes;
202 }
203
204
205
206
207
208
209
210
211 static String getTransactionType(ClassifierFacade classifier, String defaultTransactionType)
212 {
213 ExceptionUtils.checkNull("classifer", classifier);
214
215 String transactionType = (String)classifier.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_TRANSACTION_TYPE);
216 if (StringUtils.isNotBlank(transactionType))
217 {
218 transactionType = convertTransactionType(transactionType);
219 }
220 else
221 {
222 transactionType = defaultTransactionType;
223 }
224 return transactionType;
225 }
226
227
228
229
230
231
232
233
234
235 static String convertTransactionType(String transType)
236 {
237 ExceptionUtils.checkNull("transType", transType);
238
239 String type = null;
240 if (StringUtils.equalsIgnoreCase(transType, EJB3Globals.TRANSACTION_TYPE_MANDATORY))
241 {
242 type = "MANDATORY";
243 }
244 else if (StringUtils.equalsIgnoreCase(transType, EJB3Globals.TRANSACTION_TYPE_NEVER))
245 {
246 type = "NEVER";
247 }
248 else if (StringUtils.equalsIgnoreCase(transType, EJB3Globals.TRANSACTION_TYPE_NOT_SUPPORTED))
249 {
250 type = "NOT_SUPPORTED";
251 }
252 else if (StringUtils.equalsIgnoreCase(transType, EJB3Globals.TRANSACTION_TYPE_REQUIRED))
253 {
254 type = "REQUIRED";
255 }
256 else if (StringUtils.equalsIgnoreCase(transType, EJB3Globals.TRANSACTION_TYPE_REQUIRES_NEW))
257 {
258 type = "REQUIRES_NEW";
259 }
260 else if (StringUtils.equalsIgnoreCase(transType, EJB3Globals.TRANSACTION_TYPE_SUPPORTS))
261 {
262 type = "SUPPORTS";
263 }
264 return type;
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278 static Collection<AttributeFacade> getConstants(
279 ClassifierFacade classifier,
280 boolean follow)
281 {
282 ExceptionUtils.checkNull("classifer", classifier);
283
284 Collection<AttributeFacade> attributes = classifier.getStaticAttributes();
285
286 if (follow)
287 {
288 for (classifier = (ClassifierFacade)classifier.getGeneralization();
289 classifier != null; classifier = (ClassifierFacade)classifier.getGeneralization())
290 {
291 attributes.addAll(classifier.getStaticAttributes());
292 }
293 }
294
295 CollectionUtils.filter(attributes, new Predicate()
296 {
297 public boolean evaluate(Object object)
298 {
299 return !((AttributeFacade)object).hasStereotype(EJB3Profile.STEREOTYPE_ENV_ENTRY);
300 }
301 });
302
303 return attributes;
304 }
305
306
307
308
309
310
311
312 static boolean allowSyntheticCreateMethod(ClassifierFacade classifier)
313 {
314 ExceptionUtils.checkNull("classifer", classifier);
315 return !classifier.isAbstract() && classifier.findTaggedValue(
316 EJB3Profile.TAGGEDVALUE_EJB_NO_SYNTHETIC_CREATE_METHOD) == null;
317 }
318
319
320
321
322
323
324
325
326
327
328 static String getFullyQualifiedName(
329 String packageName,
330 String name,
331 String suffix)
332 {
333 StringBuilder fullyQualifiedName = new StringBuilder(StringUtils.trimToEmpty(packageName));
334 if (StringUtils.isNotBlank(packageName))
335 {
336 fullyQualifiedName.append('.');
337 }
338 fullyQualifiedName.append(StringUtils.trimToEmpty(name));
339 fullyQualifiedName.append(StringUtils.trimToEmpty(suffix));
340 return fullyQualifiedName.toString();
341 }
342
343
344
345
346
347
348
349 static boolean isSeamComponent(ClassifierFacade classifier)
350 {
351 boolean isSeamComponent = false;
352 if (classifier.hasStereotype(EJB3Profile.STEREOTYPE_SEAM_COMPONENT))
353 {
354 isSeamComponent = true;
355 }
356 return isSeamComponent;
357 }
358
359
360
361
362
363
364
365
366
367
368
369 static String getSeamComponentScopeType(ClassifierFacade classifier, boolean stateless)
370 {
371 ExceptionUtils.checkNull("classifer", classifier);
372 String scopeType = (String)classifier.findTaggedValue(EJB3Profile.TAGGEDVALUE_SEAM_SCOPE_TYPE);
373 if (StringUtils.isBlank(scopeType))
374 {
375 if (stateless)
376 {
377 scopeType = EJB3Globals.SEAM_COMPONENT_SCOPE_STATELESS;
378 }
379 else
380 {
381 scopeType = EJB3Globals.SEAM_COMPONENT_SCOPE_CONVERSATION;
382 }
383 }
384 return scopeType;
385 }
386
387
388
389
390
391
392
393
394 static String getSeamComponentName(ClassifierFacade classifier)
395 {
396 ExceptionUtils.checkNull("classifer", classifier);
397 String componentName = (String)classifier.findTaggedValue(EJB3Profile.TAGGEDVALUE_SEAM_COMPONENT_NAME);
398 if (StringUtils.isBlank(componentName))
399 {
400 componentName = StringUtils.uncapitalize(classifier.getName());
401 }
402 return componentName;
403 }
404
405
406
407
408
409
410 static String buildAnnotationParameters(Collection<String> parameters)
411 {
412 StringBuilder buf = new StringBuilder();
413 if(!parameters.isEmpty())
414 {
415 buf.append("(");
416 Iterator it = parameters.iterator();
417 while(it.hasNext())
418 {
419 String option = (String) it.next();
420 buf.append(option);
421 if(it.hasNext())
422 {
423 buf.append(", ");
424 }
425 }
426 buf.append(")");
427 return buf.toString();
428 }
429 else
430 {
431 return null;
432 }
433 }
434
435
436
437
438
439
440
441 static String buildAnnotationMultivalueParameter(String name, Collection<String> values)
442 {
443 return buildAnnotationMultivalueParameter(name, values, true);
444 }
445
446
447
448
449
450
451
452
453 static String buildAnnotationMultivalueParameter(String name, Collection<String> values, boolean areStrings)
454 {
455 return buildAnnotationMultivalueParameter(name, values, areStrings, null);
456 }
457
458
459
460
461
462
463
464
465
466
467 static String buildAnnotationMultivalueParameter(String name, Collection<String> values, boolean areStrings, String suffix)
468 {
469 if(values.isEmpty())
470 {
471 return null;
472 }
473 else
474 {
475 StringBuilder buf = new StringBuilder();
476 buf.append(name + " = {");
477
478 Iterator it = values.iterator();
479 while(it.hasNext())
480 {
481 String parameter = (String) it.next();
482 if(areStrings)
483 {
484 buf.append("\"");
485 }
486 buf.append(parameter);
487 if((suffix != null) && !parameter.endsWith(suffix))
488 {
489 buf.append(suffix);
490 }
491 if(areStrings)
492 {
493 buf.append("\"");
494 }
495 if(it.hasNext())
496 {
497 buf.append(", ");
498 }
499 }
500 buf.append("}");
501 return buf.toString();
502 }
503 }
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522 public static String getSqlNameFromTaggedValue(
523 String prefix,
524 final EJB3AssociationFacade element,
525 String name,
526 final Short nameMaxLength,
527 String suffix,
528 final Object separator,
529 final Object shortenSqlNameMethod)
530 {
531 if (element != null)
532 {
533 Object value = element.findTaggedValue(name);
534 StringBuilder buffer = new StringBuilder(StringUtils.trimToEmpty((String)value));
535 if (StringUtils.isBlank(buffer.toString()))
536 {
537
538
539 buffer = new StringBuilder(
540 EntityMetafacadeUtils.toSqlName(
541 element.getName(),
542 separator));
543
544 suffix = StringUtils.trimToEmpty(suffix);
545 prefix = StringUtils.trimToEmpty(prefix);
546 if (nameMaxLength != null)
547 {
548 final short maxLength = (short)(nameMaxLength.shortValue() - suffix.length() - prefix.length());
549 buffer =
550 new StringBuilder(
551 EntityMetafacadeUtils.ensureMaximumNameLength(
552 buffer.toString(),
553 new Short(maxLength),
554 (String)shortenSqlNameMethod));
555 }
556 if (StringUtils.isNotBlank(prefix))
557 {
558 buffer.insert(
559 0,
560 prefix);
561 }
562 if (StringUtils.isNotBlank(suffix))
563 {
564 buffer.append(suffix);
565 }
566 }
567 name = buffer.toString();
568 }
569 return name;
570 }
571 }