/* 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.camunda.bpm.engine.impl.jobexecutor;

import java.util.HashMap;
import java.util.Map;

import org.camunda.bpm.engine.ProcessEngineException;
import org.camunda.bpm.engine.impl.interceptor.CommandContext;
import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity;
import org.camunda.bpm.engine.impl.pvm.process.ActivityImpl;
import org.camunda.bpm.engine.impl.pvm.process.TransitionImpl;
import org.camunda.bpm.engine.impl.pvm.runtime.operation.PvmAtomicOperation;

import static org.camunda.bpm.engine.impl.util.EnsureUtil.ensureNotNull;

/**
 *
 * @author Daniel Meyer
 * @author Thorben Lindhauer
 */
public class AsyncContinuationJobHandler implements JobHandler {

  public final static String TYPE = "async-continuation";

  private Map<String, PvmAtomicOperation> supportedOperations;

  public AsyncContinuationJobHandler() {
    supportedOperations = new HashMap<String, PvmAtomicOperation>();
    // async before activity
    supportedOperations.put(PvmAtomicOperation.TRANSITION_CREATE_SCOPE.getCanonicalName(), PvmAtomicOperation.TRANSITION_CREATE_SCOPE);
    // async before start event
    supportedOperations.put(PvmAtomicOperation.PROCESS_START.getCanonicalName(), PvmAtomicOperation.PROCESS_START);
    // async after activity depending if an outgoing sequence flow exists
    supportedOperations.put(PvmAtomicOperation.TRANSITION_NOTIFY_LISTENER_TAKE.getCanonicalName(), PvmAtomicOperation.TRANSITION_NOTIFY_LISTENER_TAKE);
    supportedOperations.put(PvmAtomicOperation.ACTIVITY_END.getCanonicalName(), PvmAtomicOperation.ACTIVITY_END);
  }

  public String getType() {
    return TYPE;
  }

  public void execute(String configuration, ExecutionEntity execution, CommandContext commandContext) {

    String operationName = null;
    String transitionId = null;
    if (configuration.contains("$")) {
      String[] configParts = configuration.split("\\$");
      if (configParts.length != 2) {
        throw new ProcessEngineException("Illegal async continuation job handler configuration: '" + configuration + "': exprecting two parts seperated by '$'.");
      }
      operationName = configParts[0];
      transitionId = configParts[1];

    } else {
      operationName = configuration;
    }

    PvmAtomicOperation atomicOperation = findMatchingAtomicOperation(operationName);
    ensureNotNull("Cannot process job with configuration " + configuration, "atomicOperation", atomicOperation);

    // reset transition id.
    if (transitionId != null) {
      ActivityImpl activity = execution.getActivity();
      TransitionImpl transition = activity.findOutgoingTransition(transitionId);
      execution.setTransition(transition);
    }

    commandContext
      .performOperation(atomicOperation, execution);
  }

  protected PvmAtomicOperation findMatchingAtomicOperation(String configuration) {
    if (configuration == null) {
      // default operation for backwards compatibility
      return PvmAtomicOperation.TRANSITION_CREATE_SCOPE;
    } else {
      return supportedOperations.get(configuration);
    }
  }
}

@@ -603,16 +603,90 @@
 cutor;%0A%0A
+import static org.camunda.bpm.engine.impl.util.EnsureUtil.ensureNotNull;%0A%0A
 import j
@@ -1120,82 +1120,8 @@
 n;%0A%0A
-import static org.camunda.bpm.engine.impl.util.EnsureUtil.ensureNotNull;%0A%0A
 /**%0A
@@ -2353,24 +2353,62 @@
 nId = null;%0A
+%0A    if (configuration != null ) %7B%0A%0A  
     if (conf
@@ -2430,24 +2430,26 @@
 ins(%22$%22)) %7B%0A
+  
       String
@@ -2495,24 +2495,26 @@
 %5C$%22);%0A      
+  
 if (configPa
@@ -2528,24 +2528,26 @@
 gth != 2) %7B%0A
+  
         thro
@@ -2703,18 +2703,22 @@
 ;%0A      
-%7D%0A
+  %7D%0A  
       op
@@ -2753,16 +2753,18 @@
 ;%0A      
+  
 transiti
@@ -2787,16 +2787,18 @@
 ts%5B1%5D;%0A%0A
+  
     %7D el
@@ -2804,24 +2804,26 @@
 lse %7B%0A      
+  
 operationNam
@@ -2841,16 +2841,25 @@
 ration;%0A
+      %7D%0A%0A
     %7D%0A%0A 

add a null-check