001package org.andromda.cartridges.ejb3.metafacades;
002
003import java.text.MessageFormat;
004import java.util.Collection;
005import java.util.Iterator;
006import java.util.LinkedHashSet;
007import org.andromda.cartridges.ejb3.EJB3Globals;
008import org.andromda.cartridges.ejb3.EJB3Profile;
009import org.andromda.metafacades.uml.AttributeFacade;
010import org.andromda.metafacades.uml.DependencyFacade;
011import org.andromda.metafacades.uml.ModelElementFacade;
012import org.andromda.metafacades.uml.Role;
013import org.apache.commons.collections.Closure;
014import org.apache.commons.collections.CollectionUtils;
015import org.apache.commons.collections.Predicate;
016import org.apache.commons.collections.Transformer;
017import org.apache.commons.lang.BooleanUtils;
018import org.apache.commons.lang.StringUtils;
019import org.apache.commons.lang.math.NumberUtils;
020
021/**
022 * MetafacadeLogic implementation for org.andromda.cartridges.ejb3.metafacades.EJB3MessageDrivenFacade.
023 *
024 * @see EJB3MessageDrivenFacade
025 */
026public class EJB3MessageDrivenFacadeLogicImpl
027    extends EJB3MessageDrivenFacadeLogic
028{
029    private static final long serialVersionUID = 34L;
030    /**
031     * The property which stores the default destination type
032     */
033    public static final String MESSAGE_DRIVEN_DESTINATION_TYPE = "messageDrivenDestinationType";
034
035    /**
036     * The property which stores the default subscription durability for a Topic
037     */
038    public static final String MESSAGE_DRIVEN_TOPIC_SUBSCRIPTION_DURABILITY =
039        "messageDrivenTopicSubscriptionDurability";
040
041    /**
042     * The property which stores the pattern defining the JMS message driven bean name.
043     */
044    public static final String MESSAGE_DRIVEN_NAME_PATTERN = "messageDrivenNamePattern";
045
046    /**
047     * The property which stores the pattern defining the JMS message driven bean listener callback name.
048     */
049    private static final String MESSAGE_DRIVEN_LISTENER_NAME_PATTERN = "messageDrivenListenerNamePattern";
050
051    /**
052     * The property which stores the pattern defining the JMS message driven bean implementation name.
053     */
054    private static final String MESSAGE_DRIVEN_IMPLEMENTATION_NAME_PATTERN = "messageDrivenImplementationNamePattern";
055
056    /**
057     * The property which stores the pattern defining the JMS message driven bean test class name
058     */
059    private static final String MESSAGE_DRIVEN_TEST_NAME_PATTERN = "messageDrivenTestNamePattern";
060
061    /**
062     * The property which stores the pattern defining the JMS message driven bean test package
063     */
064    private static final String MESSAGE_DRIVEN_TEST_PACKAGE_NAME_PATTERN = "messageDrivenTestPackageNamePattern";
065
066    /**
067     * The property which stores the pattern defining the JMS durable subscription ID
068     */
069    private static final String MESSAGE_DRIVEN_DURABLE_SUBSCRIPTION_ID_PATTERN =
070        "messageDrivenDurableSubscriptionIdPattern";
071
072    //  ---------------- constructor -------------------------------
073
074    /**
075     * @param metaObject
076     * @param context
077     */
078    public EJB3MessageDrivenFacadeLogicImpl(final Object metaObject, final String context)
079    {
080        super (metaObject, context);
081    }
082
083    //  ---------------- methods -------------------------------
084
085    /**
086     * @see EJB3MessageDrivenFacade#getAcknowledgeMode()
087     */
088    @Override
089    protected String handleGetAcknowledgeMode()
090    {
091        return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_ACKNOWLEDGE_MODE);
092    }
093
094    /**
095     * @see EJB3MessageDrivenFacade#getDestination()
096     */
097    @Override
098    protected String handleGetDestination()
099    {
100        String destination = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_DESTINATION);
101        if (StringUtils.isBlank(destination))
102        {
103            destination = (getDestinationType().equalsIgnoreCase(EJB3Globals.MDB_DESTINATION_TYPE_TOPIC) ?
104                    "topic/" : "queue/") + getMessageDrivenName();
105        }
106        return destination;
107    }
108
109    /**
110     * @see EJB3MessageDrivenFacade#getDestinationType()
111     */
112    @Override
113    protected String handleGetDestinationType()
114    {
115        String destinationType = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_DESTINATION_TYPE);
116        if (StringUtils.isBlank(destinationType))
117        {
118            destinationType = (String)this.getConfiguredProperty(MESSAGE_DRIVEN_DESTINATION_TYPE);
119        }
120
121        /**
122         * Formatting required to replace '_' with '.' and return correct casing
123         */
124        destinationType = StringUtils.replaceChars(destinationType, '_', '.');
125        if (destinationType.equalsIgnoreCase(EJB3Globals.MDB_DESTINATION_TYPE_QUEUE))
126        {
127            destinationType = EJB3Globals.MDB_DESTINATION_TYPE_QUEUE;
128        }
129        else if (destinationType.equalsIgnoreCase(EJB3Globals.MDB_DESTINATION_TYPE_TOPIC))
130        {
131            destinationType = EJB3Globals.MDB_DESTINATION_TYPE_TOPIC;
132        }
133        return destinationType;
134    }
135
136    /**
137     * @see EJB3MessageDrivenFacadeLogic#handleGetTestPackageName()
138     */
139    @Override
140    protected String handleGetTestPackageName()
141    {
142        String namespacePattern = String.valueOf(this.getConfiguredProperty(MESSAGE_DRIVEN_TEST_PACKAGE_NAME_PATTERN));
143        return MessageFormat.format(
144                namespacePattern,
145                this.getPackageName());
146    }
147
148    /**
149     * @see EJB3MessageDrivenFacade#getFullyQualifiedMessageDrivenImplementationName()
150     */
151    @Override
152    protected String handleGetFullyQualifiedMessageDrivenImplementationName()
153    {
154        return EJB3MetafacadeUtils.getFullyQualifiedName(
155                this.getPackageName(),
156                this.getMessageDrivenImplementationName(),
157                null);
158    }
159
160    /**
161     * @see EJB3MessageDrivenFacade#getFullyQualifiedMessageDrivenListenerName()
162     */
163    @Override
164    protected String handleGetFullyQualifiedMessageDrivenListenerName()
165    {
166        return EJB3MetafacadeUtils.getFullyQualifiedName(
167                this.getPackageName(),
168                this.getMessageDrivenListenerName(),
169                null);
170    }
171
172    /**
173     * @see EJB3MessageDrivenFacade#getFullyQualifiedMessageDrivenName()
174     */
175    @Override
176    protected String handleGetFullyQualifiedMessageDrivenName()
177    {
178        return EJB3MetafacadeUtils.getFullyQualifiedName(
179                this.getPackageName(),
180                this.getMessageDrivenName(),
181                null);
182    }
183
184    /**
185     * @see EJB3MessageDrivenFacadeLogic#handleGetFullyQualifiedMessageDrivenTestName()
186     */
187    @Override
188    protected String handleGetFullyQualifiedMessageDrivenTestName()
189    {
190        return EJB3MetafacadeUtils.getFullyQualifiedName(
191                this.getTestPackageName(),
192                this.getMessageDrivenTestName(),
193                null);
194    }
195
196    /**
197     * @see EJB3MessageDrivenFacade#getMessageDrivenImplementationName()
198     */
199    @Override
200    protected String handleGetMessageDrivenImplementationName()
201    {
202        String messageDrivenImplNamePattern =
203            (String)this.getConfiguredProperty(MESSAGE_DRIVEN_IMPLEMENTATION_NAME_PATTERN);
204
205        return MessageFormat.format(
206                messageDrivenImplNamePattern,
207                StringUtils.trimToEmpty(this.getName()));
208    }
209
210    /**
211     * @see EJB3MessageDrivenFacade#getMessageDrivenListenerName()
212     */
213    @Override
214    protected String handleGetMessageDrivenListenerName()
215    {
216        String messageDrivenListenerNamePattern =
217            (String)this.getConfiguredProperty(MESSAGE_DRIVEN_LISTENER_NAME_PATTERN);
218
219        return MessageFormat.format(
220                messageDrivenListenerNamePattern,
221                StringUtils.trimToEmpty(this.getName()));
222    }
223
224    /**
225     * @see EJB3MessageDrivenFacade#getMessageDrivenName()
226     */
227    @Override
228    protected String handleGetMessageDrivenName()
229    {
230        String messageDrivenNamePattern =
231            (String)this.getConfiguredProperty(MESSAGE_DRIVEN_NAME_PATTERN);
232
233        return MessageFormat.format(
234                messageDrivenNamePattern,
235                StringUtils.trimToEmpty(this.getName()));
236    }
237
238    /**
239     * @see EJB3MessageDrivenFacadeLogic#handleGetMessageDrivenTestName()
240     */
241    @Override
242    protected String handleGetMessageDrivenTestName()
243    {
244        String messageDrivenTestNamePattern =
245            (String)this.getConfiguredProperty(MESSAGE_DRIVEN_TEST_NAME_PATTERN);
246
247        return MessageFormat.format(
248                messageDrivenTestNamePattern,
249                StringUtils.trimToEmpty(this.getName()));
250    }
251
252    /**
253     * @see EJB3MessageDrivenFacade#getMessageSelector()
254     */
255    @Override
256    protected String handleGetMessageSelector()
257    {
258        return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_SELECTOR);
259    }
260
261    /**
262     * @see EJB3MessageDrivenFacade#getRunAs()
263     */
264    @Override
265    protected String handleGetRunAs()
266    {
267        String runAsRole = null;
268        DependencyFacade dependency = (DependencyFacade)CollectionUtils.find(
269            this.getTargetDependencies(),
270            new Predicate()
271            {
272                public boolean evaluate(final Object object)
273                {
274                    DependencyFacade dependency = (DependencyFacade)object;
275                    return dependency != null
276                            && dependency.getSourceElement() != null
277                            && dependency.getSourceElement() instanceof Role
278                            && dependency.hasStereotype(EJB3Profile.STEREOTYPE_SECURITY_RUNAS);
279                }
280            });
281        if (dependency != null)
282        {
283            Role role = (Role)dependency.getSourceElement();
284            runAsRole = role.getName();
285        }
286        return runAsRole;
287    }
288
289    /**
290     * @see EJB3MessageDrivenFacade#getSubscriptionDurability()
291     */
292    @Override
293    protected String handleGetSubscriptionDurability()
294    {
295        String subscriptionDurability = null;
296        if (this.isDestinationTypeTopic())
297        {
298            subscriptionDurability = String.valueOf(
299                    this.getConfiguredProperty(MESSAGE_DRIVEN_TOPIC_SUBSCRIPTION_DURABILITY));
300            if (this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_DURABILITY) != null)
301            {
302                subscriptionDurability = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_DURABILITY);
303            }
304        }
305        return subscriptionDurability;
306    }
307
308    /**
309     * @see EJB3MessageDrivenFacadeLogic#handleIsSubscriptionDurable()
310     */
311    @Override
312    protected boolean handleIsSubscriptionDurable()
313    {
314        return StringUtils.equalsIgnoreCase(this.getSubscriptionDurability(), EJB3Globals.MDB_SUBSCRIPTION_DURABLE) ?
315                true : false;
316    }
317
318    /**
319     * @see EJB3MessageDrivenFacadeLogic#handleIsSubscriptionNonDurable()
320     */
321    @Override
322    protected boolean handleIsSubscriptionNonDurable()
323    {
324        return StringUtils.equalsIgnoreCase(this.getSubscriptionDurability(), EJB3Globals.MDB_SUBSCRIPTION_NONDURABLE) ?
325                true : false;
326    }
327
328    /**
329     * @see EJB3MessageDrivenFacadeLogic#handleGetDurableSubscriptionId()
330     */
331    @Override
332    protected String handleGetDurableSubscriptionId()
333    {
334        String durableSubscriptionIdPattern =
335            (String)this.getConfiguredProperty(MESSAGE_DRIVEN_DURABLE_SUBSCRIPTION_ID_PATTERN);
336
337        return MessageFormat.format(
338                durableSubscriptionIdPattern,
339                StringUtils.trimToEmpty(this.getName()));
340    }
341
342    /**
343     * @see EJB3MessageDrivenFacade#getTransactionManagement()
344     */
345    @Override
346    protected String handleGetTransactionManagement()
347    {
348        return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_TRANSACTION_MANAGEMENT);
349    }
350
351    /**
352     * @see EJB3MessageDrivenFacade#isTransactionManagementBean()
353     */
354    @Override
355    protected boolean handleIsTransactionManagementBean()
356    {
357        return StringUtils.equalsIgnoreCase(getTransactionManagement(), EJB3Globals.TRANSACTION_MANAGEMENT_BEAN);
358    }
359
360    /**
361     * @see EJB3MessageDrivenFacade#getTransactionType()
362     */
363    @Override
364    protected String handleGetTransactionType()
365    {
366        return EJB3MetafacadeUtils.getTransactionType(this,
367                String.valueOf(this.getConfiguredProperty(EJB3Globals.TRANSACTION_TYPE)));
368    }
369
370    /**
371     * @see EJB3MessageDrivenFacade#getAttributesAsList(Collection, boolean, boolean)
372     */
373    @Override
374    protected String handleGetAttributesAsList(
375            Collection attributes,
376            boolean includeTypes,
377            boolean includeNames)
378    {
379        if (!includeNames && !includeTypes || attributes == null)
380        {
381            return "";
382        }
383
384        StringBuilder sb = new StringBuilder();
385        String separator = "";
386
387        for (final Iterator it = attributes.iterator(); it.hasNext();)
388        {
389            AttributeFacade attr = (AttributeFacade)it.next();
390            sb.append(separator);
391            separator = ", ";
392            if (includeTypes)
393            {
394                sb.append(attr.getGetterSetterTypeName());
395                sb.append(" ");
396            }
397            if (includeNames)
398            {
399                sb.append(attr.getName());
400            }
401        }
402        return sb.toString();
403    }
404
405    /**
406     * @param follow
407     * @return EJB3MetafacadeUtils.getConstants(this, follow)
408     * @see EJB3MessageDrivenFacade#getConstants(boolean)
409     */
410    protected Collection handleGetConstants(boolean follow)
411    {
412        return EJB3MetafacadeUtils.getConstants(this, follow);
413    }
414
415    /**
416     * @see EJB3MessageDrivenFacadeLogic#getServiceReferences()
417     */
418    public Collection<DependencyFacade> getServiceReferences()
419    {
420        Collection<DependencyFacade> references = super.getServiceReferences();
421        CollectionUtils.filter(
422            references,
423            new Predicate()
424            {
425                public boolean evaluate(Object object)
426                {
427                    ModelElementFacade targetElement = ((DependencyFacade)object).getTargetElement();
428                    return targetElement.hasStereotype(EJB3Profile.STEREOTYPE_SERVICE);
429                }
430            });
431        return references;
432    }
433
434    /**
435     * @see EJB3MessageDrivenFacadeLogic#handleIsDestinationTypeQueue()
436     */
437    @Override
438    protected boolean handleIsDestinationTypeQueue()
439    {
440        boolean typeQueue = false;
441        if (StringUtils.equalsIgnoreCase(this.getDestinationType(), EJB3Globals.MDB_DESTINATION_TYPE_QUEUE))
442        {
443            typeQueue = true;
444        }
445        return typeQueue;
446    }
447
448    /**
449     * @see EJB3MessageDrivenFacadeLogic#handleIsDestinationTypeTopic()
450     */
451    @Override
452    protected boolean handleIsDestinationTypeTopic()
453    {
454        boolean typeTopic = false;
455        if (StringUtils.equalsIgnoreCase(this.getDestinationType(), EJB3Globals.MDB_DESTINATION_TYPE_TOPIC))
456        {
457            typeTopic = true;
458        }
459        return typeTopic;
460    }
461
462    /**
463     * @see EJB3MessageDrivenFacadeLogic#handleGetEnvironmentEntries(boolean)
464     */
465    protected Collection handleGetEnvironmentEntries(boolean follow)
466    {
467        return EJB3MetafacadeUtils.getEnvironmentEntries(this, follow);
468    }
469
470    /**
471     * @see EJB3MessageDrivenFacadeLogic#handleGetMinimumPoolSize()
472     */
473    protected int handleGetMinimumPoolSize()
474    {
475        int minPoolSize = 0;
476        String minPoolSizeStr = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_MINIMUM_POOL_SIZE);
477        if (StringUtils.isNotBlank(minPoolSizeStr) && NumberUtils.isDigits(minPoolSizeStr))
478        {
479            minPoolSize = Integer.parseInt(minPoolSizeStr);
480        }
481        return minPoolSize;
482    }
483
484    /**
485     * @see EJB3MessageDrivenFacadeLogic#handleGetMaximumPoolSize()
486     */
487    protected int handleGetMaximumPoolSize()
488    {
489        int maxPoolSize = 0;
490        String maxPoolSizeStr = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_MAXIMUM_POOL_SIZE);
491        if (StringUtils.isNotBlank(maxPoolSizeStr) && NumberUtils.isDigits(maxPoolSizeStr))
492        {
493            maxPoolSize = Integer.parseInt(maxPoolSizeStr);
494        }
495        return maxPoolSize;
496    }
497
498    /**
499     * @see EJB3MessageDrivenFacadeLogic#handleIsListenerEnabled()
500     */
501    @Override
502    protected boolean handleIsListenerEnabled()
503    {
504        return this.hasStereotype(EJB3Profile.STEREOTYPE_LISTENER);
505    }
506
507    /**
508     * @see EJB3MessageDrivenFacadeLogic#handleGetInterceptorReferences()
509     */
510    protected Collection<DependencyFacade> handleGetInterceptorReferences()
511    {
512        Collection<DependencyFacade> references = this.getSourceDependencies();
513        CollectionUtils.filter(
514            references,
515            new Predicate()
516            {
517                public boolean evaluate(Object object)
518                {
519                    DependencyFacade dependency = (DependencyFacade)object;
520                    ModelElementFacade targetElement = dependency.getTargetElement();
521                    return (targetElement != null && targetElement.hasStereotype(EJB3Profile.STEREOTYPE_INTERCEPTOR));
522                }
523            });
524        CollectionUtils.transform(
525            references,
526            new Transformer()
527            {
528                public Object transform(final Object object)
529                {
530                    return ((DependencyFacade)object).getTargetElement();
531                }
532            });
533        final Collection<DependencyFacade> interceptors = new LinkedHashSet<DependencyFacade>(references);
534        CollectionUtils.forAllDo(
535                references,
536                new Closure()
537                {
538                    public void execute(Object object)
539                    {
540                        if (object instanceof EJB3InterceptorFacade)
541                        {
542                            interceptors.addAll(((EJB3InterceptorFacade)object).getInterceptorReferences());
543                        }
544                    }
545                });
546        return interceptors;
547    }
548
549    /**
550     * @see EJB3MessageDrivenFacadeLogic#handleIsExcludeDefaultInterceptors()
551     */
552    @Override
553    protected boolean handleIsExcludeDefaultInterceptors()
554    {
555        boolean excludeDefault = false;
556        String excludeDefaultStr =
557            (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_SERVICE_INTERCEPTOR_EXCLUDE_DEFAULT);
558        if (excludeDefaultStr != null)
559        {
560            excludeDefault = BooleanUtils.toBoolean(excludeDefaultStr);
561        }
562        return excludeDefault;
563    }
564}