/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.csu.sdmx.validation.model;

import cz.csu.sdmx.Utils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

/**
 *
 * @author themi
 */
@Entity
@Table(name = "SDMX_VALIDATION_RESULTS")
@XmlAccessorType(XmlAccessType.FIELD)
public class ValidationResult implements Serializable{
    
    @XmlElement(name = "id")
    private int id;
    
    private String dataFilename;
    
    private Date date;
    
    private List<String> dsds;
    
    @XmlElement(name = "error")
    private List<ValidationError> errors;

    public ValidationResult(){
        date = new Date();
        this.errors = new ArrayList<>();
        this.dsds = new ArrayList<>();
    }

    public void setErrors(List<ValidationError> errors) {
        this.errors = errors;
    }
    
    public void addError(ValidationError error) {
        error.setValidationResult(this);
        this.errors.add(error);
    }
    
    public void addErrors(List<ValidationError> errors) {
        for(ValidationError error : errors)
            addError(error);
    }
    
    public boolean hasErrors(){
        return !errors.isEmpty();
    }
    
    @Column(name = "DSD")
    @OneToMany(mappedBy = "validationResult", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    public List<ValidationError> getErrors(){
        return errors;
    }

    @Id
    @GeneratedValue
    @Column(name = "ID")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "DATA_FILENAME")
    public String getDataFilename() {
        return dataFilename;
    }

    public void setDataFilename(String dataFilename) {
        this.dataFilename = dataFilename;
    }

    @Column(name = "VALIDATION_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
    
    public String getDsds() {
        return Utils.join(dsds, ", ");
    }
     
    @Transient
    public List<String> getDsdList() {
        return dsds;
    }

    public void setDsds(String dsds) {
        if(dsds != null)
            this.dsds = Arrays.asList(dsds.split(", "));
    }
    
    public void addDsd(String dsd){
        dsds.add(dsd);
    }

    @Transient
    public int getErrorCount() {
        return errors.size();
    }
}
