001package org.andromda.cartridges.spring.metafacades;
002
003import java.util.Arrays;
004import java.util.Collection;
005import org.andromda.cartridges.spring.SpringProfile;
006import org.andromda.core.common.ExceptionUtils;
007import org.andromda.metafacades.uml.ClassifierFacade;
008import org.andromda.metafacades.uml.ModelElementFacade;
009import org.andromda.metafacades.uml.OperationFacade;
010import org.andromda.metafacades.uml.UMLProfile;
011import org.apache.commons.collections.CollectionUtils;
012import org.apache.commons.collections.Predicate;
013import org.apache.commons.lang.StringUtils;
014
015/**
016 * Contains utilities specific to dealing with the Spring cartridge metafacades.
017 *
018 * @author Chad Brandon
019 * @author Peter Friese
020 */
021class SpringMetafacadeUtils
022{
023    /**
024     * Creates a fully qualified name from the given <code>packageName</code>, <code>name</code>, and
025     * <code>suffix</code>.
026     *
027     * @param packageName the name of the model element package.
028     * @param name        the name of the model element.
029     * @param suffix      the suffix to append.
030     * @return the new fully qualified name.
031     */
032    static String getFullyQualifiedName(String packageName, String name, String suffix)
033    {
034        StringBuilder fullyQualifiedName = new StringBuilder(StringUtils.trimToEmpty(packageName));
035        if (StringUtils.isNotBlank(packageName))
036        {
037            fullyQualifiedName.append('.');
038        }
039        fullyQualifiedName.append(StringUtils.trimToEmpty(name));
040        if (StringUtils.isNotBlank(suffix))
041        {
042            fullyQualifiedName.append(StringUtils.trimToEmpty(suffix));
043        }
044        return fullyQualifiedName.toString();
045    }
046
047    /**
048     * Creates a fully qualified name from the given <code>packageName</code>, <code>name</code>, and
049     * <code>suffix</code>.
050     *
051     * @param packageName the name of the model element package.
052     * @param name        the name of the model element.
053     * @return the new fully qualified name.
054     */
055    static String getFullyQualifiedName(String packageName, String name)
056    {
057        return getFullyQualifiedName(packageName, name, null);
058    }
059
060    /**
061     * Gets the remoting type for the passed in <code>classifier</code>. If the remoting type can be retrieved from the
062     * <code>classifier</code>, then that is used, otherwise the <code>defaultRemotingType</code> is returned.
063     * @param classifier
064     * @param defaultServiceRemotingType
065     * @return String the remoting type name.
066     */
067    static String getServiceRemotingType(ClassifierFacade classifier, String defaultServiceRemotingType)
068    {
069        ExceptionUtils.checkNull("classifer", classifier);
070        String remotingType = null;
071        if (classifier.hasStereotype(UMLProfile.STEREOTYPE_SERVICE))
072        {
073            String remotingTypeValue = (String)classifier.findTaggedValue(
074                    SpringProfile.TAGGEDVALUE_SPRING_SERVICE_REMOTING_TYPE);
075            // if the remoting type wasn't found, search all super classes
076            if (StringUtils.isBlank(remotingTypeValue))
077            {
078                remotingType = (String)CollectionUtils.find(classifier.getAllGeneralizations(), new Predicate()
079                {
080                    public boolean evaluate(Object object)
081                    {
082                        return ((ModelElementFacade)object).findTaggedValue(
083                                SpringProfile.TAGGEDVALUE_SPRING_SERVICE_REMOTING_TYPE) != null;
084                    }
085                });
086            }
087            if (StringUtils.isNotBlank(remotingTypeValue))
088            {
089                remotingType = remotingTypeValue;
090            }
091        }
092        if (StringUtils.isBlank(remotingType) || remotingType == null)
093        {
094            remotingType = defaultServiceRemotingType;
095        }
096        return remotingType.toLowerCase().trim();
097    }
098
099    /**
100     * Get the interceptors for the passed in <code>classifier</code>. If the interceptors can be retrieved from the
101     * <code>classifier</code>, then these will be used, otherwise the <code>defaultInterceptors</code> are
102     * returned.
103     *
104     * @param classifier the classifier whose interceptors we are looking for.
105     * @param defaultInterceptors a list of interceptors to use if the classifier itself has no explicit interceptors.
106     *
107     * @return String[] the interceptors.
108     */
109    static Collection<String> getServiceInterceptors(ClassifierFacade classifier,
110            Collection<String> defaultInterceptors)
111    {
112        ExceptionUtils.checkNull("classifier", classifier);
113        Collection<String> interceptors = null;
114        if (classifier.hasStereotype(UMLProfile.STEREOTYPE_SERVICE))
115        {
116            String interceptorsValue = (String)classifier.findTaggedValue(
117                    SpringProfile.TAGGEDVALUE_SPRING_SERVICE_INTERCEPTORS);
118            // if the interceptors weren't found, search all super classes
119            if (StringUtils.isBlank(interceptorsValue))
120            {
121                interceptorsValue = (String)CollectionUtils.find(classifier.getAllGeneralizations(), new Predicate()
122                {
123                    public boolean evaluate(Object object)
124                    {
125                        return ((ModelElementFacade)object).findTaggedValue(
126                                SpringProfile.TAGGEDVALUE_SPRING_SERVICE_INTERCEPTORS) != null;
127                    }
128                });
129            }
130            // interceptors are a comma-separated list of strings, go and split the list
131            if (StringUtils.isNotBlank(interceptorsValue))
132            {
133                interceptors = Arrays.asList(interceptorsValue.split(","));
134            }
135        }
136        if (interceptors == null || interceptors.isEmpty())
137        {
138            interceptors = defaultInterceptors;
139        }
140        return interceptors;
141    }
142
143    /**
144     * Gets the remote service port for the passed in <code>classifier</code>. If the remote service
145     * port can be retrieved from the <code>classifier</code>, then that is used, otherwise the
146     * <code>defaultRemoteServicePort</code> is returned.
147     * @param classifier
148     * @param defaultRemoteServicePort
149     * @return String the remote service port.
150     */
151    static String getServiceRemotePort(ClassifierFacade classifier, String defaultRemoteServicePort)
152    {
153        ExceptionUtils.checkNull("classifer", classifier);
154        String remoteServicePort = null;
155        if (classifier.hasStereotype(UMLProfile.STEREOTYPE_SERVICE))
156        {
157            String remoteServicePortValue = (String)classifier.findTaggedValue(
158                    SpringProfile.TAGGEDVALUE_SPRING_SERVICE_REMOTE_PORT);
159            // if the remote service port wasn't found, search all super classes
160            if (StringUtils.isBlank(remoteServicePortValue))
161            {
162                remoteServicePort = (String)CollectionUtils.find(classifier.getAllGeneralizations(), new Predicate()
163                {
164                    public boolean evaluate(Object object)
165                    {
166                        return ((ModelElementFacade)object).findTaggedValue(
167                                SpringProfile.TAGGEDVALUE_SPRING_SERVICE_REMOTE_PORT) != null;
168                    }
169                });
170            }
171            if (StringUtils.isNotBlank(remoteServicePortValue))
172            {
173                remoteServicePort = remoteServicePortValue;
174            }
175        }
176        if (StringUtils.isBlank(remoteServicePort) || remoteServicePort == null)
177        {
178            remoteServicePort = defaultRemoteServicePort;
179        }
180        return remoteServicePort.toLowerCase().trim();
181    }
182
183    /**
184     * Checks whether the passed in operation is a query and should be using named parameters.
185     *
186     * @param operation the operation.
187     * @param defaultUseNamedParameters the default value.
188     * @return whether named parameters should be used.
189     */
190    static boolean getUseNamedParameters(OperationFacade operation,
191        boolean defaultUseNamedParameters)
192    {
193        ExceptionUtils.checkNull("operation", operation);
194        boolean useNamedParameters = defaultUseNamedParameters;
195        if (operation.isQuery())
196        {
197            String useNamedParametersValue = StringUtils.trimToEmpty((String)operation
198                    .findTaggedValue(SpringProfile.TAGGEDVALUE_HIBERNATE_USE_NAMED_PARAMETERS));
199            if (StringUtils.isNotBlank(useNamedParametersValue))
200            {
201                useNamedParameters = Boolean.valueOf(useNamedParametersValue).booleanValue();
202            }
203        }
204        return useNamedParameters;
205    }
206}