Search in sources :

Example 1 with FileUploadException

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

the class FileHandler method getStudyPatchRequests.

public static List<StudyPatchRequest> getStudyPatchRequests(FileUploadRequest fileUploadRequest) {
    CsvMapper mapper = new CsvMapper();
    CsvSchema schema = getSchemaFromMultiPartFile(fileUploadRequest.getMultipartFile());
    List<StudyPatchRequest> studyPatchRequests;
    try {
        InputStream inputStream = fileUploadRequest.getMultipartFile().getInputStream();
        MappingIterator<StudyPatchRequest> iterator = mapper.readerFor(StudyPatchRequest.class).with(schema).readValues(inputStream);
        studyPatchRequests = iterator.readAll();
    } catch (IOException e) {
        throw new FileUploadException("Could not read the file");
    }
    return studyPatchRequests;
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) InputStream(java.io.InputStream) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) IOException(java.io.IOException) FileUploadException(uk.ac.ebi.spot.goci.curation.exception.FileUploadException) StudyPatchRequest(uk.ac.ebi.spot.goci.curation.dto.StudyPatchRequest)

Example 2 with FileUploadException

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

the class FileHandler method serializePojoToTsv.

public static String serializePojoToTsv(List<?> pojoList) {
    CsvMapper csvMapper = new CsvMapper();
    List<Map<String, Object>> dataList = csvMapper.convertValue(pojoList, new TypeReference<Object>() {
    });
    List<List<String>> csvData = new ArrayList<>();
    List<String> csvHead = new ArrayList<>();
    AtomicInteger counter = new AtomicInteger();
    dataList.forEach(row -> {
        List<String> rowData = new ArrayList<>();
        row.forEach((key, value) -> {
            rowData.add(String.valueOf(value));
            if (counter.get() == 0) {
                csvHead.add(key);
            }
        });
        csvData.add(rowData);
        counter.getAndIncrement();
    });
    CsvSchema.Builder builder = CsvSchema.builder();
    csvHead.forEach(builder::addColumn);
    CsvSchema schema = builder.build().withHeader().withLineSeparator("\n").withColumnSeparator('\t');
    String result = "";
    try {
        result = csvMapper.writer(schema).writeValueAsString(csvData);
    } catch (IOException e) {
        throw new FileUploadException("Could not read the file");
    }
    return result;
}
Also used : CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) FileUploadException(uk.ac.ebi.spot.goci.curation.exception.FileUploadException)

Example 3 with FileUploadException

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

the class FileHandler method serializeDiseaseTraitAnalysisFile.

public static List<AnalysisDTO> serializeDiseaseTraitAnalysisFile(FileUploadRequest fileUploadRequest) {
    CsvMapper mapper = new CsvMapper();
    CsvSchema schema = getSchemaFromMultiPartFile(fileUploadRequest.getMultipartFile());
    List<AnalysisDTO> analysisDTOS;
    try {
        InputStream inputStream = fileUploadRequest.getMultipartFile().getInputStream();
        MappingIterator<AnalysisDTO> iterator = mapper.readerFor(AnalysisDTO.class).with(schema).readValues(inputStream);
        analysisDTOS = iterator.readAll();
    } catch (IOException e) {
        throw new FileUploadException("Could not read the file");
    }
    return analysisDTOS;
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) InputStream(java.io.InputStream) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) AnalysisDTO(uk.ac.ebi.spot.goci.curation.dto.AnalysisDTO) IOException(java.io.IOException) FileUploadException(uk.ac.ebi.spot.goci.curation.exception.FileUploadException)

Example 4 with FileUploadException

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

the class StudyFileService method upload.

/**
 * Upload a file to a study specific dir
 *
 * @param fileFromUpload File user is uploading
 * @param studyId        Study ID which is used to find study specific dir
 */
public synchronized void upload(MultipartFile fileFromUpload, Long studyId) throws IOException {
    if (!fileFromUpload.isEmpty()) {
        File file = createFileInStudyDir(fileFromUpload.getOriginalFilename(), studyId);
        // Copy contents of multipart request to newly created file
        try {
            fileFromUpload.transferTo(file);
            // Set some permissions
            file.setWritable(true, false);
            file.setReadable(true, false);
        } catch (IOException e) {
            getLog().error("Unable to copy file: " + fileFromUpload.getName() + " to study dir");
            getLog().error(e.getMessage().toString());
            throw new FileUploadException("Unable to copy file");
        }
    } else {
        getLog().error(fileFromUpload.getName() + " is empty");
        throw new FileUploadException("Upload file is empty");
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) FileUploadException(uk.ac.ebi.spot.goci.curation.exception.FileUploadException)

Example 5 with FileUploadException

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

the class StudyFileService method createFileInStudyDir.

/**
 * Create a file object
 *
 * @param name Name of file
 * @param id   Study ID
 */
private File createFileInStudyDir(String name, Long id) throws IOException {
    // Check study dir exists, if not create it
    File studyDirPath = getStudyDirPath(id);
    getLog().error("Debug hotfix: " + studyDirPath.toString());
    if (!studyDirPath.exists()) {
        createStudyDir(id);
    }
    // Create our new file
    String fileName = getStudyDirRoot() + File.separator + id + File.separator + name;
    File file = new File(fileName);
    // If file already exists, backup current file
    if (file.exists()) {
        getLog().info("File " + file.getName() + " already exists, will backup current file");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        String now = dateFormat.format(new Date());
        String newFileName = getStudyDirRoot() + File.separator + id + File.separator + file.getName().concat("_").concat(now);
        File backUpExistingFile = new File(newFileName);
        boolean fileBackupSuccess = file.renameTo(backUpExistingFile);
        if (!fileBackupSuccess) {
            getLog().error("Could not create a backup of file " + file.getName());
            throw new FileUploadException("Could not create a backup of file");
        }
    }
    boolean success = file.createNewFile();
    if (!success) {
        getLog().error("Could not create a file: " + fileName);
        throw new FileUploadException("Could not create a file");
    }
    return file;
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) FileUploadException(uk.ac.ebi.spot.goci.curation.exception.FileUploadException)

Aggregations

FileUploadException (uk.ac.ebi.spot.goci.curation.exception.FileUploadException)9 IOException (java.io.IOException)7 CsvMapper (com.fasterxml.jackson.dataformat.csv.CsvMapper)4 CsvSchema (com.fasterxml.jackson.dataformat.csv.CsvSchema)4 InputStream (java.io.InputStream)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 ArrayList (java.util.ArrayList)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 MultipartFile (org.springframework.web.multipart.MultipartFile)2 DataIntegrityException (uk.ac.ebi.spot.goci.curation.exception.DataIntegrityException)2 EnsemblMappingException (uk.ac.ebi.spot.goci.exception.EnsemblMappingException)2 SheetProcessingException (uk.ac.ebi.spot.goci.exception.SheetProcessingException)2 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1