package com.capgemini.reports.execution.actions;

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 com.capgemini.reports.logging.ReportLoggerHelper;
import lombok.EqualsAndHashCode;

import java.util.List;

/**
 * Logs typical report events (begin/end/error) to a special Logger setup in JBoss (using {@link ReportLoggerHelper}).
 */
@EqualsAndHashCode
public class LoggingAction implements IPostExecutionStep, IPreExecutionStep, IErrorExecutionStep {

    private static final long serialVersionUID = -2216273173886557249L;

    @Override
    public <T extends AbstractExecutionReport<T, ?>> void performPreAction(T report, DbReport dbReport, TcSession session) {
        // Log this event in the events log
        ReportLoggerHelper.logStart(dbReport, report);
    }

    @Override
    public <T extends AbstractExecutionReport<T, ?>> void performPostAction(TcSession session, T report, DbReport dbReport,
                                                                            String fileNameWithExtension) {
        ReportLoggerHelper.logCompletion(dbReport, fileNameWithExtension, report);
    }

    @Override
    public void performTechnicalErrorAction(AbstractExecutionReport<?, ?> report, List<DbUser> owners, Throwable theOrginalException) {
        ReportLoggerHelper.logError(theOrginalException.getMessage(), theOrginalException, report);
    }

    @Override
    public void performFunctionalErrorAction(AbstractExecutionReport<?, ?> report, List<DbUser> owners, NoConfChangesException theOrginalException) {
        ReportLoggerHelper.logError(theOrginalException.getMessage(), theOrginalException, report);
    }

    @Override
    public void performFunctionalErrorAction(AbstractExecutionReport<?, ?> report, List<DbUser> owners, InvalidInputException theOrginalException) {
        ReportLoggerHelper.logError(theOrginalException.getMessage(), theOrginalException, report);
    }
}
