View Javadoc
1   package org.andromda.metafacades.emf.uml22;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.List;
6   import org.andromda.metafacades.uml.ClassifierFacade;
7   import org.andromda.metafacades.uml.ModelElementFacade;
8   import org.andromda.metafacades.uml.UMLMetafacadeProperties;
9   import org.andromda.utils.StringUtilsHelper;
10  import org.apache.commons.lang.StringUtils;
11  import org.apache.commons.lang.SystemUtils;
12  import org.apache.log4j.Logger;
13  import org.eclipse.uml2.uml.Classifier;
14  import org.eclipse.uml2.uml.ClassifierTemplateParameter;
15  import org.eclipse.uml2.uml.Comment;
16  import org.eclipse.uml2.uml.NamedElement;
17  import org.eclipse.uml2.uml.ParameterableElement;
18  import org.eclipse.uml2.uml.TemplateParameter;
19  
20  /**
21   * MetafacadeLogic implementation for
22   * org.andromda.metafacades.uml.TemplateParameterFacade.
23   *
24   * @see org.andromda.metafacades.uml.TemplateParameterFacade
25   */
26  public class TemplateParameterFacadeLogicImpl
27      extends TemplateParameterFacadeLogic
28  {
29      /**
30       * The logger instance.
31       */
32      private static final Logger LOGGER = Logger.getLogger(TemplateParameterFacadeLogicImpl.class);
33      private static final long serialVersionUID = 3750035061795880358L;
34  
35      /**
36       * @param metaObject
37       * @param context
38       */
39      public TemplateParameterFacadeLogicImpl(
40          final ClassifierTemplateParameter metaObject,
41          final String context)
42      {
43          super(metaObject, context);
44      }
45  
46      /**
47       * @see org.andromda.metafacades.uml.TemplateParameterFacade#getParameter()
48       */
49      @Override
50      protected ParameterableElement handleGetParameter()
51      {
52          return this.metaObject.getOwnedParameteredElement();
53      }
54  
55      /**
56       * @see org.andromda.metafacades.uml.TemplateParameterFacade#getDefaultElement()
57       */
58      @Override
59      protected ParameterableElement handleGetDefaultElement()
60      {
61          return this.metaObject.getDefault();
62      }
63  
64      // TemplateParameter no longer inherits from Parameter in UML2
65      // so the Parameter methods normally part of the FacadeLogic must be implemented here. Model hierarchy is:
66      // ownedTemplateSignature (type=RedefinableTemplateSignature) with ownedParameteredElement
67      //   -> ownedParameter (type=ClassifierTemplateParameter) with constrainingClassifier
68      //     -> ownedParameteredElement (type=ParameterableElement) with name and type DataType
69      /**
70       * @return getOwnedParameteredElement().getName()
71       */
72      @Override
73      public final String handleGetName()
74      {
75          // ParameterableElement is actually uml:DataType or uml:Class in the model, even though it doesn't inherit from
76          final NamedElement type = (NamedElement) this.metaObject.getOwnedParameteredElement();
77          // Assumes no templating of template types, no array types, same declared/impl types
78          return type.getName();
79      }
80  
81      /**
82       * @return getOwnedParameteredElement()
83       * @see org.andromda.metafacades.emf.uml22.TemplateParameterFacadeLogic#handleGetType()
84       */
85      @Override
86      public final ClassifierFacade handleGetType()
87      {
88          ClassifierFacade getType2r = null;
89          ClassifierTemplateParameter param = (ClassifierTemplateParameter)this.metaObject;
90          ParameterableElement element = this.metaObject.getOwnedParameteredElement();
91          // param.getConstrainingClassifiers()) for UML2 3.0, allows multiple classifiers
92          Classifier type = param.getConstrainingClassifier(this.getName());
93          if (type==null && param.getConstrainingClassifiers()!=null && param.getConstrainingClassifiers().size()>0)
94          {
95              type = param.getConstrainingClassifiers().get(0);
96          }
97          if (type == null && element instanceof Classifier)
98          {
99              type = (Classifier)element;
100         }
101         /*if (type == null)
102         {
103             LOGGER.info(this.getFullyQualifiedName() + " type=" + type + " metaObject=" + this.metaObject + " element=" + element);
104         }*/
105         final Object result = this.shieldedElement(type);
106         try
107         {
108             getType2r = (ClassifierFacade)result;
109         }
110         catch (ClassCastException ex)
111         {
112             // Bad things happen if the metafacade type mapping in metafacades.xml is wrong - Warn
113             TemplateParameterFacadeLogicImpl.LOGGER.warn("incorrect metafacade cast for TemplateParameterFacadeLogicImpl.getType ClassifierFacade " + handleGetParameter() + ": " + result);
114         }
115         /*if (type==null && param.getConstrainingClassifiers()!=null && param.getConstrainingClassifiers().size()>0)
116         {
117             LOGGER.info("NULL " + this.getFullyQualifiedName() + " type=" + type + " metaObject=" + this.metaObject + " element=" + element + " class=" + param.getConstrainingClassifiers());
118         }*/
119         return getType2r;
120     }
121 
122     /**
123      * @return getOwnedParameteredElement().getName()
124      * @see org.andromda.metafacades.emf.uml22.TemplateParameterFacadeLogic#handleGetGetterSetterTypeName()
125      */
126     @Override
127     public final String handleGetGetterSetterTypeName()
128     {
129         String type = null;
130         // ParameterableElement is actually uml:DataType in the model, even though it doesn't inherit from
131         // Assumes no templating of template types, no array types, same declared/impl types
132         // Classifier name is the parameter type
133         if (this.handleGetType() != null)
134         {
135             type = this.getType().getFullyQualifiedName();
136         }
137         else
138         {
139             type = "java.lang.Class";
140         }
141         return type;
142     }
143 
144     /**
145      * @return getOwnedParameteredElement().getName()
146      * @see org.andromda.metafacades.emf.uml22.TemplateParameterFacadeLogic#handleGetFullyQualifiedName()
147      */
148     @Override
149     public final String handleGetFullyQualifiedName()
150     {
151         String name = this.handleGetName();
152         final String className = ((NamedElement)this.metaObject.getOwner().getOwner()).getName();
153         if (className != null)
154         {
155             name = className + '.' + name;
156         }
157         final String pkg = this.metaObject.getNearestPackage().getName();
158         if (pkg != null)
159         {
160             name = pkg + '.' + name;
161         }
162         return name;
163     }
164 
165     /**
166      * @return getOwnedParameteredElement().getName()
167      */
168     public final int getLower()
169     {
170         int lower = 0;
171         final ClassifierFacade type = this.getType();
172         if (type != null && type.isPrimitive())
173         {
174             lower = 1;
175         }
176         return lower;
177     }
178 
179     /**
180      * Takes lower bound and datatype primitive/wrapped into account.
181      * @return GetterName
182      * @see org.andromda.metafacades.emf.uml22.TemplateParameterFacadeLogic#handleGetGetterName()
183      */
184     public final String handleGetGetterName()
185     {
186         return "get" + StringUtils.capitalize(this.getName());
187     }
188 
189     /**
190      * @see org.andromda.metafacades.emf.uml22.TemplateParameterFacadeLogic#handleGetSetterName()
191      */
192     public final String handleGetSetterName()
193     {
194         return "set" + StringUtils.capitalize(this.getName());
195     }
196 
197     /**
198      * @see org.andromda.metafacades.emf.uml22.TemplateParameterFacadeLogic#handleGetConstrainingClassifiers()
199      */
200     @Override
201     public final List<Classifier> handleGetConstrainingClassifiers()
202     {
203         TemplateParameter param = this.metaObject;
204         // param.getConstrainingClassifiers()) for UML2 3.0, allows multiple classifiers
205         if (param instanceof ClassifierTemplateParameter)
206         {
207             return ((ClassifierTemplateParameter)param).getConstrainingClassifiers();
208         }
209         else
210         {
211             return new ArrayList<Classifier>();
212         }
213     }
214 
215     /**
216      * UML2 only. UML14 has no template/template parameter owner
217      * @see org.andromda.metafacades.emf.uml22.TemplateParameterFacadeLogic#handleGetOwner()
218      */
219     @Override
220     protected ModelElementFacade handleGetOwner()
221     {
222         return (ModelElementFacade)this.shieldedElement(this.metaObject.getOwner());
223     }
224 
225     /**
226      * @see org.andromda.metafacades.emf.uml22.TemplateParameterFacadeLogic#handleGetDocumentation(java.lang.String)
227      */
228     @Override
229     protected String handleGetDocumentation(String indent)
230     {
231         return this.handleGetDocumentation(
232                 indent,
233                 100 - indent.length());
234     }
235 
236     /**
237      * @see org.andromda.metafacades.emf.uml22.TemplateParameterFacadeLogic#handleGetDocumentation(java.lang.String, int)
238      */
239     @Override
240     protected String handleGetDocumentation(String indent, int lineLength)
241     {
242         // We really don't want the start/end paragraph returns on the first/last lines. Very annoying. Makes the docs too long with mixed html.
243         // That's the only thing that htmlStyle does: convert each line to <p>line</p>. Not necessary.
244         return this.handleGetDocumentation(
245             indent,
246             lineLength,
247             false);
248     }
249 
250     /**
251      * @see org.andromda.metafacades.emf.uml22.TemplateParameterFacadeLogic#handleGetDocumentation(java.lang.String, int, boolean)
252      */
253     @Override
254     protected String handleGetDocumentation(String indent, int lineLength, boolean htmlStyle)
255     {
256         final StringBuilder documentation = new StringBuilder();
257 
258         if (lineLength < 1)
259         {
260             lineLength = Integer.MAX_VALUE;
261         }
262 
263         // Try to get doc comments from the template parameter, the template, and the template signature
264         Collection<Comment> comments = this.metaObject.getOwnedComments();
265         if (comments == null || comments.isEmpty())
266         {
267             comments = this.metaObject.getParameteredElement().getOwnedComments();
268         }
269         if (comments == null || comments.isEmpty())
270         {
271             comments = this.metaObject.getSignature().getOwnedComments();
272         }
273         if (comments != null && !comments.isEmpty())
274         {
275             for (final Comment comment : comments)
276             {
277                 String commentString = StringUtils.trimToEmpty(comment.getBody());
278                 documentation.append(StringUtils.trimToEmpty(commentString));
279                 documentation.append(SystemUtils.LINE_SEPARATOR);
280             }
281         }
282 
283         // if there still isn't anything, create a todo tag.
284         if (StringUtils.isBlank(documentation.toString()))
285         {
286             if (Boolean.valueOf((String)this.getConfiguredProperty(UMLMetafacadeProperties.TODO_FOR_MISSING_DOCUMENTATION)))
287             {
288                 String todoTag = (String)this.getConfiguredProperty(UMLMetafacadeProperties.TODO_TAG);
289                 documentation.append(todoTag).append(": Model Documentation for " + this.handleGetFullyQualifiedName());
290             }
291         }
292 
293         return StringUtilsHelper.format(
294             StringUtils.trimToEmpty(documentation.toString()),
295             indent,
296             lineLength,
297             htmlStyle);
298     }
299 
300     /**
301      * @see org.andromda.core.metafacade.MetafacadeBase#getValidationName
302      */
303     @Override
304     public String getValidationName()
305     {
306         return this.getName();
307     }
308 
309     /**
310      * @see org.andromda.core.metafacade.MetafacadeBase#getValidationOwner
311      */
312     @Override
313     public ModelElementFacade getValidationOwner()
314     {
315         return (ModelElementFacade)this.shieldedElement(this.metaObject.getOwner());
316     }
317 }