class WorkflowActionBBFailure {

    public void updateRequestStatusToFailed(DelegateExecution execution) {
        try {
            String requestId = (String) execution.getVariable("mso-request-id");
            InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
            String rollbackErrorMsg = "";
            String errorMsg = "";
            Boolean rollbackCompletedSuccessfully = (Boolean) execution.getVariable("isRollbackComplete");
            Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback");
            ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
            if (rollbackCompletedSuccessfully == null)
                rollbackCompletedSuccessfully = false;

            if (isRollbackFailure == null)
                isRollbackFailure = false;

            if (rollbackCompletedSuccessfully) {
                rollbackErrorMsg = "Rollback has been completed successfully.";
                request.setRollbackStatusMessage(rollbackErrorMsg);
                execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
                String rollbackTargetState = (String) execution.getVariable(ROLLBACK_TARGET_STATE);
                request.setRequestStatus(rollbackTargetState);
            } else if (isRollbackFailure) {
                if (ebb != null && ebb.getBuildingBlock() != null && ebb.getBuildingBlock().getBpmnFlowName() != null) {
                    String flowName = ebb.getBuildingBlock().getBpmnFlowName();
                    if (DEACTIVATE_FABRIC_CONFIGURATION_FLOW.equalsIgnoreCase(flowName)
                            || UNASSIGN_FABRIC_CONFIGURATION_FLOW.equalsIgnoreCase(flowName)
                            || DELETE_FABRIC_CONFIGURATION_FLOW.equalsIgnoreCase(flowName)) {
                        String statusMessage = String.format(
                                "%s Warning: The vf-module is active but configuration was not removed completely for one or more VMs.",
                                request.getStatusMessage());
                        request.setStatusMessage(statusMessage);
                    }
                }
                Optional<String> rollbackErrorMsgOp = retrieveErrorMessage(execution);
                if (rollbackErrorMsgOp.isPresent()) {
                    rollbackErrorMsg = rollbackErrorMsgOp.get();
                } else {
                    rollbackErrorMsg = "Failed to determine rollback error message.";
                }
                request.setRollbackStatusMessage(rollbackErrorMsg);
                execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
                request.setRequestStatus(Status.FAILED.toString());
            } else {
                Optional<String> errorMsgOp = retrieveErrorMessage(execution);
                if (errorMsgOp.isPresent()) {
                    errorMsg = errorMsgOp.get();
                } else {
                    errorMsg = "Failed to determine error message";
                }
                request.setStatusMessage(errorMsg);
                execution.setVariable("ErrorMessage", errorMsg);
                String handlingCode = (String) execution.getVariable("handlingCode");
                if ("Abort".equalsIgnoreCase(handlingCode)) {
                    request.setRequestStatus(Status.ABORTED.toString());
                } else {
                    request.setRequestStatus(Status.FAILED.toString());
                }
            }
            if (ebb != null && ebb.getBuildingBlock() != null) {
                String flowStatus = "";
                if (rollbackCompletedSuccessfully) {
                    flowStatus = "All Rollback flows have completed successfully";
                } else {
                    flowStatus = ebb.getBuildingBlock().getBpmnFlowName() + " has failed.";
                }
                request.setFlowStatus(flowStatus);
                execution.setVariable("flowStatus", flowStatus);
            }

            request.setProgress(Long.valueOf(100));
            request.setLastModifiedBy("CamundaBPMN");
            request.setEndTime(new Timestamp(System.currentTimeMillis()));
            requestDbclient.updateInfraActiveRequests(request);
        } catch (Exception e) {
            workflowAction.buildAndThrowException(execution, "Error Updating Request Database", e);
        }
    }

}