View Javadoc
1   package org.andromda.metafacades.emf.uml22;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.LinkedHashSet;
6   import org.andromda.metafacades.uml.AttributeFacade;
7   import org.andromda.metafacades.uml.ClassifierFacade;
8   import org.andromda.metafacades.uml.EventFacade;
9   import org.andromda.metafacades.uml.FrontEndAction;
10  import org.andromda.metafacades.uml.FrontEndControllerOperation;
11  import org.andromda.metafacades.uml.FrontEndEvent;
12  import org.andromda.metafacades.uml.FrontEndForward;
13  import org.andromda.metafacades.uml.ModelElementFacade;
14  import org.andromda.metafacades.uml.OperationFacade;
15  import org.andromda.metafacades.uml.TransitionFacade;
16  import org.andromda.metafacades.uml.UMLProfile;
17  import org.apache.commons.collections.CollectionUtils;
18  import org.apache.commons.collections.Predicate;
19  import org.apache.commons.lang.ObjectUtils;
20  import org.apache.commons.lang.StringUtils;
21  import org.eclipse.uml2.uml.NamedElement;
22  
23  /**
24   * MetafacadeLogic implementation for
25   * org.andromda.metafacades.uml.FrontEndParameter.
26   *
27   * @see org.andromda.metafacades.uml.FrontEndParameter
28   */
29  public class FrontEndParameterLogicImpl
30      extends FrontEndParameterLogic
31  {
32      private static final long serialVersionUID = -5932754222510758357L;
33  
34      /**
35       * @param metaObject
36       * @param context
37       */
38      public FrontEndParameterLogicImpl(
39          final Object metaObject,
40          final String context)
41      {
42          super(metaObject, context);
43      }
44  
45      /**
46       * @see org.andromda.metafacades.uml.FrontEndParameter#isControllerOperationArgument()
47       */
48      @Override
49      protected boolean handleIsControllerOperationArgument()
50      {
51          return this.getControllerOperation() != null;
52      }
53  
54      /**
55       * @see org.andromda.metafacades.uml.FrontEndParameter#getControllerOperation()
56       */
57      @Override
58      protected OperationFacade handleGetControllerOperation()
59      {
60          return this.getOperation();
61      }
62  
63      /**
64       * @see org.andromda.metafacades.uml.FrontEndParameter#isContainedInFrontEndUseCase()
65       */
66      @Override
67      protected boolean handleIsContainedInFrontEndUseCase()
68      {
69          return this.getEvent() instanceof FrontEndEvent || this.getOperation() instanceof FrontEndControllerOperation;
70      }
71  
72      /**
73       * @see org.andromda.metafacades.uml.FrontEndParameter#getView()
74       */
75      @Override
76      protected Object handleGetView()
77      {
78          Object view = null;
79          final EventFacade event = this.getEvent();
80          if (event != null)
81          {
82              final TransitionFacade transition = event.getTransition();
83              if (transition instanceof FrontEndAction)
84              {
85                  final FrontEndAction action = (FrontEndAction)transition;
86                  view = action.getInput();
87              }
88              else if (transition instanceof FrontEndForward)
89              {
90                  final FrontEndForward forward = (FrontEndForward)transition;
91                  if (forward.isEnteringView())
92                  {
93                      view = forward.getTarget();
94                  }
95              }
96          }
97          return view;
98      }
99  
100     /**
101      * @see org.andromda.metafacades.uml.FrontEndParameter#isActionParameter()
102      */
103     @Override
104     protected boolean handleIsActionParameter()
105     {
106         final FrontEndAction action = this.getAction();
107         return action != null && action.getParameters().contains(this.THIS());
108     }
109 
110     /**
111      * @see org.andromda.metafacades.uml.FrontEndParameter#getAction()
112      */
113     @Override
114     protected FrontEndAction handleGetAction()
115     {
116         FrontEndAction actionObject = null;
117         final EventFacade event = this.getEvent();
118         if (event != null)
119         {
120             final TransitionFacade transition = event.getTransition();
121             if (transition instanceof FrontEndAction)
122             {
123                 actionObject = (FrontEndAction)transition;
124             }
125         }
126         return actionObject;
127     }
128 
129     /**
130      * @see org.andromda.metafacades.uml.FrontEndParameter#isTable()
131      */
132     @Override
133     protected boolean handleIsTable()
134     {
135         boolean isTable = false;
136         final ClassifierFacade type = this.getType();
137         if (type != null)
138         {
139             isTable = isMany() || type.isCollectionType() || type.isArrayType();
140             if (isTable)
141             {
142                 final String tableTaggedValue = ObjectUtils.toString(this.findTaggedValue(UMLProfile.TAGGEDVALUE_PRESENTATION_IS_TABLE));
143                 isTable =
144                     StringUtils.isNotBlank(tableTaggedValue) ? Boolean.valueOf(tableTaggedValue.trim()) : true;
145                 if (!isTable)
146                 {
147                     isTable = !this.getTableColumnNames().isEmpty();
148                 }
149             }
150         }
151         return isTable && this.getOperation() == null;
152     }
153 
154     /**
155      * @see org.andromda.metafacades.uml.FrontEndParameter#getTableColumnNames()
156      */
157     @Override
158     protected Collection<String> handleGetTableColumnNames()
159     {
160         final Collection<String> tableColumnNames = new LinkedHashSet<String>();
161         final Collection<Object> taggedValues = this.findTaggedValues(UMLProfile.TAGGEDVALUE_PRESENTATION_TABLE_COLUMNS);
162         if (!taggedValues.isEmpty())
163         {
164             for (final Object value : taggedValues)
165             {
166                 final String taggedValue = StringUtils.trimToNull(String.valueOf(value));
167                 if (taggedValue != null)
168                 {
169                     final String[] properties = taggedValue.split("[,\\s]+");
170                     for (int ctr = 0; ctr < properties.length; ctr++)
171                     {
172                         final String property = properties[ctr];
173                         tableColumnNames.add(property);
174                     }
175                 }
176             }
177         }
178 
179         // - if we have no table column names explicitly defined, use the table
180         // attribute names.
181         if (tableColumnNames.isEmpty())
182         {
183             tableColumnNames.addAll(this.getTableAttributeNames());
184         }
185         return tableColumnNames;
186     }
187 
188     /**
189      * @see org.andromda.metafacades.uml.FrontEndParameter#getTableColumns()
190      */
191     @Override
192     protected Collection<String> handleGetTableColumns()
193     {
194         final Collection<String> tableColumns = new ArrayList(this.getNonArrayAttributes());
195         final Collection<String> tableColumnNames = this.getTableColumnNames();
196         CollectionUtils.filter(
197             tableColumns,
198             new Predicate()
199             {
200                 public boolean evaluate(final Object object)
201                 {
202                     final ModelElementFacade attribute = (ModelElementFacade)object;
203                     final String attributeName = attribute.getName();
204                     return attributeName != null && tableColumnNames.contains(attributeName);
205                 }
206             });
207         return tableColumns;
208     }
209 
210     /**
211      * Gets all attributes for an array type that has a corresponding non-array
212      * type.
213      *
214      * @return the collection of attributes.
215      */
216     private Collection<AttributeFacade> getNonArrayAttributes()
217     {
218         final Collection<AttributeFacade> nonArrayAttributes = new ArrayList<AttributeFacade>();
219         final ClassifierFacade type = this.getType();
220         if (type != null && (type.isArrayType() || isMany()))
221         {
222             final ClassifierFacade nonArrayType = type.getNonArray();
223             if (nonArrayType != null)
224             {
225                 nonArrayAttributes.addAll(nonArrayType.getAttributes(true));
226             }
227         }
228         return nonArrayAttributes;
229     }
230 
231     /**
232      * @see org.andromda.metafacades.uml.FrontEndParameter#getTableAttributeNames()
233      */
234     @Override
235     protected Collection<String> handleGetTableAttributeNames()
236     {
237         final Collection<String> tableAttributeNames = new ArrayList<String>();
238         for (AttributeFacade attribute : this.getNonArrayAttributes())
239         {
240             tableAttributeNames.add(attribute.getName());
241         }
242         return tableAttributeNames;
243     }
244 
245     /**
246      * UML2 v5: FrontEndParameter returns NULL for namespace. Need another way to get package name
247      * @see org.andromda.metafacades.uml.FrontEndEvent#getPackageName()
248      */
249     @Override
250     protected String handleGetPackageName()
251     {
252         String packageName = UmlUtilities.getPackageName((NamedElement)this.metaObject, ".", false);
253         if (StringUtils.isBlank(packageName))
254         {
255             packageName = this.getPackage().getFullyQualifiedName();
256         }
257         return packageName;
258     }
259 }