View Javadoc
1   package org.andromda.cartridges.webservice.metafacades;
2   
3   import java.util.Collection;
4   import org.andromda.cartridges.webservice.WebServiceGlobals;
5   import org.andromda.core.metafacade.MetafacadeBase;
6   import org.andromda.core.metafacade.ModelValidationMessage;
7   import org.andromda.metafacades.uml.ClassifierFacade;
8   import org.andromda.translation.ocl.validation.OCLIntrospector;
9   import org.andromda.utils.StringUtilsHelper;
10  import org.apache.commons.lang.StringUtils;
11  import org.apache.log4j.Logger;
12  
13  /**
14   * MetafacadeLogic implementation for org.andromda.cartridges.webservice.metafacades.WebServiceParameter.
15   *
16   * @see org.andromda.cartridges.webservice.metafacades.WebServiceParameter
17   * @author Bob Fields
18   */
19  public class WebServiceParameterLogicImpl
20          extends WebServiceParameterLogic
21  {
22      private static final long serialVersionUID = 34L;
23      // ---------------- constructor -------------------------------
24  
25      /**
26       * @param metaObject
27       * @param context
28       */
29      public WebServiceParameterLogicImpl(Object metaObject, String context)
30      {
31          super(metaObject, context);
32      }
33  
34      /**
35       * The logger instance.
36       */
37      private static final Logger logger = Logger.getLogger(WebServiceParameterLogicImpl.class);
38  
39      /**
40       * @return !this.isRequired()
41       * @see org.andromda.cartridges.webservice.metafacades.WebServiceParameter#isNillable()
42       */
43      protected boolean handleIsNillable()
44      {
45          return !this.isRequired();
46      }
47  
48      /**
49       * @return testTypeName
50       * @see org.andromda.cartridges.webservice.metafacades.WebServiceParameter#getTestTypeName()
51       */
52      protected String handleGetTestTypeName()
53      {
54          String testTypeName = null;
55          final ClassifierFacade type = this.getType();
56          if (type instanceof WSDLType || type instanceof WSDLEnumerationType)
57          {
58              ClassifierFacade service = this.getOperation().getOwner();
59              if (service instanceof WebService)
60              {
61                  WebService webService = (WebService)service;
62                  final String testPackageName = webService.getTestPackageName();
63                  if (type instanceof WSDLType)
64                  {
65                      final WSDLType wsdlType = (WSDLType)type;
66                      if (!webService.isRpcStyle() && wsdlType.isArrayType())
67                      {
68                          testTypeName = testPackageName + '.' + wsdlType.getWsdlArrayName();
69                      }
70                      else if (!type.isDataType())
71                      {
72                          testTypeName = testPackageName + '.' + wsdlType.getName();
73                      }
74                  }
75                  else
76                  {
77                      final WSDLEnumerationType wsdlType = (WSDLEnumerationType)type;
78                      if (!webService.isRpcStyle() && wsdlType.isArrayType())
79                      {
80                          testTypeName = testPackageName + '.' + wsdlType.getWsdlArrayName();
81                      }
82                      else if (!type.isDataType())
83                      {
84                          testTypeName = testPackageName + '.' + wsdlType.getName();
85                      }
86                  }
87              }
88              if (testTypeName == null)
89              {
90                  testTypeName = this.getType().getFullyQualifiedName();
91              }
92          }
93          return testTypeName;
94      }
95  
96      /**
97       * The property defining the web service XML Adapter for Jaxb.
98       */
99      private static final String USE_ATTRIBUTES = "useAttributes";
100 
101     /**
102      * @see org.andromda.cartridges.webservice.metafacades.WebServiceParameterLogic#handleIsAttribute()
103      */
104     @Override
105     protected boolean handleIsAttribute()
106     {
107         boolean isAttribute = this.hasStereotype(WebServiceGlobals.STEREOTYPE_XML_ATTRIBUTE);
108         if (!isAttribute)
109         {
110             String attributes = String.valueOf(this.getConfiguredProperty(USE_ATTRIBUTES));
111             if (StringUtils.isEmpty(attributes))
112             {
113                 attributes = "true";
114             }
115             isAttribute = Boolean.parseBoolean(attributes);
116         }
117         return isAttribute;
118     }
119 
120     /**
121      * @see org.andromda.cartridges.webservice.metafacades.WebServiceParameterLogic#handleIsElement()
122      */
123     @Override
124     protected boolean handleIsElement()
125     {
126         boolean isAttribute = this.hasStereotype(WebServiceGlobals.STEREOTYPE_XML_ELEMENT);
127         if (!isAttribute)
128         {
129             String attributes = String.valueOf(this.getConfiguredProperty(USE_ATTRIBUTES));
130             if (StringUtils.isEmpty(attributes))
131             {
132                 attributes = "true";
133             }
134             isAttribute = Boolean.parseBoolean(attributes);
135         }
136         return !isAttribute;
137     }
138 
139     private static final String DEFAULT = "default";
140     private static final String EMPTY_STRING = "";
141     private static final String BOOLEAN_FALSE = "false";
142     //private static final String DEFAULT_TYPE = "PathParam";
143 
144     private static final String QUOTE = "\"";
145     private static final String RPARENS = "(";
146     private static final String LPARENS = ")";
147     /**
148      * @see org.andromda.cartridges.webservice.metafacades.WebServiceParameterLogic#getRestParamType()
149      */
150     @Override
151     protected String handleGetRestParamType()
152     {
153         String paramType = (String)this.findTaggedValue(WebServiceGlobals.REST_PARAM_TYPE);
154         if (StringUtils.isBlank(paramType) || paramType.equals(DEFAULT))
155         {
156             paramType = EMPTY_STRING;
157         }
158         else
159         {
160             String pathSegment = handleGetRestPathSegment();
161             if (StringUtils.isBlank(pathSegment))
162             {
163                 // paramType always needed with annotation
164                 pathSegment = this.getName();
165             }
166             paramType = "@javax.ws.rs." + paramType + RPARENS + QUOTE + pathSegment + QUOTE + LPARENS;
167         }
168 
169         return paramType;
170     }
171 
172     private static final String AT = "@";
173     /**
174      * @see org.andromda.cartridges.webservice.metafacades.WebServiceParameterLogic#getRestPathParam()
175      */
176     @Override
177     protected String handleGetRestPathParam()
178     {
179         String pathParam = (String)this.findTaggedValue(WebServiceGlobals.REST_PATH_PARAM);
180         if (StringUtils.isBlank(pathParam) || pathParam.equals(DEFAULT))
181         {
182             pathParam = this.getName();
183         }
184         pathParam = AT + handleGetRestParamType() + "(\"" + pathParam + "\")";
185         return pathParam;
186     }
187 
188     /**
189      * @see org.andromda.cartridges.webservice.metafacades.WebServiceParameterLogic#getRestPathSegment()
190      */
191     @Override
192     protected String handleGetRestPathSegment()
193     {
194         String pathSegment = (String)this.findTaggedValue(WebServiceGlobals.REST_PATH_SEGMENT);
195         if (StringUtils.isBlank(pathSegment) || pathSegment.equals(DEFAULT))
196         {
197             pathSegment = EMPTY_STRING;
198         }
199         return pathSegment;
200     }
201 
202     /**
203      * @see org.andromda.cartridges.webservice.metafacades.WebServiceParameterLogic#isRestEncoded()
204      */
205     @Override
206     protected boolean handleIsRestEncoded()
207     {
208         String restEncoded = (String)this.findTaggedValue(WebServiceGlobals.REST_ENCODED);
209         if (StringUtils.isBlank(restEncoded) || restEncoded.equals(DEFAULT))
210         {
211             restEncoded = BOOLEAN_FALSE;
212         }
213         return Boolean.valueOf(restEncoded);
214     }
215 
216     /**
217      * <p><b>Constraint:</b> org::andromda::cartridges::webservice::metafacades::WebServiceParameter::parameter must start with a lowercase letter</p>
218      * <p><b>Error:</b> Parameter name must start with a lowercase letter.</p>
219      * @param validationMessages Collection<ModelValidationMessage>
220      * @see MetafacadeBase#validateInvariants(Collection validationMessages)
221      */
222     @Override
223     public void validateInvariants(Collection<ModelValidationMessage> validationMessages)
224     {
225         super.validateInvariants(validationMessages);
226         try
227         {
228             final Object contextElement = this.THIS();
229             final String name = (String)OCLIntrospector.invoke(contextElement,"name");
230             if (name != null && name.length()>0 && !StringUtilsHelper.startsWithLowercaseLetter(name) && !this.isReturn())
231             {
232                 validationMessages.add(
233                     new ModelValidationMessage(
234                         (MetafacadeBase)contextElement ,
235                         "org::andromda::cartridges::webservice::metafacades::WebServiceParameter::parameter must start with a lowercase letter",
236                         "Parameter name must start with a lowercase letter."));
237             }
238         }
239         catch (Throwable th)
240         {
241             Throwable cause = th.getCause();
242             int depth = 0; // Some throwables have infinite recursion
243             while (cause != null && depth < 7)
244             {
245                 th = cause;
246                 depth++;
247             }
248             logger.error("Error validating constraint 'org::andromda::cartridges::webservice::WSDLTypeAttribute::attribute must start with a lowercase letter' ON "
249                 + this.THIS().toString() + ": " + th.getMessage(), th);
250         }
251     }
252 }