package com.capgemini.reports.database.javabean;

import java.io.Serializable;
import java.sql.Timestamp;

import com.capgemini.framework.common.DateHelper;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/** Persisted object in table REPORT. **/
@Getter
@Setter
@ToString
public class DbReport implements Serializable, IPersistedObject {

    /** Required by serialization. **/
    private static final long serialVersionUID = 1L;

    /** Unique identifier recommended for using all features from Hibernate. **/
    private Long id;

    /** Identification code for the report, ie: R01, R22... **/
    private String reportCode;

    /** Date when the generation started. **/
    private Timestamp startGenerationDate;

    /** Date when the generation ended. **/
    private Timestamp endGenerationDate;

    /** Duration of the generation. **/
    private Long generationDuration;

    /** Unique identifier of the report. **/
    private String reportUid;

    /** Current report state (WAITING, INWORK, FAILED, COMPLETED). **/
    private String state;

    /** Content of error message when state is "FAILED". **/
    private String errorMessage;

    /** Type of the error: FUNCTIONAL or TECHNICAL (ie: JAVA Exception). **/
    private String errorType;

    /** Physical file name on disk. **/
    private String fileName;

    /** Technical field only for filtering for statistics. **/
    private String monthPeriod;

    /** Identifier of the scheduling report. **/
    private Long schedulingReportId;

    /** JAVA convention requires an empty constructor for each JAVA bean. **/
    public DbReport() {
        // Nothing to see, move along...
    }

    /**
     * Override of the lombok setter.
     *
     * @param aTimestamp Set value
     **/
    public void setEndGenerationDate(final Timestamp aTimestamp) {

        endGenerationDate = aTimestamp;
        setMonthPeriod(DateHelper.getMonthPeriod());

        /* Compute duration */
        if ((startGenerationDate != null) && (endGenerationDate != null)) {
            generationDuration = endGenerationDate.getTime() - startGenerationDate.getTime();
        }
    }
}
