View Javadoc
1   package org.andromda.cartridges.java.metafacades;
2   
3   import java.util.Collection;
4   import java.util.Iterator;
5   import org.andromda.metafacades.uml.ClassifierFacade;
6   import org.andromda.metafacades.uml.GeneralizableElementFacade;
7   import org.andromda.metafacades.uml.ModelElementFacade;
8   import org.andromda.metafacades.uml.ParameterFacade;
9   import org.andromda.metafacades.uml.TemplateParameterFacade;
10  import org.andromda.metafacades.uml.UMLMetafacadeProperties;
11  import org.apache.commons.lang.BooleanUtils;
12  import org.apache.commons.lang.ObjectUtils;
13  import org.apache.commons.lang.StringUtils;
14  
15  
16  /**
17   * MetafacadeLogic implementation for org.andromda.cartridges.java.metafacades.JavaClass.
18   *
19   * @see org.andromda.cartridges.java.metafacades.JavaClass
20   */
21  public class JavaClassLogicImpl
22      extends JavaClassLogic
23  {
24      private static final long serialVersionUID = 34L;
25      /**
26       * @param metaObject
27       * @param context
28       */
29      public JavaClassLogicImpl(
30          Object metaObject,
31          String context)
32      {
33          super(metaObject, context);
34      }
35  
36      /**
37       * If WebFault stereotype, change name from XXException to XXFault. Class should
38       * also have XXException stereotype so that the corresponding Exception referencing the
39       * fault is also created.
40       * @param fault true if name is to be converted to Fault version, for VO Template only
41       * @return the class name.
42       */
43      public String getName(boolean fault)
44      {
45          String name = super.getName();
46          if (fault && this.hasStereotype("WebFault"))
47          {
48              name = name.replace("Exception", "Fault");
49              if (!name.contains("Fault"))
50              {
51                  name += "Fault";
52              }
53          }
54          return name;
55      }
56  
57      /**
58       * UML22 implementation for TemplateParameter logic
59       * @return the class name.
60       */
61      public Object getType()
62      {
63          /*if (this.metaObject instanceof TemplateParameterFacade)
64          {
65              return
66          }
67          System.out.println(this.metaObject);*/
68          return this.metaObject;
69      }
70  
71      /**
72       * UML22 implementation for TemplateParameter logic
73       * @return the class name.
74      public Object getOwner()
75      {
76          if (this.metaObject instanceof ClassifierFacade)
77          {
78              ClassifierFacade facade = (ClassifierFacade)this.metaObject;
79              return facade.getOwner();
80          }
81          return this.metaObject;
82      }
83       */
84  
85      /**
86       * @return InterfaceImplementionName
87       * @see org.andromda.cartridges.java.metafacades.JavaClass#getInterfaceImplementationName()
88       */
89      @Override
90      protected String handleGetInterfaceImplementationName()
91      {
92          return this.getInterfaceImplementionName().replaceAll(
93              "\\{0\\}",
94              this.getName());
95      }
96  
97      /**
98       * Gets the value of the {@link JavaGlobals#INTERFACE_IMPLEMENTATION_NAME_PATTERN}.
99       *
100      * @return the interface implementation name..
101      */
102     private String getInterfaceImplementionName()
103     {
104         return String.valueOf(this.getConfiguredProperty(JavaGlobals.INTERFACE_IMPLEMENTATION_NAME_PATTERN));
105     }
106 
107     /**
108      * @return InterfaceImplementationName
109      * @see org.andromda.cartridges.java.metafacades.JavaClass#getFullyQualifiedInterfaceImplementationName()
110      */
111     @Override
112     protected String handleGetFullyQualifiedInterfaceImplementationName()
113     {
114         final StringBuilder fullName = new StringBuilder();
115         final String packageName = this.getPackageName();
116         if (StringUtils.isNotBlank(packageName))
117         {
118             fullName.append(packageName).append('.');
119         }
120         return fullName.append(this.getInterfaceImplementationName()).toString();
121     }
122 
123     /**
124      * @return abstractImplementation
125      * @see org.andromda.cartridges.java.metafacades.JavaClass#isAbstractInterfaceImplementation()
126      */
127     @Override
128     protected boolean handleIsAbstractInterfaceImplementation()
129     {
130         boolean abstractImplementation = !this.getOperations().isEmpty();
131         if (!abstractImplementation)
132         {
133             for (GeneralizableElementFacade generalizableElementFacade : this.getAllGeneralizations())
134             {
135                 final ClassifierFacade classifier = (ClassifierFacade) generalizableElementFacade;
136                 abstractImplementation = !classifier.getOperations().isEmpty();
137                 if (abstractImplementation)
138                 {
139                     break;
140                 }
141             }
142         }
143         return abstractImplementation;
144     }
145 
146     /**
147      * @return templateParams
148      * @see org.andromda.cartridges.java.metafacades.JavaClassLogic#getTemplateParameters()
149      */
150     //@Override
151     protected String handleGetTemplateParams()
152     {
153         String fullName = "";
154         if (this.isTemplateParametersPresent() &&
155             BooleanUtils.toBoolean(
156                 ObjectUtils.toString(this.getConfiguredProperty(UMLMetafacadeProperties.ENABLE_TEMPLATING))))
157         {
158             // we'll be constructing the parameter list in this buffer
159             final StringBuilder buffer = new StringBuilder();
160 
161             // add the name we've constructed so far
162             buffer.append(fullName);
163 
164             // start the parameter list
165             buffer.append('<');
166 
167             // loop over the parameters, we are so to have at least one (see
168             // outer condition)
169             final Collection<TemplateParameterFacade> templateParameters = this.getTemplateParameters();
170             for (Iterator<TemplateParameterFacade> parameterIterator = templateParameters.iterator(); parameterIterator.hasNext();)
171             {
172                 final ModelElementFacade modelElement =
173                     (parameterIterator.next()).getParameter();
174 
175                 // TODO: UML14 returns ParameterFacade, UML2 returns ModelElementFacade, so types are wrong from fullyQualifiedName
176                 // Mapping from UML2 should return ParameterFacade, with a getType method. Need TemplateParameter.getName method.
177                 if (modelElement instanceof ParameterFacade)
178                 {
179                     buffer.append(((ParameterFacade)modelElement).getType().getFullyQualifiedName());
180                 }
181                 else
182                 {
183                     buffer.append(modelElement.getFullyQualifiedName());
184                 }
185 
186                 if (parameterIterator.hasNext())
187                 {
188                     buffer.append(", ");
189                 }
190             }
191 
192             // we're finished listing the parameters
193             buffer.append('>');
194 
195             // we have constructed the full name in the buffer
196             fullName = buffer.toString();
197         }
198         return fullName;
199     }
200 }