001package org.andromda.metafacades.uml14;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.Iterator;
006import java.util.LinkedHashSet;
007import java.util.Set;
008import org.andromda.metafacades.uml.GeneralizableElementFacade;
009import org.andromda.metafacades.uml.GeneralizationFacade;
010import org.andromda.metafacades.uml.ModelElementFacade;
011import org.apache.commons.collections.CollectionUtils;
012import org.apache.commons.collections.Transformer;
013import org.omg.uml.foundation.core.GeneralizableElement;
014import org.omg.uml.foundation.core.Generalization;
015
016/**
017 * MetafacadeLogic implementation.
018 *
019 * @see org.andromda.metafacades.uml.GeneralizableElementFacade
020 * @author Bob Fields
021 */
022public class GeneralizableElementFacadeLogicImpl
023        extends GeneralizableElementFacadeLogic
024{
025    private static final long serialVersionUID = 34L;
026    /**
027     * @param metaObject
028     * @param context
029     */
030    public GeneralizableElementFacadeLogicImpl(GeneralizableElement metaObject,
031                                               String context)
032    {
033        super(metaObject, context);
034    }
035
036    /**
037     * @see org.andromda.metafacades.uml.GeneralizableElementFacade#getAllGeneralizations()
038     */
039    @Override
040    public Collection<GeneralizableElementFacade> handleGetAllGeneralizations()
041    {
042        final Collection<GeneralizableElementFacade> generalizations = new ArrayList<GeneralizableElementFacade>();
043        for (final Iterator<GeneralizableElementFacade> iterator = this.getGeneralizations().iterator(); iterator.hasNext();)
044        {
045            final GeneralizableElementFacade element = iterator.next();
046            generalizations.add(element);
047            generalizations.addAll(element.getAllGeneralizations());
048        }
049        return generalizations;
050    }
051
052    // ------------- relations ------------------
053
054    /**
055     * @see org.andromda.metafacades.uml.GeneralizableElementFacade#getGeneralization()
056     */
057    @Override
058    public GeneralizableElement handleGetGeneralization()
059    {
060        GeneralizableElement parent = null;
061        Collection<Generalization> generalizations = metaObject.getGeneralization();
062        if (generalizations != null)
063        {
064            Iterator<Generalization> iterator = generalizations.iterator();
065            if (iterator.hasNext())
066            {
067                parent = iterator.next().getParent();
068            }
069        }
070        return parent;
071    }
072
073    /**
074     * @see org.andromda.metafacades.uml.GeneralizableElementFacade#getGeneralizations()
075     */
076    @Override
077    protected Collection<GeneralizableElementFacade> handleGetGeneralizations()
078    {
079        Collection<GeneralizableElement> parents = new LinkedHashSet<GeneralizableElement>();
080        Collection<Generalization> generalizations = metaObject.getGeneralization();
081        if (generalizations != null && !generalizations.isEmpty())
082        {
083            for (Generalization generalization : generalizations)
084            {
085                parents.add(generalization.getParent());
086            }
087        }
088        return this.shieldedElements(parents);
089    }
090
091    /**
092     * @see org.andromda.metafacades.uml.GeneralizableElementFacade#getGeneralizationLinks()
093     */
094    @Override
095    protected Collection<GeneralizationFacade> handleGetGeneralizationLinks()
096    {
097        return this.shieldedElements(metaObject.getGeneralization());
098    }
099
100    /**
101     * @see org.andromda.metafacades.uml.ClassifierFacade#getSpecializations()
102     */
103    @Override
104    public Collection<GeneralizableElement> handleGetSpecializations()
105    {
106        Collection specializations = new ArrayList(UML14MetafacadeUtils.getCorePackage().getAParentSpecialization()
107                .getSpecialization(this.metaObject));
108        CollectionUtils.transform(specializations, new Transformer()
109        {
110            public Object transform(Object object)
111            {
112                return ((Generalization)object).getChild();
113            }
114        });
115        return specializations;
116    }
117
118    /**
119     * @see org.andromda.metafacades.uml.GeneralizableElementFacade#getGeneralizationList()
120     */
121    @Override
122    protected String handleGetGeneralizationList()
123    {
124        final StringBuilder list = new StringBuilder();
125        if (this.getGeneralizations() != null)
126        {
127            for (final Iterator iterator = this.getGeneralizations().iterator(); iterator.hasNext();)
128            {
129                list.append(((ModelElementFacade)iterator.next()).getFullyQualifiedName());
130                if (iterator.hasNext())
131                {
132                    list.append(", ");
133                }
134            }
135        }
136        return list.toString();
137    }
138
139    /**
140     * @see org.andromda.metafacades.uml.GeneralizableElementFacade#getAllSpecializations()
141     */
142    @Override
143    protected Collection<GeneralizableElementFacade> handleGetAllSpecializations()
144    {
145        final Set<GeneralizableElementFacade> allSpecializations = new LinkedHashSet<GeneralizableElementFacade>();
146        if (this.getSpecializations() != null)
147        {
148            allSpecializations.addAll(this.getSpecializations());
149            for (final Iterator iterator = this.getSpecializations().iterator(); iterator.hasNext();)
150            {
151                final GeneralizableElementFacade element = (GeneralizableElementFacade)iterator.next();
152                allSpecializations.addAll(element.getAllSpecializations());
153            }
154        }
155        return allSpecializations;
156    }
157
158    /**
159     * @see org.andromda.metafacades.uml.GeneralizableElementFacade#findTaggedValue(String, boolean)
160     */
161    @Override
162    protected Object handleFindTaggedValue(final String tagName, boolean follow)
163    {
164        Object value = this.findTaggedValue(tagName);
165        if (value == null)
166        {
167            for (GeneralizableElementFacade element = this.getGeneralization();
168                 value == null && element != null; element = element.getGeneralization())
169           {
170               value = element.findTaggedValue(tagName, follow);
171           }
172        }
173        return value;
174    }
175
176    /**
177     * @see org.andromda.metafacades.uml14.GeneralizableElementFacadeLogic#handleGetGeneralizationRoot()
178     */
179    protected GeneralizableElementFacade handleGetGeneralizationRoot()
180    {
181        return this.getGeneralization() == null
182            ? (GeneralizableElementFacade)THIS()
183            : this.getGeneralization().getGeneralizationRoot();
184    }
185}