package com.capgemini.reports.execution.actions;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.framework.teamcenter.exceptions.KurtException;
import com.capgemini.reports.database.DataBaseReportHelper;
import com.capgemini.reports.database.javabean.DbReport;
import com.capgemini.reports.database.javabean.DbUser;
import com.capgemini.reports.database.javabean.enumeration.ReportErrorTypeEnum;
import com.capgemini.reports.database.manager.MngReport;
import com.capgemini.reports.exceptions.InvalidInputException;
import com.capgemini.reports.exceptions.NoConfChangesException;
import com.capgemini.reports.exceptions.NotFoundException;
import com.capgemini.reports.execution.AbstractExecutionReport;
import lombok.EqualsAndHashCode;

import java.util.List;

/**
 * This class manages typical interaction with DB during a report lifecycle. <br>
 * Can be used for:
 * <ul>
 * <li><b>Before executing the report:</b> set the state as STARTED & register the report start generation date in DB</li>
 * <li><b>After executing the report:</b> set the state as FINISHED & register the report end generation date in DB, as well as its filename</li>
 * <li><b>When an error occurs during execution:</b> set the state as FAILED & register the report failure date in DB, as well as failure message</li>
 * </ul>
 */
@EqualsAndHashCode
public class UpdateReportInDbAction implements IPreExecutionStep, IPostExecutionStep, IErrorExecutionStep {

  private static final long serialVersionUID = -5967051491832029054L;

  @Override
  public <T extends AbstractExecutionReport<T, ?>> void performPreAction(T report, DbReport dbReport, TcSession session)
      throws NotFoundException {
    DataBaseReportHelper.registerStart(new MngReport(), report.getReportTechnicalUid());
  }

  @Override
  public <T extends AbstractExecutionReport<T, ?>> void performPostAction(TcSession session, T report, DbReport theExecutingReport,
                                                                          String fileNameWithExtension) throws KurtException {
    MngReport theReportManager = new MngReport();
    DataBaseReportHelper.registerCompletion(theExecutingReport, fileNameWithExtension, theReportManager);
  }

  @Override
  public void performTechnicalErrorAction(AbstractExecutionReport<?, ?> report, List<DbUser> owners, Throwable theOriginalException) {
    DataBaseReportHelper.registerError(theOriginalException.getMessage(), ReportErrorTypeEnum.TECHNICAL, report.getReportTechnicalUid());
  }

  @Override
  public void performFunctionalErrorAction(AbstractExecutionReport<?, ?> report, List<DbUser> owners, NoConfChangesException theOriginalException) {
    DataBaseReportHelper.registerError(theOriginalException.getMessage(), ReportErrorTypeEnum.FUNCTIONAL, report.getReportTechnicalUid());
  }

  @Override
  public void performFunctionalErrorAction(AbstractExecutionReport<?, ?> report, List<DbUser> owners, InvalidInputException theOriginalException) {
    DataBaseReportHelper.registerError(theOriginalException.getMessage(), ReportErrorTypeEnum.FUNCTIONAL, report.getReportTechnicalUid());
  }
}
