View Javadoc
1   package org.andromda.metafacades.uml14;
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.ClassifierFacade;
8   import org.andromda.metafacades.uml.DependencyFacade;
9   import org.andromda.metafacades.uml.FrontEndUseCase;
10  import org.andromda.metafacades.uml.GeneralizableElementFacade;
11  import org.andromda.metafacades.uml.NameMasker;
12  import org.andromda.metafacades.uml.Service;
13  import org.andromda.metafacades.uml.ServiceOperation;
14  import org.andromda.metafacades.uml.UMLMetafacadeProperties;
15  import org.andromda.metafacades.uml.UMLProfile;
16  import org.apache.commons.collections.CollectionUtils;
17  import org.apache.commons.collections.Predicate;
18  import org.apache.commons.lang.StringUtils;
19  
20  /**
21   * MetafacadeLogic implementation for org.andromda.metafacades.uml.Role.
22   *
23   * @see org.andromda.metafacades.uml.Role
24   * @author Bob Fields
25   */
26  public class RoleLogicImpl
27      extends RoleLogic
28  {
29      private static final long serialVersionUID = -2715508135820019632L;
30      // ---------------- constructor -------------------------------
31      /**
32       * @param metaObject
33       * @param context
34       */
35      public RoleLogicImpl(
36          Object metaObject,
37          String context)
38      {
39          super(metaObject, context);
40      }
41  
42      /**
43       * @see org.andromda.metafacades.uml14.ModelElementFacadeLogic#handleGetName()
44       */
45      public String handleGetName()
46      {
47          String name;
48          Object value = this.findTaggedValue(UMLProfile.TAGGEDVALUE_ROLE_NAME);
49          if (value != null)
50          {
51              name = StringUtils.trimToEmpty(String.valueOf(value));
52          }
53          else
54          {
55              name = super.handleGetName();
56              String mask =
57                  StringUtils.trimToEmpty(
58                      String.valueOf(this.getConfiguredProperty(UMLMetafacadeProperties.ROLE_NAME_MASK)));
59              name = NameMasker.mask(name, mask);
60          }
61          return name;
62      }
63  
64      /**
65       * @see org.andromda.metafacades.uml.Role#isReferencesPresent()
66       */
67      @Override
68      protected boolean handleIsReferencesPresent()
69      {
70          final Collection<DependencyFacade> allSourceDependencies = new LinkedHashSet<DependencyFacade>(this.getSourceDependencies());
71          for (
72              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(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                  final ClassifierFacade classifier = associationEnd.getOtherEnd().getType();
98                  present = classifier instanceof FrontEndUseCase;
99              }
100 
101             // - a generalized role is still a role, and therefore is associated with the FrontEndUseCase
102             if (!present)
103             {
104                 present = !this.getGeneralizedActors().isEmpty();
105             }
106         }
107 
108         return present;
109     }
110 }