package uk.ac.ebi.spot.goci.curation.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import uk.ac.ebi.spot.goci.curation.model.AssociationValidationView;
import uk.ac.ebi.spot.goci.model.Association;
import uk.ac.ebi.spot.goci.model.AssociationValidationReport;
import uk.ac.ebi.spot.goci.model.ValidationError;
import uk.ac.ebi.spot.goci.repository.AssociationRepository;
import uk.ac.ebi.spot.goci.repository.AssociationValidationReportRepository;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by emma on 04/07/2016.
 *
 * @author emma
 *         <p>
 *         Service to handle operations related to AssociationValidationReports
 */
@Service
public class AssociationValidationReportService {

    private AssociationRepository associationRepository;
    private AssociationValidationReportRepository associationValidationReportRepository;

    @Autowired
    public AssociationValidationReportService(AssociationRepository associationRepository,
                                              AssociationValidationReportRepository associationValidationReportRepository) {
        this.associationRepository = associationRepository;
        this.associationValidationReportRepository = associationValidationReportRepository;
    }

    /**
     * Create association validation reports and add to association
     *
     * @param errors List of errors
     * @param id     Association ID
     */
    public void createAssociationValidationReport(Collection<ValidationError> errors, Long id) {

        // Get list of existing reports and create list of existing warnings
        Association association = associationRepository.findOne(id);
        Collection<AssociationValidationReport> existingReports = association.getAssociationValidationReport();
        List<String> currentWarnings = new ArrayList<>();
        if (!existingReports.isEmpty()) {
            existingReports.forEach(associationValidationReport -> {
                currentWarnings.add(associationValidationReport.getWarning());
            });
        }

        // Create current association validation report
        errors.forEach(validationError -> {
            // if warning is not already linked to association
            if (!currentWarnings.contains(validationError.getError())) {
                AssociationValidationReport associationValidationReport =
                        new AssociationValidationReport(validationError.getError(),
                                                        validationError.getField(),
                                                        false,
                                                        association);
                associationValidationReportRepository.save(associationValidationReport);
            }
        });
    }


    /**
     * Retrieve validation warnings for an association and return this is a structure accessible by view
     *
     * @param associationId ID of association to get warning for
     */
    public List<AssociationValidationView> getAssociationWarnings(Long associationId) {

        List<AssociationValidationView> associationValidationViews = new ArrayList<>();
        associationValidationReportRepository.findByAssociationId(associationId)
                .forEach(associationValidationReport -> {
                    associationValidationViews.add(new AssociationValidationView(associationValidationReport.getValidatedField(),
                                                                                 associationValidationReport.getWarning(),
                                                                                 true));
                });
        return associationValidationViews;
    }
}
@@ -1826,328 +1826,244 @@
 ts =
- association.getAssociationValidationReport();%0A        List%3CString%3E currentWarnings = new ArrayList%3C%3E();%0A        if (!existingReports.isEmpty()) %7B%0A            existingReports.forEach(associationValidationReport -%3E %7B%0A                currentWarnings.add(associationValidationReport.getWarning());%0A            %7D);%0A        %7D
+%0A                associationValidationReportRepository.findByAssociationId(id);%0A%0A        existingReports.forEach(associationValidationReport -%3E associationValidationReportRepository.delete(%0A                associationValidationReport));
 %0A%0A  
@@ -2082,16 +2082,8 @@
 ate 
-current 
 asso
@@ -2107,16 +2107,17 @@
 n report
+s
 %0A       
@@ -2220,85 +2220,8 @@
 ion%0A
-            if (!currentWarnings.contains(validationError.getError())) %7B%0A    
     
@@ -2286,20 +2286,16 @@
 eport =%0A
-    
         
@@ -2418,20 +2418,16 @@
         
-    
 validati
@@ -2502,23 +2502,15 @@
     
-   
- 
 false,%0A
-    
     
@@ -2575,36 +2575,32 @@
 n);%0A            
- 
-   
 associationValid
@@ -2660,22 +2660,8 @@
 t);%0A
-            %7D%0A
     
@@ -2675,17 +2675,16 @@
 %0A    %7D%0A%0A
-%0A
     /**%0A

Update method to add validation reports