View Javadoc

1   package org.andromda.cartridges.spring.metafacades;
2   
3   import org.andromda.cartridges.spring.SpringProfile;
4   import org.andromda.core.common.ExceptionUtils;
5   import org.andromda.metafacades.uml.ClassifierFacade;
6   import org.andromda.metafacades.uml.ModelElementFacade;
7   import org.andromda.metafacades.uml.OperationFacade;
8   import org.andromda.metafacades.uml.UMLProfile;
9   import org.apache.commons.collections.CollectionUtils;
10  import org.apache.commons.collections.Predicate;
11  import org.apache.commons.lang.StringUtils;
12  
13  /***
14   * Contains utilities specific to dealing with the Spring cartridge metafacades.
15   *
16   * @author Chad Brandon
17   * @author Peter Friese
18   */
19  class SpringMetafacadeUtils
20  {
21      /***
22       * Creates a fully qualified name from the given <code>packageName</code>, <code>name</code>, and
23       * <code>suffix</code>.
24       *
25       * @param packageName the name of the model element package.
26       * @param name        the name of the model element.
27       * @param suffix      the suffix to append.
28       * @return the new fully qualified name.
29       */
30      staticong> String getFullyQualifiedName(String packageName, String name, String suffix)
31      {
32          StringBuffer fullyQualifiedName = new StringBuffer(StringUtils.trimToEmpty(packageName));
33          if</strong> (StringUtils.isNotBlank(packageName))
34          {
35              fullyQualifiedName.append(".");
36          }
37          fullyQualifiedName.append(StringUtils.trimToEmpty(name));
38          if (StringUtils.isNotEmpty(suffix))
39          {
40              fullyQualifiedName.append(StringUtils.trimToEmpty(suffix));
41          }
42          return fullyQualifiedName.toString();
43      }
44  
45      /***
46       * Creates a fully qualified name from the given <code>packageName</code>, <code>name</code>, and
47       * <code>suffix</code>.
48       *
49       * @param packageName the name of the model element package.
50       * @param name        the name of the model element.
51       * @return the new fully qualified name.
52       */
53      staticong> String getFullyQualifiedName(String packageName, String name)
54      {
55          return</strong> getFullyQualifiedName(packageName, name, null);
56      }
57  
58      /***
59       * Gets the remoting type for the passed in <code>classifier</code>. If the remoting type can be retrieved from the
60       * <code>classifier</code>, then that is used, otherwise the <code>defaultRemotingType</code> is returned.
61       *
62       * @return String the remoting type name.
63       */
64      static String getServiceRemotingType(ClassifierFacade classifier, String defaultServiceRemotingType)
65      {
66          ExceptionUtils.checkNull("classifer", classifier);
67          String remotingType = null;
68          if (classifier.hasStereotype(UMLProfile.STEREOTYPE_SERVICE))
69          {
70              String remotingTypeValue = (String)classifier.findTaggedValue(
71                      SpringProfile.TAGGEDVALUE_SPRING_SERVICE_REMOTING_TYPE);
72              // if the remoting type wasn't found, search all super classes
73              if (StringUtils.isEmpty(remotingTypeValue))
74              {
75                  remotingType = (String)CollectionUtils.find(classifier.getAllGeneralizations(), new Predicate()
76                  {
77                      public boolean evaluate(Object object)
78                      {
79                          return ((ModelElementFacade)object).findTaggedValue(
80                                  SpringProfile.TAGGEDVALUE_SPRING_SERVICE_REMOTING_TYPE) != null;
81                      }
82                  });
83              }
84              if (StringUtils.isNotEmpty(remotingTypeValue))
85              {
86                  remotingType = remotingTypeValue;
87              }
88          }
89          if (StringUtils.isEmpty(remotingType))
90          {
91              remotingType = defaultServiceRemotingType;
92          }
93          return remotingType.toLowerCase().trim();
94      }
95  
96      /***
97       * Get the interceptors for the passed in <code>classifier</code>. If the interceptors can be retrieved from the
98       * <code>classifier</code>, then these will be used, otherwise the <code>defaultInterceptors</code> are
99       * returned.
100      *
101      * @param classifier the classifier whose interceptors we are looking for.
102      * @param defaultInterceptors a list of interceptors to use if the classifier itself has no explicit interceptors.
103      *
104      * @return String[] the interceptors.
105      */
106     static String[] getServiceInterceptors(ClassifierFacade classifier,
107         String[] defaultInterceptors)
108     {
109         ExceptionUtils.checkNull("classifier", classifier);
110         String[] interceptors = null;
111         if (classifier.hasStereotype(UMLProfile.STEREOTYPE_SERVICE))
112         {
113             String interceptorsValue = (String)classifier.findTaggedValue(
114                     SpringProfile.TAGGEDVALUE_SPRING_SERVICE_INTERCEPTORS);
115             // if the interceptors weren't found, search all super classes
116             if (StringUtils.isEmpty(interceptorsValue))
117             {
118                 interceptorsValue = (String)CollectionUtils.find(classifier.getAllGeneralizations(), new Predicate()
119                 {
120                     public boolean evaluate(Object object)
121                     {
122                         return ((ModelElementFacade)object).findTaggedValue(
123                                 SpringProfile.TAGGEDVALUE_SPRING_SERVICE_INTERCEPTORS) != null;
124                     }
125                 });
126             }
127             // interceptors are a comma-separated list of strings, go and split the list
128             if (StringUtils.isNotEmpty(interceptorsValue))
129             {
130                 interceptors = interceptorsValue.split(",");
131             }
132         }
133         if (interceptors == null || interceptors.length == 0)
134         {
135             interceptors = defaultInterceptors;
136         }
137         return interceptors;
138     }
139 
140     /***
141      * Gets the remote service port for the passed in <code>classifier</code>. If the remote service
142      * port can be retrieved from the <code>classifier</code>, then that is used, otherwise the
143      * <code>defaultRemoteServicePort</code> is returned.
144      *
145      * @return String the remote service port.
146      */
147     static String getServiceRemotePort(ClassifierFacade classifier, String defaultRemoteServicePort)
148     {
149         ExceptionUtils.checkNull("classifer", classifier);
150         String remoteServicePort = null;
151         if (classifier.hasStereotype(UMLProfile.STEREOTYPE_SERVICE))
152         {
153             String remoteServicePortValue = (String)classifier.findTaggedValue(
154                     SpringProfile.TAGGEDVALUE_SPRING_SERVICE_REMOTE_PORT);
155             // if the remote service port wasn't found, search all super classes
156             if (StringUtils.isEmpty(remoteServicePortValue))
157             {
158                 remoteServicePort = (String)CollectionUtils.find(classifier.getAllGeneralizations(), new Predicate()
159                 {
160                     public boolean evaluate(Object object)
161                     {
162                         return ((ModelElementFacade)object).findTaggedValue(
163                                 SpringProfile.TAGGEDVALUE_SPRING_SERVICE_REMOTE_PORT) != null;
164                     }
165                 });
166             }
167             if (StringUtils.isNotEmpty(remoteServicePortValue))
168             {
169                 remoteServicePort = remoteServicePortValue;
170             }
171         }
172         if (StringUtils.isEmpty(remoteServicePort))
173         {
174             remoteServicePort = defaultRemoteServicePort;
175         }
176         return remoteServicePort.toLowerCase().trim();
177     }
178 
179     /***
180      * Checks whether the passed in operation is a query and should be using named parameters.
181      *
182      * @param operation the operation.
183      * @param defaultUseNamedParameters the default value.
184      * @return whether named parameters should be used.
185      */
186     static boolean getUseNamedParameters(OperationFacade operation,
187         boolean defaultUseNamedParameters)
188     {
189         ExceptionUtils.checkNull("operation", operation);
190         boolean useNamedParameters = defaultUseNamedParameters;
191         if (operation.isQuery())
192         {
193             String useNamedParametersValue = StringUtils.trimToEmpty((String)operation
194                     .findTaggedValue(SpringProfile.TAGGEDVALUE_HIBERNATE_USE_NAMED_PARAMETERS));
195             if (StringUtils.isNotEmpty(useNamedParametersValue))
196             {
197                 useNamedParameters = Boolean.valueOf(useNamedParametersValue).booleanValue();
198             }
199         }
200         return useNamedParameters;
201     }
202 }