View Javadoc
1   package org.andromda.metafacades.emf.uml22;
2   
3   import java.util.Collection;
4   import java.util.Iterator;
5   import java.util.LinkedHashSet;
6   import org.andromda.metafacades.uml.AssociationEndFacade;
7   import org.andromda.metafacades.uml.DependencyFacade;
8   import org.andromda.metafacades.uml.FrontEndUseCase;
9   import org.andromda.metafacades.uml.GeneralizableElementFacade;
10  import org.andromda.metafacades.uml.NameMasker;
11  import org.andromda.metafacades.uml.Service;
12  import org.andromda.metafacades.uml.ServiceOperation;
13  import org.andromda.metafacades.uml.UMLMetafacadeProperties;
14  import org.andromda.metafacades.uml.UMLProfile;
15  import org.apache.commons.collections.CollectionUtils;
16  import org.apache.commons.collections.Predicate;
17  import org.apache.commons.lang.StringUtils;
18  
19  /**
20   * MetafacadeLogic implementation for org.andromda.metafacades.uml.Role.
21   *
22   * @see org.andromda.metafacades.uml.Role
23   */
24  public class RoleLogicImpl
25      extends RoleLogic
26  {
27      private static final long serialVersionUID = -8313975343481981741L;
28  
29      /**
30       * @param metaObject
31       * @param context
32       */
33      public RoleLogicImpl(
34          final Object metaObject,
35          final String context)
36      {
37          super(metaObject, context);
38      }
39  
40      /**
41       * @see org.andromda.metafacades.emf.uml22.ModelElementFacadeLogic#handleGetName()
42       */
43      @Override
44      public String handleGetName()
45      {
46          String name;
47          final Object value = this.findTaggedValue(UMLProfile.TAGGEDVALUE_ROLE_NAME);
48          if (value == null)
49          {
50              name = super.handleGetName();
51              final String mask =
52                  StringUtils.trimToEmpty(
53                      String.valueOf(this.getConfiguredProperty(UMLMetafacadeProperties.ROLE_NAME_MASK)));
54              name = NameMasker.mask(
55                      name,
56                      mask);
57          }
58          else
59          {
60              name = StringUtils.trimToEmpty(String.valueOf(value));
61          }
62          return name;
63      }
64  
65      /**
66       * @see org.andromda.metafacades.uml.Role#isReferencesPresent()
67       */
68      @Override
69      protected boolean handleIsReferencesPresent()
70      {
71          final Collection<DependencyFacade> allSourceDependencies = new LinkedHashSet<DependencyFacade>(this.getSourceDependencies());
72          for (GeneralizableElementFacade parent = this.getGeneralization(); parent != null;
73              parent = parent.getGeneralization())
74          {
75              allSourceDependencies.addAll(parent.getSourceDependencies());
76          }
77          boolean present =
78              CollectionUtils.find(
79                  allSourceDependencies,
80                  new Predicate()
81                  {
82                      public boolean evaluate(final Object object)
83                      {
84                          DependencyFacade dependency = (DependencyFacade)object;
85                          Object target = dependency.getTargetElement();
86                          return target instanceof Service || target instanceof ServiceOperation;
87                      }
88                  }) != null;
89  
90          // - if no references on any services, try the FrontEndUseCases
91          if (!present)
92          {
93              final Collection<AssociationEndFacade> associationEnds = this.getAssociationEnds();
94              for (final Iterator<AssociationEndFacade> iterator = associationEnds.iterator(); iterator.hasNext() && !present;)
95              {
96                  final AssociationEndFacade associationEnd = iterator.next();
97                  if (associationEnd.getOtherEnd() != null && associationEnd.getOtherEnd().getType() != null)
98                  {
99                      present = associationEnd.getOtherEnd().getType() instanceof FrontEndUseCase;
100                 }
101             }
102 
103             // - a generalized role is still a role, and therefore is associated
104             // with the FrontEndUseCase
105             if (!present)
106             {
107                 present = !this.getGeneralizedActors().isEmpty();
108             }
109         }
110 
111         return present;
112     }
113 }