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;
}
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;
}
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;
}
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");
}
}
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;
}
Aggregations