AtomicOperationTransitionNotifyListenerTake.java 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.activiti5.engine.impl.pvm.runtime;

import java.util.List;

17
import org.activiti.engine.delegate.ExecutionListener;
18 19 20 21 22 23 24 25 26 27
import org.activiti5.engine.delegate.event.ActivitiEventType;
import org.activiti5.engine.delegate.event.impl.ActivitiEventBuilder;
import org.activiti5.engine.impl.context.Context;
import org.activiti5.engine.impl.pvm.PvmException;
import org.activiti5.engine.impl.pvm.process.ActivityImpl;
import org.activiti5.engine.impl.pvm.process.ScopeImpl;
import org.activiti5.engine.impl.pvm.process.TransitionImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

28

29 30 31 32
/**
 * @author Tom Baeyens
 */
public class AtomicOperationTransitionNotifyListenerTake implements AtomicOperation {
33
  
34
  private static Logger log = LoggerFactory.getLogger(AtomicOperationTransitionNotifyListenerTake.class);
35
  
36 37 38
  public boolean isAsync(InterpretableExecution execution) {
    return false;
  }
T
Tijs Rademakers 已提交
39

40 41
  public void execute(InterpretableExecution execution) {
    TransitionImpl transition = execution.getTransition();
42
    
43 44
    List<ExecutionListener> executionListeners = transition.getExecutionListeners();
    int executionListenerIndex = execution.getExecutionListenerIndex();
45 46
    
    if (executionListeners.size()>executionListenerIndex) {
47 48 49 50 51 52 53 54
      execution.setEventName(org.activiti5.engine.impl.pvm.PvmEvent.EVENTNAME_TAKE);
      execution.setEventSource(transition);
      ExecutionListener listener = executionListeners.get(executionListenerIndex);
      try {
        listener.notify(execution);
      } catch (RuntimeException e) {
        throw e;
      } catch (Exception e) {
55
        throw new PvmException("couldn't execute event listener : "+e.getMessage(), e);
56
      }
57
      execution.setExecutionListenerIndex(executionListenerIndex+1);
58
      execution.performOperation(this);
T
Tijs Rademakers 已提交
59

60
    } else {
61 62 63
    	if (log.isDebugEnabled()) {
    		log.debug("{} takes transition {}", execution, transition);
    	}
64 65 66
      execution.setExecutionListenerIndex(0);
      execution.setEventName(null);
      execution.setEventSource(null);
T
Tijs Rademakers 已提交
67

68 69 70
      ActivityImpl activity = (ActivityImpl) execution.getActivity();
      ActivityImpl nextScope = findNextScope(activity.getParent(), transition.getDestination());
      execution.setActivity(nextScope);
71 72 73 74 75 76 77
      
      // Firing event that transition is being taken     	
      if(Context.getProcessEngineConfiguration() != null && Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
      	Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
    			ActivitiEventBuilder.createSequenceFlowTakenEvent(ActivitiEventType.SEQUENCEFLOW_TAKEN, transition.getId(),
    					activity.getId(), (String) activity.getProperties().get("name") ,(String) activity.getProperties().get("type"), activity.getActivityBehavior().getClass().getCanonicalName(),
    					nextScope.getId(), (String) nextScope.getProperties().get("name"), (String) nextScope.getProperties().get("type"), nextScope.getActivityBehavior().getClass().getCanonicalName()));
78
      }
79
      
80
      execution.performOperation(TRANSITION_CREATE_SCOPE);
81
    }
82
  }
83

84
  /** finds the next scope to enter.  the most outer scope is found first */
85 86
  public static ActivityImpl findNextScope(ScopeImpl outerScopeElement, ActivityImpl destination) {
    ActivityImpl nextScope = destination;
87 88 89
    while( (nextScope.getParent() instanceof ActivityImpl)
           && (nextScope.getParent() != outerScopeElement)
         ) {
90
      nextScope = (ActivityImpl) nextScope.getParent();
91
    }
92 93
    return nextScope;
  }
94
}