/**
   * Finalizes a {@code Step} execution by a given context in which the step was performed and by
   * the exit status of the step.
   *
   * @param context The context in which the {@code Step} was executed.
   * @param step The step to finalize.
   * @param exitStatus Indicates if the execution described by the step ended successfully or not.
   */
  public static void endStep(ExecutionContext context, Step step, boolean exitStatus) {
    if (context == null) {
      return;
    }
    if (context.isMonitored()) {
      Job job = context.getJob();
      try {
        if (step != null) {
          step.markStepEnded(exitStatus);
          JobRepositoryFactory.getJobRepository().updateStep(step);
        }

        if (context.getExecutionMethod() == ExecutionMethod.AsJob && job != null && !exitStatus) {
          // step failure will cause the job to be marked as failed
          context.setCompleted(true);
          job.markJobEnded(false);
          JobRepositoryFactory.getJobRepository().updateCompletedJobAndSteps(job);
        } else {
          Step parentStep = context.getStep();
          if (context.getExecutionMethod() == ExecutionMethod.AsStep && parentStep != null) {
            context.setCompleted(true);
            if (!exitStatus) {
              job.markJobEnded(false);
              JobRepositoryFactory.getJobRepository().updateCompletedJobAndSteps(job);
            }
          }
        }
      } catch (Exception e) {
        log.error(e);
      }
    }
  }
  /**
   * Method should be called when finalizing the command. The execution step is being ended with
   * success and the finalization step is started.
   *
   * @param executionContext The context of the job
   * @return A created instance of the Finalizing step
   */
  public static Step startFinalizingStep(ExecutionContext executionContext) {
    if (executionContext == null) {
      return null;
    }
    Step step = null;

    try {
      if (executionContext.getExecutionMethod() == ExecutionMethod.AsJob) {
        Job job = executionContext.getJob();
        if (job != null) {
          Step executingStep = job.getStep(StepEnum.EXECUTING);
          Step finalizingStep =
              job.addStep(
                  StepEnum.FINALIZING,
                  ExecutionMessageDirector.getInstance().getStepMessage(StepEnum.FINALIZING));

          if (executingStep != null) {
            executingStep.markStepEnded(true);
            JobRepositoryFactory.getJobRepository()
                .updateExistingStepAndSaveNewStep(executingStep, finalizingStep);
          } else {
            JobRepositoryFactory.getJobRepository().saveStep(finalizingStep);
          }
        }
      } else if (executionContext.getExecutionMethod() == ExecutionMethod.AsStep) {
        Step parentStep = executionContext.getStep();
        if (parentStep != null) {
          Step executingStep = parentStep.getStep(StepEnum.EXECUTING);
          Step finalizingStep =
              parentStep.addStep(
                  StepEnum.FINALIZING,
                  ExecutionMessageDirector.getInstance().getStepMessage(StepEnum.FINALIZING));
          if (executingStep != null) {
            executingStep.markStepEnded(true);
            JobRepositoryFactory.getJobRepository()
                .updateExistingStepAndSaveNewStep(executingStep, finalizingStep);
          } else {
            JobRepositoryFactory.getJobRepository().saveStep(finalizingStep);
          }
        }
      }
    } catch (Exception e) {
      log.error(e);
    }
    return step;
  }
  /**
   * Finalizes Job with VDSM tasks, as this case requires verification that no other steps are
   * running in order to close the entire Job
   *
   * @param executionContext The context of the execution which defines how the job should be ended
   * @param exitStatus Indicates if the execution described by the job ended successfully or not.
   */
  public static void endTaskJob(ExecutionContext context, boolean exitStatus) {
    if (context == null) {
      return;
    }

    try {
      if (context.getExecutionMethod() == ExecutionMethod.AsJob && context.getJob() != null) {
        endJob(context, exitStatus);
      } else {
        Step parentStep = context.getStep();
        if (context.getExecutionMethod() == ExecutionMethod.AsStep && parentStep != null) {
          Step finalizingStep = parentStep.getStep(StepEnum.FINALIZING);
          if (finalizingStep != null) {
            finalizingStep.markStepEnded(exitStatus);
            JobRepositoryFactory.getJobRepository().updateStep(finalizingStep);
          }
          parentStep.markStepEnded(exitStatus);
          JobRepositoryFactory.getJobRepository().updateStep(parentStep);

          List<Step> steps =
              DbFacade.getInstance().getStepDao().getStepsByJobId(parentStep.getJobId());
          boolean hasChildStepsRunning = false;
          for (Step step : steps) {
            if (step.getStatus() == JobExecutionStatus.STARTED && step.getParentStepId() != null) {
              hasChildStepsRunning = true;
              break;
            }
          }
          if (!hasChildStepsRunning) {
            endJob(
                exitStatus, JobRepositoryFactory.getJobRepository().getJob(parentStep.getJobId()));
          }
        }
      }
    } catch (RuntimeException e) {
      log.error(e);
    }
  }
  /**
   * Finalizes a {@code Step} execution which represents a VDSM task. In case of a failure status,
   * the job will not be marked as failed at this stage, but via executing the {@code
   * CommandBase.endAction} with the proper status by {@code the AsyncTaskManager}.
   *
   * @param stepId A unique identifier of the step to finalize.
   * @param exitStatus The status which the step should be ended with.
   */
  public static void endTaskStep(Guid stepId, JobExecutionStatus exitStatus) {
    try {
      if (stepId != null) {
        Step step = JobRepositoryFactory.getJobRepository().getStep(stepId);

        if (step != null) {
          step.markStepEnded(exitStatus);
          JobRepositoryFactory.getJobRepository().updateStep(step);
        }
      }
    } catch (Exception e) {
      log.errorFormat("Failed to terminate step {0} with status {1}", stepId, exitStatus, e);
    }
  }