001package org.andromda.cartridges.jbpm.metafacades;
002
003import java.util.ArrayList;
004import java.util.List;
005import org.andromda.metafacades.uml.ActionStateFacade;
006import org.andromda.metafacades.uml.ActivityGraphFacade;
007import org.andromda.metafacades.uml.FinalStateFacade;
008import org.andromda.metafacades.uml.PartitionFacade;
009import org.andromda.metafacades.uml.PseudostateFacade;
010import org.andromda.metafacades.uml.StateFacade;
011import org.andromda.metafacades.uml.UMLProfile;
012import org.andromda.utils.StringUtilsHelper;
013import org.apache.commons.lang.StringUtils;
014
015/**
016 * MetafacadeLogic implementation for org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinition.
017 *
018 * @see org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinition
019 */
020public class JBpmProcessDefinitionLogicImpl
021    extends JBpmProcessDefinitionLogic
022{
023    private static final long serialVersionUID = 34L;
024    /**
025     * @param metaObject
026     * @param context
027     */
028    public JBpmProcessDefinitionLogicImpl(Object metaObject, String context)
029    {
030        super(metaObject, context);
031    }
032
033    /**
034     * @see org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinitionLogic#handleGetStates()
035     */
036    protected List<JBpmState> handleGetStates()
037    {
038        final List<JBpmState> states = new ArrayList<JBpmState>();
039        final ActivityGraphFacade graph = this.getFirstActivityGraph();
040        if (graph != null)
041        {
042            for (final StateFacade state : graph.getStates())
043            {
044                if (state instanceof JBpmState)
045                {
046                    states.add((JBpmState)state);
047                }
048            }
049        }
050        return states;
051    }
052
053    /**
054     * @see org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinitionLogic#handleGetNodes()
055     */
056    protected List<JBpmNode> handleGetNodes()
057    {
058        final List<JBpmNode> states = new ArrayList<JBpmNode>();
059        final ActivityGraphFacade graph = this.getFirstActivityGraph();
060        if (graph != null)
061        {
062            for (final ActionStateFacade state : graph.getActionStates())
063            {
064                if (state instanceof JBpmNode && !((JBpmNode)state).isTaskNode())
065                {
066                    states.add((JBpmNode)state);
067                }
068            }
069        }
070        return states;
071    }
072
073    /**
074     * @see org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinitionLogic#handleGetTaskNodes()
075     */
076    protected List<JBpmNode> handleGetTaskNodes()
077    {
078        final List<JBpmNode> taskNodes = new ArrayList<JBpmNode>();
079        final ActivityGraphFacade graph = this.getFirstActivityGraph();
080        if (graph != null)
081        {
082            for (final ActionStateFacade state : graph.getActionStates())
083            {
084                if (state instanceof JBpmNode && ((JBpmNode)state).isTaskNode())
085                {
086                    taskNodes.add((JBpmNode)state);
087                }
088            }
089        }
090        return taskNodes;
091    }
092
093    /**
094     * @see org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinitionLogic#handleGetTasks()
095     */
096    protected List<JBpmAction> handleGetTasks()
097    {
098        final List<JBpmAction> tasks = new ArrayList<JBpmAction>();
099        for (JBpmNode node : getTaskNodes())
100        {
101            tasks.addAll(node.getTasks());
102        }
103        return tasks;
104    }
105
106    /**
107     * @see org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinitionLogic#handleIsBusinessProcess()
108     */
109    protected boolean handleIsBusinessProcess()
110    {
111        return true;
112    }
113
114    /**
115     * @see org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinitionLogic#handleGetSwimlanes()
116     */
117    protected List<PartitionFacade> handleGetSwimlanes()
118    {
119        final List<PartitionFacade> swimlanes = new ArrayList<PartitionFacade>();
120        final ActivityGraphFacade graph = this.getFirstActivityGraph();
121        if (graph != null)
122        {
123            swimlanes.addAll(graph.getPartitions());
124        }
125        return swimlanes;
126    }
127
128    /**
129     * @see org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinitionLogic#handleGetStartState()
130     */
131    protected PseudostateFacade  handleGetStartState()
132    {
133        PseudostateFacade  startState = null;
134        final ActivityGraphFacade graph = this.getFirstActivityGraph();
135        if (graph != null)
136        {
137            startState = graph.getInitialState();
138        }
139        return startState;
140    }
141
142    /**
143     * @see org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinitionLogic#handleGetEndStates()
144     */
145    protected List<FinalStateFacade> handleGetEndStates()
146    {
147        final List<FinalStateFacade> endStates = new ArrayList<FinalStateFacade>();
148        final ActivityGraphFacade graph = this.getFirstActivityGraph();
149        if (graph != null)
150        {
151            endStates.addAll(graph.getFinalStates());
152        }
153        return endStates;
154    }
155
156    /**
157     * @see org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinitionLogic#handleGetDecisions()
158     */
159    protected List<PseudostateFacade> handleGetDecisions()
160    {
161        final List<PseudostateFacade> decisions = new ArrayList<PseudostateFacade>();
162        final ActivityGraphFacade graph = this.getFirstActivityGraph();
163        if (graph != null)
164        {
165            for (final PseudostateFacade pseudostate : graph.getPseudostates())
166            {
167                if (pseudostate.isDecisionPoint())
168                {
169                    decisions.add(pseudostate);
170                }
171            }
172        }
173        return decisions;
174    }
175
176    /**
177     * @see org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinitionLogic#handleGetForks()
178     */
179    protected List<PseudostateFacade> handleGetForks()
180    {
181        final List<PseudostateFacade> forks = new ArrayList<PseudostateFacade>();
182        final ActivityGraphFacade graph = this.getFirstActivityGraph();
183        if (graph != null)
184        {
185            for (final PseudostateFacade pseudostate : graph.getPseudostates())
186            {
187                if (pseudostate.isSplit())
188                {
189                    forks.add(pseudostate);
190                }
191            }
192        }
193        return forks;
194    }
195
196    /**
197     * @see org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinitionLogic#handleGetJoins()
198     */
199    protected List<PseudostateFacade> handleGetJoins()
200    {
201        final List<PseudostateFacade> joins = new ArrayList<PseudostateFacade>();
202        final ActivityGraphFacade graph = this.getFirstActivityGraph();
203        if (graph != null)
204        {
205            for (final PseudostateFacade pseudostate : graph.getPseudostates())
206            {
207                if (pseudostate.isCollect())
208                {
209                    joins.add(pseudostate);
210                }
211            }
212        }
213        return joins;
214    }
215
216    /**
217     * @see org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinitionLogic#handleGetDescriptorFullPath()
218     */
219    protected String handleGetDescriptorFullPath()
220    {
221        final StringBuilder pathBuffer = new StringBuilder();
222
223        if (StringUtils.isNotBlank(this.getPackagePath()))
224        {
225            pathBuffer.append(this.getPackagePath());
226            pathBuffer.append('/');
227        }
228        pathBuffer.append(StringUtilsHelper.separate(this.getName(), "-").toLowerCase());
229        pathBuffer.append(".pdl.xml");
230
231        return pathBuffer.toString();
232    }
233
234    /**
235     * @see org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinitionLogic#handleGetNodeInterfaceName()
236     */
237    protected String handleGetNodeInterfaceName()
238    {
239        return StringUtilsHelper.upperCamelCaseName(this.getName()) + "ProcessNode";
240    }
241
242    /**
243     * Overwritten because we want to be able to make use of the AndroMDA tagged value for use-case
244     * to activity graph linking.
245     * @see org.andromda.cartridges.jbpm.metafacades.JBpmProcessDefinitionLogic#getFirstActivityGraph()
246     */
247    public ActivityGraphFacade getFirstActivityGraph()
248    {
249        ActivityGraphFacade activityGraph;
250
251        final Object activity = this.findTaggedValue(UMLProfile.TAGGEDVALUE_PRESENTATION_USECASE_ACTIVITY);
252        if (activity == null)
253        {
254            activityGraph = super.getFirstActivityGraph();
255        }
256        else
257        {
258            final String activityName = String.valueOf(activity.toString());
259            activityGraph = this.getModel().findActivityGraphByName(activityName);
260        }
261
262        return activityGraph;
263    }
264}