1 package org.andromda.cartridges.spring.metafacades;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.Set;
7 import org.andromda.cartridges.spring.SpringProfile;
8 import org.andromda.metafacades.uml.MetafacadeUtils;
9 import org.andromda.metafacades.uml.ParameterFacade;
10 import org.andromda.metafacades.uml.UMLProfile;
11 import org.andromda.utils.StringUtilsHelper;
12 import org.apache.commons.lang.BooleanUtils;
13 import org.apache.commons.lang.ObjectUtils;
14 import org.apache.commons.lang.StringUtils;
15
16
17
18
19
20
21 public class SpringServiceOperationLogicImpl
22 extends SpringServiceOperationLogic
23 {
24 private static final long serialVersionUID = 34L;
25
26
27
28
29
30
31 public SpringServiceOperationLogicImpl(Object metaObject, String context)
32 {
33 super(metaObject, context);
34 }
35
36
37
38
39
40 protected boolean handleIsWebserviceExposed()
41 {
42 return this.hasStereotype(UMLProfile.STEREOTYPE_WEBSERVICE_OPERATION);
43 }
44
45
46
47
48
49
50 protected String handleGetImplementationName()
51 {
52 return this.getImplementationOperationName(StringUtils.capitalize(this.getName()));
53 }
54
55
56
57
58
59 protected String handleGetImplementationSignature()
60 {
61 String signature = null;
62 if (this.isIncomingMessageOperation())
63 {
64 signature = this.getIncomingMessageImplementationSignature();
65 }
66 else if (this.isOutgoingMessageOperation())
67 {
68 signature = this.getOutgoingMessageImplementationSignature();
69 }
70 else
71 {
72 signature = this.getImplementationOperationName(StringUtils.capitalize(this.getSignature()));
73 }
74 return signature;
75 }
76
77
78
79
80
81
82 public String getCall()
83 {
84 String call = null;
85 if (this.isIncomingMessageOperation() && this.getArguments().isEmpty())
86 {
87 call = this.getName() + "(message)";
88 }
89 else
90 {
91 call = super.getCall();
92 }
93 return call;
94 }
95
96
97
98
99
100
101 public String getSignature(String modifier)
102 {
103 String signature = null;
104 if (this.isIncomingMessageOperation() && this.getArguments().isEmpty())
105 {
106 signature = this.getIncomingMessageSignature(modifier);
107 }
108 else
109 {
110 signature = super.getSignature(modifier);
111 }
112 return signature;
113 }
114
115
116
117
118
119
120 public String getSignature(final boolean withArgumentNames)
121 {
122 String signature = null;
123 if (this.isIncomingMessageOperation() && this.getArguments().isEmpty())
124 {
125 signature = this.getIncomingMessageSignature(null);
126 }
127 else
128 {
129 signature = super.getSignature(withArgumentNames);
130 }
131 return signature;
132 }
133
134
135
136
137
138
139
140 public String getSignature()
141 {
142 return this.getSignature(true);
143 }
144
145
146
147
148
149 protected String handleGetImplementationCall()
150 {
151 return this.getImplementationOperationName(StringUtils.capitalize(this.getCall()));
152 }
153
154
155
156
157
158
159
160
161 private String getImplementationOperationName(String replacement)
162 {
163 return StringUtils.trimToEmpty(String.valueOf(this.getConfiguredProperty(
164 SpringGlobals.IMPLEMENTATION_OPERATION_NAME_PATTERN))).replaceAll("\\{0\\}", replacement);
165 }
166
167
168
169
170 private static final String SERVICE_OPERATION_TRANSACTION_TYPE = "serviceOperationTransactionType";
171
172
173
174
175
176 public String handleGetTransactionType()
177 {
178 String transactionType = (String)this.findTaggedValue(SpringProfile.TAGGEDVALUE_TRANSACTION_TYPE);
179 if (StringUtils.isBlank(transactionType))
180 {
181 transactionType = (String)this.getOwner().findTaggedValue(SpringProfile.TAGGEDVALUE_TRANSACTION_TYPE);
182 }
183 if (StringUtils.isBlank(transactionType))
184 {
185 transactionType = String.valueOf(this.getConfiguredProperty(SERVICE_OPERATION_TRANSACTION_TYPE));
186 }
187 return transactionType;
188 }
189
190
191
192
193 private static final String EJB_SERVICE_OPERATION_TRANSACTION_TYPE = "ejbServiceOperationTransactionType";
194
195
196
197
198
199 protected String handleGetEjbTransactionType()
200 {
201 String transactionType = (String)this.findTaggedValue(SpringProfile.TAGGEDVALUE_EJB_TRANSACTION_TYPE);
202 if (StringUtils.isBlank(transactionType))
203 {
204 transactionType = (String)this.getOwner().findTaggedValue(SpringProfile.TAGGEDVALUE_EJB_TRANSACTION_TYPE);
205 }
206 if (StringUtils.isBlank(transactionType))
207 {
208 transactionType = String.valueOf(this.getConfiguredProperty(EJB_SERVICE_OPERATION_TRANSACTION_TYPE));
209 }
210 return transactionType;
211 }
212
213
214
215
216
217 protected String handleGetThrowsClause()
218 {
219 StringBuilder throwsClause = null;
220 if (this.isExceptionsPresent())
221 {
222 throwsClause = new StringBuilder(this.getExceptionList());
223 }
224 if (throwsClause != null)
225 {
226 throwsClause.insert(0, "throws ");
227 }
228 return throwsClause != null ? throwsClause.toString() : null;
229 }
230
231
232
233
234
235
236 protected String handleGetThrowsClause(String initialExceptions)
237 {
238 final StringBuilder throwsClause = new StringBuilder(initialExceptions);
239 if (this.getThrowsClause() != null)
240 {
241 throwsClause.insert(0, ", ");
242 throwsClause.insert(0, this.getThrowsClause());
243 }
244 else
245 {
246 throwsClause.insert(0, "throws ");
247 }
248 return throwsClause.toString();
249 }
250
251
252
253
254
255 protected String handleGetOutgoingMessageImplementationCall()
256 {
257 return this.getMessageImplementationCall("session");
258 }
259
260 private String getMessageImplementationCall(String firstArgument)
261 {
262 final StringBuilder buffer = new StringBuilder();
263 buffer.append(StringUtils.capitalize(this.getName()));
264 buffer.append('(');
265 final boolean outgoingMessageOperation = this.isOutgoingMessageOperation();
266 if (outgoingMessageOperation || (this.isIncomingMessageOperation() && this.getArguments().isEmpty()))
267 {
268 buffer.append(firstArgument);
269 }
270 final String argumentNames = this.getArgumentNames();
271 if (outgoingMessageOperation && StringUtils.isNotBlank(argumentNames))
272 {
273 buffer.append(", ");
274 }
275 if (StringUtils.isNotBlank(argumentNames))
276 {
277 buffer.append(argumentNames);
278 }
279 buffer.append(')');
280 return this.getImplementationOperationName(buffer.toString());
281 }
282
283
284
285
286
287 protected String handleGetOutgoingMessageImplementationSignature()
288 {
289 return this.getMessagingImplementationSignature("javax.jms.Session session");
290 }
291
292 private String getMessagingImplementationSignature(final String firstArgument)
293 {
294 return this.getMessagingOperationSignature(this.getImplementationName(), firstArgument, null);
295 }
296
297
298
299
300
301
302 private String getIncomingMessageSignature(String modifier)
303 {
304 return this.getMessagingOperationSignature(this.getName(), "javax.jms.Message message", modifier);
305 }
306
307
308
309
310
311
312
313
314
315
316 private String getMessagingOperationSignature(final String operationName, final String firstArgument, final String modifier)
317 {
318 final StringBuilder signature = new StringBuilder(operationName);
319 signature.append('(');
320 if (StringUtils.isNotBlank(modifier))
321 {
322 signature.append(modifier).append(' ');
323 }
324 final Collection<ParameterFacade> arguments = this.getArguments();
325 final boolean outgoingMessageOperation = this.isOutgoingMessageOperation();
326 if (outgoingMessageOperation || (this.isIncomingMessageOperation() && arguments.isEmpty()))
327 {
328 signature.append(firstArgument);
329 }
330 final String argumentList = MetafacadeUtils.getTypedArgumentList(
331 this.getArguments(),
332 true,
333 modifier);
334 if (outgoingMessageOperation && StringUtils.isNotBlank(argumentList))
335 {
336 signature.append(", ");
337 }
338 if (StringUtils.isNotBlank(argumentList))
339 {
340 signature.append(argumentList);
341 }
342 signature.append(')');
343 return signature.toString();
344 }
345
346
347
348
349
350 protected String handleGetIncomingMessageImplementationCall()
351 {
352 return this.getMessageImplementationCall("message");
353 }
354
355
356
357
358
359 protected String handleGetIncomingMessageImplementationSignature()
360 {
361 return this.getMessagingImplementationSignature("javax.jms.Message message");
362 }
363
364
365
366
367
368 protected String handleGetImplementationReturnTypeName()
369 {
370 String returnTypeName = null;
371 if (this.isOutgoingMessageOperation())
372 {
373 returnTypeName = "javax.jms.Message";
374 }
375 else
376 {
377 returnTypeName = getGetterSetterReturnTypeName();
378 }
379 return returnTypeName;
380 }
381
382
383
384
385
386 protected String handleGetFullyQualifiedMessageListenerName()
387 {
388 StringBuilder name = new StringBuilder();
389 final String packageName = this.getPackageName();
390 if (StringUtils.isNotBlank(packageName))
391 {
392 name.append(packageName).append('.');
393 }
394 name.append(this.getMessageListenerName());
395 return name.toString();
396 }
397
398
399
400
401
402 protected String handleGetMessageListenerName()
403 {
404 return this.getOwner().getName() +
405 StringUtilsHelper.upperCamelCaseName(this.getName());
406 }
407
408
409
410
411
412 protected String handleGetMessageListenerBeanName()
413 {
414 return StringUtils.uncapitalize(this.getMessageListenerName());
415 }
416
417
418
419
420
421 protected String handleGetMessageListenerContainerReferenceName()
422 {
423 return this.getName() + MESSAGE_LISTENER_CONTAINER_SUFFIX;
424 }
425
426
427
428
429 private static final String MESSAGE_LISTENER_CONTAINER_SUFFIX = "ListenerContainer";
430
431
432
433
434
435 protected String handleGetMessageListenerContainerBeanName()
436 {
437 return this.getMessageListenerBeanName() + MESSAGE_LISTENER_CONTAINER_SUFFIX;
438 }
439
440
441
442
443
444 protected String handleGetSessionAcknowledgeMode()
445 {
446
447 String mode = null;
448
449
450 Object value = findTaggedValue(SpringProfile.TAGGEDVALUEVALUE_MESSAGING_SESSION_ACKNOWLEDGE_MODE);
451 if (value != null)
452 {
453 mode = ObjectUtils.toString(value);
454 }
455
456 return mode;
457 }
458
459
460
461
462
463 protected boolean handleIsOptimizeAcknowledge()
464 {
465 return BooleanUtils.toBoolean(ObjectUtils.toString(this.findTaggedValue(SpringProfile.TAGGEDVALUEVALUE_ACTIVEMQ_OPTIMIZE_ACKNOWLEDGE)));
466 }
467
468
469
470
471
472 protected boolean handleIsNullMessageConverterRequired()
473 {
474 boolean result = false;
475
476 Collection<ParameterFacade> arguments = getArguments();
477 if (arguments != null && arguments.size() == 1)
478 {
479 ParameterFacade parameter = arguments.iterator().next();
480 String parameterType = parameter.getType().getFullyQualifiedName();
481
482 Set<String> jmsMessageTypes = new HashSet<String>();
483 Collections.addAll(jmsMessageTypes, SpringGlobals.jmsMessageTypes);
484
485 result = jmsMessageTypes.contains(parameterType);
486 }
487
488 return result;
489 }
490
491
492
493
494 protected boolean handleIsInitMethod()
495 {
496 return hasStereotype(SpringProfile.STEREOTYPE_POST_CONSTRUCT_METHOD);
497 }
498
499
500
501
502 protected boolean handleIsDestroyMethod()
503 {
504 return hasStereotype(SpringProfile.STEREOTYPE_PRE_DESTROY_METHOD);
505 }
506 }