/* 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.flowable.engine.impl.agenda;

import org.flowable.bpmn.model.BoundaryEvent;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.FlowNode;
import org.flowable.engine.common.api.FlowableException;
import org.flowable.engine.impl.delegate.ActivityBehavior;
import org.flowable.engine.impl.delegate.TriggerableActivityBehavior;
import org.flowable.engine.impl.interceptor.CommandContext;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;

/**
 * Operation that triggers a wait state and continues the process, leaving that activity.
 * 
 * The {@link ExecutionEntity} for this operations should be in a wait state (receive task for example) and have a {@link FlowElement} that has a behaviour that implements the
 * {@link TriggerableActivityBehavior}.
 * 
 * @author Joram Barrez
 */
public class TriggerExecutionOperation extends AbstractOperation {

    public TriggerExecutionOperation(CommandContext commandContext, ExecutionEntity execution) {
        super(commandContext, execution);
    }

    @Override
    public void run() {
        FlowElement currentFlowElement = getCurrentFlowElement(execution);
        if (currentFlowElement instanceof FlowNode) {

            ActivityBehavior activityBehavior = (ActivityBehavior) ((FlowNode) currentFlowElement).getBehavior();
            if (activityBehavior instanceof TriggerableActivityBehavior) {

                if (currentFlowElement instanceof BoundaryEvent) {
                    commandContext.getHistoryManager().recordActivityStart(execution);
                }

                ((TriggerableActivityBehavior) activityBehavior).trigger(execution, null, null);

                if (currentFlowElement instanceof BoundaryEvent) {
                    commandContext.getHistoryManager().recordActivityEnd(execution, null);
                }

            } else {
                throw new FlowableException("Invalid behavior: " + activityBehavior + " should implement " + TriggerableActivityBehavior.class.getName());
            }

        } else {
            throw new FlowableException("Programmatic error: no current flow element found or invalid type: " + currentFlowElement + ". Halting.");
        }
    }

}

@@ -2123,32 +2123,48 @@
               %7D%0A
+                
 %0A               
@@ -2493,25 +2493,109 @@
 on(%22
-Invalid b
+Cannot trigger execution with id %22 + execution.getId()%0A                    + %22 : the activityB
 ehavior
-:
  %22 +
@@ -2615,31 +2615,69 @@
 vior
- + %22 should implement %22
+.getClass() + %22 does not implement the %22 %0A                   
  + T
@@ -2718,20 +2718,43 @@
 etName()
-);%0A 
+ + %22 interface%22);%0A        %0A
         
@@ -2748,32 +2748,33 @@
     %0A           
+ 
 %7D%0A%0A        %7D els
@@ -2766,32 +2766,64 @@
 %0A%0A        %7D else
+ if (currentFlowElement == null)
  %7B%0A            t
@@ -2854,114 +2854,535 @@
 on(%22
-Programmatic error: no current flow element found or invalid type: %22 + currentFlowElement + %22. Halting.%22);
+Cannot trigger execution with id %22 + execution.getId()%0A                    + %22 : no current flow element found. Check the execution id that is being passed %22%0A                    + %22(it should not be a process instance execution, but a child execution currently referencing a flow element).%22);%0A            %0A        %7D else %7B%0A            throw new FlowableException(%22Programmatic error: cannot trigger execution, invalid flowelement type found: %22 %0A                    + currentFlowElement.getClass().getName() + %22.%22);%0A            
 %0A   

Improve exception messages when calling runtimeService.trigger(executionId)