use of nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException in project timbuctoo by HuygensING.
the class DataPerfectLoader method writeZipToTempDir.
private File writeZipToTempDir(File source) throws IOException, InvalidFileException {
File output = createTempDir();
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(source))) {
ZipEntry ze;
byte[] buffer = new byte[1024];
try {
while ((ze = zis.getNextEntry()) != null) {
String fileName = ze.getName();
File newFile = new File(output.getAbsolutePath() + File.separator + fileName);
if (ze.isDirectory()) {
newFile.mkdirs();
} else {
// create all non exists folders
// else you will hit FileNotFoundException for compressed folder
new File(newFile.getParent()).mkdirs();
try (FileOutputStream fos = new FileOutputStream(newFile)) {
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
}
}
} catch (ZipException e) {
throw new InvalidFileException("The dataperfect archive should be sent as a ZIP encoded directory");
}
}
return output;
}
use of nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException in project timbuctoo by HuygensING.
the class BulkUploadService method saveToDb.
public void saveToDb(String vreName, Loader loader, List<Tuple<String, File>> tempFiles, String vreLabel, Consumer<String> statusUpdate) throws IOException, InvalidFileException {
String fileNamesDisplay;
if (tempFiles.size() == 1) {
fileNamesDisplay = tempFiles.get(0).getLeft();
} else {
fileNamesDisplay = "multiple files: " + tempFiles.stream().map(Tuple::getLeft).collect(joining(", "));
}
try (TinkerpopSaver saver = new TinkerpopSaver(vres, graphwrapper, vreName, vreLabel, maxVertices, fileNamesDisplay)) {
try {
loader.loadData(tempFiles, new Importer(new StateMachine(saver), new ResultReporter(statusUpdate)));
saver.setUploadFinished(vreName, Vre.PublishState.MAPPING_CREATION);
} catch (IOException | InvalidFileException e) {
saver.setUploadFinished(vreName, Vre.PublishState.UPLOAD_FAILED);
throw e;
}
}
}
use of nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException in project timbuctoo by HuygensING.
the class AllSheetLoaderTest method importExcel.
@Test
public void importExcel() throws Exception {
URL excelFileUrl = AllSheetLoaderTest.class.getResource("test.xlsx");
File excelFile = new File(excelFileUrl.toURI());
AllSheetLoader instance = new AllSheetLoader();
try {
List<String> results = new ArrayList<>();
AtomicBoolean failure = new AtomicBoolean(false);
instance.loadData(newArrayList(tuple("test.csv", excelFile)), ImporterStubs.withCustomReporter(logline -> {
if (logline.matches("failure.*")) {
failure.set(true);
}
results.add(logline);
}));
if (failure.get()) {
throw new RuntimeException("Failure during import: \n" + String.join("\n", results));
}
} catch (InvalidFileException e) {
throw new RuntimeException(e);
}
}
use of nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException in project timbuctoo by HuygensING.
the class CsvLoaderTest method importCsv.
@Test
public void importCsv() throws Exception {
File csvFile = new File(CsvLoaderTest.class.getResource("test.csv").toURI());
CsvLoader loader = new CsvLoader(new HashMap<>());
try {
List<String> results = new ArrayList<>();
AtomicBoolean failure = new AtomicBoolean(false);
loader.loadData(newArrayList(tuple("testcollection.csv", csvFile)), ImporterStubs.withCustomReporter(logline -> {
if (logline.matches("failure.*")) {
failure.set(true);
}
results.add(logline);
}));
if (failure.get()) {
throw new RuntimeException("Failure during import: \n" + String.join("\n", results));
}
} catch (InvalidFileException e) {
throw new RuntimeException(e);
}
}
use of nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException in project timbuctoo by HuygensING.
the class DataPerfectLoader method loadData.
@Override
public void loadData(List<Tuple<String, File>> files, Importer importer) throws InvalidFileException, IOException {
Database dbPerfectdb = null;
try {
File output = writeZipToTempDir(files.get(0).getRight());
File dirWithDb = unwrapToplevelDirIfNeeded(output);
File structureFile = getStructureFile(dirWithDb);
dbPerfectdb = new Database(structureFile);
importDbPerfect(importer, dbPerfectdb);
delete(output);
} catch (DataPerfectLibException e) {
throw new InvalidFileException("Not a valid dataperfect file", e);
} finally {
try {
if (dbPerfectdb != null) {
dbPerfectdb.close();
}
} catch (IOException e) {
LOG.error("Something went wrong while closing the resources in the data import", e);
}
}
}
Aggregations