/* 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.migration.instance.parser;

import org.camunda.bpm.engine.impl.migration.instance.MigratingActivityInstance;
import org.camunda.bpm.engine.impl.migration.instance.MigratingExternalTaskInstance;
import org.camunda.bpm.engine.impl.migration.instance.MigratingIncident;
import org.camunda.bpm.engine.impl.migration.instance.MigratingJobInstance;
import org.camunda.bpm.engine.impl.persistence.entity.IncidentEntity;
import org.camunda.bpm.engine.impl.persistence.entity.JobDefinitionEntity;

/**
 * @author Thorben Lindhauer
 *
 */
public class IncidentInstanceHandler implements MigratingInstanceParseHandler<IncidentEntity> {

  @Override
  public void handle(MigratingInstanceParseContext parseContext, IncidentEntity incident) {
    if (incident.getConfiguration() == null &&
            (isFailedJobIncident(incident) || isExternalTaskIncident(incident))) {
      handleCallActivityIncident(parseContext, incident);
    }
    else if (isFailedJobIncident(incident)) {
      handleFailedJobIncident(parseContext, incident);
    }
    else if (isExternalTaskIncident(incident)) {
      handleExternalTaskIncident(parseContext, incident);
    }
  }

  protected void handleCallActivityIncident(MigratingInstanceParseContext parseContext, IncidentEntity incident) {
    MigratingActivityInstance owningInstance = parseContext.getMigratingActivityInstanceById(incident.getExecution().getActivityInstanceId());
    if (owningInstance != null) {
      parseContext.consume(incident);
      MigratingIncident migratingIncident = new MigratingIncident(incident, owningInstance.getTargetScope());
      owningInstance.addMigratingDependentInstance(migratingIncident);
    }
  }

  protected boolean isFailedJobIncident(IncidentEntity incident) {
    return IncidentEntity.FAILED_JOB_HANDLER_TYPE.equals(incident.getIncidentType());
  }

  protected void handleFailedJobIncident(MigratingInstanceParseContext parseContext, IncidentEntity incident) {
    MigratingJobInstance owningInstance = parseContext.getMigratingJobInstanceById(incident.getConfiguration());
    if (owningInstance != null && owningInstance.migrates()) {
      parseContext.consume(incident);
      MigratingIncident migratingIncident = new MigratingIncident(incident, owningInstance.getTargetScope());
      JobDefinitionEntity targetJobDefinitionEntity = owningInstance.getTargetJobDefinitionEntity();
      if (targetJobDefinitionEntity != null) {
        migratingIncident.setTargetJobDefinitionId(targetJobDefinitionEntity.getId());
      }
      owningInstance.addMigratingDependentInstance(migratingIncident);
    }
  }

  protected boolean isExternalTaskIncident(IncidentEntity incident) {
    return IncidentEntity.EXTERNAL_TASK_HANDLER_TYPE.equals(incident.getIncidentType());
  }

  protected void handleExternalTaskIncident(MigratingInstanceParseContext parseContext, IncidentEntity incident) {
    MigratingExternalTaskInstance owningInstance = parseContext.getMigratingExternalTaskInstanceById(incident.getConfiguration());
    if (owningInstance != null) {
      parseContext.consume(incident);
      MigratingIncident migratingIncident = new MigratingIncident(incident, owningInstance.getTargetScope());
      owningInstance.addMigratingDependentInstance(migratingIncident);
    }
  }

}

@@ -1362,19 +1362,16 @@
  == null
- &&
 %0A       
@@ -1374,16 +1374,19 @@
         
+ &&
  (isFail
@@ -1504,36 +1504,32 @@
 incident);%0A    %7D
-%0A   
  else if (isFail
@@ -1615,20 +1615,16 @@
 );%0A    %7D
-%0A   
  else if
@@ -2669,41 +2669,12 @@
 null
- && owningInstance.migrates()
 ) %7B%0A
+
     
@@ -2699,32 +2699,73 @@
 sume(incident);%0A
+      if (owningInstance.migrates()) %7B%0A  
       MigratingI
@@ -2864,16 +2864,18 @@
 ;%0A      
+  
 JobDefin
@@ -2961,16 +2961,18 @@
 tity();%0A
+  
       if
@@ -3018,16 +3018,18 @@
         
+  
 migratin
@@ -3105,18 +3105,22 @@
 ;%0A      
-%7D%0A
+  %7D%0A  
       ow
@@ -3174,32 +3174,40 @@
 atingIncident);%0A
+      %7D%0A
     %7D%0A  %7D%0A%0A  pro

fix failing migration test cases