package com.capgemini.reports.execution.actions;

import com.capgemini.framework.common.TeamcenterDateFormatter;
import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.reports.database.javabean.DbReport;
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.List;

/**
 * This action updates the {@link AbstractExecutionReport}'s internal state (primarily event times) according to events on the report lifecycle.
 */
@EqualsAndHashCode
public class UpdateReportAction implements IPreExecutionStep, IPostExecutionStep, IErrorExecutionStep {

    private static final long serialVersionUID = -5967051491832029054L;

    @Override
    public <T extends AbstractExecutionReport<T, ?>> void performPreAction(T abstractExecutionReport, DbReport dbReport, TcSession session) {
        // Register current time as the posting time
        abstractExecutionReport.setStartTime(TeamcenterDateFormatter.getCurrentTime());
    }

    @Override
    public <T extends AbstractExecutionReport<T, ?>> void performPostAction(TcSession session, T report, DbReport theExecutingReport,
                                                                            String fileNameWithExtension) {
        // Register current time as the end time
        report.setEndTime(TeamcenterDateFormatter.getCurrentTime());
    }

    @Override
    public void performTechnicalErrorAction(AbstractExecutionReport<?, ?> report, List<DbUser> owners, Throwable theOriginalException) {
        // Register current time as the failure time
        report.setFailureTime(TeamcenterDateFormatter.getCurrentTime());
    }

    @Override
    public void performFunctionalErrorAction(AbstractExecutionReport<?, ?> report, List<DbUser> owners, NoConfChangesException theOriginalException) {
        // Register current time as the failure time
        report.setFailureTime(TeamcenterDateFormatter.getCurrentTime());
    }

    @Override
    public void performFunctionalErrorAction(AbstractExecutionReport<?, ?> report, List<DbUser> owners, InvalidInputException theOriginalException) {
        // Register current time as the failure time
        report.setFailureTime(TeamcenterDateFormatter.getCurrentTime());
    }
}
