package org.camunda.bpm.engine.rest.exception;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import org.camunda.bpm.engine.AuthorizationException;
import org.camunda.bpm.engine.BadUserRequestException;
import org.camunda.bpm.engine.ProcessEngineException;
import org.camunda.bpm.engine.migration.MigratingProcessInstanceValidationException;
import org.camunda.bpm.engine.migration.MigrationPlanValidationException;
import org.camunda.bpm.engine.rest.dto.AuthorizationExceptionDto;
import org.camunda.bpm.engine.rest.dto.ExceptionDto;
import org.camunda.bpm.engine.rest.dto.migration.MigratingProcessInstanceValidationExceptionDto;
import org.camunda.bpm.engine.rest.dto.migration.MigrationPlanValidationExceptionDto;

/**
 * @author Svetlana Dorokhova.
 */
public class ExceptionHandlerHelper {

  private static ExceptionHandlerHelper INSTANCE = new ExceptionHandlerHelper();

  private ExceptionHandlerHelper() {
  }

  public static ExceptionHandlerHelper getInstance(){
    return INSTANCE;
  }

  public ExceptionDto fromException(Throwable e) {
    if (e instanceof MigratingProcessInstanceValidationException) {
      return MigratingProcessInstanceValidationExceptionDto.from((MigratingProcessInstanceValidationException)e);
    } else if (e instanceof MigrationPlanValidationException) {
      return MigrationPlanValidationExceptionDto.from((MigrationPlanValidationException)e);
    } else if (e instanceof AuthorizationException) {
      return AuthorizationExceptionDto.fromException((AuthorizationException)e);
    } else {
      return ExceptionDto.fromException(e);
    }
  }

  public Response.Status getStatus(Throwable exception) {
    Response.Status responseStatus = Response.Status.INTERNAL_SERVER_ERROR;

    if (exception instanceof ProcessEngineException) {
      responseStatus = getStatus((ProcessEngineException)exception);
    }
    else if (exception instanceof MigrationPlanValidationException
      || exception instanceof MigratingProcessInstanceValidationException
      || exception instanceof BadUserRequestException) {
      responseStatus = Response.Status.BAD_REQUEST;
    } else if (exception instanceof RestException) {
      responseStatus = getStatus((RestException) exception);
    } else if (exception instanceof WebApplicationException) {
      //we need to check this, as otherwise the logic for processing WebApplicationException will be overridden
      final int statusCode = ((WebApplicationException) exception).getResponse().getStatus();
      responseStatus = Response.Status.fromStatusCode(statusCode);
    }
    return responseStatus;
  }

  public Response.Status getStatus(ProcessEngineException exception) {
    Response.Status responseStatus = Response.Status.INTERNAL_SERVER_ERROR;

    // provide custom handling of authorization exception
    if (exception instanceof AuthorizationException) {
      responseStatus = Response.Status.FORBIDDEN;
    }
    else if (exception instanceof MigrationPlanValidationException
      || exception instanceof MigratingProcessInstanceValidationException
      || exception instanceof BadUserRequestException) {
      responseStatus = Response.Status.BAD_REQUEST;
    }
    return responseStatus;
  }

  public Response.Status getStatus(RestException exception) {
    if (exception.getStatus() != null) {
      return exception.getStatus();
    }
    return Response.Status.INTERNAL_SERVER_ERROR;
  }
}

@@ -1890,260 +1890,8 @@
 %0A   
- else if (exception instanceof MigrationPlanValidationException%0A      %7C%7C exception instanceof MigratingProcessInstanceValidationException%0A      %7C%7C exception instanceof BadUserRequestException) %7B%0A      responseStatus = Response.Status.BAD_REQUEST;%0A    %7D
  els
@@ -1992,32 +1992,36 @@
 xception);%0A    %7D
+%0A   
  else if (except

remove duplicated check