001package org.andromda.cartridges.jbpm.metafacades;
002
003import java.util.ArrayList;
004import java.util.List;
005import org.andromda.metafacades.uml.ActivityGraphFacade;
006import org.andromda.metafacades.uml.EventFacade;
007import org.andromda.metafacades.uml.StateFacade;
008import org.andromda.metafacades.uml.StateMachineFacade;
009import org.andromda.metafacades.uml.TransitionFacade;
010import org.andromda.utils.StringUtilsHelper;
011import org.apache.commons.lang.StringUtils;
012
013/**
014 * MetafacadeLogic implementation for org.andromda.cartridges.jbpm.metafacades.JBpmHandler.
015 *
016 * @see org.andromda.cartridges.jbpm.metafacades.JBpmHandler
017 */
018public class JBpmHandlerLogicImpl
019        extends JBpmHandlerLogic
020{
021    private static final long serialVersionUID = 34L;
022    /**
023     * @param metaObject
024     * @param context
025     */
026    public JBpmHandlerLogicImpl(Object metaObject, String context)
027    {
028        super(metaObject, context);
029    }
030
031    /**
032     * @see org.andromda.cartridges.jbpm.metafacades.JBpmHandlerLogic#handleIsContainedInBusinessProcess()
033     */
034    protected boolean handleIsContainedInBusinessProcess()
035    {
036        return this.getOwner().getStateMachineContext() instanceof ActivityGraphFacade &&
037                ((ActivityGraphFacade)this.getOwner().getStateMachineContext()).getUseCase()
038                        instanceof JBpmProcessDefinition;
039    }
040
041    /**
042     * @see org.andromda.cartridges.jbpm.metafacades.JBpmHandlerLogic#handleIsAssignmentHandler()
043     */
044    protected boolean handleIsAssignmentHandler()
045    {
046        boolean assignmentHandler = false;
047
048        final List actions = internalJBpmActions();
049        for (int i = 0; i < actions.size() && !assignmentHandler; i++)
050        {
051            final JBpmAction action = (JBpmAction)actions.get(i);
052            assignmentHandler = action.isTask();
053        }
054
055        return assignmentHandler;
056    }
057
058    /**
059     * @see org.andromda.cartridges.jbpm.metafacades.JBpmHandlerLogic#handleIsActionHandler()
060     */
061    protected boolean handleIsActionHandler()
062    {
063        boolean actionHandler = false;
064
065        final List actions = internalJBpmActions();
066        for (int i = 0; i < actions.size() && !actionHandler; i++)
067        {
068            final JBpmAction action = (JBpmAction)actions.get(i);
069            actionHandler =
070                    action.isAfterSignal() || action.isBeforeSignal() || action.isNodeEnter() || action.isNodeLeave() ||
071                            action.isTimer();
072        }
073
074        return actionHandler;
075    }
076
077    private List<EventFacade> internalActions = null;
078
079    private List<EventFacade> internalJBpmActions()
080    {
081        if (this.internalActions == null)
082        {
083            internalActions = new ArrayList<EventFacade>();
084            final StateMachineFacade stateMachine = getOwner().getStateMachineContext();
085            if (stateMachine instanceof ActivityGraphFacade)
086            {
087                final ActivityGraphFacade graph = (ActivityGraphFacade)stateMachine;
088                for (final StateFacade state : graph.getStates())
089                {
090                    for (final EventFacade event : state.getDeferrableEvents())
091                    {
092                        if (event instanceof JBpmAction)
093                        {
094                            final JBpmAction action = (JBpmAction)event;
095                            if (this.equals(action.getOperation()))
096                            {
097                                internalActions.add(event);
098                            }
099                        }
100                    }
101                }
102
103                for (final TransitionFacade transition : graph.getTransitions())
104                {
105                    final EventFacade event = transition.getTrigger();
106                    if (event != null)
107                    {
108                        if (event instanceof JBpmAction)
109                        {
110                            final JBpmAction action = (JBpmAction)event;
111                            if (this.equals(action.getOperation()))
112                            {
113                                internalActions.add(event);
114                            }
115                        }
116                    }
117                }
118            }
119        }
120
121        return internalActions;
122    }
123
124    /**
125     * @see org.andromda.cartridges.jbpm.metafacades.JBpmHandlerLogic#handleGetHandlerPackageName()
126     */
127    protected String handleGetHandlerPackageName()
128    {
129        return this.getOwner().getPackageName();
130    }
131
132    /**
133     * @see org.andromda.cartridges.jbpm.metafacades.JBpmHandlerLogic#handleGetHandlerFullPath()
134     */
135    protected String handleGetHandlerFullPath()
136    {
137        return StringUtils.replace(this.getClazz(), ".", "/");
138    }
139
140    /**
141     * @see org.andromda.cartridges.jbpm.metafacades.JBpmHandlerLogic#handleGetHandlerClassName()
142     */
143    protected String handleGetHandlerClassName()
144    {
145        return StringUtilsHelper.upperCamelCaseName(this.getName());
146    }
147
148    /**
149     * @see org.andromda.cartridges.jbpm.metafacades.JBpmHandlerLogic#handleGetClazz()
150     */
151    protected String handleGetClazz()
152    {
153        String handlerClass = null;
154
155        final StringBuilder clazzBuffer = new StringBuilder();
156        if (StringUtils.isNotBlank(this.getHandlerPackageName()))
157        {
158            clazzBuffer.append(this.getHandlerPackageName());
159            clazzBuffer.append('.');
160        }
161        clazzBuffer.append(this.getHandlerClassName());
162        handlerClass = clazzBuffer.toString();
163
164        return handlerClass;
165    }
166}