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.translation.ocl.validation.OCLIntrospector;
8   import org.andromda.utils.StringUtilsHelper;
9   import org.apache.commons.lang.StringUtils;
10  
11  /**
12   * MetafacadeLogic implementation for org.andromda.cartridges.webservice.metafacades.WSDLTypeAttribute.
13   *
14   * @see org.andromda.cartridges.webservice.metafacades.WSDLTypeAttribute
15   * @author Bob Fields
16   */
17  public class WSDLTypeAttributeLogicImpl
18          extends WSDLTypeAttributeLogic
19  {
20      private static final long serialVersionUID = 34L;
21      // ---------------- constructor -------------------------------
22  
23      /**
24       * @param metaObject
25       * @param context
26       */
27      public WSDLTypeAttributeLogicImpl(Object metaObject, String context)
28      {
29          super(metaObject, context);
30      }
31  
32      /**
33       * @return !this.isRequired()
34       * @see org.andromda.cartridges.webservice.metafacades.WSDLTypeAttribute#isNillable()
35       */
36      protected boolean handleIsNillable()
37      {
38          return !this.isRequired();
39      }
40  
41      /**
42       * The property defining the web service XML Adapter for Jaxb for the @XmlAdapter annotation.
43       */
44      private static final String PROPERTY_XML_ADAPTER = "schemaValidation";
45  
46      /**
47       * @see org.andromda.cartridges.webservice.metafacades.WSDLTypeAttributeLogic#handleGetXmlAdapter()
48       */
49      @Override
50      protected String handleGetXmlAdapter()
51      {
52          String adapter = (String)this.findTaggedValue(WebServiceGlobals.XML_ADAPTER);
53          if (StringUtils.isEmpty(adapter))
54          {
55              adapter = String.valueOf(this.getConfiguredProperty(PROPERTY_XML_ADAPTER));
56          }
57          return adapter;
58      }
59  
60      /**
61       * The property defining the web service XML Adapter for Jaxb.
62       */
63      private static final String PROPERTY_XML_TYPE = "schemaValidation";
64  
65      /**
66       * @see org.andromda.cartridges.webservice.metafacades.WSDLTypeAttributeLogic#handleGetXmlSchemaType()
67       */
68      @Override
69      protected String handleGetXmlSchemaType()
70      {
71          String mode = (String)this.findTaggedValue(WebServiceGlobals.XML_TYPE);
72          if (StringUtils.isEmpty(mode))
73          {
74              mode = String.valueOf(this.getConfiguredProperty(PROPERTY_XML_TYPE));
75          }
76          if (StringUtils.isEmpty(mode))
77          {
78              //mode = "false";
79          }
80          return mode;
81      }
82  
83      /**
84       * The property defining the web service XML Adapter for Jaxb.
85       */
86      private static final String USE_ATTRIBUTES = "useAttributes";
87  
88      /**
89       * @see org.andromda.cartridges.webservice.metafacades.WSDLTypeAttributeLogic#handleIsAttribute()
90       */
91      @Override
92      protected boolean handleIsAttribute()
93      {
94          boolean isAttribute = this.hasStereotype(WebServiceGlobals.STEREOTYPE_XML_ATTRIBUTE);
95          boolean isElement = this.hasStereotype(WebServiceGlobals.STEREOTYPE_XML_ELEMENT);
96          if (isElement)
97          {
98              if (isAttribute)
99              {
100                 logger.error("Attribute has both XmlAttribute and XmlElement stereotypes, it cannot be both! " + this.getFullyQualifiedName());
101                 // leave attribute = true anyway, because it has the stereotype
102             }
103             // else: Attribute = false because it is an element, not an attribute
104         }
105         else if (!isAttribute)
106         {
107             String attributes = String.valueOf(this.getConfiguredProperty(USE_ATTRIBUTES));
108             if (StringUtils.isEmpty(attributes))
109             {
110                 attributes = "true";
111             }
112             isAttribute = Boolean.parseBoolean(attributes);
113         }
114         // else: Attribute = true because of stereotype
115         return isAttribute;
116     }
117 
118     /**
119      * @see org.andromda.cartridges.webservice.metafacades.WSDLTypeAttributeLogic#handleIsElement()
120      */
121     @Override
122     protected boolean handleIsElement()
123     {
124         boolean isAttribute = this.hasStereotype(WebServiceGlobals.STEREOTYPE_XML_ELEMENT);
125         boolean isElement = this.hasStereotype(WebServiceGlobals.STEREOTYPE_XML_ELEMENT);
126         if (isElement)
127         {
128             if (isAttribute)
129             {
130                 logger.error("Attribute has both XmlAttribute and XmlElement stereotypes, it cannot be both! " + this.getFullyQualifiedName());
131                 // leave element = true anyway, because it has the stereotype
132             }
133             // else: Element = true because it is an element, not an attribute
134         }
135         else if (!isAttribute)
136         {
137             String attributes = String.valueOf(this.getConfiguredProperty(USE_ATTRIBUTES));
138             if (StringUtils.isEmpty(attributes))
139             {
140                 attributes = "true";
141             }
142             // useAttributes = true by default
143             isElement = !Boolean.parseBoolean(attributes);
144         }
145         // else: Attribute = true because of stereotype
146         return isElement;
147     }
148 
149     /**
150      * <p><b>Constraint:</b> org::andromda::cartridges::webservice::metafacades::WSDLTypeAttribute::attribute must start with a lowercase letter</p>
151      * <p><b>Error:</b> Attribute name must start with a lowercase letter.</p>
152      * @param validationMessages Collection<ModelValidationMessage>
153      * @see org.andromda.core.metafacade.MetafacadeBase#validateInvariants(Collection validationMessages)
154      */
155     @Override
156     public void validateInvariants(Collection<ModelValidationMessage> validationMessages)
157     {
158         super.validateInvariants(validationMessages);
159         try
160         {
161             final Object contextElement = this.THIS();
162             final String name = (String)OCLIntrospector.invoke(contextElement,"name");
163             final boolean isStatic = this.isStatic() && this.isLeaf();
164             if (!isStatic && name != null && name.length()>0 && !StringUtilsHelper.startsWithLowercaseLetter(name))
165             {
166                 validationMessages.add(
167                     new ModelValidationMessage(
168                         (MetafacadeBase)contextElement ,
169                         "org::andromda::cartridges::webservice::metafacades::WSDLTypeAttribute::attribute must start with a lowercase letter",
170                         "Attribute name must start with a lowercase letter."));
171             }
172         }
173         catch (Throwable th)
174         {
175             Throwable cause = th.getCause();
176             int depth = 0; // Some throwables have infinite recursion
177             while (cause != null && depth < 7)
178             {
179                 th = cause;
180                 depth++;
181             }
182             logger.error("Error validating constraint 'org::andromda::cartridges::webservice::WSDLTypeAttribute::attribute must start with a lowercase letter' ON "
183                 + this.THIS().toString() + ": " + th.getMessage(), th);
184         }
185     }
186 }