View Javadoc
1   package org.andromda.cartridges.spring.metafacades;
2   
3   import java.util.Arrays;
4   import java.util.Collection;
5   import org.andromda.cartridges.spring.SpringProfile;
6   import org.andromda.core.common.ExceptionUtils;
7   import org.andromda.metafacades.uml.ClassifierFacade;
8   import org.andromda.metafacades.uml.ModelElementFacade;
9   import org.andromda.metafacades.uml.OperationFacade;
10  import org.andromda.metafacades.uml.UMLProfile;
11  import org.apache.commons.collections.CollectionUtils;
12  import org.apache.commons.collections.Predicate;
13  import org.apache.commons.lang.StringUtils;
14  
15  /**
16   * Contains utilities specific to dealing with the Spring cartridge metafacades.
17   *
18   * @author Chad Brandon
19   * @author Peter Friese
20   */
21  class SpringMetafacadeUtils
22  {
23      /**
24       * Creates a fully qualified name from the given <code>packageName</code>, <code>name</code>, and
25       * <code>suffix</code>.
26       *
27       * @param packageName the name of the model element package.
28       * @param name        the name of the model element.
29       * @param suffix      the suffix to append.
30       * @return the new fully qualified name.
31       */
32      static String getFullyQualifiedName(String packageName, String name, String suffix)
33      {
34          StringBuilder fullyQualifiedName = new StringBuilder(StringUtils.trimToEmpty(packageName));
35          if (StringUtils.isNotBlank(packageName))
36          {
37              fullyQualifiedName.append('.');
38          }
39          fullyQualifiedName.append(StringUtils.trimToEmpty(name));
40          if (StringUtils.isNotBlank(suffix))
41          {
42              fullyQualifiedName.append(StringUtils.trimToEmpty(suffix));
43          }
44          return fullyQualifiedName.toString();
45      }
46  
47      /**
48       * Creates a fully qualified name from the given <code>packageName</code>, <code>name</code>, and
49       * <code>suffix</code>.
50       *
51       * @param packageName the name of the model element package.
52       * @param name        the name of the model element.
53       * @return the new fully qualified name.
54       */
55      static String getFullyQualifiedName(String packageName, String name)
56      {
57          return getFullyQualifiedName(packageName, name, null);
58      }
59  
60      /**
61       * Gets the remoting type for the passed in <code>classifier</code>. If the remoting type can be retrieved from the
62       * <code>classifier</code>, then that is used, otherwise the <code>defaultRemotingType</code> is returned.
63       * @param classifier
64       * @param defaultServiceRemotingType
65       * @return String the remoting type name.
66       */
67      static String getServiceRemotingType(ClassifierFacade classifier, String defaultServiceRemotingType)
68      {
69          ExceptionUtils.checkNull("classifer", classifier);
70          String remotingType = null;
71          if (classifier.hasStereotype(UMLProfile.STEREOTYPE_SERVICE))
72          {
73              String remotingTypeValue = (String)classifier.findTaggedValue(
74                      SpringProfile.TAGGEDVALUE_SPRING_SERVICE_REMOTING_TYPE);
75              // if the remoting type wasn't found, search all super classes
76              if (StringUtils.isBlank(remotingTypeValue))
77              {
78                  remotingType = (String)CollectionUtils.find(classifier.getAllGeneralizations(), new Predicate()
79                  {
80                      public boolean evaluate(Object object)
81                      {
82                          return ((ModelElementFacade)object).findTaggedValue(
83                                  SpringProfile.TAGGEDVALUE_SPRING_SERVICE_REMOTING_TYPE) != null;
84                      }
85                  });
86              }
87              if (StringUtils.isNotBlank(remotingTypeValue))
88              {
89                  remotingType = remotingTypeValue;
90              }
91          }
92          if (StringUtils.isBlank(remotingType) || remotingType == null)
93          {
94              remotingType = defaultServiceRemotingType;
95          }
96          return remotingType.toLowerCase().trim();
97      }
98  
99      /**
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 }