Search in sources :

Example 1 with SheetProcessingException

use of uk.ac.ebi.spot.goci.exception.SheetProcessingException in project goci by EBISPOT.

the class AssociationUploadServiceTest method uploadIncorrectlyFormattedFile.

@Test(expected = SheetProcessingException.class)
public void uploadIncorrectlyFormattedFile() throws Exception {
    MockMultipartFile incorrectlyFormattedFile = new MockMultipartFile("data", "filename.txt", "text/plain", "Not an Excel file".getBytes());
    File file = new File(incorrectlyFormattedFile.getOriginalFilename());
    // Stubbing
    when(studyFileService.getFileFromFileName(STUDY.getId(), incorrectlyFormattedFile.getOriginalFilename())).thenReturn(file);
    when(associationFileUploadService.processAndValidateAssociationFile(file, "full")).thenThrow(new SheetProcessingException());
    associationUploadService.upload(incorrectlyFormattedFile, STUDY, SECURE_USER);
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) SheetProcessingException(uk.ac.ebi.spot.goci.exception.SheetProcessingException) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) File(java.io.File) Test(org.junit.Test)

Example 2 with SheetProcessingException

use of uk.ac.ebi.spot.goci.exception.SheetProcessingException in project goci by EBISPOT.

the class AssociationFileUploadService method processAndValidateAssociationFile.

/**
     * Process uploaded file and return a list of errors
     *
     * @param file XLSX file supplied by user
     */
public ValidationSummary processAndValidateAssociationFile(File file, String validationLevel) throws FileNotFoundException, SheetProcessingException {
    ValidationSummary validationSummary = new ValidationSummary();
    Collection<RowValidationSummary> rowValidationSummaries = new ArrayList<>();
    Collection<AssociationSummary> associationSummaries = new ArrayList<>();
    Collection<AssociationUploadRow> fileRows = new ArrayList<>();
    if (file.exists()) {
        // Create sheet
        XSSFSheet sheet = null;
        try {
            // Create a sheet for reading
            sheet = sheetCreationService.createSheet(file.getAbsolutePath());
            // Process file, depending on validation level, into a generic row object
            UploadSheetProcessor uploadSheetProcessor = uploadSheetProcessorBuilder.buildProcessor(validationLevel);
            fileRows = uploadSheetProcessor.readSheetRows(sheet);
        } catch (InvalidFormatException | InvalidOperationException | IOException e) {
            getLog().error("File: " + file.getName() + " cannot be processed", e);
            file.delete();
            throw new SheetProcessingException("File: " + file.getName() + " cannot be processed", e);
        }
    } else {
        getLog().error("File: " + file.getName() + " cannot be found");
        throw new FileNotFoundException("File does not exist");
    }
    String eRelease = ensemblRestTemplateService.getRelease();
    // Error check each row
    if (!fileRows.isEmpty()) {
        // Check for missing values and syntax errors that would prevent code creating an association
        for (AssociationUploadRow row : fileRows) {
            getLog().info("Syntax checking row: " + row.getRowNumber() + " of file, " + file.getAbsolutePath());
            RowValidationSummary rowValidationSummary = createRowValidationSummary(row, eRelease);
            // Only store summary if there is an error
            if (!rowValidationSummary.getErrors().isEmpty()) {
                rowValidationSummaries.add(rowValidationSummary);
            }
        }
        if (rowValidationSummaries.isEmpty()) {
            //Proceed to carry out full checks of values
            fileRows.forEach(row -> {
                associationSummaries.add(createAssociationSummary(row, validationLevel, eRelease));
            });
        }
    } else {
        getLog().error("Parsing file failed");
    }
    validationSummary.setAssociationSummaries(associationSummaries);
    validationSummary.setRowValidationSummaries(rowValidationSummaries);
    return validationSummary;
}
Also used : SheetProcessingException(uk.ac.ebi.spot.goci.exception.SheetProcessingException) RowValidationSummary(uk.ac.ebi.spot.goci.model.RowValidationSummary) RowValidationSummary(uk.ac.ebi.spot.goci.model.RowValidationSummary) ValidationSummary(uk.ac.ebi.spot.goci.model.ValidationSummary) AssociationSummary(uk.ac.ebi.spot.goci.model.AssociationSummary) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) AssociationUploadRow(uk.ac.ebi.spot.goci.model.AssociationUploadRow)

Aggregations

SheetProcessingException (uk.ac.ebi.spot.goci.exception.SheetProcessingException)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)1 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)1 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)1 Test (org.junit.Test)1 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)1 AssociationSummary (uk.ac.ebi.spot.goci.model.AssociationSummary)1 AssociationUploadRow (uk.ac.ebi.spot.goci.model.AssociationUploadRow)1 RowValidationSummary (uk.ac.ebi.spot.goci.model.RowValidationSummary)1 ValidationSummary (uk.ac.ebi.spot.goci.model.ValidationSummary)1