/**
 * Copyright 2005 JBoss Inc
 *
 * 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.jbpm.workflow.instance.node;

import java.util.List;

import org.drools.runtime.process.NodeInstance;
import org.drools.spi.ProcessContext;
import org.jbpm.process.core.context.exception.ExceptionScope;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.process.core.event.EventTransformer;
import org.jbpm.process.instance.context.exception.ExceptionScopeInstance;
import org.jbpm.process.instance.context.variable.VariableScopeInstance;
import org.jbpm.process.instance.impl.Action;
import org.jbpm.workflow.core.DroolsAction;
import org.jbpm.workflow.core.impl.ExtendedNodeImpl;
import org.jbpm.workflow.core.node.EndNode;
import org.jbpm.workflow.core.node.EventNode;
import org.jbpm.workflow.instance.impl.NodeInstanceImpl;

/**
 * Runtime counterpart of an event node.
 * 
 * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
 */
public class EventNodeInstance extends NodeInstanceImpl implements EventNodeInstanceInterface {

    private static final long serialVersionUID = 510l;

    public void signalEvent(String type, Object event) {
    	String variableName = getEventNode().getVariableName();
    	if (variableName != null) {
    		VariableScopeInstance variableScopeInstance = (VariableScopeInstance)
    			resolveContextInstance(VariableScope.VARIABLE_SCOPE, variableName);
    		if (variableScopeInstance == null) {
    			throw new IllegalArgumentException(
					"Could not find variable for event node: " + variableName);
    		}
    		EventTransformer transformer = getEventNode().getEventTransformer();
    		if (transformer != null) {
    			event = transformer.transformEvent(event);
    		}
    		variableScopeInstance.setVariable(variableName, event);
    	}
    	triggerCompleted();
    }
    
    public void internalTrigger(final NodeInstance from, String type) {
    	if (!org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
            throw new IllegalArgumentException(
                "An EventNode only accepts default incoming connections!");
        }
        // Do nothing, event activated
    }
    
    public EventNode getEventNode() {
        return (EventNode) getNode();
    }

    public void triggerCompleted() {
        List<DroolsAction> actions = getEventNode().getActions(EndNode.EVENT_NODE_EXIT);
        if (actions != null) {
            for (DroolsAction droolsAction: actions) {
                executeAction(droolsAction);
            }
        }
        triggerCompleted(org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE, true);
    }
    
    protected void executeAction(DroolsAction droolsAction) {
        Action action = (Action) droolsAction.getMetaData("Action");
        ProcessContext context = new ProcessContext(getProcessInstance().getKnowledgeRuntime());
        context.setNodeInstance(this);
        try {
            action.execute(context);
        } catch (Exception exception) {
            exception.printStackTrace();
            String exceptionName = exception.getClass().getName();
            ExceptionScopeInstance exceptionScopeInstance = (ExceptionScopeInstance)
                resolveContextInstance(ExceptionScope.EXCEPTION_SCOPE, exceptionName);
            if (exceptionScopeInstance == null) {
                exception.printStackTrace();
                throw new IllegalArgumentException(
                    "Could not find exception handler for " + exceptionName + " while executing node " + getNodeId());
            }
            exceptionScopeInstance.handleException(exceptionName, exception);
        }
    }
}

@@ -635,32 +635,8 @@
 e;%0A%0A
-import java.util.List;%0A%0A
 impo
@@ -683,109 +683,8 @@
 ce;%0A
-import org.drools.spi.ProcessContext;%0Aimport org.jbpm.process.core.context.exception.ExceptionScope;%0A
 impo
@@ -838,299 +838,37 @@
 ext.
-exception.ExceptionScopeInstance;%0Aimport org.jbpm.process.instance.context.variable.VariableScopeInstance;%0Aimport org.jbpm.process.instance.impl.Action;%0Aimport org.jbpm.workflow.core.DroolsAction;%0Aimport org.jbpm.workflow.core.impl.ExtendedNodeImpl;%0Aimport org.jbpm.workflow.core.node.EndNod
+variable.VariableScopeInstanc
 e;%0Ai
@@ -951,16 +951,24 @@
 ce.impl.
+Extended
 NodeInst
@@ -1144,16 +1144,24 @@
 extends 
+Extended
 NodeInst
@@ -2464,1350 +2464,94 @@
     
-List%3CDroolsAction%3E actions = getEventNode().getActions(EndNode.EVENT_NODE_EXIT);%0A        if (actions != null) %7B%0A            for (DroolsAction droolsAction: actions) %7B%0A                executeAction(droolsAction);%0A            %7D%0A        %7D%0A        triggerCompleted(org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE, true);%0A    %7D%0A    %0A    protected void executeAction(DroolsAction droolsAction) %7B%0A        Action action = (Action) droolsAction.getMetaData(%22Action%22);%0A        ProcessContext context = new ProcessContext(getProcessInstance().getKnowledgeRuntime());%0A        context.setNodeInstance(this);%0A        try %7B%0A            action.execute(context);%0A        %7D catch (Exception exception) %7B%0A            exception.printStackTrace();%0A            String exceptionName = exception.getClass().getName();%0A            ExceptionScopeInstance exceptionScopeInstance = (ExceptionScopeInstance)%0A                resolveContextInstance(ExceptionScope.EXCEPTION_SCOPE, exceptionName);%0A            if (exceptionScopeInstance == null) %7B%0A                exception.printStackTrace();%0A                throw new IllegalArgumentException(%0A                    %22Could not find exception handler for %22 + exceptionName + %22 while executing node %22 + getNodeId());%0A            %7D%0A            exceptionScopeInstance.handleException(exceptionName, exception);%0A        %7D%0A    %7D
+triggerCompleted(org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE, true);%0A    %7D%0A    
 %0A%7D%0A

remove duplicated code and use ExtendedNodeInstanceImpl as super class instead