001package org.andromda.metafacades.uml14;
002
003import java.util.Collection;
004import java.util.LinkedHashSet;
005import java.util.Set;
006import org.andromda.metafacades.uml.ActivityGraphFacade;
007import org.andromda.metafacades.uml.ClassifierFacade;
008import org.andromda.metafacades.uml.UseCaseFacade;
009import org.apache.commons.collections.Predicate;
010import org.omg.uml.UmlPackage;
011import org.omg.uml.behavioralelements.activitygraphs.ActionState;
012import org.omg.uml.behavioralelements.activitygraphs.ActivityGraph;
013import org.omg.uml.behavioralelements.statemachines.CompositeState;
014import org.omg.uml.behavioralelements.usecases.UseCase;
015import org.omg.uml.foundation.core.ModelElement;
016
017/**
018 * Metaclass facade implementation.
019 * @author Bob Fields
020 */
021public class ModelFacadeLogicImpl
022    extends ModelFacadeLogic
023{
024    private static final long serialVersionUID = 34L;
025    // ---------------- constructor -------------------------------
026    /**
027     * @param metaObject
028     * @param context
029     */
030    public ModelFacadeLogicImpl(
031        UmlPackage metaObject,
032        String context)
033    {
034        super(metaObject, context);
035    }
036
037    /**
038     * @see org.andromda.metafacades.uml.ModelFacade#getRootPackage()
039     */
040    @Override
041    protected Object handleGetRootPackage()
042    {
043        Collection rootPackages = metaObject.getModelManagement().getModel().refAllOfType();
044        return rootPackages.iterator().next();
045    }
046
047    /**
048     * @see org.andromda.metafacades.uml.ModelFacade#getAllActors()
049     */
050    @Override
051    protected Collection handleGetAllActors()
052    {
053        return metaObject.getUseCases().getActor().refAllOfType();
054    }
055
056    /**
057     * @see org.andromda.metafacades.uml.ModelFacade#getAllActionStates()
058     */
059    @Override
060    protected Collection handleGetAllActionStates()
061    {
062        return metaObject.getActivityGraphs().getActionState().refAllOfType();
063    }
064
065    /**
066     * @see org.andromda.metafacades.uml.ModelFacade#getAllUseCases()
067     */
068    @Override
069    protected Collection handleGetAllUseCases()
070    {
071        return metaObject.getUseCases().getUseCase().refAllOfType();
072    }
073
074    /**
075     * @see org.andromda.metafacades.uml.ModelFacade#getAllClasses()
076     */
077    @Override
078    protected Collection handleGetAllClasses()
079    {
080        return metaObject.getCore().getUmlClass().refAllOfType();
081    }
082
083    /**
084     * @see org.andromda.metafacades.uml.ModelFacade#getAllObjectFlowStates()
085     */
086    @Override
087    protected Collection handleGetAllObjectFlowStates()
088    {
089        return metaObject.getActivityGraphs().getObjectFlowState().refAllOfType();
090    }
091
092    /**
093     * @see org.andromda.metafacades.uml.ModelFacade#findUseCaseWithTaggedValueOrHyperlink(String, String)
094     */
095    @Override
096    protected UseCaseFacade handleFindUseCaseWithTaggedValueOrHyperlink(
097        String tag,
098        String value)
099    {
100        return (UseCaseFacade)shieldedElement(UML14MetafacadeUtils.findUseCaseWithTaggedValueOrHyperlink(tag, value));
101    }
102
103    /**
104     * @see org.andromda.metafacades.uml.ModelFacade#findClassWithTaggedValueOrHyperlink(String, String)
105     */
106    @Override
107    protected ClassifierFacade handleFindClassWithTaggedValueOrHyperlink(
108        String tag,
109        String value)
110    {
111        return (ClassifierFacade)shieldedElement(UML14MetafacadeUtils.findClassWithTaggedValueOrHyperlink(tag, value));
112    }
113
114    /**
115     * @see org.andromda.metafacades.uml.ModelFacade#findActivityGraphByName(String)
116     */
117    @Override
118    protected ActivityGraphFacade handleFindActivityGraphByName(String name)
119    {
120        return (ActivityGraphFacade)shieldedElement(UML14MetafacadeUtils.findFirstActivityGraphWithName(name));
121    }
122
123    /**
124     * @see org.andromda.metafacades.uml.ModelFacade#findActivityGraphByNameAndStereotype(String, String)
125     */
126    @Override
127    protected ActivityGraphFacade handleFindActivityGraphByNameAndStereotype(
128        String name,
129        String stereotypeName)
130    {
131        return (ActivityGraphFacade)shieldedElement(
132            UML14MetafacadeUtils.findFirstActivityGraphWithNameAndStereotype(name, stereotypeName));
133    }
134
135    /**
136     * @see org.andromda.metafacades.uml.ModelFacade#findUseCaseByName(String)
137     */
138    @Override
139    protected UseCaseFacade handleFindUseCaseByName(String name)
140    {
141        return (UseCaseFacade)shieldedElement(UML14MetafacadeUtils.findFirstUseCaseWithName(name));
142    }
143
144    /**
145     * @see org.andromda.metafacades.uml.ModelFacade#findUseCaseWithNameAndStereotype(String, String)
146     */
147    @Override
148    protected UseCaseFacade handleFindUseCaseWithNameAndStereotype(
149        String name,
150        String stereotypeName)
151    {
152        return (UseCaseFacade)shieldedElement(
153            UML14MetafacadeUtils.findFirstUseCaseWithNameAndStereotype(name, stereotypeName));
154    }
155
156    /**
157     * @see org.andromda.metafacades.uml.ModelFacade#findFinalStatesWithNameOrHyperlink(org.andromda.metafacades.uml.UseCaseFacade)
158     */
159    @Override
160    protected Collection handleFindFinalStatesWithNameOrHyperlink(UseCaseFacade useCase)
161    {
162        UseCase useCaseMetaClass = UML14MetafacadeUtils.getMetaClass(useCase);
163        return shieldedElements(UML14MetafacadeUtils.findFinalStatesWithNameOrHyperlink(useCaseMetaClass));
164    }
165
166    /**
167     * @see org.andromda.metafacades.uml.ModelFacade#getAllActionStatesWithStereotype(org.andromda.metafacades.uml.ActivityGraphFacade, String)
168     */
169    @Override
170    protected Collection handleGetAllActionStatesWithStereotype(
171        ActivityGraphFacade activityGraph,
172        String stereotypeName)
173    {
174        ActivityGraph activityGraphMetaClass = UML14MetafacadeUtils.getMetaClass(activityGraph);
175
176        CompositeState compositeState = (CompositeState)activityGraphMetaClass.getTop();
177        return filter(
178            compositeState.getSubvertex(),
179            new ActionStateWithStereotypeFilter(stereotypeName));
180    }
181
182    /**
183     * @see org.andromda.metafacades.uml14.ModelFacadeLogic#handleGetAllTransitions()
184     */
185    protected Collection handleGetAllTransitions()
186    {
187        return this.metaObject.getStateMachines().getTransition().refAllOfType();
188    }
189
190    private static final class ActionStateWithStereotypeFilter
191        implements Predicate
192    {
193        private String stereotypeName = null;
194
195        public ActionStateWithStereotypeFilter(final String stereotypeName)
196        {
197            this.stereotypeName = stereotypeName;
198        }
199
200        public boolean evaluate(final Object object)
201        {
202            return (object instanceof ActionState) &&
203            UML14MetafacadeUtils.isStereotypePresent((ModelElement)object, stereotypeName);
204        }
205    }
206
207    private Collection filter(
208        Collection collection,
209        Predicate collectionFilter)
210    {
211        final Set filteredCollection = new LinkedHashSet();
212        for (Object object : collection)
213        {
214            if (collectionFilter.evaluate(object))
215            {
216                filteredCollection.add(object);
217            }
218        }
219        return filteredCollection;
220    }
221}