package com.capgemini.reports.execution.actions;

import com.capgemini.reports.database.javabean.DbUser;
import com.capgemini.reports.exceptions.InvalidInputException;
import com.capgemini.reports.exceptions.NoConfChangesException;
import com.capgemini.reports.execution.AbstractExecutionReport;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * If you need to perform several {@link IErrorExecutionStep}s, use this class to execute them orderly.
 */
@EqualsAndHashCode
public class CompositeErrorAction implements IErrorExecutionStep {

  private static final long serialVersionUID = -7228217924123134048L;

  private final List<IErrorExecutionStep> actions = new ArrayList<>();

  /**
   * Creates a composite error handler, capable of executing several actions in series.
   *
   * @param inputActions the actions to be carried out sequentially.
   */
  public CompositeErrorAction(IErrorExecutionStep... inputActions) {
    Collections.addAll(this.actions, inputActions);
  }

  @Override
  public void performFunctionalErrorAction(AbstractExecutionReport<?, ?> report, List<DbUser> owners, NoConfChangesException theOrginalException) {
    for (IErrorExecutionStep action : actions) {
      action.performFunctionalErrorAction(report, owners, theOrginalException);
    }
  }

  @Override
  public void performFunctionalErrorAction(AbstractExecutionReport<?, ?> report, List<DbUser> owners, InvalidInputException theOrginalException) {
    for (IErrorExecutionStep action : actions) {
      action.performFunctionalErrorAction(report, owners, theOrginalException);
    }
  }

  @Override
  public void performTechnicalErrorAction(AbstractExecutionReport<?, ?> report, List<DbUser> owners, Throwable theOrginalException) {
    for (IErrorExecutionStep action : actions) {
      action.performTechnicalErrorAction(report, owners, theOrginalException);
    }
  }

}
